mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 07:04:52 +09:00
Remove extra whitespace, use more consistent spacing, add missing semicolons, using dangling commas, break up long lines, consistent multiline operator placement, add missing JSDoc strings and so on.
57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import * as lib from "@clusterio/lib";
|
|
|
|
import { FetchPlayerCodeRequest, SetVerifyCodeRequest } from "./messages";
|
|
|
|
type IpcPlayerAuth = {
|
|
type: "open_dialog",
|
|
player: string,
|
|
} | {
|
|
type: "set_verify_code",
|
|
player: string,
|
|
verify_code: string,
|
|
}
|
|
|
|
export class InstancePlugin extends lib.BaseInstancePlugin {
|
|
async init() {
|
|
if (!this.instance.config.get("factorio.enable_save_patching")) {
|
|
throw new Error("player_auth plugin requires save patching.");
|
|
}
|
|
|
|
this.instance.server.on(
|
|
"ipc-player_auth",
|
|
(ipcPlayerAuth: IpcPlayerAuth) => this.handleEvent(ipcPlayerAuth).catch(
|
|
err => this.logger.error(`Error handling event:\n${err.stack}`)
|
|
)
|
|
);
|
|
}
|
|
|
|
async handleEvent(event: IpcPlayerAuth) {
|
|
if (event.type === "open_dialog") {
|
|
if (!this.host.connector.connected) {
|
|
await this.sendRcon(`/web-login error ${event.player} login is temporarily unavailable`);
|
|
return;
|
|
}
|
|
|
|
let response;
|
|
try {
|
|
response = await this.instance.sendTo("controller", new FetchPlayerCodeRequest(event.player));
|
|
} catch (err: any) {
|
|
await this.sendRcon(`/web-login error ${event.player} ${err.message}`);
|
|
return;
|
|
}
|
|
await this.sendRcon(`/web-login open ${event.player} ${response.controllerUrl} ${response.playerCode}`);
|
|
|
|
} else if (event.type === "set_verify_code") {
|
|
try {
|
|
await this.instance.sendTo("controller", new SetVerifyCodeRequest(event.player, event.verify_code));
|
|
|
|
} catch (err: any) {
|
|
await this.sendRcon(`/web-login error ${event.player} ${err.message}`);
|
|
return;
|
|
}
|
|
|
|
await this.sendRcon(`/web-login code_set ${event.player}`);
|
|
}
|
|
}
|
|
}
|