mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-22 15:33:12 +09:00
Load datastores with SubscribableDatastore.fromProvider
clusterio#1129 replaced DatastoreProvider.bootstrap with async factories and Instance now takes its FactorioServer in the constructor, so the instance tests pass their server stub there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e84bd97b99
commit
afc1d35668
11 files changed
+69
-87
No files matched your search
@@ -95,12 +95,10 @@ export class ControllerPlugin {
|
|||||||
const controller = context.controller;
|
const controller = context.controller;
|
||||||
const databaseDirectory = controller.config.get("controller.database_directory");
|
const databaseDirectory = controller.config.get("controller.database_directory");
|
||||||
|
|
||||||
const accounts = new lib.SubscribableDatastore(
|
const accounts = await lib.SubscribableDatastore.fromProvider(new lib.JsonIdDatastoreProvider(
|
||||||
...await new lib.JsonIdDatastoreProvider(
|
path.join(databaseDirectory, "exp_discord_auth", "accounts.json"),
|
||||||
path.join(databaseDirectory, "exp_discord_auth", "accounts.json"),
|
messages.DiscordAccountRecord.fromJSON.bind(messages.DiscordAccountRecord),
|
||||||
messages.DiscordAccountRecord.fromJSON.bind(messages.DiscordAccountRecord),
|
));
|
||||||
).bootstrap()
|
|
||||||
);
|
|
||||||
|
|
||||||
return new ControllerPlugin(controller, context.logger, context.plugin.name, accounts);
|
return new ControllerPlugin(controller, context.logger, context.plugin.name, accounts);
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-18
@@ -26,26 +26,20 @@ export class ControllerPlugin {
|
|||||||
const controller = context.controller;
|
const controller = context.controller;
|
||||||
const databaseDirectory = controller.config.get("controller.database_directory");
|
const databaseDirectory = controller.config.get("controller.database_directory");
|
||||||
|
|
||||||
const groups = new lib.SubscribableDatastore(
|
const groups = await lib.SubscribableDatastore.fromProvider(new lib.JsonIdDatastoreProvider(
|
||||||
...await new lib.JsonIdDatastoreProvider(
|
path.join(databaseDirectory, "exp_groups", "groups.json"),
|
||||||
path.join(databaseDirectory, "exp_groups", "groups.json"),
|
messages.GroupRecord.fromJSON.bind(messages.GroupRecord),
|
||||||
messages.GroupRecord.fromJSON.bind(messages.GroupRecord),
|
));
|
||||||
).bootstrap()
|
|
||||||
);
|
|
||||||
|
|
||||||
const roleMappings = new lib.SubscribableDatastore(
|
const roleMappings = await lib.SubscribableDatastore.fromProvider(new lib.JsonIdDatastoreProvider(
|
||||||
...await new lib.JsonIdDatastoreProvider(
|
path.join(databaseDirectory, "exp_groups", "role_mappings.json"),
|
||||||
path.join(databaseDirectory, "exp_groups", "role_mappings.json"),
|
messages.RoleMappingRecord.fromJSON.bind(messages.RoleMappingRecord),
|
||||||
messages.RoleMappingRecord.fromJSON.bind(messages.RoleMappingRecord),
|
));
|
||||||
).bootstrap()
|
|
||||||
);
|
|
||||||
|
|
||||||
const manualAssignments = new lib.SubscribableDatastore(
|
const manualAssignments = await lib.SubscribableDatastore.fromProvider(new lib.JsonIdDatastoreProvider(
|
||||||
...await new lib.JsonIdDatastoreProvider(
|
path.join(databaseDirectory, "exp_groups", "assignments.json"),
|
||||||
path.join(databaseDirectory, "exp_groups", "assignments.json"),
|
messages.AssignmentRecord.fromJSON.bind(messages.AssignmentRecord),
|
||||||
messages.AssignmentRecord.fromJSON.bind(messages.AssignmentRecord),
|
));
|
||||||
).bootstrap()
|
|
||||||
);
|
|
||||||
|
|
||||||
const plugin = new ControllerPlugin(
|
const plugin = new ControllerPlugin(
|
||||||
controller, context.logger, context.plugin.name, groups, roleMappings, manualAssignments,
|
controller, context.logger, context.plugin.name, groups, roleMappings, manualAssignments,
|
||||||
|
|||||||
@@ -7,12 +7,10 @@ import * as path from "node:path";
|
|||||||
export default async function (context: ControllerPluginContext) {
|
export default async function (context: ControllerPluginContext) {
|
||||||
const { controller, plugin } = context;
|
const { controller, plugin } = context;
|
||||||
const databaseDirectory = controller.config.get("controller.database_directory");
|
const databaseDirectory = controller.config.get("controller.database_directory");
|
||||||
const players = new lib.SubscribableDatastore(
|
const players = await lib.SubscribableDatastore.fromProvider(new lib.JsonIdDatastoreProvider(
|
||||||
...await new lib.JsonIdDatastoreProvider(
|
path.join(databaseDirectory, "exp_player_statistics", "players.json"),
|
||||||
path.join(databaseDirectory, "exp_player_statistics", "players.json"),
|
messages.PlayerStatistics.fromJSON.bind(messages.PlayerStatistics),
|
||||||
messages.PlayerStatistics.fromJSON.bind(messages.PlayerStatistics),
|
));
|
||||||
).bootstrap()
|
|
||||||
);
|
|
||||||
|
|
||||||
function addDeltas(deltas: messages.StatisticsDelta[]) {
|
function addDeltas(deltas: messages.StatisticsDelta[]) {
|
||||||
const changed = new Map<string, messages.PlayerStatistics>();
|
const changed = new Map<string, messages.PlayerStatistics>();
|
||||||
|
|||||||
@@ -29,15 +29,16 @@ async function startPlugin(t2, { status = "running", stats = { Kills: 3 } } = {}
|
|||||||
instanceConfig.set("instance.id", 1);
|
instanceConfig.set("instance.id", 1);
|
||||||
instanceConfig.set("instance.name", "test");
|
instanceConfig.set("instance.name", "test");
|
||||||
|
|
||||||
const instance = new Instance(
|
|
||||||
{ assignGamePort: () => 1 }, new TestConnector(), t2.testdir(), "factorioDir", instanceConfig
|
|
||||||
);
|
|
||||||
|
|
||||||
const state = { sent: [], rcons: [], warnings: [] };
|
const state = { sent: [], rcons: [], warnings: [] };
|
||||||
const ipc = new Map();
|
const ipc = new Map();
|
||||||
instance.server = {
|
const server = {
|
||||||
|
on: () => {},
|
||||||
handle: (name, handler) => ipc.set(name, handler),
|
handle: (name, handler) => ipc.set(name, handler),
|
||||||
};
|
};
|
||||||
|
const instance = new Instance(
|
||||||
|
{ assignGamePort: () => 1 }, new TestConnector(), t2.testdir(), instanceConfig, server
|
||||||
|
);
|
||||||
instance.sendRcon = async command => {
|
instance.sendRcon = async command => {
|
||||||
state.rcons.push(command);
|
state.rcons.push(command);
|
||||||
return "";
|
return "";
|
||||||
|
|||||||
@@ -11,19 +11,15 @@ function migrateReports(rawJson: unknown[]) {
|
|||||||
export default async function (context: ControllerPluginContext) {
|
export default async function (context: ControllerPluginContext) {
|
||||||
const { controller, logger, plugin } = context;
|
const { controller, logger, plugin } = context;
|
||||||
const databaseDirectory = controller.config.get("controller.database_directory");
|
const databaseDirectory = controller.config.get("controller.database_directory");
|
||||||
const reports = new lib.SubscribableDatastore(
|
const reports = await lib.SubscribableDatastore.fromProvider(new lib.JsonIdDatastoreProvider(
|
||||||
...await new lib.JsonIdDatastoreProvider(
|
path.join(databaseDirectory, "exp_reports", "reports.json"),
|
||||||
path.join(databaseDirectory, "exp_reports", "reports.json"),
|
messages.ReportRecord.fromJSON.bind(messages.ReportRecord),
|
||||||
messages.ReportRecord.fromJSON.bind(messages.ReportRecord),
|
migrateReports,
|
||||||
migrateReports,
|
));
|
||||||
).bootstrap()
|
const watchList = await lib.SubscribableDatastore.fromProvider(new lib.JsonIdDatastoreProvider(
|
||||||
);
|
path.join(databaseDirectory, "exp_reports", "watch_list.json"),
|
||||||
const watchList = new lib.SubscribableDatastore(
|
messages.WatchListRecord.fromJSON.bind(messages.WatchListRecord),
|
||||||
...await new lib.JsonIdDatastoreProvider(
|
));
|
||||||
path.join(databaseDirectory, "exp_reports", "watch_list.json"),
|
|
||||||
messages.WatchListRecord.fromJSON.bind(messages.WatchListRecord),
|
|
||||||
).bootstrap()
|
|
||||||
);
|
|
||||||
|
|
||||||
/** Every report, or only those against one player. */
|
/** Every report, or only those against one player. */
|
||||||
function listReports(playerName?: string) {
|
function listReports(playerName?: string) {
|
||||||
|
|||||||
@@ -31,20 +31,20 @@ async function startPlugin(t2, { reports = [report(1, "bob", "alice"), report(2,
|
|||||||
instanceConfig.set("instance.id", 1);
|
instanceConfig.set("instance.id", 1);
|
||||||
instanceConfig.set("instance.name", "test");
|
instanceConfig.set("instance.name", "test");
|
||||||
|
|
||||||
const instance = new Instance(
|
|
||||||
{ assignGamePort: () => 1 }, new TestConnector(), t2.testdir(), "factorioDir", instanceConfig
|
|
||||||
);
|
|
||||||
|
|
||||||
// Spies which record the messages and commands leaving the instance
|
// Spies which record the messages and commands leaving the instance
|
||||||
const state = { sent: [], rcons: [], failNext: null };
|
const state = { sent: [], rcons: [], failNext: null };
|
||||||
const ipc = new Map();
|
const ipc = new Map();
|
||||||
instance.server = {
|
const server = {
|
||||||
|
on: () => {},
|
||||||
handle: (name, handler) => ipc.set(name, handler),
|
handle: (name, handler) => ipc.set(name, handler),
|
||||||
sendRcon: async command => {
|
sendRcon: async command => {
|
||||||
state.rcons.push(command);
|
state.rcons.push(command);
|
||||||
return "";
|
return "";
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
const instance = new Instance(
|
||||||
|
{ assignGamePort: () => 1 }, new TestConnector(), t2.testdir(), instanceConfig, server
|
||||||
|
);
|
||||||
instance.sendTo = async (dst, request) => {
|
instance.sendTo = async (dst, request) => {
|
||||||
state.sent.push(request);
|
state.sent.push(request);
|
||||||
if (state.failNext) {
|
if (state.failNext) {
|
||||||
|
|||||||
@@ -22,12 +22,10 @@ export class ControllerPlugin {
|
|||||||
const controller = context.controller;
|
const controller = context.controller;
|
||||||
const databaseDirectory = controller.config.get("controller.database_directory");
|
const databaseDirectory = controller.config.get("controller.database_directory");
|
||||||
|
|
||||||
const roleMeta = new lib.SubscribableDatastore(
|
const roleMeta = await lib.SubscribableDatastore.fromProvider(new lib.JsonIdDatastoreProvider(
|
||||||
...await new lib.JsonIdDatastoreProvider(
|
path.join(databaseDirectory, "exp_roles", "role_meta.json"),
|
||||||
path.join(databaseDirectory, "exp_roles", "role_meta.json"),
|
messages.RoleMetaRecord.fromJSON.bind(messages.RoleMetaRecord),
|
||||||
messages.RoleMetaRecord.fromJSON.bind(messages.RoleMetaRecord),
|
));
|
||||||
).bootstrap()
|
|
||||||
);
|
|
||||||
|
|
||||||
const plugin = new ControllerPlugin(controller, context.logger, context.plugin.name, roleMeta);
|
const plugin = new ControllerPlugin(controller, context.logger, context.plugin.name, roleMeta);
|
||||||
loaded.set(controller, plugin);
|
loaded.set(controller, plugin);
|
||||||
|
|||||||
@@ -40,21 +40,21 @@ async function startPlugin(t2, { syncMode = "bidirectional", roles = sampleRoles
|
|||||||
instanceConfig.set("instance.name", "test");
|
instanceConfig.set("instance.name", "test");
|
||||||
instanceConfig.set("exp_roles.sync_mode", syncMode);
|
instanceConfig.set("exp_roles.sync_mode", syncMode);
|
||||||
|
|
||||||
const instance = new Instance(
|
|
||||||
{ assignGamePort: () => 1 }, new TestConnector(), t2.testdir(), "factorioDir", instanceConfig
|
|
||||||
);
|
|
||||||
|
|
||||||
// Spies which record the messages and commands leaving the instance
|
// Spies which record the messages and commands leaving the instance
|
||||||
const state = { sent: [], rcons: [], warnings: [], failNext: null };
|
const state = { sent: [], rcons: [], warnings: [], failNext: null };
|
||||||
const handlers = new Map();
|
const handlers = new Map();
|
||||||
instance.handle = (Event, handler) => handlers.set(Event, handler);
|
const server = {
|
||||||
instance.server = {
|
on: () => {},
|
||||||
handle: (name, handler) => handlers.set(name, handler),
|
handle: (name, handler) => handlers.set(name, handler),
|
||||||
sendRcon: async command => {
|
sendRcon: async command => {
|
||||||
state.rcons.push(command);
|
state.rcons.push(command);
|
||||||
return "";
|
return "";
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
const instance = new Instance(
|
||||||
|
{ assignGamePort: () => 1 }, new TestConnector(), t2.testdir(), instanceConfig, server
|
||||||
|
);
|
||||||
|
instance.handle = (Event, handler) => handlers.set(Event, handler);
|
||||||
instance.sendTo = async (dst, request) => {
|
instance.sendTo = async (dst, request) => {
|
||||||
state.sent.push(request);
|
state.sent.push(request);
|
||||||
if (state.failNext) {
|
if (state.failNext) {
|
||||||
@@ -96,10 +96,10 @@ t.test("entrypoint", async t2 => {
|
|||||||
const instanceConfig = new lib.InstanceConfig("host");
|
const instanceConfig = new lib.InstanceConfig("host");
|
||||||
instanceConfig.set("instance.id", 1);
|
instanceConfig.set("instance.id", 1);
|
||||||
instanceConfig.set("instance.name", "test");
|
instanceConfig.set("instance.name", "test");
|
||||||
|
const server = { on: () => {}, handle: () => {} };
|
||||||
const instance = new Instance(
|
const instance = new Instance(
|
||||||
{ assignGamePort: () => 1 }, new TestConnector(), t2.testdir(), "factorioDir", instanceConfig
|
{ assignGamePort: () => 1 }, new TestConnector(), t2.testdir(), instanceConfig, server
|
||||||
);
|
);
|
||||||
instance.server = { handle: () => {} };
|
|
||||||
await entrypoint({ instance, host: {}, logger, plugin: { name: "exp_roles" } });
|
await entrypoint({ instance, host: {}, logger, plugin: { name: "exp_roles" } });
|
||||||
for (const hook of ["instanceConfigFieldChanged", "start"]) {
|
for (const hook of ["instanceConfigFieldChanged", "start"]) {
|
||||||
t2.ok([...instance.hooks[hook].attached].includes("exp_roles"), `attached to ${hook}`);
|
t2.ok([...instance.hooks[hook].attached].includes("exp_roles"), `attached to ${hook}`);
|
||||||
|
|||||||
@@ -12,18 +12,14 @@ import { SeedRole, SeedGroup, seedRoles, seedGroups, flattenSeedPermissions } fr
|
|||||||
export default async function (context: ControllerPluginContext) {
|
export default async function (context: ControllerPluginContext) {
|
||||||
const { controller, logger, plugin } = context;
|
const { controller, logger, plugin } = context;
|
||||||
const databaseDirectory = controller.config.get("controller.database_directory");
|
const databaseDirectory = controller.config.get("controller.database_directory");
|
||||||
const featureStore = new lib.SubscribableDatastore(
|
const featureStore = await lib.SubscribableDatastore.fromProvider(new lib.JsonIdDatastoreProvider(
|
||||||
...await new lib.JsonIdDatastoreProvider(
|
path.join(databaseDirectory, "exp_scenario", "features.json"),
|
||||||
path.join(databaseDirectory, "exp_scenario", "features.json"),
|
messages.FeatureRecord.fromJSON.bind(messages.FeatureRecord),
|
||||||
messages.FeatureRecord.fromJSON.bind(messages.FeatureRecord),
|
));
|
||||||
).bootstrap()
|
const playerStore = await lib.SubscribableDatastore.fromProvider(new lib.JsonIdDatastoreProvider(
|
||||||
);
|
path.join(databaseDirectory, "exp_scenario", "players.json"),
|
||||||
const playerStore = new lib.SubscribableDatastore(
|
messages.PlayerRecord.fromJSON.bind(messages.PlayerRecord),
|
||||||
...await new lib.JsonIdDatastoreProvider(
|
));
|
||||||
path.join(databaseDirectory, "exp_scenario", "players.json"),
|
|
||||||
messages.PlayerRecord.fromJSON.bind(messages.PlayerRecord),
|
|
||||||
).bootstrap()
|
|
||||||
);
|
|
||||||
|
|
||||||
type PlayerSettings = Pick<messages.PlayerRecord, "joinMessage" | "toolbar">;
|
type PlayerSettings = Pick<messages.PlayerRecord, "joinMessage" | "toolbar">;
|
||||||
|
|
||||||
|
|||||||
@@ -32,19 +32,19 @@ async function startPlugin(
|
|||||||
instanceConfig.set("instance.id", 1);
|
instanceConfig.set("instance.id", 1);
|
||||||
instanceConfig.set("instance.name", "test");
|
instanceConfig.set("instance.name", "test");
|
||||||
|
|
||||||
const instance = new Instance(
|
|
||||||
{ assignGamePort: () => 1 }, new TestConnector(), t2.testdir(), "factorioDir", instanceConfig
|
|
||||||
);
|
|
||||||
|
|
||||||
const state = { sent: [], rcons: [] };
|
const state = { sent: [], rcons: [] };
|
||||||
const ipcHandlers = new Map();
|
const ipcHandlers = new Map();
|
||||||
instance.server = {
|
const server = {
|
||||||
|
on: () => {},
|
||||||
handle: (channel, handler) => ipcHandlers.set(channel, handler),
|
handle: (channel, handler) => ipcHandlers.set(channel, handler),
|
||||||
sendRcon: async command => {
|
sendRcon: async command => {
|
||||||
state.rcons.push(command);
|
state.rcons.push(command);
|
||||||
return "";
|
return "";
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
const instance = new Instance(
|
||||||
|
{ assignGamePort: () => 1 }, new TestConnector(), t2.testdir(), instanceConfig, server
|
||||||
|
);
|
||||||
instance.sendTo = async (dst, request) => {
|
instance.sendTo = async (dst, request) => {
|
||||||
state.sent.push(request);
|
state.sent.push(request);
|
||||||
if (request instanceof messages.PlayerGetRequest) {
|
if (request instanceof messages.PlayerGetRequest) {
|
||||||
|
|||||||
@@ -35,18 +35,19 @@ async function startPlugin(t2, servers = [server(1), server(2)]) {
|
|||||||
instanceConfig.set("instance.id", 1);
|
instanceConfig.set("instance.id", 1);
|
||||||
instanceConfig.set("instance.name", "test");
|
instanceConfig.set("instance.name", "test");
|
||||||
|
|
||||||
const instance = new Instance(
|
|
||||||
{ assignGamePort: () => 1 }, new TestConnector(), t2.testdir(), "factorioDir", instanceConfig
|
|
||||||
);
|
|
||||||
|
|
||||||
const state = { sent: [], rcons: [] };
|
const state = { sent: [], rcons: [] };
|
||||||
instance.server = {
|
const server = {
|
||||||
|
on: () => {},
|
||||||
handle: () => {},
|
handle: () => {},
|
||||||
sendRcon: async command => {
|
sendRcon: async command => {
|
||||||
state.rcons.push(command);
|
state.rcons.push(command);
|
||||||
return "";
|
return "";
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
const instance = new Instance(
|
||||||
|
{ assignGamePort: () => 1 }, new TestConnector(), t2.testdir(), instanceConfig, server
|
||||||
|
);
|
||||||
instance.sendTo = async (dst, request) => {
|
instance.sendTo = async (dst, request) => {
|
||||||
if (request instanceof lib.InstanceStatusChangedEvent) {
|
if (request instanceof lib.InstanceStatusChangedEvent) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|||||||
Reference in new issue
Block a user