mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 19:45:22 +09:00
* Fix bugs in core and add default args to Gui defs * Refactor production Gui * Refactor landfill blueprint button * Fix more bugs in core * Consistent naming of new guis * Refactor module inserter gui * Refactor surveillance gui * Add shorthand for data from arguments * Make element names consistent * Add types * Change how table rows work * Refactor player stats gui * Refactor quick actions gui * Refactor research milestones gui * Refactor player bonus gui * Refactor science production gui * Refactor autofill gui * Cleanup use of aligned flow * Rename "Gui.element" to "Gui.define" * Rename Gui types * Rename property_from_arg * Add guide for making guis * Add full reference document * Add condensed reference * Apply style guide to refactored guis * Bug fixes
49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import * as lib from "@clusterio/lib";
|
|
import { BaseInstancePlugin } from "@clusterio/host";
|
|
|
|
export class InstancePlugin extends BaseInstancePlugin {
|
|
private updateInterval?: ReturnType<typeof setInterval>;
|
|
private gameTimes: number[] = [];
|
|
|
|
async onStart() {
|
|
this.updateInterval = setInterval(this.updateUps.bind(this), this.instance.config.get("exp_server_ups.update_interval"));
|
|
}
|
|
|
|
onExit() {
|
|
if (this.updateInterval) {
|
|
clearInterval(this.updateInterval);
|
|
}
|
|
}
|
|
|
|
async onInstanceConfigFieldChanged(field: string, curr: unknown): Promise<void> {
|
|
if (field === "exp_server_ups.update_interval") {
|
|
this.onExit();
|
|
await this.onStart();
|
|
} else if (field === "exp_server_ups.average_interval") {
|
|
this.gameTimes.splice(curr as number);
|
|
}
|
|
}
|
|
|
|
async updateUps() {
|
|
let ups = 0;
|
|
const collected = this.gameTimes.length - 1;
|
|
if (collected > 0) {
|
|
const minTick = this.gameTimes[0];
|
|
const maxTick = this.gameTimes[collected];
|
|
const interval = this.instance.config.get("exp_server_ups.update_interval") / 1000;
|
|
ups = (maxTick - minTick) / (collected * interval);
|
|
}
|
|
|
|
try {
|
|
const newGameTime = await this.sendRcon(`/_rcon return exp_server_ups.refresh(${ups})`);
|
|
this.gameTimes.push(Number(newGameTime));
|
|
} catch (error: any) {
|
|
this.logger.error(`Failed to receive new game time: ${error}`);
|
|
}
|
|
|
|
if (collected > this.instance.config.get("exp_server_ups.average_interval")) {
|
|
this.gameTimes.shift();
|
|
}
|
|
}
|
|
}
|