|
|
|
@@ -70,6 +70,12 @@ public class CharacterStore
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var documents = await _locations.Find(filter).ToListAsync();
|
|
|
|
|
if (ensureGenerated)
|
|
|
|
|
{
|
|
|
|
|
foreach (var document in documents)
|
|
|
|
|
await EnsureLocationObjectAsync(document);
|
|
|
|
|
documents = await _locations.Find(filter).ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
return documents.Select(MapVisibleLocation).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -95,6 +101,9 @@ public class CharacterStore
|
|
|
|
|
{ "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
|
|
|
|
@@ -123,6 +132,7 @@ public class CharacterStore
|
|
|
|
|
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)
|
|
|
|
@@ -140,10 +150,180 @@ public class CharacterStore
|
|
|
|
|
{
|
|
|
|
|
X = coord.GetValue("x", 0).ToInt32(),
|
|
|
|
|
Y = coord.GetValue("y", 0).ToInt32()
|
|
|
|
|
},
|
|
|
|
|
BiomeKey = document.GetValue("biomeKey", "").AsString,
|
|
|
|
|
LocationObject = locationObject
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task EnsureLocationObjectAsync(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 x = coord.GetValue("x", 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 filter = Builders<BsonDocument>.Filter.And(
|
|
|
|
|
Builders<BsonDocument>.Filter.Eq("_id", idValue)
|
|
|
|
|
);
|
|
|
|
|
var update = Builders<BsonDocument>.Update
|
|
|
|
|
.Set("biomeKey", biomeKey)
|
|
|
|
|
.Set("locationObjectResolved", true)
|
|
|
|
|
.Set("locationObject", locationObject);
|
|
|
|
|
await _locations.UpdateOneAsync(filter, update);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static BsonDocument? TryMigrateLegacyResource(BsonDocument document)
|
|
|
|
|
{
|
|
|
|
|
if (!document.TryGetValue("resources", out var resourcesValue) || resourcesValue is not BsonArray resources)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
foreach (var resourceValue in resources)
|
|
|
|
|
{
|
|
|
|
|
if (resourceValue is not BsonDocument resource)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var remainingQuantity = resource.GetValue("remainingQuantity", 0).ToInt32();
|
|
|
|
|
if (remainingQuantity <= 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var itemKey = NormalizeItemKey(resource.GetValue("itemKey", "").AsString);
|
|
|
|
|
var gatherQuantity = Math.Max(1, resource.GetValue("gatherQuantity", 1).ToInt32());
|
|
|
|
|
return CreateGatherableObjectDocument(itemKey, remainingQuantity, gatherQuantity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static VisibleLocationObject? MapVisibleLocationObject(BsonValue value)
|
|
|
|
|
{
|
|
|
|
|
if (value.IsBsonNull || value is not BsonDocument document)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var stateDoc = document.GetValue("state", new BsonDocument()).AsBsonDocument;
|
|
|
|
|
return new VisibleLocationObject
|
|
|
|
|
{
|
|
|
|
|
Id = document.GetValue("id", "").AsString,
|
|
|
|
|
ObjectType = document.GetValue("objectType", "").AsString,
|
|
|
|
|
ObjectKey = document.GetValue("objectKey", "").AsString,
|
|
|
|
|
Name = document.GetValue("name", "").AsString,
|
|
|
|
|
State = new VisibleLocationObjectState
|
|
|
|
|
{
|
|
|
|
|
ItemKey = stateDoc.GetValue("itemKey", "").AsString,
|
|
|
|
|
RemainingQuantity = stateDoc.GetValue("remainingQuantity", 0).ToInt32(),
|
|
|
|
|
GatherQuantity = stateDoc.GetValue("gatherQuantity", 1).ToInt32()
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
var indexes = _locations.Indexes.List().ToList();
|
|
|
|
|