Merge uploads by the components they were serialized with

The controller merged uploads using the uploading instance's current config. An upload serialized under different settings, for example one sent from the save right after an admin turned on sync_inventories and restarted the instance, was then merged as if it held a character and wiped the stored one. Uploads now carry the components they were serialized with, the controller merges by those and drops the key before storing.

The scenario keeps the components a player's synced session started with and serializes their upload with them, so changing the options while a player is online takes effect the next time they join. A player whose session lacks a component that is now synced downloads again even if the generation matches. Downloads snapshot the components when they start, so a change during a download can't mix two sets.

A failed acquire unsyncs the player again regardless of the inventories setting, as before this branch.

The god and dead player inventories move from the inventories component to the controller one, matching what the scenario syncs, which removes the special case in mergePlayerData.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-10-17 15:25:06 +00:00
1 parent 8487c6d923
commit b0e07ea73b
11 files changed
+150 -58

No files matched your search

+45 -11
View File
@@ -1,6 +1,8 @@
import assert from "node:assert/strict";
import { components, enabledComponents, mergePlayerData, stripPlayerData } from "../dist/node/components.js";
import {
components, enabledComponents, mergePlayerData, stripPlayerData, uploadedComponents,
} from "../dist/node/components.js";
const allComponents = Object.keys(components);
@@ -69,15 +71,45 @@ describe("player_sync components", function() {
}
assert.equal(playerData.force, "enemy");
});
it("should remove the god inventory with the controller", function() {
const playerData = structuredClone(stored);
stripPlayerData(playerData, new Set(allComponents.filter(c => c !== "controller")));
assert.equal("inventories" in playerData, false);
assert.equal("controller" in playerData, false);
assert.equal(playerData.tag, "stored");
});
});
describe("uploadedComponents()", function() {
it("should sync everything when the upload has no components", function() {
assert.deepEqual([...uploadedComponents(undefined)], allComponents);
});
it("should sync the components marked true", function() {
const enabled = uploadedComponents({ force: true, quick_bar: true, settings: false });
assert.deepEqual([...enabled], ["force", "quick_bar"]);
});
it("should sync nothing for an empty table encoded as an array", function() {
assert.equal(uploadedComponents([]).size, 0);
});
it("should not sync the controller without the inventories", function() {
assert.equal(uploadedComponents({ controller: true }).has("controller"), false);
});
});
describe("mergePlayerData()", function() {
it("should store the upload as is when all components are synced", function() {
assert.deepEqual(mergePlayerData(stored, uploaded, new Set(allComponents)), uploaded);
function withComponents(data, disabled) {
const enabled = allComponents.filter(c => !disabled.includes(c));
return { ...data, components: Object.fromEntries(enabled.map(c => [c, true])) };
}
it("should store the upload as is when it has no components", function() {
assert.deepEqual(mergePlayerData(stored, uploaded), uploaded);
});
it("should not store the components of the upload", function() {
assert.deepEqual(mergePlayerData(stored, withComponents(uploaded, [])), uploaded);
});
it("should keep the stored fields of disabled components", function() {
const enabled = new Set(allComponents.filter(c => !["force", "appearance"].includes(c)));
const merged = mergePlayerData(stored, uploaded, enabled);
const merged = mergePlayerData(stored, withComponents(uploaded, ["force", "appearance"]));
assert.equal(merged.force, "player");
assert.deepEqual(merged.color, [1, 0, 0]);
assert.equal(merged.tag, "stored");
@@ -85,27 +117,29 @@ describe("player_sync components", function() {
assert.equal(merged.generation, 2);
});
it("should drop disabled components without stored data", function() {
const merged = mergePlayerData(undefined, uploaded, new Set(allComponents.filter(c => c !== "force")));
const merged = mergePlayerData(undefined, withComponents(uploaded, ["force"]));
assert.equal("force" in merged, false);
assert.equal(merged.tag, "uploaded");
});
it("should take the character but not the controller when the controller is not synced", function() {
const merged = mergePlayerData(stored, uploaded, new Set(allComponents.filter(c => c !== "controller")));
const merged = mergePlayerData(stored, withComponents(uploaded, ["controller"]));
assert.equal(merged.controller, "god");
assert.equal(merged.cheat_mode, true);
assert.equal(merged.character, character);
assert.deepEqual(merged.inventories, { main: ["stored god item"] }, "god inventory is kept");
});
it("should keep the character data for a player without one when the controller is not synced", function() {
const storedCharacter = { ...stored, character, crafting_queue: ["stored"], personal_logistic_slots: [] };
const storedCharacter = {
...stored, character, crafting_queue: ["stored"], personal_logistic_slots: [{ name: "stored" }],
};
const god = { ...uploaded, controller: "god", inventories: { main: ["new god item"] } };
delete god.character;
delete god.crafting_queue;
delete god.personal_logistic_slots;
const enabled = new Set(allComponents.filter(c => c !== "controller"));
const merged = mergePlayerData(storedCharacter, god, enabled);
god.personal_logistic_slots = [{ name: "uploaded" }];
const merged = mergePlayerData(storedCharacter, withComponents(god, ["controller"]));
assert.equal(merged.character, character);
assert.deepEqual(merged.crafting_queue, ["stored"]);
assert.deepEqual(merged.personal_logistic_slots, [{ name: "stored" }], "logistics stay with the character");
assert.deepEqual(merged.inventories, { main: ["stored god item"] });
});
});
+6 -4
View File
@@ -5,6 +5,7 @@ import path from "node:path";
import * as lib from "@clusterio/lib";
import * as mock from "../../../test/mock.js";
import controllerEntrypoint from "../dist/node/controller.js";
import { components as allComponents } from "../dist/node/components.js";
import { plugin as info } from "../dist/node/index.js";
import { DatabaseStatsRequest, DownloadRequest, UploadRequest } from "../dist/node/messages.js";
@@ -75,14 +76,15 @@ describe("player_sync controller", function() {
.playerData;
}
it("should store the upload as is when everything is synced", async function() {
it("should store the upload as is when it has no components", async function() {
const playerData = { generation: 2, name: "test", force: "enemy", tag: "uploaded" };
assert.deepEqual(await upload(playerData), playerData);
});
it("should keep stored fields the uploading instance does not sync", async function() {
disabled = ["force"];
it("should merge by the payload's components and ignore the instance config", async function() {
disabled = ["appearance"];
const components = Object.fromEntries(Object.keys(allComponents).map(c => [c, c !== "force"]));
assert.deepEqual(
await upload({ generation: 2, name: "test", tag: "uploaded" }),
await upload({ generation: 2, name: "test", tag: "uploaded", components }),
{ generation: 2, name: "test", force: "player", tag: "uploaded" },
);
});
+9
View File
@@ -169,5 +169,14 @@ describe("player_sync", function() {
await instance.hooks.instanceConfigFieldChanged.invoke("player_sync.sync_force", false, true);
assert.equal(instance.server.rconCommands.length, 1);
});
it("should not send the components when the instance is not running", async function() {
instance.status = "stopped";
try {
await instance.hooks.instanceConfigFieldChanged.invoke("player_sync.sync_force", false, true);
} finally {
instance.status = "running";
}
assert.equal(instance.server.rconCommands.length, 0);
});
});
});