Adding endpoint to only get locations visible to character
All checks were successful
All checks were successful
This commit is contained in:
parent
84f8087647
commit
097ef4a22c
@ -66,7 +66,7 @@ public class CharactersController : ControllerBase
|
||||
if (character is null)
|
||||
return NotFound();
|
||||
|
||||
var locations = await _characters.GetVisibleLocationsAsync(character);
|
||||
var locations = await _characters.GetOrCreateVisibleLocationsAsync(character);
|
||||
return Ok(locations);
|
||||
}
|
||||
|
||||
|
||||
@ -41,3 +41,4 @@ Outbound JSON documents
|
||||
}
|
||||
]
|
||||
```
|
||||
Missing locations within the character's vision window are created automatically before this response is returned.
|
||||
|
||||
@ -6,5 +6,5 @@ See `DOCUMENTS.md` for request payloads and stored document shapes.
|
||||
## Endpoints
|
||||
- `POST /api/characters` Create a character.
|
||||
- `GET /api/characters` List characters for the current user.
|
||||
- `GET /api/characters/{id}/visible-locations` List locations visible to that owned character.
|
||||
- `GET /api/characters/{id}/visible-locations` Ensure and list locations visible to that owned character.
|
||||
- `DELETE /api/characters/{id}` Delete a character owned by the current user.
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using CharacterApi.Models;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace CharacterApi.Services;
|
||||
@ -41,6 +42,17 @@ public class CharacterStore
|
||||
}
|
||||
|
||||
public Task<List<VisibleLocation>> GetVisibleLocationsAsync(Character character)
|
||||
{
|
||||
return GetVisibleLocationsInternalAsync(character, ensureGenerated: false);
|
||||
}
|
||||
|
||||
public async Task<List<VisibleLocation>> GetOrCreateVisibleLocationsAsync(Character character)
|
||||
{
|
||||
await EnsureVisibleLocationsExistAsync(character);
|
||||
return await GetVisibleLocationsInternalAsync(character, ensureGenerated: true);
|
||||
}
|
||||
|
||||
private Task<List<VisibleLocation>> GetVisibleLocationsInternalAsync(Character character, bool ensureGenerated)
|
||||
{
|
||||
var radius = character.VisionRadius > 0 ? character.VisionRadius : 3;
|
||||
var minX = character.Coord.X - radius;
|
||||
@ -58,6 +70,44 @@ public class CharacterStore
|
||||
return _locations.Find(filter).ToListAsync();
|
||||
}
|
||||
|
||||
private async Task EnsureVisibleLocationsExistAsync(Character character)
|
||||
{
|
||||
var radius = character.VisionRadius > 0 ? character.VisionRadius : 3;
|
||||
|
||||
for (var x = character.Coord.X - radius; x <= character.Coord.X + radius; x++)
|
||||
{
|
||||
for (var y = character.Coord.Y - radius; y <= character.Coord.Y + radius; y++)
|
||||
{
|
||||
var filter = Builders<VisibleLocation>.Filter.And(
|
||||
Builders<VisibleLocation>.Filter.Eq(l => l.Coord.X, x),
|
||||
Builders<VisibleLocation>.Filter.Eq(l => l.Coord.Y, y)
|
||||
);
|
||||
|
||||
var update = Builders<VisibleLocation>.Update
|
||||
.SetOnInsert(l => l.Name, DefaultLocationName(x, y))
|
||||
.SetOnInsert(l => l.Coord, new Coord { X = x, Y = y })
|
||||
.SetOnInsert(l => l.Id, ObjectId.GenerateNewId().ToString())
|
||||
.SetOnInsert("createdUtc", DateTime.UtcNow);
|
||||
|
||||
try
|
||||
{
|
||||
await _locations.UpdateOneAsync(filter, update, new UpdateOptions { IsUpsert = true });
|
||||
}
|
||||
catch (MongoWriteException ex) when (ex.WriteError.Category == ServerErrorCategory.DuplicateKey)
|
||||
{
|
||||
// Another request or service instance created it first.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string DefaultLocationName(int x, int y)
|
||||
{
|
||||
if (x == 0 && y == 0)
|
||||
return "Origin";
|
||||
return $"Location {x},{y}";
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteForOwnerAsync(string id, string ownerUserId, bool allowAnyOwner)
|
||||
{
|
||||
var filter = Builders<Character>.Filter.Eq(c => c.Id, id);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user