mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-23 23:33:12 +09:00
Sync the toolbar of players through the controller
The toolbar order, favourites, and open panels were saved into exp_legacy's PlayerData, which was never synced, and the file doing so was no longer loaded. When a player leaves, the lua side now sends Gui.toolbar.get_state as json compressed with helpers.encode_string, and the controller stores the string on the player record. When the player joins again the instance plugin sends the stored string over rcon to exp_scenario.player_settings.receive_toolbar, which decodes it and calls Gui.toolbar.set_state. exp_gui builds the toolbar in on_player_joined_game, so it exists by the time the rcon arrives. A string which does not decode, or a state naming buttons which no longer exist, is logged and ignored rather than raising in the game. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ebe7504c00
commit
54941b85aa
15 files changed
+228
-45
No files matched your search
@@ -290,6 +290,18 @@ t.test("player settings", t2 => {
|
||||
t3.equal(cleared.locale, "pt-BR", "clearing the message keeps the locale");
|
||||
});
|
||||
|
||||
t2.test("PlayerToolbarEvent stores the toolbar and it is saved", async t3 => {
|
||||
const databaseDirectory = t3.testdir();
|
||||
const { send, save } = await startPlugin(t3, { withPlugins: false, databaseDirectory });
|
||||
await send(new messages.PlayerToolbarEvent("Alice", "abc"));
|
||||
t3.equal((await send(new messages.PlayerGetRequest("Alice"))).toolbar, "abc");
|
||||
|
||||
await save();
|
||||
const stored = await loadPlayers(databaseDirectory);
|
||||
const alice = stored.get("Alice");
|
||||
t3.strictSame(alice.toJSON(), { id: "Alice", toolbar: "abc", updated_at_ms: alice.updatedAtMs });
|
||||
});
|
||||
|
||||
t2.test("permissions", async t3 => {
|
||||
const user = (name, granted = []) => ({
|
||||
name,
|
||||
|
||||
@@ -195,6 +195,27 @@ t.test("player settings", t2 => {
|
||||
t3.strictSame(sent.map(request => [request.playerName, request.message]), [["Alice", "Hi"], ["Alice", null]]);
|
||||
});
|
||||
|
||||
t2.test("a joining player's stored toolbar is sent to the lua side", async t3 => {
|
||||
const bob = new messages.PlayerRecord("Bob", undefined, undefined, "eNqrVkrLz1eyUlBKSixSqgUAGQwD8g==");
|
||||
const { start, playerEvent, setStatus, state } = await startPlugin(t3, [], [bob]);
|
||||
await start();
|
||||
setStatus("running");
|
||||
state.rcons.length = 0;
|
||||
|
||||
await playerEvent({ type: "join", name: "Bob" });
|
||||
t3.equal(state.rcons.length, 1, "only the toolbar, Bob has no join message");
|
||||
const { data, code } = decodePlayerRcon(state.rcons[0]);
|
||||
t3.strictSame(data, { toolbar: bob.toolbar, name: "Bob" });
|
||||
t3.equal(code, "exp_scenario.player_settings.receive_toolbar(d.name, d.toolbar)");
|
||||
});
|
||||
|
||||
t2.test("ipc with a toolbar is forwarded to the controller", async t3 => {
|
||||
const { ipc, state } = await startPlugin(t3);
|
||||
await ipc("exp_scenario:toolbar", { player_name: "Alice", toolbar: "abc" });
|
||||
const [event] = state.sent.filter(request => request instanceof messages.PlayerToolbarEvent);
|
||||
t3.strictSame([event.playerName, event.toolbar], ["Alice", "abc"]);
|
||||
});
|
||||
|
||||
t2.test("ipc with a locale is forwarded to the controller", async t3 => {
|
||||
const { ipc, state } = await startPlugin(t3);
|
||||
await ipc("exp_scenario:locale", { player_name: "Alice", locale: "de" });
|
||||
|
||||
@@ -6,3 +6,6 @@ const envFile = path.join(import.meta.dirname, "module", "env.lua");
|
||||
|
||||
// Each file runs in its own lua state, and each test in a fresh environment
|
||||
t.test("features.lua", t2 => reportLuaTests(t2, envFile, path.join(import.meta.dirname, "module", "features.lua")));
|
||||
t.test("player_settings.lua", t2 => reportLuaTests(
|
||||
t2, envFile, path.join(import.meta.dirname, "module", "player_settings.lua"),
|
||||
));
|
||||
@@ -17,6 +17,7 @@ local Framework = assert(loadfile(shared_root .. "/framework.lua"))() --- @type
|
||||
--- @field storage table The table registered with storage, as on_init would store it
|
||||
--- @field on_load fun(tbl: table) Act out on_load with the given storage table
|
||||
--- @field load_config fun(name: string): ExpScenario.Feature Load module/config/<name>.lua
|
||||
--- @field load_module fun(name: string): any Load module/<name>.lua, such as "control/player_settings"
|
||||
--- @field logged string[] Messages passed to log
|
||||
--- @field events table[] Raised events, name and tick filled in
|
||||
|
||||
@@ -51,5 +52,9 @@ return Framework.suite(function(env)
|
||||
return assert(loadfile(plugin_root .. "/module/config/" .. name .. ".lua"))()
|
||||
end
|
||||
|
||||
function env.load_module(name)
|
||||
return assert(loadfile(plugin_root .. "/module/" .. name .. ".lua"))()
|
||||
end
|
||||
|
||||
return env
|
||||
end)
|
||||
@@ -0,0 +1,79 @@
|
||||
local Suite = ... --- @type Suite<ExpScenario.TestEnv>
|
||||
|
||||
local valid_state = { order = { { name = "readme", favourite = true } }, open = {}, visible = true }
|
||||
|
||||
--- Load control/player_settings.lua with a stubbed toolbar, encoding is a "enc:" prefix
|
||||
--- @param env ExpScenario.TestEnv
|
||||
--- @param toolbar table? Replaces the Gui.toolbar stub
|
||||
--- @return table PlayerSettings
|
||||
--- @return table restored Calls to set_state as { player name, state }
|
||||
local function load(env, toolbar)
|
||||
local restored = {}
|
||||
env.extend_defines{ events = { on_player_locale_changed = 3, on_player_left_game = 4 } }
|
||||
env.extend_requires{
|
||||
["modules/exp_gui"] = {
|
||||
toolbar = toolbar or {
|
||||
get_state = function() return valid_state end,
|
||||
set_state = function(player, state) restored[#restored + 1] = { player.name, state } end,
|
||||
},
|
||||
},
|
||||
}
|
||||
local json_values = { ['{"valid"}'] = valid_state, ['{"wrong shape"}'] = { order = "x" } }
|
||||
helpers = {
|
||||
encode_string = function(str) return "enc:" .. str end,
|
||||
decode_string = function(str) return str:match("^enc:(.*)$") end,
|
||||
table_to_json = function(tbl) return tbl == valid_state and '{"valid"}' or "{}" end,
|
||||
json_to_table = function(json) return json_values[json] end,
|
||||
}
|
||||
return env.load_module("control/player_settings"), restored
|
||||
end
|
||||
|
||||
Suite.test("send_locale() sends the locale of the player", function(env)
|
||||
local PlayerSettings = load(env)
|
||||
rawset(env.add_player{ name = "Alice" }, "locale", "pt-BR")
|
||||
PlayerSettings.events[defines.events.on_player_joined_game]{ player_index = 1 }
|
||||
Suite.eq(env.sent, { { channel = "exp_scenario:locale", data = { player_name = "Alice", locale = "pt-BR" } } }, "sent")
|
||||
end)
|
||||
|
||||
Suite.test("send_toolbar() sends the compressed toolbar when a player leaves", function(env)
|
||||
local PlayerSettings = load(env)
|
||||
env.add_player{ name = "Alice" }
|
||||
PlayerSettings.events[defines.events.on_player_left_game]{ player_index = 1 }
|
||||
Suite.eq(env.sent, { { channel = "exp_scenario:toolbar", data = { player_name = "Alice", toolbar = 'enc:{"valid"}' } } }, "sent")
|
||||
end)
|
||||
|
||||
Suite.test("send_toolbar() sends nothing when the toolbar can not be read", function(env)
|
||||
local PlayerSettings = load(env, { get_state = function() error("no toolbar") end })
|
||||
env.add_player{ name = "Alice" }
|
||||
PlayerSettings.events[defines.events.on_player_left_game]{ player_index = 1 }
|
||||
Suite.eq(env.sent, {}, "nothing sent")
|
||||
Suite.eq(#env.logged, 1, "a warning is logged")
|
||||
end)
|
||||
|
||||
Suite.test("receive_toolbar() restores the toolbar of online players", function(env)
|
||||
local PlayerSettings, restored = load(env)
|
||||
env.add_player{ name = "Alice" }
|
||||
env.add_player{ name = "Bob", connected = false }
|
||||
PlayerSettings.receive_toolbar("Alice", 'enc:{"valid"}')
|
||||
PlayerSettings.receive_toolbar("Bob", 'enc:{"valid"}')
|
||||
PlayerSettings.receive_toolbar("Unknown", 'enc:{"valid"}')
|
||||
Suite.eq(restored, { { "Alice", valid_state } }, "only Alice is online")
|
||||
end)
|
||||
|
||||
Suite.test("receive_toolbar() logs malformed toolbars instead of raising", function(env)
|
||||
local PlayerSettings, restored = load(env)
|
||||
env.add_player{ name = "Alice" }
|
||||
for _, toolbar in ipairs{ "not encoded", "enc:not json", 'enc:{"wrong shape"}' } do
|
||||
PlayerSettings.receive_toolbar("Alice", toolbar)
|
||||
end
|
||||
Suite.eq(restored, {}, "nothing is restored")
|
||||
Suite.eq(#env.logged, 3, "each one is logged")
|
||||
end)
|
||||
|
||||
Suite.test("receive_toolbar() logs errors from set_state", function(env)
|
||||
local PlayerSettings = load(env, { set_state = function() error("Missing toolbox item for removed") end })
|
||||
env.add_player{ name = "Alice" }
|
||||
PlayerSettings.receive_toolbar("Alice", 'enc:{"valid"}')
|
||||
Suite.eq(#env.logged, 1, "a warning is logged")
|
||||
end)
|
||||
return Suite.run()
|
||||
Reference in new issue
Block a user