Skip to content

Commit

Permalink
Add support for editing COIL through an action
Browse files Browse the repository at this point in the history
  • Loading branch information
mia-pi-git committed Jul 20, 2024
1 parent 870d2ac commit 9f5b6ab
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/config-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ exports.mainserver = 'showdown';
exports.serverlist = '/var/www/html/play.pokemonshowdown.com/config/servers.inc.php';
/** @type {string | null} */
exports.colorpath = null;
/** @type {string | null} */
exports.coilpath = null;

/** @type {string | null} Password, whether to debug error stacks in request or not*/
exports.devmode = null;
Expand Down
35 changes: 35 additions & 0 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,41 @@ export const actions: {[k: string]: QueryHandler} = {
return {success: true};
},

async updatecoil(params) {
await this.requireMainServer();

const formatid = toID(params.format);
if (!formatid) {
throw new ActionError('No format was specified.');
}
const source = parseInt(toID(params.coil_b));
if ('coil_b' in params && (isNaN(source) || !source || source < 1)) {
throw new ActionError('No B value was specified.');
}
if (!Config.coilpath) {
throw new ActionError("Editing COIL is disabled");
}
const coil = {} as Record<string, number>;
try {
const content = await fs.readFile(Config.coilpath, 'utf-8');
Object.assign(coil, JSON.parse(content));
} catch (e) {
throw new ActionError(`Could not read COIL file (${e})`);
}
if (!('coil_b' in params)) {
if (!coil[formatid]) {
throw new ActionError('That format does not have COIL set.');
} else {
delete coil[formatid];
}
} else {
coil[formatid] = source;
}
await fs.writeFile(Config.coilpath, JSON.stringify(coil));

return {success: true};
},

async setstanding(params) {
await this.requireMainServer();

Expand Down

0 comments on commit 9f5b6ab

Please sign in to comment.