BlueGit

timfa questward

Files

questward / server / src / editor / service.js

service.js

service.js 7.1 KiB Download Raw
1import { groundDefinition, blockDefinition, normalizeGround, normalizeBlock } 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";
7import { closedNeighborEdges } from "../world/chunkFlags.js";
8import { coerceSettingValue, diffSettings, allSettingFields } from "../../../shared/definitions/worldSettings.js";
9
10export class EditorService
11{
12 constructor (worldDb)
13 {
14 this.worldDb = worldDb;
15 }
16
17 loadChunk (chunkX, chunkY)
18 {
19 const revisionId = this.worldDb.getEditorRevisionId();
20 const settings = this.worldDb.getLiveSettings();
21 const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId);
22 const data = row
23 ? decodeChunkBlob(row.data_blob, settings.defaultGroundTypeId)
24 : createEmptyChunkData(settings.defaultGroundTypeId);
25 const isOpen = (x, y) =>
26 {
27 const neighbor = this.worldDb.getChunkVersion(x, y, revisionId);
28 if (!neighbor)
29 { return false; }
30 return !!decodeChunkBlob(neighbor.data_blob, settings.defaultGroundTypeId).open;
31 };
32 return {
33 chunkX,
34 chunkY,
35 revisionId,
36 pendingRevisionId: this.worldDb.getPendingRevisionId(),
37 activeRevisionId: this.worldDb.getActiveRevisionId(),
38 authored: !!row,
39 settings,
40 data,
41 closedEdges: closedNeighborEdges(chunkX, chunkY, isOpen),
42 changelog: this.worldDb.listChangelog(chunkX, chunkY)
43 };
44 }
45
46 saveChunk (chunkX, chunkY, draft, editorAdminId)
47 {
48 const previous = this.loadChunk(chunkX, chunkY);
49 const next = cloneChunkData(previous.data);
50 const settings = previous.settings ?? this.worldDb.getLiveSettings();
51 next.open = !!draft.open;
52 if (Array.isArray(draft.grounds))
53 { next.grounds = draft.grounds.map((ground) => normalizeGround(ground, settings.defaultGroundTypeId)); }
54 if (Array.isArray(draft.blocks))
55 { next.blocks = draft.blocks.map((block) => normalizeBlock(block)); }
56
57 const diffEntries = diffChunks(chunkX, chunkY, previous.data, next, previous.authored);
58 const revisionId = this.worldDb.ensurePendingRevision(editorAdminId);
59 const baked = bakeLightMaps(
60 chunkX,
61 chunkY,
62 (x, y) => this.#loadForBake(x, y, revisionId, settings.defaultGroundTypeId, chunkX, chunkY, next),
63 maxDefinitionLightRange(BLOCK_DEFINITIONS)
64 );
65
66 for (const result of baked)
67 {
68 const shouldStore = result.chunkX === chunkX && result.chunkY === chunkY
69 || this.worldDb.getChunkVersion(result.chunkX, result.chunkY, revisionId)
70 || result.data.light.some((value) => value > 0);
71
72 if (!shouldStore)
73 { continue; }
74
75 this.worldDb.saveChunkVersion(
76 result.chunkX,
77 result.chunkY,
78 revisionId,
79 result.data.open ? 1 : 0,
80 encodeChunkBlob(result.data)
81 );
82 }
83
84 if (diffEntries.length > 0)
85 { this.worldDb.appendChangelog(chunkX, chunkY, revisionId, editorAdminId, diffEntries); }
86
87 return this.loadChunk(chunkX, chunkY);
88 }
89
90 loadSettings ()
91 {
92 return {
93 settings: this.worldDb.getLiveSettings(),
94 changelog: this.worldDb.listSettingsChangelog()
95 };
96 }
97
98 saveSettings (partial, editorAdminId)
99 {
100 const current = this.worldDb.getLiveSettings();
101 const next = { ...current };
102 for (const field of allSettingFields())
103 {
104 if (!Object.prototype.hasOwnProperty.call(partial, field.key))
105 { continue; }
106 const value = coerceSettingValue(field, partial[field.key]);
107 if (value == null)
108 { continue; }
109 next[field.key] = value;
110 }
111 const { bullets, changed } = diffSettings(current, next);
112 if (Object.keys(changed).length === 0)
113 {
114 return {
115 settings: current,
116 changelog: this.worldDb.listSettingsChangelog(),
117 changed: {}
118 };
119 }
120 const settings = { ...current, ...changed };
121 this.worldDb.saveLiveSettings(settings, editorAdminId);
122 this.worldDb.appendSettingsChangelog(editorAdminId, bullets);
123 return {
124 settings,
125 changelog: this.worldDb.listSettingsChangelog(),
126 changed,
127 diffEntries: bullets
128 };
129 }
130
131 applyPendingRevision ()
132 {
133 return this.worldDb.promotePendingRevision();
134 }
135
136 revisionStatus ()
137 {
138 return {
139 activeRevisionId: this.worldDb.getActiveRevisionId(),
140 pendingRevisionId: this.worldDb.getPendingRevisionId()
141 };
142 }
143
144 #loadForBake (chunkX, chunkY, revisionId, defaultGroundTypeId, savedX, savedY, savedData)
145 {
146 if (chunkX === savedX && chunkY === savedY)
147 { return savedData; }
148
149 const row = this.worldDb.getChunkVersion(chunkX, chunkY, revisionId);
150 return row
151 ? decodeChunkBlob(row.data_blob, defaultGroundTypeId)
152 : createEmptyChunkData(defaultGroundTypeId);
153 }
154}
155
156export function diffChunks (chunkX, chunkY, previous, next, previousAuthored)
157{
158 const bullets = [];
159 const prevOpen = previousAuthored ? !!previous.open : false;
160 if (prevOpen !== !!next.open)
161 { bullets.push(next.open ? "Chunk opened." : "Chunk closed."); }
162
163 for (let localY = 0; localY < CHUNK_SIZE; localY++)
164 {
165 for (let localX = 0; localX < CHUNK_SIZE; localX++)
166 {
167 const index = localY * CHUNK_SIZE + localX;
168 const tileX = localToTile(chunkX, localX);
169 const tileY = localToTile(chunkY, localY);
170 const prevGround = normalizeGround(previous.grounds[index]);
171 const nextGround = normalizeGround(next.grounds[index]);
172 if (prevGround.typeId !== nextGround.typeId)
173 {
174 bullets.push(
175 `Ground at ${tileX}, ${tileY} changed from ${nameGround(prevGround.typeId)} to ${nameGround(nextGround.typeId)}.`
176 );
177 }
178 else if ((prevGround.state ?? "") !== (nextGround.state ?? ""))
179 {
180 bullets.push(
181 `Ground ${nameGround(nextGround.typeId)} at ${tileX}, ${tileY} state changed from ${stateLabel(prevGround.state)} to ${stateLabel(nextGround.state)}.`
182 );
183 }
184
185 const prevBlock = normalizeBlock(previous.blocks[index]);
186 const nextBlock = normalizeBlock(next.blocks[index]);
187 const prevId = prevBlock?.typeId ?? null;
188 const nextId = nextBlock?.typeId ?? null;
189 const prevConfig = JSON.stringify(prevBlock?.config ?? null);
190 const nextConfig = JSON.stringify(nextBlock?.config ?? null);
191 if (prevId !== nextId || prevConfig !== nextConfig)
192 {
193 if (prevId == null && nextId != null)
194 { bullets.push(`Block ${nameBlock(nextId)} placed at ${tileX}, ${tileY}.`); }
195 else if (prevId != null && nextId == null)
196 { bullets.push(`Block ${nameBlock(prevId)} removed at ${tileX}, ${tileY}.`); }
197 else
198 { bullets.push(`Block at ${tileX}, ${tileY} changed from ${nameBlock(prevId)} to ${nameBlock(nextId)}.`); }
199 }
200 else if (prevBlock && nextBlock && (prevBlock.state ?? "") !== (nextBlock.state ?? ""))
201 {
202 bullets.push(
203 `Block ${nameBlock(nextId)} at ${tileX}, ${tileY} state changed from ${stateLabel(prevBlock.state)} to ${stateLabel(nextBlock.state)}.`
204 );
205 }
206 }
207 }
208 return bullets;
209}
210
211function nameGround (typeId)
212{
213 return groundDefinition(typeId)?.displayName ?? `ground ${typeId}`;
214}
215
216function nameBlock (typeId)
217{
218 return blockDefinition(typeId)?.displayName ?? `block ${typeId}`;
219}
220
221function stateLabel (state)
222{
223 return state || "default";
224}