Adding loggin to troubleshoot location generation issue
This commit is contained in:
@@ -9,6 +9,8 @@ public class CharacterStore
|
||||
private readonly IMongoCollection<Character> _col;
|
||||
private readonly IMongoCollection<VisibleLocation> _locations;
|
||||
|
||||
public sealed record VisibleLocationResult(List<VisibleLocation> Locations, int GeneratedCount);
|
||||
|
||||
public CharacterStore(IConfiguration cfg)
|
||||
{
|
||||
var cs = cfg["MongoDB:ConnectionString"] ?? "mongodb://127.0.0.1:27017";
|
||||
@@ -46,10 +48,11 @@ public class CharacterStore
|
||||
return GetVisibleLocationsInternalAsync(character, ensureGenerated: false);
|
||||
}
|
||||
|
||||
public async Task<List<VisibleLocation>> GetOrCreateVisibleLocationsAsync(Character character)
|
||||
public async Task<VisibleLocationResult> GetOrCreateVisibleLocationsAsync(Character character)
|
||||
{
|
||||
await EnsureVisibleLocationsExistAsync(character);
|
||||
return await GetVisibleLocationsInternalAsync(character, ensureGenerated: true);
|
||||
var generatedCount = await EnsureVisibleLocationsExistAsync(character);
|
||||
var locations = await GetVisibleLocationsInternalAsync(character, ensureGenerated: true);
|
||||
return new VisibleLocationResult(locations, generatedCount);
|
||||
}
|
||||
|
||||
private Task<List<VisibleLocation>> GetVisibleLocationsInternalAsync(Character character, bool ensureGenerated)
|
||||
@@ -70,9 +73,10 @@ public class CharacterStore
|
||||
return _locations.Find(filter).ToListAsync();
|
||||
}
|
||||
|
||||
private async Task EnsureVisibleLocationsExistAsync(Character character)
|
||||
private async Task<int> EnsureVisibleLocationsExistAsync(Character character)
|
||||
{
|
||||
var radius = character.VisionRadius > 0 ? character.VisionRadius : 3;
|
||||
var generatedCount = 0;
|
||||
|
||||
for (var x = character.Coord.X - radius; x <= character.Coord.X + radius; x++)
|
||||
{
|
||||
@@ -91,7 +95,9 @@ public class CharacterStore
|
||||
|
||||
try
|
||||
{
|
||||
await _locations.UpdateOneAsync(filter, update, new UpdateOptions { IsUpsert = true });
|
||||
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)
|
||||
{
|
||||
@@ -99,6 +105,8 @@ public class CharacterStore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return generatedCount;
|
||||
}
|
||||
|
||||
private static string DefaultLocationName(int x, int y)
|
||||
|
||||
Reference in New Issue
Block a user