Merge branch 'main' of https://git.ranaze.com/null/promiscuity into main
All checks were successful
Deploy Promiscuity Auth API / deploy (push) Successful in 46s
Deploy Promiscuity Character API / deploy (push) Successful in 58s
Deploy Promiscuity Inventory API / deploy (push) Successful in 45s
Deploy Promiscuity Locations API / deploy (push) Successful in 59s
k8s smoke test / test (push) Successful in 7s
All checks were successful
Deploy Promiscuity Auth API / deploy (push) Successful in 46s
Deploy Promiscuity Character API / deploy (push) Successful in 58s
Deploy Promiscuity Inventory API / deploy (push) Successful in 45s
Deploy Promiscuity Locations API / deploy (push) Successful in 59s
k8s smoke test / test (push) Successful in 7s
This commit is contained in:
commit
82e4ae8a81
File diff suppressed because it is too large
Load Diff
@ -46,7 +46,15 @@ Outbound JSON documents
|
|||||||
"coord": {
|
"coord": {
|
||||||
"x": "number",
|
"x": "number",
|
||||||
"y": "number"
|
"y": "number"
|
||||||
}
|
},
|
||||||
|
"biomeKey": "plains",
|
||||||
|
"resources": [
|
||||||
|
{
|
||||||
|
"itemKey": "wood",
|
||||||
|
"remainingQuantity": 100,
|
||||||
|
"gatherQuantity": 3
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|||||||
@ -1,23 +1,23 @@
|
|||||||
using MongoDB.Bson.Serialization.Attributes;
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
|
|
||||||
namespace CharacterApi.Models;
|
namespace CharacterApi.Models;
|
||||||
|
|
||||||
[BsonIgnoreExtraElements]
|
[BsonIgnoreExtraElements]
|
||||||
public class VisibleLocation
|
public class VisibleLocation
|
||||||
{
|
{
|
||||||
[BsonId]
|
[BsonId]
|
||||||
[BsonRepresentation(BsonType.ObjectId)]
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
public string? Id { get; set; }
|
public string? Id { get; set; }
|
||||||
|
|
||||||
[BsonElement("name")]
|
[BsonElement("name")]
|
||||||
public string Name { get; set; } = string.Empty;
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
[BsonElement("coord")]
|
[BsonElement("coord")]
|
||||||
public LocationCoord Coord { get; set; } = new();
|
public LocationCoord Coord { get; set; } = new();
|
||||||
|
|
||||||
[BsonElement("biomeKey")]
|
[BsonElement("biomeKey")]
|
||||||
public string BiomeKey { get; set; } = string.Empty;
|
public string BiomeKey { get; set; } = "plains";
|
||||||
|
|
||||||
[BsonElement("locationObject")]
|
[BsonElement("locationObject")]
|
||||||
public VisibleLocationObject? LocationObject { get; set; }
|
public VisibleLocationObject? LocationObject { get; set; }
|
||||||
|
|||||||
16
microservices/CharacterApi/Models/VisibleLocationResource.cs
Normal file
16
microservices/CharacterApi/Models/VisibleLocationResource.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
|
|
||||||
|
namespace CharacterApi.Models;
|
||||||
|
|
||||||
|
[BsonIgnoreExtraElements]
|
||||||
|
public class VisibleLocationResource
|
||||||
|
{
|
||||||
|
[BsonElement("itemKey")]
|
||||||
|
public string ItemKey { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[BsonElement("remainingQuantity")]
|
||||||
|
public int RemainingQuantity { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("gatherQuantity")]
|
||||||
|
public int GatherQuantity { get; set; } = 1;
|
||||||
|
}
|
||||||
@ -1,16 +1,57 @@
|
|||||||
using CharacterApi.Models;
|
using CharacterApi.Models;
|
||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
|
||||||
namespace CharacterApi.Services;
|
namespace CharacterApi.Services;
|
||||||
|
|
||||||
public class CharacterStore
|
public class CharacterStore
|
||||||
{
|
{
|
||||||
private readonly IMongoCollection<Character> _col;
|
private readonly IMongoCollection<Character> _col;
|
||||||
private readonly IMongoCollection<BsonDocument> _locations;
|
private readonly IMongoCollection<BsonDocument> _locations;
|
||||||
private const string CoordIndexName = "coord_x_1_coord_y_1";
|
private const string CoordIndexName = "coord_x_1_coord_y_1";
|
||||||
|
private const int WorldSeed = 1729;
|
||||||
|
private static readonly string[] BiomeOrder = ["plains", "forest", "wetlands", "rocky", "desert"];
|
||||||
|
private static readonly Dictionary<string, BiomeDefinition> Biomes = new()
|
||||||
|
{
|
||||||
|
["plains"] = new("plains", 3.0, new()
|
||||||
|
{
|
||||||
|
["forest"] = 1.7,
|
||||||
|
["wetlands"] = 0.9,
|
||||||
|
["rocky"] = 0.8,
|
||||||
|
["desert"] = 0.4
|
||||||
|
}),
|
||||||
|
["forest"] = new("forest", 3.4, new()
|
||||||
|
{
|
||||||
|
["plains"] = 1.6,
|
||||||
|
["wetlands"] = 1.3,
|
||||||
|
["rocky"] = 0.5,
|
||||||
|
["desert"] = 0.1
|
||||||
|
}),
|
||||||
|
["wetlands"] = new("wetlands", 3.1, new()
|
||||||
|
{
|
||||||
|
["forest"] = 1.5,
|
||||||
|
["plains"] = 1.1,
|
||||||
|
["rocky"] = 0.2,
|
||||||
|
["desert"] = 0.05
|
||||||
|
}),
|
||||||
|
["rocky"] = new("rocky", 3.0, new()
|
||||||
|
{
|
||||||
|
["plains"] = 1.2,
|
||||||
|
["forest"] = 0.6,
|
||||||
|
["desert"] = 1.1,
|
||||||
|
["wetlands"] = 0.1
|
||||||
|
}),
|
||||||
|
["desert"] = new("desert", 3.2, new()
|
||||||
|
{
|
||||||
|
["rocky"] = 1.4,
|
||||||
|
["plains"] = 0.8,
|
||||||
|
["forest"] = 0.1,
|
||||||
|
["wetlands"] = 0.05
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
public sealed record VisibleLocationResult(List<VisibleLocation> Locations, int GeneratedCount);
|
public sealed record VisibleLocationResult(List<VisibleLocation> Locations, int GeneratedCount);
|
||||||
|
private sealed record BiomeDefinition(string Key, double ContinuationWeight, Dictionary<string, double> TransitionWeights);
|
||||||
|
|
||||||
public CharacterStore(IConfiguration cfg)
|
public CharacterStore(IConfiguration cfg)
|
||||||
{
|
{
|
||||||
@ -25,9 +66,9 @@ public class CharacterStore
|
|||||||
var ownerIndex = Builders<Character>.IndexKeys.Ascending(c => c.OwnerUserId);
|
var ownerIndex = Builders<Character>.IndexKeys.Ascending(c => c.OwnerUserId);
|
||||||
_col.Indexes.CreateOne(new CreateIndexModel<Character>(ownerIndex));
|
_col.Indexes.CreateOne(new CreateIndexModel<Character>(ownerIndex));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task CreateAsync(Character character) => _col.InsertOneAsync(character);
|
public Task CreateAsync(Character character) => _col.InsertOneAsync(character);
|
||||||
|
|
||||||
public Task<List<Character>> GetForOwnerAsync(string ownerUserId) =>
|
public Task<List<Character>> GetForOwnerAsync(string ownerUserId) =>
|
||||||
_col.Find(c => c.OwnerUserId == ownerUserId).ToListAsync();
|
_col.Find(c => c.OwnerUserId == ownerUserId).ToListAsync();
|
||||||
|
|
||||||
@ -42,10 +83,8 @@ public class CharacterStore
|
|||||||
return result.ModifiedCount > 0 || result.MatchedCount > 0;
|
return result.ModifiedCount > 0 || result.MatchedCount > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<List<VisibleLocation>> GetVisibleLocationsAsync(Character character)
|
public Task<List<VisibleLocation>> GetVisibleLocationsAsync(Character character) =>
|
||||||
{
|
GetVisibleLocationsInternalAsync(character, ensureGenerated: false);
|
||||||
return GetVisibleLocationsInternalAsync(character, ensureGenerated: false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<VisibleLocationResult> GetOrCreateVisibleLocationsAsync(Character character)
|
public async Task<VisibleLocationResult> GetOrCreateVisibleLocationsAsync(Character character)
|
||||||
{
|
{
|
||||||
@ -73,9 +112,10 @@ public class CharacterStore
|
|||||||
if (ensureGenerated)
|
if (ensureGenerated)
|
||||||
{
|
{
|
||||||
foreach (var document in documents)
|
foreach (var document in documents)
|
||||||
await EnsureLocationObjectAsync(document);
|
await BackfillLocationStateAsync(document);
|
||||||
documents = await _locations.Find(filter).ToListAsync();
|
documents = await _locations.Find(filter).ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
return documents.Select(MapVisibleLocation).ToList();
|
return documents.Select(MapVisibleLocation).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,101 +128,202 @@ public class CharacterStore
|
|||||||
{
|
{
|
||||||
for (var y = character.Coord.Y - radius; y <= character.Coord.Y + radius; y++)
|
for (var y = character.Coord.Y - radius; y <= character.Coord.Y + radius; y++)
|
||||||
{
|
{
|
||||||
var filter = Builders<BsonDocument>.Filter.And(
|
if (await EnsureLocationStateAsync(x, y))
|
||||||
Builders<BsonDocument>.Filter.Eq("coord.x", x),
|
generatedCount += 1;
|
||||||
Builders<BsonDocument>.Filter.Eq("coord.y", y)
|
|
||||||
);
|
|
||||||
|
|
||||||
var update = Builders<BsonDocument>.Update
|
|
||||||
.SetOnInsert("_id", ObjectId.GenerateNewId())
|
|
||||||
.SetOnInsert("name", DefaultLocationName(x, y))
|
|
||||||
.SetOnInsert("coord", new BsonDocument
|
|
||||||
{
|
|
||||||
{ "x", x },
|
|
||||||
{ "y", y }
|
|
||||||
})
|
|
||||||
.SetOnInsert("biomeKey", DetermineBiomeKey(x, y))
|
|
||||||
.SetOnInsert("locationObject", CreateLocationObjectValueForBiome(DetermineBiomeKey(x, y), x, y))
|
|
||||||
.SetOnInsert("locationObjectResolved", true)
|
|
||||||
.SetOnInsert("createdUtc", DateTime.UtcNow);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var result = await _locations.UpdateOneAsync(filter, update, new UpdateOptions { IsUpsert = true });
|
|
||||||
if (result.UpsertedId is not null)
|
|
||||||
generatedCount += 1;
|
|
||||||
}
|
|
||||||
catch (MongoWriteException ex) when (ex.WriteError.Category == ServerErrorCategory.DuplicateKey)
|
|
||||||
{
|
|
||||||
// Another request or service instance created it first.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return generatedCount;
|
return generatedCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string DefaultLocationName(int x, int y)
|
private async Task<bool> EnsureLocationStateAsync(int x, int y)
|
||||||
{
|
{
|
||||||
if (x == 0 && y == 0)
|
var filter = Builders<BsonDocument>.Filter.And(
|
||||||
return "Origin";
|
Builders<BsonDocument>.Filter.Eq("coord.x", x),
|
||||||
return $"Location {x},{y}";
|
Builders<BsonDocument>.Filter.Eq("coord.y", y)
|
||||||
}
|
);
|
||||||
|
|
||||||
private static VisibleLocation MapVisibleLocation(BsonDocument document)
|
var existing = await _locations.Find(filter).FirstOrDefaultAsync();
|
||||||
{
|
if (existing is not null)
|
||||||
var coord = document.GetValue("coord", new BsonDocument()).AsBsonDocument;
|
|
||||||
var locationObject = MapVisibleLocationObject(document.GetValue("locationObject", BsonNull.Value));
|
|
||||||
var idValue = document.GetValue("_id", BsonNull.Value);
|
|
||||||
string? id = null;
|
|
||||||
if (!idValue.IsBsonNull)
|
|
||||||
{
|
{
|
||||||
id = idValue.BsonType == BsonType.ObjectId
|
await BackfillLocationStateAsync(existing);
|
||||||
? idValue.AsObjectId.ToString()
|
return false;
|
||||||
: idValue.ToString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new VisibleLocation
|
var biomeKey = await DetermineBiomeKeyAsync(x, y);
|
||||||
{
|
var update = Builders<BsonDocument>.Update
|
||||||
Id = id,
|
.SetOnInsert("_id", ObjectId.GenerateNewId())
|
||||||
Name = document.GetValue("name", "").AsString,
|
.SetOnInsert("name", DefaultLocationName(x, y))
|
||||||
Coord = new LocationCoord
|
.SetOnInsert("coord", new BsonDocument
|
||||||
{
|
{
|
||||||
X = coord.GetValue("x", 0).ToInt32(),
|
{ "x", x },
|
||||||
Y = coord.GetValue("y", 0).ToInt32()
|
{ "y", y }
|
||||||
},
|
})
|
||||||
BiomeKey = document.GetValue("biomeKey", "").AsString,
|
.SetOnInsert("biomeKey", biomeKey)
|
||||||
LocationObject = locationObject
|
.SetOnInsert("locationObject", CreateLocationObjectValueForBiome(biomeKey, x, y))
|
||||||
};
|
.SetOnInsert("locationObjectResolved", true)
|
||||||
|
.SetOnInsert("createdUtc", DateTime.UtcNow);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await _locations.UpdateOneAsync(filter, update, new UpdateOptions { IsUpsert = true });
|
||||||
|
return result.UpsertedId is not null;
|
||||||
|
}
|
||||||
|
catch (MongoWriteException ex) when (ex.WriteError.Category == ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task EnsureLocationObjectAsync(BsonDocument document)
|
private async Task BackfillLocationStateAsync(BsonDocument document)
|
||||||
{
|
{
|
||||||
var hasBiome = document.TryGetValue("biomeKey", out var biomeValue) &&
|
|
||||||
biomeValue.BsonType == BsonType.String &&
|
|
||||||
!string.IsNullOrWhiteSpace(biomeValue.AsString);
|
|
||||||
var resolved = document.TryGetValue("locationObjectResolved", out var resolvedValue) &&
|
|
||||||
resolvedValue.ToBoolean();
|
|
||||||
if (hasBiome && resolved)
|
|
||||||
return;
|
|
||||||
|
|
||||||
var idValue = document.GetValue("_id", BsonNull.Value);
|
|
||||||
if (idValue.IsBsonNull)
|
|
||||||
return;
|
|
||||||
|
|
||||||
var coord = document.GetValue("coord", new BsonDocument()).AsBsonDocument;
|
var coord = document.GetValue("coord", new BsonDocument()).AsBsonDocument;
|
||||||
var x = coord.GetValue("x", 0).ToInt32();
|
var x = coord.GetValue("x", 0).ToInt32();
|
||||||
var y = coord.GetValue("y", 0).ToInt32();
|
var y = coord.GetValue("y", 0).ToInt32();
|
||||||
var biomeKey = hasBiome ? biomeValue.AsString : DetermineBiomeKey(x, y);
|
|
||||||
var locationObject = TryMigrateLegacyResource(document) ?? CreateLocationObjectValueForBiome(biomeKey, x, y);
|
var updates = new List<UpdateDefinition<BsonDocument>>();
|
||||||
var filter = Builders<BsonDocument>.Filter.And(
|
var biomeKey = document.GetValue("biomeKey", BsonNull.Value).IsBsonNull
|
||||||
Builders<BsonDocument>.Filter.Eq("_id", idValue)
|
? await DetermineBiomeKeyAsync(x, y)
|
||||||
);
|
: document.GetValue("biomeKey", "plains").AsString;
|
||||||
var update = Builders<BsonDocument>.Update
|
var objectResolved = document.TryGetValue("locationObjectResolved", out var resolvedValue) &&
|
||||||
.Set("biomeKey", biomeKey)
|
resolvedValue.ToBoolean();
|
||||||
.Set("locationObjectResolved", true)
|
|
||||||
.Set("locationObject", locationObject);
|
if (!document.Contains("biomeKey"))
|
||||||
await _locations.UpdateOneAsync(filter, update);
|
updates.Add(Builders<BsonDocument>.Update.Set("biomeKey", biomeKey));
|
||||||
|
if (!objectResolved)
|
||||||
|
{
|
||||||
|
var locationObject = TryMigrateLegacyResource(document) ?? CreateLocationObjectValueForBiome(biomeKey, x, y);
|
||||||
|
updates.Add(Builders<BsonDocument>.Update.Set("locationObject", locationObject));
|
||||||
|
updates.Add(Builders<BsonDocument>.Update.Set("locationObjectResolved", true));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (updates.Count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var id = document.GetValue("_id").AsObjectId;
|
||||||
|
await _locations.UpdateOneAsync(
|
||||||
|
Builders<BsonDocument>.Filter.Eq("_id", id),
|
||||||
|
Builders<BsonDocument>.Update.Combine(updates));
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<string> DetermineBiomeKeyAsync(int x, int y)
|
||||||
|
{
|
||||||
|
if (x == 0 && y == 0)
|
||||||
|
return "plains";
|
||||||
|
|
||||||
|
var neighbors = await LoadNeighborBiomeKeysAsync(x, y);
|
||||||
|
var baseBiome = DetermineBaseBiomeKey(x, y);
|
||||||
|
if (neighbors.Count == 0)
|
||||||
|
return baseBiome;
|
||||||
|
|
||||||
|
var dominantNeighbor = neighbors
|
||||||
|
.GroupBy(key => key)
|
||||||
|
.OrderByDescending(group => group.Count())
|
||||||
|
.ThenBy(group => group.Key)
|
||||||
|
.First().Key;
|
||||||
|
|
||||||
|
var bestBiome = baseBiome;
|
||||||
|
var bestScore = double.NegativeInfinity;
|
||||||
|
foreach (var candidate in BiomeOrder)
|
||||||
|
{
|
||||||
|
var score = candidate == baseBiome ? 2.5 : 0.35;
|
||||||
|
if (candidate == dominantNeighbor)
|
||||||
|
score += 1.8;
|
||||||
|
|
||||||
|
foreach (var neighbor in neighbors)
|
||||||
|
{
|
||||||
|
if (!Biomes.TryGetValue(neighbor, out var neighborDefinition))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (candidate == neighbor)
|
||||||
|
score += neighborDefinition.ContinuationWeight;
|
||||||
|
else if (neighborDefinition.TransitionWeights.TryGetValue(candidate, out var transitionWeight))
|
||||||
|
score += transitionWeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
score += StableNoise(x, y, StableHash(candidate)) * 0.25;
|
||||||
|
if (score > bestScore)
|
||||||
|
{
|
||||||
|
bestScore = score;
|
||||||
|
bestBiome = candidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bestBiome;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<List<string>> LoadNeighborBiomeKeysAsync(int x, int y)
|
||||||
|
{
|
||||||
|
var coords = new[] { (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1) };
|
||||||
|
var filters = coords.Select(coord =>
|
||||||
|
Builders<BsonDocument>.Filter.And(
|
||||||
|
Builders<BsonDocument>.Filter.Eq("coord.x", coord.Item1),
|
||||||
|
Builders<BsonDocument>.Filter.Eq("coord.y", coord.Item2)))
|
||||||
|
.ToList();
|
||||||
|
var filter = Builders<BsonDocument>.Filter.Or(filters);
|
||||||
|
var neighbors = await _locations.Find(filter).ToListAsync();
|
||||||
|
|
||||||
|
return neighbors
|
||||||
|
.Where(doc => doc.Contains("biomeKey"))
|
||||||
|
.Select(doc => doc.GetValue("biomeKey", "plains").AsString)
|
||||||
|
.Where(key => !string.IsNullOrWhiteSpace(key))
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string DetermineBaseBiomeKey(int x, int y)
|
||||||
|
{
|
||||||
|
var temperature = StableNoise(x, y, 101);
|
||||||
|
var moisture = StableNoise(x, y, 202);
|
||||||
|
var ruggedness = StableNoise(x, y, 303);
|
||||||
|
|
||||||
|
if (ruggedness > 0.74)
|
||||||
|
return "rocky";
|
||||||
|
if (moisture > 0.72 && temperature < 0.75)
|
||||||
|
return "wetlands";
|
||||||
|
if (moisture > 0.56)
|
||||||
|
return "forest";
|
||||||
|
if (moisture < 0.22 && temperature > 0.58)
|
||||||
|
return "desert";
|
||||||
|
return "plains";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static BsonValue CreateLocationObjectValueForBiome(string biomeKey, int x, int y)
|
||||||
|
{
|
||||||
|
var roll = StableNoise(x, y, 401);
|
||||||
|
return biomeKey switch
|
||||||
|
{
|
||||||
|
"forest" => roll switch
|
||||||
|
{
|
||||||
|
< 0.35 => BsonNull.Value,
|
||||||
|
< 0.80 => CreateGatherableObjectDocument("wood", 60, 3),
|
||||||
|
< 0.95 => CreateGatherableObjectDocument("grass", 120, 10),
|
||||||
|
_ => CreateGatherableObjectDocument("stone", 40, 2)
|
||||||
|
},
|
||||||
|
"rocky" => roll switch
|
||||||
|
{
|
||||||
|
< 0.60 => BsonNull.Value,
|
||||||
|
< 0.90 => CreateGatherableObjectDocument("stone", 40, 2),
|
||||||
|
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
||||||
|
},
|
||||||
|
"wetlands" => roll switch
|
||||||
|
{
|
||||||
|
< 0.40 => BsonNull.Value,
|
||||||
|
< 0.90 => CreateGatherableObjectDocument("grass", 120, 10),
|
||||||
|
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
||||||
|
},
|
||||||
|
"desert" => roll switch
|
||||||
|
{
|
||||||
|
< 0.70 => BsonNull.Value,
|
||||||
|
< 0.95 => CreateGatherableObjectDocument("stone", 40, 2),
|
||||||
|
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
||||||
|
},
|
||||||
|
_ => roll switch
|
||||||
|
{
|
||||||
|
< 0.50 => BsonNull.Value,
|
||||||
|
< 0.85 => CreateGatherableObjectDocument("grass", 120, 10),
|
||||||
|
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BsonDocument? TryMigrateLegacyResource(BsonDocument document)
|
private static BsonDocument? TryMigrateLegacyResource(BsonDocument document)
|
||||||
@ -207,6 +348,51 @@ public class CharacterStore
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static BsonDocument CreateGatherableObjectDocument(string itemKey, int remainingQuantity, int gatherQuantity)
|
||||||
|
{
|
||||||
|
var normalizedItemKey = NormalizeItemKey(itemKey);
|
||||||
|
return new BsonDocument
|
||||||
|
{
|
||||||
|
{ "id", Guid.NewGuid().ToString("N") },
|
||||||
|
{ "objectType", "gatherable" },
|
||||||
|
{ "objectKey", $"{normalizedItemKey}_node" },
|
||||||
|
{ "name", HumanizeItemKey(normalizedItemKey) },
|
||||||
|
{
|
||||||
|
"state", new BsonDocument
|
||||||
|
{
|
||||||
|
{ "itemKey", normalizedItemKey },
|
||||||
|
{ "remainingQuantity", remainingQuantity },
|
||||||
|
{ "gatherQuantity", gatherQuantity }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static VisibleLocation MapVisibleLocation(BsonDocument document)
|
||||||
|
{
|
||||||
|
var coord = document.GetValue("coord", new BsonDocument()).AsBsonDocument;
|
||||||
|
var locationObject = MapVisibleLocationObject(document.GetValue("locationObject", BsonNull.Value));
|
||||||
|
var idValue = document.GetValue("_id", BsonNull.Value);
|
||||||
|
string? id = null;
|
||||||
|
if (!idValue.IsBsonNull)
|
||||||
|
{
|
||||||
|
id = idValue.BsonType == BsonType.ObjectId ? idValue.AsObjectId.ToString() : idValue.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new VisibleLocation
|
||||||
|
{
|
||||||
|
Id = id,
|
||||||
|
Name = document.GetValue("name", "").AsString,
|
||||||
|
Coord = new LocationCoord
|
||||||
|
{
|
||||||
|
X = coord.GetValue("x", 0).ToInt32(),
|
||||||
|
Y = coord.GetValue("y", 0).ToInt32()
|
||||||
|
},
|
||||||
|
BiomeKey = document.GetValue("biomeKey", "plains").AsString,
|
||||||
|
LocationObject = locationObject
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private static VisibleLocationObject? MapVisibleLocationObject(BsonValue value)
|
private static VisibleLocationObject? MapVisibleLocationObject(BsonValue value)
|
||||||
{
|
{
|
||||||
if (value.IsBsonNull || value is not BsonDocument document)
|
if (value.IsBsonNull || value is not BsonDocument document)
|
||||||
@ -228,102 +414,6 @@ public class CharacterStore
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string DetermineBiomeKey(int x, int y)
|
|
||||||
{
|
|
||||||
if (x == 0 && y == 0)
|
|
||||||
return "plains";
|
|
||||||
|
|
||||||
var regionX = FloorDiv(x, 4);
|
|
||||||
var regionY = FloorDiv(y, 4);
|
|
||||||
var roll = Math.Abs(HashCode.Combine(regionX, regionY, 7919)) % 100;
|
|
||||||
if (roll < 35)
|
|
||||||
return "plains";
|
|
||||||
if (roll < 60)
|
|
||||||
return "forest";
|
|
||||||
if (roll < 80)
|
|
||||||
return "rocky";
|
|
||||||
if (roll < 92)
|
|
||||||
return "wetlands";
|
|
||||||
return "desert";
|
|
||||||
}
|
|
||||||
|
|
||||||
private static BsonValue CreateLocationObjectValueForBiome(string biomeKey, int x, int y)
|
|
||||||
{
|
|
||||||
var roll = Math.Abs(HashCode.Combine(x, y, 1543)) % 100;
|
|
||||||
return biomeKey switch
|
|
||||||
{
|
|
||||||
"forest" => roll switch
|
|
||||||
{
|
|
||||||
< 35 => BsonNull.Value,
|
|
||||||
< 80 => CreateGatherableObjectDocument("wood", 60, 3),
|
|
||||||
< 95 => CreateGatherableObjectDocument("grass", 120, 10),
|
|
||||||
_ => CreateGatherableObjectDocument("stone", 40, 2)
|
|
||||||
},
|
|
||||||
"rocky" => roll switch
|
|
||||||
{
|
|
||||||
< 60 => BsonNull.Value,
|
|
||||||
< 90 => CreateGatherableObjectDocument("stone", 40, 2),
|
|
||||||
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
|
||||||
},
|
|
||||||
"wetlands" => roll switch
|
|
||||||
{
|
|
||||||
< 40 => BsonNull.Value,
|
|
||||||
< 90 => CreateGatherableObjectDocument("grass", 120, 10),
|
|
||||||
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
|
||||||
},
|
|
||||||
"desert" => roll switch
|
|
||||||
{
|
|
||||||
< 70 => BsonNull.Value,
|
|
||||||
< 95 => CreateGatherableObjectDocument("stone", 40, 2),
|
|
||||||
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
|
||||||
},
|
|
||||||
_ => roll switch
|
|
||||||
{
|
|
||||||
< 50 => BsonNull.Value,
|
|
||||||
< 85 => CreateGatherableObjectDocument("grass", 120, 10),
|
|
||||||
_ => CreateGatherableObjectDocument("wood", 60, 3)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private static BsonDocument CreateGatherableObjectDocument(string itemKey, int remainingQuantity, int gatherQuantity)
|
|
||||||
{
|
|
||||||
var normalizedItemKey = NormalizeItemKey(itemKey);
|
|
||||||
return new BsonDocument
|
|
||||||
{
|
|
||||||
{ "id", Guid.NewGuid().ToString("N") },
|
|
||||||
{ "objectType", "gatherable" },
|
|
||||||
{ "objectKey", $"{normalizedItemKey}_node" },
|
|
||||||
{ "name", HumanizeItemKey(normalizedItemKey) },
|
|
||||||
{
|
|
||||||
"state", new BsonDocument
|
|
||||||
{
|
|
||||||
{ "itemKey", normalizedItemKey },
|
|
||||||
{ "remainingQuantity", remainingQuantity },
|
|
||||||
{ "gatherQuantity", gatherQuantity }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string NormalizeItemKey(string itemKey) => itemKey.Trim().ToLowerInvariant();
|
|
||||||
|
|
||||||
private static string HumanizeItemKey(string itemKey)
|
|
||||||
{
|
|
||||||
return string.Join(" ", itemKey.Split('_', StringSplitOptions.RemoveEmptyEntries)
|
|
||||||
.Where(part => part.Length > 0)
|
|
||||||
.Select(part => char.ToUpperInvariant(part[0]) + part[1..]));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int FloorDiv(int value, int divisor)
|
|
||||||
{
|
|
||||||
var quotient = value / divisor;
|
|
||||||
var remainder = value % divisor;
|
|
||||||
if (remainder != 0 && ((remainder < 0) != (divisor < 0)))
|
|
||||||
quotient -= 1;
|
|
||||||
return quotient;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EnsureLocationCoordIndexes()
|
private void EnsureLocationCoordIndexes()
|
||||||
{
|
{
|
||||||
var indexes = _locations.Indexes.List().ToList();
|
var indexes = _locations.Indexes.List().ToList();
|
||||||
@ -338,11 +428,7 @@ public class CharacterStore
|
|||||||
_locations.Indexes.DropOne(name);
|
_locations.Indexes.DropOne(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
var coordIndex = new BsonDocument
|
var coordIndex = new BsonDocument { { "coord.x", 1 }, { "coord.y", 1 } };
|
||||||
{
|
|
||||||
{ "coord.x", 1 },
|
|
||||||
{ "coord.y", 1 }
|
|
||||||
};
|
|
||||||
var coordIndexOptions = new CreateIndexOptions { Unique = true, Name = CoordIndexName };
|
var coordIndexOptions = new CreateIndexOptions { Unique = true, Name = CoordIndexName };
|
||||||
_locations.Indexes.CreateOne(new CreateIndexModel<BsonDocument>(coordIndex, coordIndexOptions));
|
_locations.Indexes.CreateOne(new CreateIndexModel<BsonDocument>(coordIndex, coordIndexOptions));
|
||||||
}
|
}
|
||||||
@ -365,14 +451,42 @@ public class CharacterStore
|
|||||||
{
|
{
|
||||||
var filter = Builders<Character>.Filter.Eq(c => c.Id, id);
|
var filter = Builders<Character>.Filter.Eq(c => c.Id, id);
|
||||||
if (!allowAnyOwner)
|
if (!allowAnyOwner)
|
||||||
{
|
filter = Builders<Character>.Filter.And(filter, Builders<Character>.Filter.Eq(c => c.OwnerUserId, ownerUserId));
|
||||||
filter = Builders<Character>.Filter.And(
|
|
||||||
filter,
|
var result = await _col.DeleteOneAsync(filter);
|
||||||
Builders<Character>.Filter.Eq(c => c.OwnerUserId, ownerUserId)
|
return result.DeletedCount > 0;
|
||||||
);
|
}
|
||||||
}
|
|
||||||
|
private static string DefaultLocationName(int x, int y)
|
||||||
var result = await _col.DeleteOneAsync(filter);
|
{
|
||||||
return result.DeletedCount > 0;
|
if (x == 0 && y == 0)
|
||||||
}
|
return "Origin";
|
||||||
}
|
return $"Location {x},{y}";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string NormalizeItemKey(string itemKey) => itemKey.Trim().ToLowerInvariant();
|
||||||
|
|
||||||
|
private static string HumanizeItemKey(string itemKey)
|
||||||
|
{
|
||||||
|
return string.Join(" ", itemKey.Split('_', StringSplitOptions.RemoveEmptyEntries)
|
||||||
|
.Where(part => part.Length > 0)
|
||||||
|
.Select(part => char.ToUpperInvariant(part[0]) + part[1..]));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double StableNoise(int x, int y, int salt)
|
||||||
|
{
|
||||||
|
var value = Math.Sin((x * 12.9898) + (y * 78.233) + ((WorldSeed + salt) * 0.1597)) * 43758.5453;
|
||||||
|
return value - Math.Floor(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int StableHash(string value)
|
||||||
|
{
|
||||||
|
unchecked
|
||||||
|
{
|
||||||
|
var hash = 17;
|
||||||
|
foreach (var ch in value)
|
||||||
|
hash = (hash * 31) + ch;
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -39,6 +39,7 @@ Stored documents (MongoDB)
|
|||||||
"x": 0,
|
"x": 0,
|
||||||
"y": 0
|
"y": 0
|
||||||
},
|
},
|
||||||
|
"biomeKey": "plains",
|
||||||
"resources": [
|
"resources": [
|
||||||
{
|
{
|
||||||
"itemKey": "wood",
|
"itemKey": "wood",
|
||||||
|
|||||||
@ -1,32 +1,32 @@
|
|||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
using MongoDB.Bson.Serialization.Attributes;
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
|
|
||||||
namespace LocationsApi.Models;
|
namespace LocationsApi.Models;
|
||||||
|
|
||||||
public class Location
|
public class Location
|
||||||
{
|
{
|
||||||
[BsonId]
|
[BsonId]
|
||||||
[BsonRepresentation(BsonType.ObjectId)]
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
public string? Id { get; set; }
|
public string? Id { get; set; }
|
||||||
|
|
||||||
[BsonElement("name")]
|
[BsonElement("name")]
|
||||||
public string Name { get; set; } = string.Empty;
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
[BsonElement("coord")]
|
[BsonElement("coord")]
|
||||||
public required Coord Coord { get; set; }
|
public required Coord Coord { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("biomeKey")]
|
||||||
|
public string BiomeKey { get; set; } = "plains";
|
||||||
|
|
||||||
[BsonElement("resources")]
|
[BsonElement("resources")]
|
||||||
public List<LocationResource> Resources { get; set; } = [];
|
public List<LocationResource> Resources { get; set; } = [];
|
||||||
|
|
||||||
[BsonElement("biomeKey")]
|
|
||||||
public string BiomeKey { get; set; } = string.Empty;
|
|
||||||
|
|
||||||
[BsonElement("locationObject")]
|
[BsonElement("locationObject")]
|
||||||
public LocationObject? LocationObject { get; set; }
|
public LocationObject? LocationObject { get; set; }
|
||||||
|
|
||||||
[BsonElement("locationObjectResolved")]
|
[BsonElement("locationObjectResolved")]
|
||||||
public bool LocationObjectResolved { get; set; }
|
public bool LocationObjectResolved { get; set; }
|
||||||
|
|
||||||
[BsonElement("createdUtc")]
|
[BsonElement("createdUtc")]
|
||||||
public DateTime CreatedUtc { get; set; } = DateTime.UtcNow;
|
public DateTime CreatedUtc { get; set; } = DateTime.UtcNow;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -36,7 +36,7 @@ public class LocationStore
|
|||||||
"$jsonSchema", new BsonDocument
|
"$jsonSchema", new BsonDocument
|
||||||
{
|
{
|
||||||
{ "bsonType", "object" },
|
{ "bsonType", "object" },
|
||||||
{ "required", new BsonArray { "name", "coord", "createdUtc" } },
|
{ "required", new BsonArray { "name", "coord", "biomeKey", "createdUtc" } },
|
||||||
{
|
{
|
||||||
"properties", new BsonDocument
|
"properties", new BsonDocument
|
||||||
{
|
{
|
||||||
@ -55,6 +55,7 @@ public class LocationStore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{ "biomeKey", new BsonDocument { { "bsonType", "string" } } },
|
||||||
{
|
{
|
||||||
"resources", new BsonDocument
|
"resources", new BsonDocument
|
||||||
{
|
{
|
||||||
@ -76,7 +77,6 @@ public class LocationStore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ "biomeKey", new BsonDocument { { "bsonType", new BsonArray { "string", "null" } } } },
|
|
||||||
{
|
{
|
||||||
"locationObject", new BsonDocument
|
"locationObject", new BsonDocument
|
||||||
{
|
{
|
||||||
@ -273,9 +273,9 @@ public class LocationStore
|
|||||||
{
|
{
|
||||||
var filter = Builders<Location>.Filter.And(
|
var filter = Builders<Location>.Filter.And(
|
||||||
Builders<Location>.Filter.Eq(l => l.Coord.X, 0),
|
Builders<Location>.Filter.Eq(l => l.Coord.X, 0),
|
||||||
Builders<Location>.Filter.Eq(l => l.Coord.Y, 0)
|
Builders<Location>.Filter.Eq(l => l.Coord.Y, 0)
|
||||||
);
|
);
|
||||||
var existing = _col.Find(filter).FirstOrDefault();
|
var existing = _col.Find(filter).FirstOrDefault();
|
||||||
if (existing is not null)
|
if (existing is not null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user