Persist player overworld location and add world mob spawn editing/runtime payloads

This commit is contained in:
dingo
2026-02-15 08:47:09 -05:00
parent 3c03bc40fa
commit d6b691e534
7 changed files with 330 additions and 18 deletions
+56 -12
View File
@@ -89,6 +89,7 @@ public sealed class WorldHandler : IClientMessageHandler
{
if (_players.TryRemove(connectionId, out var s))
{
_ = PersistPlayerLocationAsync(s);
_ = BroadcastAsync(OpCodes.WORLD_DESPAWN, new { playerId = s.CharacterId, reason = "disconnect" }, s.RegionId, s.ConnectionId);
}
}
@@ -110,18 +111,25 @@ public sealed class WorldHandler : IClientMessageHandler
await db.SaveChangesAsync(ct);
}
var region = await ResolveRegionAsync(db, req.RegionId, ct);
var region = await ResolveRegionAsync(db, req.RegionId ?? character.WorldRegionId, ct);
if (region == null) { await SendErrorAsync(conn, "region_not_found", ct); return; }
await EnsureZonesAsync(db, region, ct);
var zx = Math.Clamp(req.ZoneX ?? 0, 0, Math.Max(0, region.ZonesPerRegionX - 1));
var zy = Math.Clamp(req.ZoneY ?? 0, 0, Math.Max(0, region.ZonesPerRegionY - 1));
var cx = Math.Clamp(req.ChunkX ?? 0, 0, Math.Max(0, region.ChunksPerZoneX - 1));
var cy = Math.Clamp(req.ChunkY ?? 0, 0, Math.Max(0, region.ChunksPerZoneY - 1));
var requestedExplicitChunk = req.ZoneX.HasValue || req.ZoneY.HasValue || req.ChunkX.HasValue || req.ChunkY.HasValue;
var zx = Math.Clamp(req.ZoneX ?? character.WorldZoneX ?? 0, 0, Math.Max(0, region.ZonesPerRegionX - 1));
var zy = Math.Clamp(req.ZoneY ?? character.WorldZoneY ?? 0, 0, Math.Max(0, region.ZonesPerRegionY - 1));
var cx = Math.Clamp(req.ChunkX ?? character.WorldChunkX ?? 0, 0, Math.Max(0, region.ChunksPerZoneX - 1));
var cy = Math.Clamp(req.ChunkY ?? character.WorldChunkY ?? 0, 0, Math.Max(0, region.ChunksPerZoneY - 1));
var tx = Math.Max(0, Math.Min(region.ChunkWidthTiles - 1, region.ChunkWidthTiles / 2));
var ty = Math.Max(0, Math.Min(region.ChunkHeightTiles - 1, region.ChunkHeightTiles / 2));
var x = WorldPxX(region, zx, cx, tx);
var y = WorldPxY(region, zy, cy, ty);
var x = requestedExplicitChunk
? WorldPxX(region, zx, cx, tx)
: (character.WorldX ?? WorldPxX(region, zx, cx, tx));
var y = requestedExplicitChunk
? WorldPxY(region, zy, cy, ty)
: (character.WorldY ?? WorldPxY(region, zy, cy, ty));
if (!float.IsFinite(x) || x < 0f) x = WorldPxX(region, zx, cx, tx);
if (!float.IsFinite(y) || y < 0f) y = WorldPxY(region, zy, cy, ty);
var (pzX, pzY, pcX, pcY) = ToChunkPos(region, x, y);
var s = new SessionState
@@ -142,6 +150,16 @@ public sealed class WorldHandler : IClientMessageHandler
};
_players[conn.Id] = s;
character.WorldRegionId = s.RegionId;
character.WorldZoneX = s.ZoneX;
character.WorldZoneY = s.ZoneY;
character.WorldChunkX = s.ChunkX;
character.WorldChunkY = s.ChunkY;
character.WorldX = s.X;
character.WorldY = s.Y;
character.LastPlayedAt = DateTime.UtcNow;
await db.SaveChangesAsync(ct);
var initial = await ChunkUpdatesAsync(db, region, s, ct);
await SendAsync(conn, OpCodes.WORLD_SPAWN, new
{
@@ -250,7 +268,7 @@ public sealed class WorldHandler : IClientMessageHandler
if (!s.ChunkVersions.TryGetValue(key, out var known) || known != data.version)
{
s.ChunkVersions[key] = data.version;
list.Add(("chunk_full", new { type = "chunk_full", chunk = data.chunk, tiles = data.tiles, objects = data.objects, npcs = data.npcs }));
list.Add(("chunk_full", new { type = "chunk_full", chunk = data.chunk, tiles = data.tiles, objects = data.objects, npcs = data.npcs, mobs = data.mobs }));
}
}
foreach (var stale in s.ChunkVersions.Keys.Where(k => !required.Contains(k)).ToList())
@@ -262,20 +280,21 @@ public sealed class WorldHandler : IClientMessageHandler
return list;
}
private async Task<(long version, object chunk, object[] tiles, object[] objects, object[] npcs)> LoadChunkAsync(GameDbContext db, WorldRegion region, int zoneX, int zoneY, int chunkX, int chunkY, CancellationToken ct)
private async Task<(long version, object chunk, object[] tiles, object[] objects, object[] npcs, object[] mobs)> LoadChunkAsync(GameDbContext db, WorldRegion region, int zoneX, int zoneY, int chunkX, int chunkY, CancellationToken ct)
{
var zone = await db.WorldZones.AsNoTracking().FirstOrDefaultAsync(z => z.RegionId == region.Id && z.ZoneX == zoneX && z.ZoneY == zoneY && z.IsEnabled, ct);
if (zone == null)
return (0, new { regionId = region.Id, zoneX, zoneY, chunkX, chunkY, version = 0, width = region.ChunkWidthTiles, height = region.ChunkHeightTiles }, Array.Empty<object>(), Array.Empty<object>(), Array.Empty<object>());
return (0, new { regionId = region.Id, zoneX, zoneY, chunkX, chunkY, version = 0, width = region.ChunkWidthTiles, height = region.ChunkHeightTiles }, Array.Empty<object>(), Array.Empty<object>(), Array.Empty<object>(), Array.Empty<object>());
var chunk = await db.WorldChunks.AsNoTracking().FirstOrDefaultAsync(c => c.WorldZoneId == zone.Id && c.ChunkX == chunkX && c.ChunkY == chunkY, ct);
if (chunk == null)
return (0, new { regionId = region.Id, zoneX, zoneY, chunkX, chunkY, version = 0, width = region.ChunkWidthTiles, height = region.ChunkHeightTiles }, Array.Empty<object>(), Array.Empty<object>(), Array.Empty<object>());
return (0, new { regionId = region.Id, zoneX, zoneY, chunkX, chunkY, version = 0, width = region.ChunkWidthTiles, height = region.ChunkHeightTiles }, Array.Empty<object>(), Array.Empty<object>(), Array.Empty<object>(), Array.Empty<object>());
var tiles = await db.WorldChunkTiles.AsNoTracking().Where(t => t.WorldChunkId == chunk.Id).OrderBy(t => t.TileY).ThenBy(t => t.TileX).Select(t => new { id = t.Id, x = t.TileX, y = t.TileY, sheetId = t.SheetId, frameIndex = t.FrameIndex, isWalkable = t.IsWalkable, isInvisiblePaywall = t.IsInvisiblePaywall }).ToArrayAsync(ct);
var objects = await db.WorldChunkObjects.AsNoTracking().Include(o => o.ObjectDefinition).Where(o => o.WorldChunkId == chunk.Id).OrderBy(o => o.TileY).ThenBy(o => o.TileX).Select(o => new { id = o.Id, x = o.TileX, y = o.TileY, objectDefinitionId = o.ObjectDefinitionId, objectName = o.ObjectDefinition != null ? o.ObjectDefinition.Name : $"Object {o.ObjectDefinitionId}", rotation = o.Rotation, metadataJson = o.MetadataJson }).ToArrayAsync(ct);
var npcs = await db.WorldChunkNpcSpawns.AsNoTracking().Include(n => n.NpcDefinition).Where(n => n.WorldChunkId == chunk.Id).OrderBy(n => n.TileY).ThenBy(n => n.TileX).Select(n => new { id = n.Id, x = n.TileX, y = n.TileY, npcDefinitionId = n.NpcDefinitionId, npcName = n.NpcDefinition != null ? n.NpcDefinition.Name : $"NPC {n.NpcDefinitionId}", rotation = n.Rotation, metadataJson = n.MetadataJson }).ToArrayAsync(ct);
return (chunk.Version, new { id = chunk.Id, regionId = region.Id, zoneX, zoneY, chunkX, chunkY, version = chunk.Version, width = region.ChunkWidthTiles, height = region.ChunkHeightTiles }, tiles.Cast<object>().ToArray(), objects.Cast<object>().ToArray(), npcs.Cast<object>().ToArray());
var mobs = await db.WorldChunkMobSpawns.AsNoTracking().Include(m => m.MobDefinition).Where(m => m.WorldChunkId == chunk.Id).OrderBy(m => m.TileY).ThenBy(m => m.TileX).Select(m => new { id = m.Id, x = m.TileX, y = m.TileY, mobDefinitionId = m.MobDefinitionId, mobName = m.MobDefinition != null ? m.MobDefinition.Name : $"Mob {m.MobDefinitionId}", rotation = m.Rotation, spawnCount = m.SpawnCount, respawnSeconds = m.RespawnSeconds, metadataJson = m.MetadataJson }).ToArrayAsync(ct);
return (chunk.Version, new { id = chunk.Id, regionId = region.Id, zoneX, zoneY, chunkX, chunkY, version = chunk.Version, width = region.ChunkWidthTiles, height = region.ChunkHeightTiles }, tiles.Cast<object>().ToArray(), objects.Cast<object>().ToArray(), npcs.Cast<object>().ToArray(), mobs.Cast<object>().ToArray());
}
private async Task<bool> WalkableAsync(GameDbContext db, WorldRegion region, float wx, float wy, bool isVip, CancellationToken ct)
@@ -408,6 +427,31 @@ public sealed class WorldHandler : IClientMessageHandler
await Task.WhenAll(tasks);
}
private async Task PersistPlayerLocationAsync(SessionState s)
{
try
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
var character = await db.DungeonCharacters.FirstOrDefaultAsync(c => c.Id == s.CharacterId);
if (character == null) return;
character.WorldRegionId = s.RegionId;
character.WorldZoneX = s.ZoneX;
character.WorldZoneY = s.ZoneY;
character.WorldChunkX = s.ChunkX;
character.WorldChunkY = s.ChunkY;
character.WorldX = s.X;
character.WorldY = s.Y;
character.LastPlayedAt = DateTime.UtcNow;
await db.SaveChangesAsync();
}
catch (Exception ex)
{
_log.LogWarning(ex, "Failed to persist world location for character {CharacterId}", s.CharacterId);
}
}
private async Task SendAsync(IConnection connection, int opCode, object payload, CancellationToken ct)
{
try