Add live chunk delta streaming for world editor and versioned apply flow

This commit is contained in:
dingo
2026-02-14 14:31:18 -05:00
parent 18d6fe683b
commit 147e7d625d
5 changed files with 312 additions and 52 deletions
+139 -35
View File
@@ -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<WorldRegion> Regions { get; set; } = new();
@@ -222,6 +226,53 @@ public class IndexModel : AdminPageModel
});
}
public async Task<IActionResult> 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<IActionResult> 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}";
}