Skip to content

Commit

Permalink
Tour-cmd: Add missing configuration option for poll sets
Browse files Browse the repository at this point in the history
  • Loading branch information
AgustinSRG committed Dec 14, 2024
1 parent e2a844f commit ab9b657
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
21 changes: 13 additions & 8 deletions src/bot-modules/tour-cmd/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,14 @@ module.exports = {

if (!this.can('ldbcustomconfig', this.room)) return this.replyAccessDenied('ldbcustomconfig');

const Mod = App.modules.tourcmd.system;
const Config = App.config.modules.tourcmd;

const subCommand = Text.toId(this.args[0] || "");

switch (subCommand) {
case "list":
{
const sets = Object.values(Mod.tourPollSets);
const sets = Object.values(Config.pollSets);

let text = "" + this.mlt("setlist") + ":\n\n";

Expand Down Expand Up @@ -519,14 +519,16 @@ module.exports = {
formats.push(formatData.name);
}

Mod.tourPollSets[id] = {
Config.pollSets[id] = {
name: name,
formats: formats,
};

Mod.tourPollSetsDB.write();
App.db.write();

this.addToSecurityLog();

this.reply(this.mlt('setok1') + " " + Chat.italics(name) + " " + this.mlt("setok2"));
this.reply(this.mlt('setok1') + " " + Chat.italics(name) + " " + this.mlt("setok2"));
}
break;
case "delete":
Expand All @@ -545,12 +547,15 @@ module.exports = {
return this.errorReply(this.usage({ desc: 'delete' }, { desc: this.mlt('name') }));
}

if (!Mod.tourPollSets[id]) {
if (!Config.pollSets[id]) {
return this.errorReply(this.mlt('setnotfound'));
}

delete Mod.tourPollSets[id];
Mod.tourPollSetsDB.write();
delete Config.pollSets[id];

App.db.write();

this.addToSecurityLog();

this.reply(this.mlt('delok1') + " " + Chat.italics(name) + " " + this.mlt("delok2"));
}
Expand Down
11 changes: 6 additions & 5 deletions src/bot-modules/tour-cmd/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ exports.setup = function (App) {
aliases: Object.create(null),
finalAnnouncement: Object.create(null),
congratsWinner: Object.create(null),
pollSets: Object.create(null),
};
}

const Config = App.config.modules.tourcmd;

if (!Config.pollSets) {
Config.pollSets = Object.create(null);
}
class TourCommandModule {
constructor() {
this.tourData = Object.create(null);
Expand All @@ -43,9 +47,6 @@ exports.setup = function (App) {
this.tourPollDB = App.dam.getDataBase('tournaments-poll.json');
this.tourPoll = this.tourPollDB.data;
this.tourPollWait = Object.create(null);

this.tourPollSetsDB = App.dam.getDataBase('tournaments-poll-sets.json');
this.tourPollSets = this.tourPollSetsDB.data;
}

newTour(room, details) {
Expand All @@ -62,7 +63,7 @@ exports.setup = function (App) {
setId = "default";
}

if (!this.tourPollSets[setId]) {
if (!Config.pollSets[setId]) {
if (setId === "default" || setId === "rand" || setId === "random") {
// All formats
return randomize(Object.values(App.bot.formats).filter(f => !f.team && !f.disableTournaments && f.chall).map(f => f.name));
Expand All @@ -81,7 +82,7 @@ exports.setup = function (App) {
return [];
}

return randomize((this.tourPollSets[setId].formats || []).map(f => {
return randomize((Config.pollSets[setId].formats || []).map(f => {
const formatData = App.bot.formats[Text.toId(f)];

if (!formatData || formatData.disableTournaments || !formatData.chall) {
Expand Down
27 changes: 27 additions & 0 deletions src/bot-modules/tour-cmd/server-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ exports.setup = function (App) {
let timer = Text.toId(context.post.timer);
let msg = (context.post.creationmsg || "").trim();
let aliases = (context.post.aliases || "").split('\n');
let pollSets = (context.post.pollsets || "").split('\n');
let finals = (context.post.finals || "").split(',');
let winnergrats = (context.post.winnergrats || "").split(',');

Expand Down Expand Up @@ -79,6 +80,26 @@ exports.setup = function (App) {
}
}
Config.aliases = aux;
aux = Object.create(null);
for (let i = 0; i < pollSets.length; i++) {
let colonIndex = pollSets[i].indexOf(":");

if (colonIndex === -1) {
continue;
}

const setName = pollSets[i].substring(0, colonIndex);
const setId = Text.toId(setName);
const setFormats = pollSets[i].substring(colonIndex + 1).split(",").map(f => f.trim()).filter(f => !!f);

if (setId && setFormats.length > 0) {
aux[setId] = {
name: setName,
formats: setFormats,
};
}
}
Config.pollSets = aux;
Config.finalAnnouncement = Object.create(null);
for (let i = 0; i < finals.length; i++) {
let roomid = Text.toRoomid(finals[i]);
Expand Down Expand Up @@ -123,6 +144,12 @@ exports.setup = function (App) {
}
htmlVars.aliases = Text.escapeHTML(aliases.join('\n'));

let pollSets = [];
for (let s in Config.pollSets) {
pollSets.push((Config.pollSets[s].name || s) + ': ' + Config.pollSets[s].formats.join(", "));
}
htmlVars.pollsets = Text.escapeHTML(pollSets.join('\n'));

htmlVars.request_result = (ok ? 'ok-msg' : (error ? 'error-msg' : ''));
htmlVars.request_msg = (ok ? ok : (error || ""));

Expand Down
1 change: 1 addition & 0 deletions src/bot-modules/tour-cmd/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ <h2>Tournament Command</h2>
<tr><td><strong>Rooms for announcing final battle (separated with commas)</strong>: </td><td><input name="finals" type="text" size="60" value="${FINALS}" /></td></tr>
<tr><td><strong>Rooms for announcing tournament winner (separated with commas)</strong>: </td><td><input name="winnergrats" type="text" size="60" value="${WINNERGRATS}" /></td></tr>
<tr><td colspan="2"><strong>Aliases </strong>(<em>alias, format</em>):<p><textarea name="aliases" cols="80" rows="4">${ALIASES}</textarea></p></td></tr>
<tr><td colspan="2"><strong>Poll sets </strong>(<em>name: format, format, ...</em>):<p><textarea name="pollsets" cols="80" rows="4">${POLLSETS}</textarea></p></td></tr>
</table>
<p><input type="submit" name="edit" value="Save Changes" /></p>
</form>
Expand Down

0 comments on commit ab9b657

Please sign in to comment.