Adding loggin to troubleshoot location generation issue
All checks were successful
All checks were successful
This commit is contained in:
parent
3b2cc9721b
commit
9b646f501c
@ -241,6 +241,7 @@ func _load_existing_locations() -> void:
|
|||||||
_locations_loaded = true
|
_locations_loaded = true
|
||||||
return
|
return
|
||||||
|
|
||||||
|
var loaded_count := 0
|
||||||
for item in parsed:
|
for item in parsed:
|
||||||
if typeof(item) != TYPE_DICTIONARY:
|
if typeof(item) != TYPE_DICTIONARY:
|
||||||
continue
|
continue
|
||||||
@ -254,5 +255,10 @@ func _load_existing_locations() -> void:
|
|||||||
if location_name.is_empty():
|
if location_name.is_empty():
|
||||||
location_name = "Location %d,%d" % [coord.x, coord.y]
|
location_name = "Location %d,%d" % [coord.x, coord.y]
|
||||||
_known_locations[coord] = location_name
|
_known_locations[coord] = location_name
|
||||||
|
loaded_count += 1
|
||||||
|
|
||||||
|
print("LocationLevel loaded %d visible locations for character %s." % [loaded_count, character_id])
|
||||||
|
if loaded_count == 0:
|
||||||
|
push_warning("Visible locations request succeeded but returned 0 locations for character %s." % character_id)
|
||||||
|
|
||||||
_locations_loaded = true
|
_locations_loaded = true
|
||||||
|
|||||||
@ -11,10 +11,12 @@ namespace CharacterApi.Controllers;
|
|||||||
public class CharactersController : ControllerBase
|
public class CharactersController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly CharacterStore _characters;
|
private readonly CharacterStore _characters;
|
||||||
|
private readonly ILogger<CharactersController> _logger;
|
||||||
|
|
||||||
public CharactersController(CharacterStore characters)
|
public CharactersController(CharacterStore characters, ILogger<CharactersController> logger)
|
||||||
{
|
{
|
||||||
_characters = characters;
|
_characters = characters;
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
@ -66,7 +68,25 @@ public class CharactersController : ControllerBase
|
|||||||
if (character is null)
|
if (character is null)
|
||||||
return NotFound();
|
return NotFound();
|
||||||
|
|
||||||
var locations = await _characters.GetOrCreateVisibleLocationsAsync(character);
|
_logger.LogInformation(
|
||||||
|
"Visible locations requested for character {CharacterId} at ({X},{Y}) radius {VisionRadius} by user {UserId}",
|
||||||
|
character.Id,
|
||||||
|
character.Coord.X,
|
||||||
|
character.Coord.Y,
|
||||||
|
character.VisionRadius,
|
||||||
|
userId
|
||||||
|
);
|
||||||
|
|
||||||
|
var generation = await _characters.GetOrCreateVisibleLocationsAsync(character);
|
||||||
|
var locations = generation.Locations;
|
||||||
|
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Visible locations resolved for character {CharacterId}: generated {GeneratedCount}, returned {ReturnedCount}",
|
||||||
|
character.Id,
|
||||||
|
generation.GeneratedCount,
|
||||||
|
locations.Count
|
||||||
|
);
|
||||||
|
|
||||||
return Ok(locations);
|
return Ok(locations);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -9,6 +9,8 @@ public class CharacterStore
|
|||||||
private readonly IMongoCollection<Character> _col;
|
private readonly IMongoCollection<Character> _col;
|
||||||
private readonly IMongoCollection<VisibleLocation> _locations;
|
private readonly IMongoCollection<VisibleLocation> _locations;
|
||||||
|
|
||||||
|
public sealed record VisibleLocationResult(List<VisibleLocation> Locations, int GeneratedCount);
|
||||||
|
|
||||||
public CharacterStore(IConfiguration cfg)
|
public CharacterStore(IConfiguration cfg)
|
||||||
{
|
{
|
||||||
var cs = cfg["MongoDB:ConnectionString"] ?? "mongodb://127.0.0.1:27017";
|
var cs = cfg["MongoDB:ConnectionString"] ?? "mongodb://127.0.0.1:27017";
|
||||||
@ -46,10 +48,11 @@ public class CharacterStore
|
|||||||
return GetVisibleLocationsInternalAsync(character, ensureGenerated: false);
|
return GetVisibleLocationsInternalAsync(character, ensureGenerated: false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<VisibleLocation>> GetOrCreateVisibleLocationsAsync(Character character)
|
public async Task<VisibleLocationResult> GetOrCreateVisibleLocationsAsync(Character character)
|
||||||
{
|
{
|
||||||
await EnsureVisibleLocationsExistAsync(character);
|
var generatedCount = await EnsureVisibleLocationsExistAsync(character);
|
||||||
return await GetVisibleLocationsInternalAsync(character, ensureGenerated: true);
|
var locations = await GetVisibleLocationsInternalAsync(character, ensureGenerated: true);
|
||||||
|
return new VisibleLocationResult(locations, generatedCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task<List<VisibleLocation>> GetVisibleLocationsInternalAsync(Character character, bool ensureGenerated)
|
private Task<List<VisibleLocation>> GetVisibleLocationsInternalAsync(Character character, bool ensureGenerated)
|
||||||
@ -70,9 +73,10 @@ public class CharacterStore
|
|||||||
return _locations.Find(filter).ToListAsync();
|
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 radius = character.VisionRadius > 0 ? character.VisionRadius : 3;
|
||||||
|
var generatedCount = 0;
|
||||||
|
|
||||||
for (var x = character.Coord.X - radius; x <= character.Coord.X + radius; x++)
|
for (var x = character.Coord.X - radius; x <= character.Coord.X + radius; x++)
|
||||||
{
|
{
|
||||||
@ -91,7 +95,9 @@ public class CharacterStore
|
|||||||
|
|
||||||
try
|
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)
|
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)
|
private static string DefaultLocationName(int x, int y)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user