Tracking character movement
This commit is contained in:
@@ -11,10 +11,12 @@ namespace LocationsApi.Controllers;
|
||||
public class LocationsController : ControllerBase
|
||||
{
|
||||
private readonly LocationStore _locations;
|
||||
private readonly IConfiguration _cfg;
|
||||
|
||||
public LocationsController(LocationStore locations)
|
||||
public LocationsController(LocationStore locations, IConfiguration cfg)
|
||||
{
|
||||
_locations = locations;
|
||||
_cfg = cfg;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
@@ -31,6 +33,7 @@ public class LocationsController : ControllerBase
|
||||
{
|
||||
Name = req.Name.Trim(),
|
||||
Coord = req.Coord,
|
||||
CharacterIds = new List<string>(),
|
||||
CreatedUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
@@ -85,4 +88,28 @@ public class LocationsController : ControllerBase
|
||||
|
||||
return Ok("Updated");
|
||||
}
|
||||
|
||||
[HttpPost("presence")]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> UpdatePresence([FromBody] UpdateLocationPresenceRequest req)
|
||||
{
|
||||
var internalKey = _cfg["Internal:Key"];
|
||||
if (!string.IsNullOrWhiteSpace(internalKey))
|
||||
{
|
||||
if (!Request.Headers.TryGetValue("X-Internal-Key", out var provided) || provided != internalKey)
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(req.CharacterId))
|
||||
return BadRequest("CharacterId required");
|
||||
|
||||
if (req.Coord is null)
|
||||
return BadRequest("Coord required");
|
||||
|
||||
var updated = await _locations.UpdatePresenceAsync(req.CharacterId.Trim(), req.Coord);
|
||||
if (!updated)
|
||||
return NotFound("Location not found");
|
||||
|
||||
return Ok("Updated");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,16 @@ Inbound JSON documents
|
||||
}
|
||||
```
|
||||
`coord` cannot be updated.
|
||||
- UpdateLocationPresenceRequest (`POST /api/locations/presence`)
|
||||
```json
|
||||
{
|
||||
"characterId": "string",
|
||||
"coord": {
|
||||
"x": 0,
|
||||
"y": 0
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Stored documents (MongoDB)
|
||||
- Location
|
||||
@@ -32,6 +42,7 @@ Stored documents (MongoDB)
|
||||
"x": 0,
|
||||
"y": 0
|
||||
},
|
||||
"characterIds": ["string"],
|
||||
"createdUtc": "string (ISO-8601 datetime)"
|
||||
}
|
||||
```
|
||||
|
||||
@@ -15,6 +15,9 @@ public class Location
|
||||
[BsonElement("coord")]
|
||||
public required Coord Coord { get; set; }
|
||||
|
||||
[BsonElement("characterIds")]
|
||||
public List<string> CharacterIds { get; set; } = new();
|
||||
|
||||
[BsonElement("createdUtc")]
|
||||
public DateTime CreatedUtc { get; set; } = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace LocationsApi.Models;
|
||||
|
||||
public class UpdateLocationPresenceRequest
|
||||
{
|
||||
public string CharacterId { get; set; } = string.Empty;
|
||||
|
||||
public Coord? Coord { get; set; }
|
||||
}
|
||||
@@ -8,3 +8,4 @@ See `DOCUMENTS.md` for request payloads and stored document shapes.
|
||||
- `GET /api/locations` List all locations (SUPER only).
|
||||
- `DELETE /api/locations/{id}` Delete a location (SUPER only).
|
||||
- `PUT /api/locations/{id}` Update a location name (SUPER only).
|
||||
- `POST /api/locations/presence` Update which characters are present at a coord (internal).
|
||||
|
||||
@@ -34,13 +34,13 @@ public class LocationStore
|
||||
{
|
||||
"$jsonSchema", new BsonDocument
|
||||
{
|
||||
{ "bsonType", "object" },
|
||||
{ "required", new BsonArray { "name", "coord", "createdUtc" } },
|
||||
{
|
||||
"properties", new BsonDocument
|
||||
{
|
||||
{ "name", new BsonDocument { { "bsonType", "string" } } },
|
||||
{
|
||||
{ "bsonType", "object" },
|
||||
{ "required", new BsonArray { "name", "coord", "createdUtc" } },
|
||||
{
|
||||
"properties", new BsonDocument
|
||||
{
|
||||
{ "name", new BsonDocument { { "bsonType", "string" } } },
|
||||
{
|
||||
"coord", new BsonDocument
|
||||
{
|
||||
{ "bsonType", "object" },
|
||||
@@ -51,14 +51,21 @@ public class LocationStore
|
||||
{ "x", new BsonDocument { { "bsonType", "int" } } },
|
||||
{ "y", new BsonDocument { { "bsonType", "int" } } }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{ "createdUtc", new BsonDocument { { "bsonType", "date" } } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"characterIds", new BsonDocument
|
||||
{
|
||||
{ "bsonType", "array" },
|
||||
{ "items", new BsonDocument { { "bsonType", "string" } } }
|
||||
}
|
||||
},
|
||||
{ "createdUtc", new BsonDocument { { "bsonType", "date" } } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var collections = db.ListCollectionNames().ToList();
|
||||
@@ -95,13 +102,31 @@ public class LocationStore
|
||||
return result.DeletedCount > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateNameAsync(string id, string name)
|
||||
{
|
||||
var filter = Builders<Location>.Filter.Eq(l => l.Id, id);
|
||||
var update = Builders<Location>.Update.Set(l => l.Name, name);
|
||||
var result = await _col.UpdateOneAsync(filter, update);
|
||||
return result.ModifiedCount > 0;
|
||||
}
|
||||
public async Task<bool> UpdateNameAsync(string id, string name)
|
||||
{
|
||||
var filter = Builders<Location>.Filter.Eq(l => l.Id, id);
|
||||
var update = Builders<Location>.Update.Set(l => l.Name, name);
|
||||
var result = await _col.UpdateOneAsync(filter, update);
|
||||
return result.ModifiedCount > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdatePresenceAsync(string characterId, Coord coord)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(characterId))
|
||||
return false;
|
||||
|
||||
var pullFilter = Builders<Location>.Filter.AnyEq(l => l.CharacterIds, characterId);
|
||||
var pullUpdate = Builders<Location>.Update.Pull(l => l.CharacterIds, characterId);
|
||||
await _col.UpdateManyAsync(pullFilter, pullUpdate);
|
||||
|
||||
var targetFilter = Builders<Location>.Filter.And(
|
||||
Builders<Location>.Filter.Eq(l => l.Coord.X, coord.X),
|
||||
Builders<Location>.Filter.Eq(l => l.Coord.Y, coord.Y)
|
||||
);
|
||||
var addUpdate = Builders<Location>.Update.AddToSet(l => l.CharacterIds, characterId);
|
||||
var result = await _col.UpdateOneAsync(targetFilter, addUpdate);
|
||||
return result.MatchedCount > 0;
|
||||
}
|
||||
|
||||
private void EnsureOriginLocation()
|
||||
{
|
||||
@@ -113,12 +138,13 @@ public class LocationStore
|
||||
if (existing is not null)
|
||||
return;
|
||||
|
||||
var origin = new Location
|
||||
{
|
||||
Name = "Origin",
|
||||
Coord = new Coord { X = 0, Y = 0 },
|
||||
CreatedUtc = DateTime.UtcNow
|
||||
};
|
||||
var origin = new Location
|
||||
{
|
||||
Name = "Origin",
|
||||
Coord = new Coord { X = 0, Y = 0 },
|
||||
CharacterIds = new List<string>(),
|
||||
CreatedUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"Kestrel": { "Endpoints": { "Http": { "Url": "http://0.0.0.0:5002" } } },
|
||||
"MongoDB": { "ConnectionString": "mongodb://192.168.86.50:27017", "DatabaseName": "promiscuity" },
|
||||
"Internal": { "Key": "dev-internal-key" },
|
||||
"Jwt": { "Key": "SuperUltraSecureJwtKeyWithAtLeast32Chars!!", "Issuer": "promiscuity", "Audience": "promiscuity-auth-api" },
|
||||
"Logging": { "LogLevel": { "Default": "Information" } }
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"Kestrel": { "Endpoints": { "Http": { "Url": "http://0.0.0.0:5002" } } },
|
||||
"MongoDB": { "ConnectionString": "mongodb://192.168.86.50:27017", "DatabaseName": "promiscuity" },
|
||||
"Internal": { "Key": "dev-internal-key" },
|
||||
"Jwt": { "Key": "SuperUltraSecureJwtKeyWithAtLeast32Chars!!", "Issuer": "promiscuity", "Audience": "promiscuity-auth-api" },
|
||||
"Logging": { "LogLevel": { "Default": "Information" } },
|
||||
"AllowedHosts": "*"
|
||||
|
||||
Reference in New Issue
Block a user