World Editor fixes
This commit is contained in:
@@ -124,4 +124,27 @@ public sealed class WorldBroadcastService
|
||||
.Select(p => SendAsync(p.Connection, opCode, payload, CancellationToken.None));
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a chunk delta to all players who currently have the given chunk key in their visible set.
|
||||
/// </summary>
|
||||
public async Task BroadcastChunkDeltaAsync(
|
||||
string chunkKey,
|
||||
object deltaPayload,
|
||||
long newVersion,
|
||||
ConcurrentDictionary<Guid, WorldSessionState> players)
|
||||
{
|
||||
var tasks = new List<Task>();
|
||||
foreach (var s in players.Values)
|
||||
{
|
||||
if (!s.ChunkVersions.ContainsKey(chunkKey))
|
||||
continue;
|
||||
|
||||
// Update the player's tracked version so the next tick doesn't re-send a full chunk_load
|
||||
s.ChunkVersions[chunkKey] = newVersion;
|
||||
tasks.Add(SendAsync(s.Connection, OpCodes.WORLD_CHUNK_DELTA, deltaPayload, CancellationToken.None));
|
||||
}
|
||||
if (tasks.Count > 0)
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text.Json;
|
||||
using DataServer.Data;
|
||||
using DataServer.Data.Entities;
|
||||
using GameServices;
|
||||
using GameServices.Auth;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -23,6 +25,7 @@ public sealed class WorldHandler : IClientMessageHandler
|
||||
private readonly WorldChunkCache _cache;
|
||||
private readonly WorldMovementService _movement;
|
||||
private readonly WorldBroadcastService _broadcast;
|
||||
private readonly WorldEditDeltaHub _deltaHub;
|
||||
|
||||
private readonly ConcurrentDictionary<Guid, WorldSessionState> _players = new();
|
||||
private readonly ConcurrentDictionary<ulong, long> _portalCooldownByCharacter = new();
|
||||
@@ -39,7 +42,8 @@ public sealed class WorldHandler : IClientMessageHandler
|
||||
IServiceScopeFactory scopeFactory,
|
||||
WorldChunkCache cache,
|
||||
WorldMovementService movement,
|
||||
WorldBroadcastService broadcast)
|
||||
WorldBroadcastService broadcast,
|
||||
WorldEditDeltaHub deltaHub)
|
||||
{
|
||||
_log = log;
|
||||
_sessions = sessions;
|
||||
@@ -47,6 +51,7 @@ public sealed class WorldHandler : IClientMessageHandler
|
||||
_cache = cache;
|
||||
_movement = movement;
|
||||
_broadcast = broadcast;
|
||||
_deltaHub = deltaHub;
|
||||
|
||||
var debugInputEnv = Environment.GetEnvironmentVariable("WORLD_INPUT_DEBUG");
|
||||
_debugInput = !string.IsNullOrWhiteSpace(debugInputEnv) &&
|
||||
@@ -65,8 +70,9 @@ public sealed class WorldHandler : IClientMessageHandler
|
||||
if (_initialized) return Task.CompletedTask;
|
||||
_tickCts = new CancellationTokenSource();
|
||||
_tickTask = Task.Run(() => TickLoopAsync(_tickCts.Token), _tickCts.Token);
|
||||
_deltaHub.OnDeltaPublished += OnWorldEditDelta;
|
||||
_initialized = true;
|
||||
_log.LogInformation("WorldHandler initialized (cached movement pipeline)");
|
||||
_log.LogInformation("WorldHandler initialized (cached movement pipeline + live delta forwarding)");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -354,6 +360,51 @@ public sealed class WorldHandler : IClientMessageHandler
|
||||
Math.Abs(p.X - self.X) <= (TileSize * 48) &&
|
||||
Math.Abs(p.Y - self.Y) <= (TileSize * 48));
|
||||
|
||||
// ---- Live delta forwarding (admin world edits -> game clients) ----
|
||||
|
||||
private void OnWorldEditDelta(string chunkKey, string deltaJson)
|
||||
{
|
||||
// Fire-and-forget: forward delta to clients and invalidate cache
|
||||
_ = ProcessDeltaAsync(chunkKey, deltaJson);
|
||||
}
|
||||
|
||||
private async Task ProcessDeltaAsync(string chunkKey, string deltaJson)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Parse the delta to extract version and operation details
|
||||
using var doc = JsonDocument.Parse(deltaJson);
|
||||
var root = doc.RootElement;
|
||||
var toVersion = root.TryGetProperty("toVersion", out var tv) && tv.ValueKind == JsonValueKind.Number
|
||||
? tv.GetInt64() : 0L;
|
||||
|
||||
// Parse chunk coordinates from the key for cache invalidation
|
||||
var parts = chunkKey.Split(':');
|
||||
if (parts.Length == 5 &&
|
||||
int.TryParse(parts[0], out var regionId) &&
|
||||
int.TryParse(parts[1], out var zoneX) &&
|
||||
int.TryParse(parts[2], out var zoneY) &&
|
||||
int.TryParse(parts[3], out var chunkX) &&
|
||||
int.TryParse(parts[4], out var chunkY))
|
||||
{
|
||||
// Invalidate cache so next access reloads fresh data from DB
|
||||
_cache.InvalidateChunk(regionId, zoneX, zoneY, chunkX, chunkY);
|
||||
|
||||
// Deserialize the delta to a dynamic object for forwarding to clients.
|
||||
// The raw JSON from the hub already has all the fields the client needs.
|
||||
var deltaPayload = JsonSerializer.Deserialize<object>(deltaJson);
|
||||
if (deltaPayload != null)
|
||||
{
|
||||
await _broadcast.BroadcastChunkDeltaAsync(chunkKey, deltaPayload, toVersion, _players);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.LogWarning(ex, "Error processing world edit delta for chunk {ChunkKey}", chunkKey);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Persistence (only on disconnect) ----
|
||||
|
||||
private async Task PersistPlayerLocationAsync(WorldSessionState s)
|
||||
|
||||
+260
-22
@@ -1,28 +1,266 @@
|
||||
# Client Fixes / Notes After WorldHandler Refactor
|
||||
# Client Changes Required After WorldHandler Refactor
|
||||
|
||||
## Server Protocol: UNCHANGED
|
||||
The wire protocol (OpCodes, payload shapes, message flows) is identical after the refactor.
|
||||
No client code changes are required for compatibility.
|
||||
## Existing Protocol: UNCHANGED
|
||||
All existing OpCodes (700-711) and payload shapes are identical. No changes needed for spawn, input, or state.
|
||||
|
||||
## Performance Impact
|
||||
## NEW: OpCode 712 - WORLD_CHUNK_DELTA (Server -> Client)
|
||||
|
||||
The server now pushes live chunk deltas to clients whenever the world is edited (admin panel, etc).
|
||||
Any player who has the affected chunk in their visible set receives the delta immediately,
|
||||
without waiting for a full chunk reload.
|
||||
|
||||
### When it fires
|
||||
- Admin places/removes a tile via the world editor
|
||||
- Admin places/removes an object, NPC, or mob spawn
|
||||
- Admin clears or deletes a chunk
|
||||
|
||||
### Payload shape
|
||||
```json
|
||||
{
|
||||
"kind": "chunk_delta",
|
||||
"regionId": 1,
|
||||
"zoneX": 0,
|
||||
"zoneY": 0,
|
||||
"chunkX": 3,
|
||||
"chunkY": 2,
|
||||
"fromVersion": 5,
|
||||
"toVersion": 6,
|
||||
"op": { ... }
|
||||
}
|
||||
```
|
||||
|
||||
### `op` types the client must handle
|
||||
|
||||
#### `tile_upsert` - A tile was added or changed
|
||||
```json
|
||||
{
|
||||
"type": "tile_upsert",
|
||||
"x": 12,
|
||||
"y": 8,
|
||||
"sheetId": 3,
|
||||
"frameIndex": 42,
|
||||
"isWalkable": true,
|
||||
"isInvisiblePaywall": false
|
||||
}
|
||||
```
|
||||
**Client action**: Find the chunk in `chunk_cache` by regionId/zoneX/zoneY/chunkX/chunkY.
|
||||
Update or insert the tile in the chunk's `tiles` array. Then call `_rebuild_world_visuals()`.
|
||||
Also update local walkability data if you cache it.
|
||||
|
||||
#### `tile_remove` - A tile was removed
|
||||
```json
|
||||
{
|
||||
"type": "tile_remove",
|
||||
"x": 12,
|
||||
"y": 8
|
||||
}
|
||||
```
|
||||
**Client action**: Remove the tile at (x, y) from the chunk's `tiles` array.
|
||||
Call `_rebuild_world_visuals()`. Mark that tile as walkable in local collision data.
|
||||
|
||||
#### `object_upsert` - An object was added or changed
|
||||
```json
|
||||
{
|
||||
"type": "object_upsert",
|
||||
"id": 456,
|
||||
"x": 5,
|
||||
"y": 3,
|
||||
"objectDefinitionId": 12,
|
||||
"objectName": "Portal Stone",
|
||||
"interactionType": "Teleport",
|
||||
"rotation": 0,
|
||||
"metadataJson": "{\"portalType\":\"world\",...}"
|
||||
}
|
||||
```
|
||||
**Client action**: Upsert by `id` in the chunk's `objects` array. Call `_rebuild_world_entities()`.
|
||||
|
||||
#### `object_remove` - An object was removed
|
||||
```json
|
||||
{
|
||||
"type": "object_remove",
|
||||
"id": 456
|
||||
}
|
||||
```
|
||||
**Client action**: Remove the object with matching `id` from `objects`. Call `_rebuild_world_entities()`.
|
||||
|
||||
#### `npc_upsert` - An NPC spawn was added or changed
|
||||
```json
|
||||
{
|
||||
"type": "npc_upsert",
|
||||
"id": 789,
|
||||
"x": 10,
|
||||
"y": 15,
|
||||
"npcDefinitionId": 5,
|
||||
"npcName": "Merchant",
|
||||
"rotation": 0,
|
||||
"metadataJson": null
|
||||
}
|
||||
```
|
||||
**Client action**: Upsert by `id` in the chunk's `npcs` array. Call `_rebuild_world_entities()`.
|
||||
|
||||
#### `npc_remove`
|
||||
```json
|
||||
{ "type": "npc_remove", "id": 789 }
|
||||
```
|
||||
**Client action**: Remove NPC with matching `id`. Call `_rebuild_world_entities()`.
|
||||
|
||||
#### `mob_upsert` - A mob spawn was added or changed
|
||||
```json
|
||||
{
|
||||
"type": "mob_upsert",
|
||||
"id": 101,
|
||||
"x": 20,
|
||||
"y": 25,
|
||||
"mobDefinitionId": 3,
|
||||
"mobName": "Slime",
|
||||
"rotation": 0,
|
||||
"spawnCount": 2,
|
||||
"respawnSeconds": 30.0,
|
||||
"metadataJson": null
|
||||
}
|
||||
```
|
||||
**Client action**: Upsert by `id` in the chunk's `mobs` array. Call `_rebuild_world_entities()`.
|
||||
|
||||
#### `mob_remove`
|
||||
```json
|
||||
{ "type": "mob_remove", "id": 101 }
|
||||
```
|
||||
**Client action**: Remove mob with matching `id`. Call `_rebuild_world_entities()`.
|
||||
|
||||
#### `chunk_clear` - All tiles/objects/npcs/mobs removed from chunk
|
||||
```json
|
||||
{ "type": "chunk_clear" }
|
||||
```
|
||||
**Client action**: Empty the chunk's `tiles`, `objects`, `npcs`, `mobs` arrays.
|
||||
Call `_rebuild_world_visuals()` and `_rebuild_world_entities()`.
|
||||
|
||||
#### `chunk_deleted` - Entire chunk deleted from DB
|
||||
```json
|
||||
{ "type": "chunk_deleted" }
|
||||
```
|
||||
**Client action**: Remove the chunk from `chunk_cache` entirely.
|
||||
Call `_rebuild_world_visuals()` and `_rebuild_world_entities()`.
|
||||
|
||||
### Implementation guide for the client
|
||||
|
||||
In your GameState (or wherever you route opcodes), add a handler for opcode 712:
|
||||
|
||||
```gdscript
|
||||
# In GameState or network handler:
|
||||
712: # WORLD_CHUNK_DELTA
|
||||
emit_signal("world_chunk_delta_received", data)
|
||||
```
|
||||
|
||||
In the world scene script, connect to the signal and apply:
|
||||
|
||||
```gdscript
|
||||
func _setup_runtime():
|
||||
# ... existing connections ...
|
||||
if GameState:
|
||||
GameState.world_chunk_delta_received.connect(_on_world_chunk_delta_received)
|
||||
|
||||
func _on_world_chunk_delta_received(data: Dictionary):
|
||||
if runtime == null:
|
||||
return
|
||||
runtime.apply_chunk_delta(data)
|
||||
_rebuild_world_visuals()
|
||||
_rebuild_world_entities()
|
||||
```
|
||||
|
||||
In the world runtime client, add the delta application logic:
|
||||
|
||||
```gdscript
|
||||
func apply_chunk_delta(data: Dictionary):
|
||||
var key = _chunk_key(
|
||||
int(data.get("regionId", 0)),
|
||||
int(data.get("zoneX", 0)),
|
||||
int(data.get("zoneY", 0)),
|
||||
int(data.get("chunkX", 0)),
|
||||
int(data.get("chunkY", 0))
|
||||
)
|
||||
var op = data.get("op", {})
|
||||
var op_type = str(op.get("type", ""))
|
||||
|
||||
if not chunk_cache.has(key):
|
||||
return # We don't have this chunk loaded, ignore
|
||||
|
||||
var chunk = chunk_cache[key]
|
||||
|
||||
match op_type:
|
||||
"tile_upsert":
|
||||
_delta_upsert_tile(chunk, op)
|
||||
"tile_remove":
|
||||
_delta_remove_tile(chunk, op)
|
||||
"object_upsert":
|
||||
_delta_upsert_by_id(chunk, "objects", op)
|
||||
"object_remove":
|
||||
_delta_remove_by_id(chunk, "objects", int(op.get("id", -1)))
|
||||
"npc_upsert":
|
||||
_delta_upsert_by_id(chunk, "npcs", op)
|
||||
"npc_remove":
|
||||
_delta_remove_by_id(chunk, "npcs", int(op.get("id", -1)))
|
||||
"mob_upsert":
|
||||
_delta_upsert_by_id(chunk, "mobs", op)
|
||||
"mob_remove":
|
||||
_delta_remove_by_id(chunk, "mobs", int(op.get("id", -1)))
|
||||
"chunk_clear":
|
||||
chunk["tiles"] = []
|
||||
chunk["objects"] = []
|
||||
chunk["npcs"] = []
|
||||
chunk["mobs"] = []
|
||||
"chunk_deleted":
|
||||
chunk_cache.erase(key)
|
||||
|
||||
# Update version tracking
|
||||
var new_version = int(data.get("toVersion", 0))
|
||||
if chunk_cache.has(key):
|
||||
chunk_cache[key]["version"] = new_version
|
||||
|
||||
func _delta_upsert_tile(chunk: Dictionary, op: Dictionary):
|
||||
var tx = int(op.get("x", -1))
|
||||
var ty = int(op.get("y", -1))
|
||||
var tiles = chunk.get("tiles", [])
|
||||
for i in range(tiles.size()):
|
||||
var t = tiles[i]
|
||||
if int(t.get("x", t.get("tileX", -1))) == tx and int(t.get("y", t.get("tileY", -1))) == ty:
|
||||
tiles[i] = op
|
||||
return
|
||||
tiles.append(op)
|
||||
|
||||
func _delta_remove_tile(chunk: Dictionary, op: Dictionary):
|
||||
var tx = int(op.get("x", -1))
|
||||
var ty = int(op.get("y", -1))
|
||||
var tiles = chunk.get("tiles", [])
|
||||
for i in range(tiles.size() - 1, -1, -1):
|
||||
var t = tiles[i]
|
||||
if int(t.get("x", t.get("tileX", -1))) == tx and int(t.get("y", t.get("tileY", -1))) == ty:
|
||||
tiles.remove_at(i)
|
||||
return
|
||||
|
||||
func _delta_upsert_by_id(chunk: Dictionary, array_name: String, op: Dictionary):
|
||||
var id = int(op.get("id", -1))
|
||||
var arr = chunk.get(array_name, [])
|
||||
for i in range(arr.size()):
|
||||
if int(arr[i].get("id", -1)) == id:
|
||||
arr[i] = op
|
||||
return
|
||||
arr.append(op)
|
||||
|
||||
func _delta_remove_by_id(chunk: Dictionary, array_name: String, id: int):
|
||||
var arr = chunk.get(array_name, [])
|
||||
for i in range(arr.size() - 1, -1, -1):
|
||||
if int(arr[i].get("id", -1)) == id:
|
||||
arr.remove_at(i)
|
||||
return
|
||||
```
|
||||
|
||||
## Performance Notes
|
||||
|
||||
### Server movement fix
|
||||
The server tick loop no longer hits the database. Movement/collision is now pure in-memory.
|
||||
The client should observe dramatically faster movement response -- the "1 tile per 3 seconds"
|
||||
latency was caused by 30+ DB round-trips per player per tick on the server side.
|
||||
|
||||
## Client Recommendations
|
||||
The "1 tile per 3 seconds" latency was caused by 30+ DB round-trips per player per tick on the server side.
|
||||
|
||||
### LOCAL_MOVE_SPEED mismatch
|
||||
The client uses `LOCAL_MOVE_SPEED = 160.0` for local prediction, but the server walk speed
|
||||
is `120 px/s` and run speed is `180 px/s`. This means the client's local prediction overshoots
|
||||
when walking, causing small rubber-banding corrections via `VISUAL_RECONCILE_RATE`.
|
||||
|
||||
Options:
|
||||
- Match client prediction to server walk speed: change `LOCAL_MOVE_SPEED` to `120.0`
|
||||
- Or read speed from WORLD_SPAWN payload and use it dynamically
|
||||
- Run speed (shift held) already reasonably matches at 180 vs client 160
|
||||
|
||||
### Other observations
|
||||
- `MOVE_SEND_INTERVAL_MS = 100` correctly matches the server 10Hz tick rate
|
||||
- `VISUAL_RECONCILE_RATE = 4.0` and `VISUAL_SNAP_DISTANCE = 256.0` are reasonable
|
||||
- Client-side collision pre-check (line 350-355) only checks 1 tile vs server's 5-point check
|
||||
-- this is fine, it just means the client may sometimes send input that the server rejects
|
||||
is `120 px/s` and run speed is `180 px/s`. This causes minor rubber-banding.
|
||||
Consider changing `LOCAL_MOVE_SPEED` to `120.0` or reading the server's speed from WORLD_SPAWN.
|
||||
|
||||
@@ -115,6 +115,12 @@ public static class OpCodes
|
||||
/// </summary>
|
||||
public const int WORLD_STATE = 711;
|
||||
|
||||
/// <summary>
|
||||
/// Server -> Client: Live chunk delta (tile/object/npc/mob added/removed/changed)
|
||||
/// Pushed immediately when an admin edits the world, to all players viewing that chunk.
|
||||
/// </summary>
|
||||
public const int WORLD_CHUNK_DELTA = 712;
|
||||
|
||||
// ===== Error Messages (900-999) =====
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -11,6 +11,13 @@ public sealed class WorldEditDeltaHub
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, ConcurrentDictionary<Guid, Channel<string>>> _subscribers = new();
|
||||
|
||||
/// <summary>
|
||||
/// Fired for every delta publish, regardless of chunk key.
|
||||
/// Parameters: (chunkKey, deltaJson).
|
||||
/// Used by WorldHandler to forward live edits to game clients.
|
||||
/// </summary>
|
||||
public event Action<string, string>? OnDeltaPublished;
|
||||
|
||||
public WorldEditSubscription Subscribe(string key)
|
||||
{
|
||||
var channel = Channel.CreateUnbounded<string>(new UnboundedChannelOptions
|
||||
@@ -28,16 +35,19 @@ public sealed class WorldEditDeltaHub
|
||||
|
||||
public void Publish(string key, object payload)
|
||||
{
|
||||
if (!_subscribers.TryGetValue(key, out var bucket) || bucket.Count == 0)
|
||||
var json = JsonSerializer.Serialize(payload);
|
||||
|
||||
// Notify channel-based subscribers (admin editor WebSocket streams)
|
||||
if (_subscribers.TryGetValue(key, out var bucket) && bucket.Count > 0)
|
||||
{
|
||||
return;
|
||||
foreach (var (_, channel) in bucket)
|
||||
{
|
||||
channel.Writer.TryWrite(json);
|
||||
}
|
||||
}
|
||||
|
||||
var json = JsonSerializer.Serialize(payload);
|
||||
foreach (var (_, channel) in bucket)
|
||||
{
|
||||
channel.Writer.TryWrite(json);
|
||||
}
|
||||
// Notify global listeners (game client delta forwarding)
|
||||
OnDeltaPublished?.Invoke(key, json);
|
||||
}
|
||||
|
||||
private void Unsubscribe(string key, Guid id)
|
||||
|
||||
Reference in New Issue
Block a user