diff --git a/Program.cs b/Program.cs index 678e1e8..9f2ef0c 100644 --- a/Program.cs +++ b/Program.cs @@ -108,6 +108,7 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddHttpClient(); // For CDN file hash computation // Health checks diff --git a/Services/Admin/Pages/WorldEditor/Index.cshtml b/Services/Admin/Pages/WorldEditor/Index.cshtml index 8a7691c..be81686 100644 --- a/Services/Admin/Pages/WorldEditor/Index.cshtml +++ b/Services/Admin/Pages/WorldEditor/Index.cshtml @@ -169,7 +169,7 @@ let currentTiles = new Map(); let currentObjects = []; let currentNpcs = []; - let pollTimer = null; + let eventSource = null; const regionCanvas = document.getElementById('regionCanvas'); const regionCtx = regionCanvas.getContext('2d'); @@ -263,26 +263,73 @@ currentNpcs = data.npcs || []; document.getElementById('chunkMeta').textContent = `Chunk ${s.zoneX}:${s.zoneY}:${s.chunkX}:${s.chunkY} v${currentChunk.version} (${currentChunk.width}x${currentChunk.height})`; renderChunk(); - - if (pollTimer) clearInterval(pollTimer); - pollTimer = setInterval(checkChunkVersion, 2000); + startStream(); } - async function checkChunkVersion() { + function startStream() { if (!currentChunk) return; - const s = selection(); - const data = await getJson(`/admin/worldeditor?handler=Chunk®ionId=${s.regionId}&zoneX=${s.zoneX}&zoneY=${s.zoneY}&chunkX=${s.chunkX}&chunkY=${s.chunkY}`); - if (!data.success) return; - if (data.chunk.version !== currentChunk.version) { - currentChunk = data.chunk; - currentTiles = new Map(); - for (const t of data.tiles) currentTiles.set(key(t.x, t.y), t); - currentObjects = data.objects || []; - currentNpcs = data.npcs || []; - document.getElementById('chunkMeta').textContent = `Chunk ${s.zoneX}:${s.zoneY}:${s.chunkX}:${s.chunkY} v${currentChunk.version} (${currentChunk.width}x${currentChunk.height})`; - renderChunk(); - loadZone(); + if (eventSource) { + eventSource.close(); + eventSource = null; } + + const s = selection(); + const url = `/admin/worldeditor?handler=Stream®ionId=${s.regionId}&zoneX=${s.zoneX}&zoneY=${s.zoneY}&chunkX=${s.chunkX}&chunkY=${s.chunkY}`; + eventSource = new EventSource(url); + eventSource.onmessage = (evt) => { + if (!evt.data) return; + try { + const delta = JSON.parse(evt.data); + applyDelta(delta); + } catch { + // ignore malformed stream payload + } + }; + } + + function applyDelta(delta) { + if (!currentChunk || !delta || delta.kind !== 'chunk_delta' || !delta.op) return; + if (delta.toVersion <= currentChunk.version) return; + + if (delta.fromVersion !== currentChunk.version) { + loadChunk(); + return; + } + + const op = delta.op; + if (op.type === 'tile_upsert') { + currentTiles.set(key(op.x, op.y), { + x: op.x, + y: op.y, + sheetId: op.sheetId, + frameIndex: op.frameIndex, + isWalkable: op.isWalkable, + isInvisiblePaywall: op.isInvisiblePaywall + }); + } else if (op.type === 'tile_remove') { + currentTiles.delete(key(op.x, op.y)); + } else if (op.type === 'object_upsert') { + const idx = currentObjects.findIndex(o => o.id === op.id); + if (idx >= 0) currentObjects[idx] = { ...currentObjects[idx], ...op }; + else currentObjects.push({ ...op }); + } else if (op.type === 'object_remove') { + currentObjects = currentObjects.filter(o => o.id !== op.id); + } else if (op.type === 'npc_upsert') { + const idx = currentNpcs.findIndex(n => n.id === op.id); + if (idx >= 0) currentNpcs[idx] = { ...currentNpcs[idx], ...op }; + else currentNpcs.push({ ...op }); + } else if (op.type === 'npc_remove') { + currentNpcs = currentNpcs.filter(n => n.id !== op.id); + } else { + loadChunk(); + return; + } + + currentChunk.version = delta.toVersion; + const s = selection(); + document.getElementById('chunkMeta').textContent = `Chunk ${s.zoneX}:${s.zoneY}:${s.chunkX}:${s.chunkY} v${currentChunk.version} (${currentChunk.width}x${currentChunk.height})`; + renderChunk(); + loadZone(); } function renderRegionMap() { @@ -526,6 +573,13 @@ loadZone(); }); + window.addEventListener('beforeunload', () => { + if (eventSource) { + eventSource.close(); + eventSource = null; + } + }); + initSelectors(); loadRegion().then(loadChunk); diff --git a/Services/Admin/Pages/WorldEditor/Index.cshtml.cs b/Services/Admin/Pages/WorldEditor/Index.cshtml.cs index 1d992ef..d7e1cdc 100644 --- a/Services/Admin/Pages/WorldEditor/Index.cshtml.cs +++ b/Services/Admin/Pages/WorldEditor/Index.cshtml.cs @@ -1,17 +1,21 @@ using DataServer.Data; using DataServer.Data.Entities; +using GameServices; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using System.Text; namespace GameServices.Admin.Pages.WorldEditor; public class IndexModel : AdminPageModel { private readonly GameDbContext _db; + private readonly WorldEditDeltaHub _deltaHub; - public IndexModel(GameDbContext db) + public IndexModel(GameDbContext db, WorldEditDeltaHub deltaHub) { _db = db; + _deltaHub = deltaHub; } public List Regions { get; set; } = new(); @@ -222,6 +226,53 @@ public class IndexModel : AdminPageModel }); } + public async Task OnGetStreamAsync(int regionId, int zoneX, int zoneY, int chunkX, int chunkY, CancellationToken ct) + { + var locate = await GetOrCreateChunkAsync(regionId, zoneX, zoneY, chunkX, chunkY); + if (!locate.Success || locate.Chunk == null) + { + return new JsonResult(new { success = false, error = locate.Error }) { StatusCode = 400 }; + } + + var key = ChunkKey(regionId, zoneX, zoneY, chunkX, chunkY); + using var subscription = _deltaHub.Subscribe(key); + + Response.Headers["Content-Type"] = "text/event-stream"; + Response.Headers["Cache-Control"] = "no-cache"; + Response.Headers["Connection"] = "keep-alive"; + Response.Headers["X-Accel-Buffering"] = "no"; + + while (!ct.IsCancellationRequested) + { + try + { + var readTask = subscription.Reader.ReadAsync(ct).AsTask(); + var heartbeatTask = Task.Delay(TimeSpan.FromSeconds(15), ct); + var completed = await Task.WhenAny(readTask, heartbeatTask); + + if (completed == readTask) + { + var json = await readTask; + var data = Encoding.UTF8.GetBytes($"data: {json}\n\n"); + await Response.Body.WriteAsync(data, ct); + await Response.Body.FlushAsync(ct); + } + else + { + var heartbeat = Encoding.UTF8.GetBytes(": ping\n\n"); + await Response.Body.WriteAsync(heartbeat, ct); + await Response.Body.FlushAsync(ct); + } + } + catch (OperationCanceledException) + { + break; + } + } + + return new EmptyResult(); + } + public async Task OnPostUpsertTileAsync( int regionId, int zoneX, @@ -271,24 +322,30 @@ public class IndexModel : AdminPageModel tile.IsWalkable = isWalkable; tile.IsInvisiblePaywall = isInvisiblePaywall; + var fromVersion = locate.Chunk.Version; locate.Chunk.Version++; locate.Chunk.UpdatedAt = DateTime.UtcNow; await _db.SaveChangesAsync(); + var op = new + { + type = "tile_upsert", + x, + y, + sheetId, + frameIndex, + isWalkable, + isInvisiblePaywall + }; + PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op); + return new JsonResult(new { success = true, chunkVersion = locate.Chunk.Version, - op = new - { - type = "tile_upsert", - x, - y, - sheetId, - frameIndex, - isWalkable, - isInvisiblePaywall - } + fromVersion, + toVersion = locate.Chunk.Version, + op }); } @@ -304,10 +361,14 @@ public class IndexModel : AdminPageModel .FirstOrDefaultAsync(t => t.WorldChunkId == locate.Chunk.Id && t.TileX == x && t.TileY == y); if (tile != null) { + var fromVersion = locate.Chunk.Version; _db.WorldChunkTiles.Remove(tile); locate.Chunk.Version++; locate.Chunk.UpdatedAt = DateTime.UtcNow; await _db.SaveChangesAsync(); + + var op = new { type = "tile_remove", x, y }; + PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op); } return new JsonResult(new @@ -342,8 +403,8 @@ public class IndexModel : AdminPageModel return new JsonResult(new { success = false, error = "tile_out_of_bounds" }) { StatusCode = 400 }; } - var objectExists = await _db.DungeonObjects.AnyAsync(o => o.Id == objectDefinitionId && o.IsEnabled); - if (!objectExists) + var objectDef = await _db.DungeonObjects.FirstOrDefaultAsync(o => o.Id == objectDefinitionId && o.IsEnabled); + if (objectDef == null) { return new JsonResult(new { success = false, error = "object_definition_not_found" }) { StatusCode = 404 }; } @@ -370,24 +431,31 @@ public class IndexModel : AdminPageModel obj.Rotation = rotation; obj.MetadataJson = string.IsNullOrWhiteSpace(metadataJson) ? null : metadataJson.Trim(); + var fromVersion = locate.Chunk.Version; locate.Chunk.Version++; locate.Chunk.UpdatedAt = DateTime.UtcNow; await _db.SaveChangesAsync(); + var op = new + { + type = "object_upsert", + id = obj.Id, + x = obj.TileX, + y = obj.TileY, + objectDefinitionId = obj.ObjectDefinitionId, + objectName = objectDef.Name, + rotation = obj.Rotation, + metadataJson = obj.MetadataJson + }; + PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op); + return new JsonResult(new { success = true, chunkVersion = locate.Chunk.Version, - op = new - { - type = "object_upsert", - id = obj.Id, - x = obj.TileX, - y = obj.TileY, - objectDefinitionId = obj.ObjectDefinitionId, - rotation = obj.Rotation, - metadataJson = obj.MetadataJson - } + fromVersion, + toVersion = locate.Chunk.Version, + op }); } @@ -402,10 +470,14 @@ public class IndexModel : AdminPageModel var obj = await _db.WorldChunkObjects.FirstOrDefaultAsync(o => o.Id == id && o.WorldChunkId == locate.Chunk.Id); if (obj != null) { + var fromVersion = locate.Chunk.Version; _db.WorldChunkObjects.Remove(obj); locate.Chunk.Version++; locate.Chunk.UpdatedAt = DateTime.UtcNow; await _db.SaveChangesAsync(); + + var op = new { type = "object_remove", id }; + PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op); } return new JsonResult(new @@ -440,8 +512,8 @@ public class IndexModel : AdminPageModel return new JsonResult(new { success = false, error = "tile_out_of_bounds" }) { StatusCode = 400 }; } - var npcExists = await _db.DungeonNpcDefinitions.AnyAsync(n => n.Id == npcDefinitionId && n.IsEnabled); - if (!npcExists) + var npcDef = await _db.DungeonNpcDefinitions.FirstOrDefaultAsync(n => n.Id == npcDefinitionId && n.IsEnabled); + if (npcDef == null) { return new JsonResult(new { success = false, error = "npc_definition_not_found" }) { StatusCode = 404 }; } @@ -468,24 +540,31 @@ public class IndexModel : AdminPageModel npc.Rotation = rotation; npc.MetadataJson = string.IsNullOrWhiteSpace(metadataJson) ? null : metadataJson.Trim(); + var fromVersion = locate.Chunk.Version; locate.Chunk.Version++; locate.Chunk.UpdatedAt = DateTime.UtcNow; await _db.SaveChangesAsync(); + var op = new + { + type = "npc_upsert", + id = npc.Id, + x = npc.TileX, + y = npc.TileY, + npcDefinitionId = npc.NpcDefinitionId, + npcName = npcDef.Name, + rotation = npc.Rotation, + metadataJson = npc.MetadataJson + }; + PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op); + return new JsonResult(new { success = true, chunkVersion = locate.Chunk.Version, - op = new - { - type = "npc_upsert", - id = npc.Id, - x = npc.TileX, - y = npc.TileY, - npcDefinitionId = npc.NpcDefinitionId, - rotation = npc.Rotation, - metadataJson = npc.MetadataJson - } + fromVersion, + toVersion = locate.Chunk.Version, + op }); } @@ -500,10 +579,14 @@ public class IndexModel : AdminPageModel var npc = await _db.WorldChunkNpcSpawns.FirstOrDefaultAsync(n => n.Id == id && n.WorldChunkId == locate.Chunk.Id); if (npc != null) { + var fromVersion = locate.Chunk.Version; _db.WorldChunkNpcSpawns.Remove(npc); locate.Chunk.Version++; locate.Chunk.UpdatedAt = DateTime.UtcNow; await _db.SaveChangesAsync(); + + var op = new { type = "npc_remove", id }; + PublishDelta(regionId, zoneX, zoneY, chunkX, chunkY, fromVersion, locate.Chunk.Version, op); } return new JsonResult(new @@ -621,4 +704,25 @@ public class IndexModel : AdminPageModel { return x >= 0 && y >= 0 && x < width && y < height; } + + private void PublishDelta(int regionId, int zoneX, int zoneY, int chunkX, int chunkY, long fromVersion, long toVersion, object op) + { + _deltaHub.Publish( + ChunkKey(regionId, zoneX, zoneY, chunkX, chunkY), + new + { + kind = "chunk_delta", + regionId, + zoneX, + zoneY, + chunkX, + chunkY, + fromVersion, + toVersion, + op + }); + } + + private static string ChunkKey(int regionId, int zoneX, int zoneY, int chunkX, int chunkY) + => $"{regionId}:{zoneX}:{zoneY}:{chunkX}:{chunkY}"; } diff --git a/Services/WorldEditDeltaHub.cs b/Services/WorldEditDeltaHub.cs new file mode 100644 index 0000000..887b9b2 --- /dev/null +++ b/Services/WorldEditDeltaHub.cs @@ -0,0 +1,85 @@ +using System.Collections.Concurrent; +using System.Text.Json; +using System.Threading.Channels; + +namespace GameServices; + +/// +/// In-memory pub/sub hub for live world editor chunk delta events. +/// +public sealed class WorldEditDeltaHub +{ + private readonly ConcurrentDictionary>> _subscribers = new(); + + public WorldEditSubscription Subscribe(string key) + { + var channel = Channel.CreateUnbounded(new UnboundedChannelOptions + { + SingleReader = true, + SingleWriter = false + }); + + var id = Guid.NewGuid(); + var bucket = _subscribers.GetOrAdd(key, _ => new ConcurrentDictionary>()); + bucket[id] = channel; + + return new WorldEditSubscription(this, key, id, channel.Reader); + } + + public void Publish(string key, object payload) + { + if (!_subscribers.TryGetValue(key, out var bucket) || bucket.Count == 0) + { + return; + } + + var json = JsonSerializer.Serialize(payload); + foreach (var (_, channel) in bucket) + { + channel.Writer.TryWrite(json); + } + } + + private void Unsubscribe(string key, Guid id) + { + if (!_subscribers.TryGetValue(key, out var bucket)) + { + return; + } + + if (bucket.TryRemove(id, out var channel)) + { + channel.Writer.TryComplete(); + } + + if (bucket.IsEmpty) + { + _subscribers.TryRemove(key, out _); + } + } + + public sealed class WorldEditSubscription : IDisposable + { + private readonly WorldEditDeltaHub _hub; + private readonly string _key; + private readonly Guid _id; + private bool _disposed; + + internal WorldEditSubscription(WorldEditDeltaHub hub, string key, Guid id, ChannelReader reader) + { + _hub = hub; + _key = key; + _id = id; + Reader = reader; + } + + public ChannelReader Reader { get; } + + public void Dispose() + { + if (_disposed) return; + _disposed = true; + _hub.Unsubscribe(_key, _id); + } + } +} diff --git a/pending-clientChanges.md b/pending-clientChanges.md index 4464744..888d15d 100644 --- a/pending-clientChanges.md +++ b/pending-clientChanges.md @@ -98,3 +98,19 @@ - Delta update payload: - `regionId`, `zoneId`, `chunkX`, `chunkY`, `fromVersion`, `toVersion` - operations list (tile upsert/remove, object upsert/remove, npc upsert/remove) + +### Implemented live stream contract (browser world editor) + +- SSE endpoint: + - `GET /admin/worldeditor?handler=Stream®ionId={id}&zoneX={x}&zoneY={y}&chunkX={x}&chunkY={y}` +- Message payload: + - `kind: "chunk_delta"` + - `regionId`, `zoneX`, `zoneY`, `chunkX`, `chunkY` + - `fromVersion`, `toVersion` + - `op`: + - `tile_upsert`: `{ x, y, sheetId, frameIndex, isWalkable, isInvisiblePaywall }` + - `tile_remove`: `{ x, y }` + - `object_upsert`: `{ id, x, y, objectDefinitionId, objectName, rotation, metadataJson }` + - `object_remove`: `{ id }` + - `npc_upsert`: `{ id, x, y, npcDefinitionId, npcName, rotation, metadataJson }` + - `npc_remove`: `{ id }`