mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 07:30:41 +09:00
Forward player data to the instance a player is switching to
An upload can carry destinationInstanceId. Once the upload is stored, the controller sends the stored data, the upload merged with its copy, in a PrefetchEvent to that instance, if it is another running instance with player_sync enabled. No lock is taken or released. A failed send is logged and doesn't fail the upload. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
4128f75328
commit
b8c1e89dff
4 files changed
+124
-2
No files matched your search
@@ -113,6 +113,26 @@ export default async function(context: ControllerPluginContext) {
|
||||
}
|
||||
}
|
||||
|
||||
// Send stored player data on to the instance the player left to switch to (set_destination in the Lua module).
|
||||
// Only another running instance with player_sync on: a copy applied on the source would rebuild the body there.
|
||||
function prefetch(sourceId: number, targetId: number, playerData: IpcPlayerData) {
|
||||
const target = controller.instances.get(targetId);
|
||||
if (
|
||||
!target || targetId === sourceId || target.status !== "running"
|
||||
|| !target.config.get("player_sync.load_plugin")
|
||||
) {
|
||||
logger.verbose(`Not prefetching ${playerData.name} to instance ${targetId}`);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
controller.sendTo({ instanceId: targetId }, new msg.PrefetchEvent(playerData));
|
||||
logger.verbose(`Prefetching ${playerData.name} to ${target.config.get("instance.name")}`);
|
||||
} catch (err: any) {
|
||||
// sendEvent throws when the instance's host isn't connected; the upload is stored either way
|
||||
logger.warn(`Failed to prefetch ${playerData.name} to instance ${targetId}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
controller.hooks.instanceStatusChanged.attach(plugin.name, async (instance: InstanceRecord) => {
|
||||
let instanceId = instance.id;
|
||||
if (["unassigned", "deleted"].includes(instance.status)) {
|
||||
@@ -225,9 +245,14 @@ export default async function(context: ControllerPluginContext) {
|
||||
|
||||
if (store) {
|
||||
logger.verbose(`Received player data for ${playerName} from ${instanceName}`);
|
||||
playerDatastore.set(playerName, mergePlayerData(playerDatastore.get(playerName), playerData));
|
||||
const merged = mergePlayerData(playerDatastore.get(playerName), playerData);
|
||||
playerDatastore.set(playerName, merged);
|
||||
tombstones.delete(playerName);
|
||||
playerDatastoreDirty = true;
|
||||
if (request.destinationInstanceId !== undefined) {
|
||||
// The stored copy: components the upload left out are the controller's
|
||||
prefetch(instanceId, request.destinationInstanceId, merged);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user