mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 15:30:41 +09:00
Add inspect and modify tools for player sync
Admins had no way to look at or fix the player data the controller stores when a sync went wrong. The web page now lists every stored entry with its generation, size and the instance holding the lock, and opens a drawer per player with an inventory summary, JSON export and import, delete and lock release. The same operations are exposed as clusterioctl player-sync commands. Import bumps the generation past the stored one so instances holding an older copy download the new data on the next join. Import and delete are refused while an instance holds the lock, since its upload on leave would undo the change. A new player_sync.inventory.modify permission gates the write operations. Closes #1003 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
5e2ecdd143
commit
a81d98e981
12 files changed
+953
-22
No files matched your search
@@ -55,14 +55,22 @@ export default async function(context: ControllerPluginContext) {
|
||||
const playerDatastore: Map<string, IpcPlayerData> = await loadDatabase(controller.config, logger);
|
||||
let playerDatastoreDirty = false;
|
||||
|
||||
function acquire(instanceId: number, playerName: string): boolean {
|
||||
// Returns the acquisition record for the player if it's still in effect
|
||||
function getAcquisition(playerName: string) {
|
||||
let acquisitionRecord = acquiredPlayers.get(playerName);
|
||||
if (
|
||||
!acquisitionRecord
|
||||
|| acquisitionRecord.instanceId === instanceId
|
||||
|| !controller.instances.has(acquisitionRecord.instanceId)
|
||||
|| acquisitionRecord.expiresMs && acquisitionRecord.expiresMs < Date.now()
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
return acquisitionRecord;
|
||||
}
|
||||
|
||||
function acquire(instanceId: number, playerName: string): boolean {
|
||||
let acquisitionRecord = getAcquisition(playerName);
|
||||
if (!acquisitionRecord || acquisitionRecord.instanceId === instanceId) {
|
||||
acquiredPlayers.set(playerName, { instanceId });
|
||||
return true;
|
||||
}
|
||||
@@ -70,6 +78,19 @@ export default async function(context: ControllerPluginContext) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Modifying stored data while an instance holds the player is pointless
|
||||
// as the instance uploads its own copy when the player leaves.
|
||||
function checkNotAcquired(playerName: string) {
|
||||
let acquisitionRecord = getAcquisition(playerName);
|
||||
if (acquisitionRecord) {
|
||||
let instanceName = controller.instances.get(acquisitionRecord.instanceId)!.config.get("instance.name");
|
||||
throw new lib.RequestError(
|
||||
`${playerName} is currently acquired by ${instanceName}, ` +
|
||||
"have the player leave or release the lock first"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
controller.hooks.instanceStatusChanged.attach(plugin.name, async (instance: InstanceRecord) => {
|
||||
let instanceId = instance.id;
|
||||
if (["unassigned", "deleted"].includes(instance.status)) {
|
||||
@@ -202,4 +223,63 @@ export default async function(context: ControllerPluginContext) {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
controller.handle(msg.ListPlayersRequest, async () => {
|
||||
let entries = new Map<string, msg.PlayerEntry>();
|
||||
for (let [name, playerData] of playerDatastore) {
|
||||
entries.set(name, new msg.PlayerEntry(name, playerData.generation, JSON.stringify(playerData).length));
|
||||
}
|
||||
for (let name of acquiredPlayers.keys()) {
|
||||
let acquisitionRecord = getAcquisition(name);
|
||||
if (!acquisitionRecord) {
|
||||
continue;
|
||||
}
|
||||
let entry = entries.get(name) ?? new msg.PlayerEntry(name);
|
||||
entry.instanceId = acquisitionRecord.instanceId;
|
||||
entries.set(name, entry);
|
||||
}
|
||||
return [...entries.values()];
|
||||
});
|
||||
|
||||
controller.handle(msg.GetPlayerDataRequest, async (request: msg.GetPlayerDataRequest) => {
|
||||
let { playerName } = request;
|
||||
return new msg.GetPlayerDataRequest.Response(
|
||||
playerDatastore.get(playerName) ?? null,
|
||||
getAcquisition(playerName)?.instanceId,
|
||||
);
|
||||
});
|
||||
|
||||
controller.handle(msg.SetPlayerDataRequest, async (request: msg.SetPlayerDataRequest) => {
|
||||
let { playerName, playerData } = request;
|
||||
checkNotAcquired(playerName);
|
||||
|
||||
// Bump the generation so instances holding an older copy download this one
|
||||
let oldPlayerData = playerDatastore.get(playerName);
|
||||
playerData.name = playerName;
|
||||
playerData.generation = Math.max(playerData.generation, (oldPlayerData?.generation ?? 0) + 1);
|
||||
playerDatastore.set(playerName, playerData);
|
||||
playerDatastoreDirty = true;
|
||||
logger.info(`Replaced player data for ${playerName} with generation ${playerData.generation}`);
|
||||
return new msg.SetPlayerDataRequest.Response(playerData.generation);
|
||||
});
|
||||
|
||||
controller.handle(msg.DeletePlayerDataRequest, async (request: msg.DeletePlayerDataRequest) => {
|
||||
let { playerName } = request;
|
||||
if (!playerDatastore.has(playerName)) {
|
||||
throw new lib.RequestError(`No player data stored for ${playerName}`);
|
||||
}
|
||||
checkNotAcquired(playerName);
|
||||
|
||||
playerDatastore.delete(playerName);
|
||||
playerDatastoreDirty = true;
|
||||
logger.info(`Deleted player data for ${playerName}`);
|
||||
});
|
||||
|
||||
controller.handle(msg.ForceReleaseRequest, async (request: msg.ForceReleaseRequest) => {
|
||||
let { playerName } = request;
|
||||
if (!acquiredPlayers.delete(playerName)) {
|
||||
throw new lib.RequestError(`${playerName} is not acquired by any instance`);
|
||||
}
|
||||
logger.info(`Released lock on ${playerName}`);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user