Gathering infra
Deploy Promiscuity Auth API / deploy (push) Successful in 46s
Deploy Promiscuity Character API / deploy (push) Successful in 45s
Deploy Promiscuity Inventory API / deploy (push) Successful in 46s
Deploy Promiscuity Locations API / deploy (push) Successful in 1m3s
k8s smoke test / test (push) Successful in 7s

This commit is contained in:
2026-03-15 14:04:12 -05:00
parent a2a4d48de5
commit 9ba725d207
10 changed files with 273 additions and 22 deletions
@@ -8,6 +8,7 @@ public class LocationStore
{
private readonly IMongoCollection<Location> _col;
private readonly IMongoCollection<BsonDocument> _rawCol;
private readonly IMongoCollection<CharacterDocument> _characters;
private const string CoordIndexName = "coord_x_1_coord_y_1";
public LocationStore(IConfiguration cfg)
@@ -20,6 +21,7 @@ public class LocationStore
EnsureLocationSchema(db, collectionName);
_col = db.GetCollection<Location>(collectionName);
_rawCol = db.GetCollection<BsonDocument>(collectionName);
_characters = db.GetCollection<CharacterDocument>("Characters");
EnsureCoordIndexes();
@@ -34,11 +36,11 @@ public class LocationStore
"$jsonSchema", new BsonDocument
{
{ "bsonType", "object" },
{ "required", new BsonArray { "name", "coord", "createdUtc" } },
{
"properties", new BsonDocument
{
{ "name", new BsonDocument { { "bsonType", "string" } } },
{ "required", new BsonArray { "name", "coord", "createdUtc" } },
{
"properties", new BsonDocument
{
{ "name", new BsonDocument { { "bsonType", "string" } } },
{
"coord", new BsonDocument
{
@@ -52,10 +54,31 @@ public class LocationStore
}
}
}
},
{ "createdUtc", new BsonDocument { { "bsonType", "date" } } }
}
}
},
{
"resources", new BsonDocument
{
{ "bsonType", new BsonArray { "array", "null" } },
{
"items", new BsonDocument
{
{ "bsonType", "object" },
{ "required", new BsonArray { "itemKey", "remainingQuantity", "gatherQuantity" } },
{
"properties", new BsonDocument
{
{ "itemKey", new BsonDocument { { "bsonType", "string" } } },
{ "remainingQuantity", new BsonDocument { { "bsonType", "int" }, { "minimum", 0 } } },
{ "gatherQuantity", new BsonDocument { { "bsonType", "int" }, { "minimum", 1 } } }
}
}
}
}
}
},
{ "createdUtc", new BsonDocument { { "bsonType", "date" } } }
}
}
}
}
};
@@ -102,6 +125,57 @@ public class LocationStore
return result.ModifiedCount > 0;
}
public sealed record GatherResult(GatherStatus Status, string ResourceKey = "", int QuantityGranted = 0, int RemainingQuantity = 0);
public async Task<GatherResult> GatherResourceAsync(string locationId, string characterId, string resourceKey, string userId, bool allowAnyOwner)
{
var normalizedKey = resourceKey.Trim().ToLowerInvariant();
var location = await _col.Find(l => l.Id == locationId).FirstOrDefaultAsync();
if (location is null)
return new GatherResult(GatherStatus.LocationNotFound);
var character = await _characters.Find(c => c.Id == characterId).FirstOrDefaultAsync();
if (character is null)
return new GatherResult(GatherStatus.CharacterNotFound);
if (!allowAnyOwner && character.OwnerUserId != userId)
return new GatherResult(GatherStatus.Forbidden);
if (character.Coord.X != location.Coord.X || character.Coord.Y != location.Coord.Y)
return new GatherResult(GatherStatus.Invalid);
var resource = location.Resources.FirstOrDefault(r => NormalizeItemKey(r.ItemKey) == normalizedKey);
if (resource is null)
return new GatherResult(GatherStatus.ResourceNotFound);
if (resource.RemainingQuantity <= 0)
return new GatherResult(GatherStatus.ResourceDepleted);
var quantityGranted = Math.Min(resource.GatherQuantity, resource.RemainingQuantity);
var filter = Builders<Location>.Filter.And(
Builders<Location>.Filter.Eq(l => l.Id, locationId),
Builders<Location>.Filter.ElemMatch(l => l.Resources, r => r.ItemKey == resource.ItemKey && r.RemainingQuantity >= quantityGranted)
);
var update = Builders<Location>.Update.Inc("resources.$.remainingQuantity", -quantityGranted);
var result = await _col.UpdateOneAsync(filter, update);
if (result.ModifiedCount == 0)
return new GatherResult(GatherStatus.ResourceDepleted);
return new GatherResult(
GatherStatus.Ok,
resource.ItemKey,
quantityGranted,
resource.RemainingQuantity - quantityGranted);
}
public async Task RestoreGatheredResourceAsync(string locationId, string resourceKey, int quantity)
{
var normalizedKey = NormalizeItemKey(resourceKey);
var filter = Builders<Location>.Filter.And(
Builders<Location>.Filter.Eq(l => l.Id, locationId),
Builders<Location>.Filter.ElemMatch(l => l.Resources, r => r.ItemKey == normalizedKey)
);
var update = Builders<Location>.Update.Inc("resources.$.remainingQuantity", quantity);
await _col.UpdateOneAsync(filter, update);
}
private void EnsureCoordIndexes()
{
var indexes = _rawCol.Indexes.List().ToList();
@@ -149,12 +223,17 @@ 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 },
Resources =
[
new LocationResource { ItemKey = "wood", RemainingQuantity = 100, GatherQuantity = 3 },
new LocationResource { ItemKey = "grass", RemainingQuantity = 500, GatherQuantity = 10 }
],
CreatedUtc = DateTime.UtcNow
};
try
{
@@ -164,5 +243,30 @@ public class LocationStore
{
// Another instance seeded it first.
}
}
}
}
private static string NormalizeItemKey(string itemKey) => itemKey.Trim().ToLowerInvariant();
[MongoDB.Bson.Serialization.Attributes.BsonIgnoreExtraElements]
private class CharacterDocument
{
[MongoDB.Bson.Serialization.Attributes.BsonId]
[MongoDB.Bson.Serialization.Attributes.BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
public string? Id { get; set; }
public string OwnerUserId { get; set; } = string.Empty;
public Coord Coord { get; set; } = new();
}
}
public enum GatherStatus
{
Ok,
LocationNotFound,
CharacterNotFound,
Forbidden,
Invalid,
ResourceNotFound,
ResourceDepleted
}