BlueGit

timfa questward

Add world editor saves, changelog diffs, and pending-revision apply.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-08-24 · 2 months ago

Hash
f780e3bce027c3530cdd8bc5a85546ad9faa7304
Parent
50128c77
3 files changed +180 −11
docs/FEATURES.md +5 −11
@@ -63,21 +63,15 @@ Each entry is a short note on behaviour and where the code lives. Update this fi
63 63
64 64 ## World editor (server)
65 65
66 **Status:** not implemented
67 **Spec:** Admin API to load/save a chunk, toggle open/closed, paint ground/blocks, write changelog bullets, and edit world settings. Marketplace/bank/spawner placement comes later with assets.
68 **Code:** —
69
70 ## World editor (client)
71
72 **Status:** not implemented
73 **Spec:** Separate admin UI. One chunk at a time, chunk-step navigation, paint palette, open/closed, changelog, night preview slider, settings modal.
74 **Code:** —
66 **Status:** full
67 **Spec:** Admin API to load/save a chunk, toggle open/closed, paint ground/blocks, write changelog bullets, edit world settings, and apply the pending revision. Marketplace/bank/spawner placement is later.
68 **Code:** `server/src/editor/service.js`
75 69
76 70 ## Per-chunk changelog
77 71
78 **Status:** not implemented
72 **Status:** full
79 73 **Spec:** Each save stores who, when, and structured bullets of ground/block/open-closed diffs.
80 **Code:** —
74 **Code:** `server/src/editor/service.js` (`diffChunks`), `server/src/persistence/worldDb.js` (`world_chunk_edit_log`), tests in `server/tests/editor-diff.test.js`
81 75
82 76 ## WebSocket gateway
83 77
@@ -63,21 +63,15 @@ Each entry is a short note on behaviour and where the code lives. Update this fi
63
63
64## World editor (server)
64## World editor (server)
65
65
66**Status:** not implemented
66**Status:** full
67**Spec:** Admin API to load/save a chunk, toggle open/closed, paint ground/blocks, write changelog bullets, and edit world settings. Marketplace/bank/spawner placement comes later with assets.
67**Spec:** Admin API to load/save a chunk, toggle open/closed, paint ground/blocks, write changelog bullets, edit world settings, and apply the pending revision. Marketplace/bank/spawner placement is later.
68**Code:** —
68**Code:** `server/src/editor/service.js`
69
70## World editor (client)
71
72**Status:** not implemented
73**Spec:** Separate admin UI. One chunk at a time, chunk-step navigation, paint palette, open/closed, changelog, night preview slider, settings modal.
74**Code:** —
75
69
76## Per-chunk changelog
70## Per-chunk changelog
77
71
78**Status:** not implemented
72**Status:** full
79**Spec:** Each save stores who, when, and structured bullets of ground/block/open-closed diffs.
73**Spec:** Each save stores who, when, and structured bullets of ground/block/open-closed diffs.
80**Code:** —
74**Code:** `server/src/editor/service.js` (`diffChunks`), `server/src/persistence/worldDb.js` (`world_chunk_edit_log`), tests in `server/tests/editor-diff.test.js`
81
75
82## WebSocket gateway
76## WebSocket gateway
83
77
server/src/editor/service.js added +157 −0
@@ -0,0 +1,157 @@
1 import { groundDefinition, blockDefinition } from "../../../shared/definitions/tiles.js";
2 import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
3 import { localToTile } from "../world/coordinates.js";
4 import { cloneChunkData, createEmptyChunkData, decodeChunkBlob, encodeChunkBlob } from "../world/chunkData.js";
5 import { BLOCK_DEFINITIONS } from "../../../shared/definitions/tiles.js";
6 import { bakeLightMaps, maxDefinitionLightRange } from "../lighting/baker.js";
7
8 export class EditorService
9 {
10 constructor (worldDb)
11 {
12 this.worldDb = worldDb;
13 }
14
15 loadChunk (chunkX, chunkY)
16 {
17 const revisionId = this.worldDb.getEditorRevisionId();
18 const settings = this.worldDb.getSettings(revisionId);
19 const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId);
20 const data = row
21 ? decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId)
22 : createEmptyChunkData(settings.defaultGroundTypeId);
23 return {
24 chunkX,
25 chunkY,
26 revisionId,
27 pendingRevisionId: this.worldDb.getPendingRevisionId(),
28 activeRevisionId: this.worldDb.getActiveRevisionId(),
29 authored: !!row,
30 settings,
31 data,
32 changelog: this.worldDb.listChangelog(chunkX, chunkY)
33 };
34 }
35
36 saveChunk (chunkX, chunkY, draft, editorAdminId)
37 {
38 const previous = this.loadChunk(chunkX, chunkY);
39 const next = cloneChunkData(previous.data);
40 next.open = !!draft.open;
41 if (Array.isArray(draft.grounds))
42 { next.grounds = draft.grounds.slice(); }
43 if (Array.isArray(draft.blocks))
44 { next.blocks = draft.blocks.map((block) => (block ? { typeId: block.typeId, config: block.config ?? null } : null)); }
45
46 const diffEntries = diffChunks(chunkX, chunkY, previous.data, next, previous.authored);
47 const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
48 const settings = this.worldDb.getSettings(revisionId);
49 const baked = bakeLightMaps(
50 chunkX,
51 chunkY,
52 (x, y) => this.#loadForBake(x, y, revisionId, settings.defaultGroundTypeId, chunkX, chunkY, next),
53 maxDefinitionLightRange(BLOCK_DEFINITIONS)
54 );
55
56 for (const result of baked)
57 {
58 const shouldStore = result.chunkX === chunkX && result.chunkY === chunkY
59 || this.worldDb.getChunkVersion(result.chunkX, result.chunkY, revisionId)
60 || result.data.light.some((value) => value > 0);
61
62 if (!shouldStore)
63 { continue; }
64
65 this.worldDb.saveChunkVersion(
66 result.chunkX,
67 result.chunkY,
68 revisionId,
69 result.data.open ? 1 : 0,
70 encodeChunkBlob(result.data)
71 );
72 }
73
74 if (diffEntries.length > 0)
75 { this.worldDb.appendChangelog(chunkX, chunkY, revisionId, editorAdminId, diffEntries); }
76
77 return this.loadChunk(chunkX, chunkY);
78 }
79
80 saveSettings (partial, editorAdminId)
81 {
82 const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
83 const current = this.worldDb.getSettings(revisionId);
84 const next = { ...current, ...partial };
85 this.worldDb.saveSettings(revisionId, next);
86 return { revisionId, settings: next };
87 }
88
89 applyPendingRevision ()
90 {
91 return this.worldDb.promotePendingRevision();
92 }
93
94 #loadForBake (chunkX, chunkY, revisionId, defaultGroundTypeId, savedX, savedY, savedData)
95 {
96 if (chunkX === savedX && chunkY === savedY)
97 { return savedData; }
98
99 const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId);
100 return row
101 ? decodeChunkBlob(row.data_blob, defaultGroundTypeId)
102 : createEmptyChunkData(defaultGroundTypeId);
103 }
104 }
105
106 export function diffChunks (chunkX, chunkY, previous, next, previousAuthored)
107 {
108 const bullets = [];
109 const prevOpen = previousAuthored ? !!previous.open : false;
110 if (prevOpen !== !!next.open)
111 { bullets.push(next.open ? "Chunk opened." : "Chunk closed."); }
112
113 for (let localY = 0; localY < CHUNK_SIZE; localY++)
114 {
115 for (let localX = 0; localX < CHUNK_SIZE; localX++)
116 {
117 const index = localY * CHUNK_SIZE + localX;
118 const tileX = localToTile(chunkX, localX);
119 const tileY = localToTile(chunkY, localY);
120 const prevGround = previous.grounds[index];
121 const nextGround = next.grounds[index];
122 if (prevGround !== nextGround)
123 {
124 bullets.push(
125 `Ground at ${tileX}, ${tileY} changed from ${nameGround(prevGround)} to ${nameGround(nextGround)}.`
126 );
127 }
128
129 const prevBlock = previous.blocks[index];
130 const nextBlock = next.blocks[index];
131 const prevId = prevBlock?.typeId ?? null;
132 const nextId = nextBlock?.typeId ?? null;
133 const prevConfig = JSON.stringify(prevBlock?.config ?? null);
134 const nextConfig = JSON.stringify(nextBlock?.config ?? null);
135 if (prevId !== nextId || prevConfig !== nextConfig)
136 {
137 if (prevId == null && nextId != null)
138 { bullets.push(`Block ${nameBlock(nextId)} placed at ${tileX}, ${tileY}.`); }
139 else if (prevId != null && nextId == null)
140 { bullets.push(`Block ${nameBlock(prevId)} removed at ${tileX}, ${tileY}.`); }
141 else
142 { bullets.push(`Block at ${tileX}, ${tileY} changed from ${nameBlock(prevId)} to ${nameBlock(nextId)}.`); }
143 }
144 }
145 }
146 return bullets;
147 }
148
149 function nameGround (typeId)
150 {
151 return groundDefinition(typeId)?.displayName ?? `ground ${typeId}`;
152 }
153
154 function nameBlock (typeId)
155 {
156 return blockDefinition(typeId)?.displayName ?? `block ${typeId}`;
157 }
@@ -0,0 +1,157 @@
1import { groundDefinition, blockDefinition } from "../../../shared/definitions/tiles.js";
2import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
3import { localToTile } from "../world/coordinates.js";
4import { cloneChunkData, createEmptyChunkData, decodeChunkBlob, encodeChunkBlob } from "../world/chunkData.js";
5import { BLOCK_DEFINITIONS } from "../../../shared/definitions/tiles.js";
6import { bakeLightMaps, maxDefinitionLightRange } from "../lighting/baker.js";
7
8export class EditorService
9{
10 constructor (worldDb)
11 {
12 this.worldDb = worldDb;
13 }
14
15 loadChunk (chunkX, chunkY)
16 {
17 const revisionId = this.worldDb.getEditorRevisionId();
18 const settings = this.worldDb.getSettings(revisionId);
19 const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId);
20 const data = row
21 ? decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId)
22 : createEmptyChunkData(settings.defaultGroundTypeId);
23 return {
24 chunkX,
25 chunkY,
26 revisionId,
27 pendingRevisionId: this.worldDb.getPendingRevisionId(),
28 activeRevisionId: this.worldDb.getActiveRevisionId(),
29 authored: !!row,
30 settings,
31 data,
32 changelog: this.worldDb.listChangelog(chunkX, chunkY)
33 };
34 }
35
36 saveChunk (chunkX, chunkY, draft, editorAdminId)
37 {
38 const previous = this.loadChunk(chunkX, chunkY);
39 const next = cloneChunkData(previous.data);
40 next.open = !!draft.open;
41 if (Array.isArray(draft.grounds))
42 { next.grounds = draft.grounds.slice(); }
43 if (Array.isArray(draft.blocks))
44 { next.blocks = draft.blocks.map((block) => (block ? { typeId: block.typeId, config: block.config ?? null } : null)); }
45
46 const diffEntries = diffChunks(chunkX, chunkY, previous.data, next, previous.authored);
47 const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
48 const settings = this.worldDb.getSettings(revisionId);
49 const baked = bakeLightMaps(
50 chunkX,
51 chunkY,
52 (x, y) => this.#loadForBake(x, y, revisionId, settings.defaultGroundTypeId, chunkX, chunkY, next),
53 maxDefinitionLightRange(BLOCK_DEFINITIONS)
54 );
55
56 for (const result of baked)
57 {
58 const shouldStore = result.chunkX === chunkX && result.chunkY === chunkY
59 || this.worldDb.getChunkVersion(result.chunkX, result.chunkY, revisionId)
60 || result.data.light.some((value) => value > 0);
61
62 if (!shouldStore)
63 { continue; }
64
65 this.worldDb.saveChunkVersion(
66 result.chunkX,
67 result.chunkY,
68 revisionId,
69 result.data.open ? 1 : 0,
70 encodeChunkBlob(result.data)
71 );
72 }
73
74 if (diffEntries.length > 0)
75 { this.worldDb.appendChangelog(chunkX, chunkY, revisionId, editorAdminId, diffEntries); }
76
77 return this.loadChunk(chunkX, chunkY);
78 }
79
80 saveSettings (partial, editorAdminId)
81 {
82 const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
83 const current = this.worldDb.getSettings(revisionId);
84 const next = { ...current, ...partial };
85 this.worldDb.saveSettings(revisionId, next);
86 return { revisionId, settings: next };
87 }
88
89 applyPendingRevision ()
90 {
91 return this.worldDb.promotePendingRevision();
92 }
93
94 #loadForBake (chunkX, chunkY, revisionId, defaultGroundTypeId, savedX, savedY, savedData)
95 {
96 if (chunkX === savedX && chunkY === savedY)
97 { return savedData; }
98
99 const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId);
100 return row
101 ? decodeChunkBlob(row.data_blob, defaultGroundTypeId)
102 : createEmptyChunkData(defaultGroundTypeId);
103 }
104}
105
106export function diffChunks (chunkX, chunkY, previous, next, previousAuthored)
107{
108 const bullets = [];
109 const prevOpen = previousAuthored ? !!previous.open : false;
110 if (prevOpen !== !!next.open)
111 { bullets.push(next.open ? "Chunk opened." : "Chunk closed."); }
112
113 for (let localY = 0; localY < CHUNK_SIZE; localY++)
114 {
115 for (let localX = 0; localX < CHUNK_SIZE; localX++)
116 {
117 const index = localY * CHUNK_SIZE + localX;
118 const tileX = localToTile(chunkX, localX);
119 const tileY = localToTile(chunkY, localY);
120 const prevGround = previous.grounds[index];
121 const nextGround = next.grounds[index];
122 if (prevGround !== nextGround)
123 {
124 bullets.push(
125 `Ground at ${tileX}, ${tileY} changed from ${nameGround(prevGround)} to ${nameGround(nextGround)}.`
126 );
127 }
128
129 const prevBlock = previous.blocks[index];
130 const nextBlock = next.blocks[index];
131 const prevId = prevBlock?.typeId ?? null;
132 const nextId = nextBlock?.typeId ?? null;
133 const prevConfig = JSON.stringify(prevBlock?.config ?? null);
134 const nextConfig = JSON.stringify(nextBlock?.config ?? null);
135 if (prevId !== nextId || prevConfig !== nextConfig)
136 {
137 if (prevId == null && nextId != null)
138 { bullets.push(`Block ${nameBlock(nextId)} placed at ${tileX}, ${tileY}.`); }
139 else if (prevId != null && nextId == null)
140 { bullets.push(`Block ${nameBlock(prevId)} removed at ${tileX}, ${tileY}.`); }
141 else
142 { bullets.push(`Block at ${tileX}, ${tileY} changed from ${nameBlock(prevId)} to ${nameBlock(nextId)}.`); }
143 }
144 }
145 }
146 return bullets;
147}
148
149function nameGround (typeId)
150{
151 return groundDefinition(typeId)?.displayName ?? `ground ${typeId}`;
152}
153
154function nameBlock (typeId)
155{
156 return blockDefinition(typeId)?.displayName ?? `block ${typeId}`;
157}
server/tests/editor-diff.test.js added +18 −0
@@ -0,0 +1,18 @@
1 import test from "node:test";
2 import assert from "node:assert/strict";
3 import { createEmptyChunkData } from "../src/world/chunkData.js";
4 import { diffChunks } from "../src/editor/service.js";
5 import { GroundType, BlockType } from "../../shared/definitions/tiles.js";
6
7 test("chunk diffs describe ground, block, and open-closed changes", () =>
8 {
9 const previous = createEmptyChunkData();
10 const next = createEmptyChunkData();
11 next.open = true;
12 next.grounds[0] = GroundType.wood;
13 next.blocks[1] = { typeId: BlockType.torch, config: null };
14 const bullets = diffChunks(0, 0, previous, next, false);
15 assert.ok(bullets.includes("Chunk opened."));
16 assert.ok(bullets.some((line) => line.includes("Ground at 0, 0 changed from Grass to Wood.")));
17 assert.ok(bullets.some((line) => line.includes("Block Torch placed at 1, 0.")));
18 });
@@ -0,0 +1,18 @@
1import test from "node:test";
2import assert from "node:assert/strict";
3import { createEmptyChunkData } from "../src/world/chunkData.js";
4import { diffChunks } from "../src/editor/service.js";
5import { GroundType, BlockType } from "../../shared/definitions/tiles.js";
6
7test("chunk diffs describe ground, block, and open-closed changes", () =>
8{
9 const previous = createEmptyChunkData();
10 const next = createEmptyChunkData();
11 next.open = true;
12 next.grounds[0] = GroundType.wood;
13 next.blocks[1] = { typeId: BlockType.torch, config: null };
14 const bullets = diffChunks(0, 0, previous, next, false);
15 assert.ok(bullets.includes("Chunk opened."));
16 assert.ok(bullets.some((line) => line.includes("Ground at 0, 0 changed from Grass to Wood.")));
17 assert.ok(bullets.some((line) => line.includes("Block Torch placed at 1, 0.")));
18});