Skip to content

Commit

Permalink
Tour-cmd: Add poll option for the format
Browse files Browse the repository at this point in the history
  • Loading branch information
AgustinSRG committed Dec 14, 2024
1 parent 97b6629 commit 1446ff9
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 22 deletions.
93 changes: 72 additions & 21 deletions src/bot-modules/tour-cmd/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Commands File
*
* tour: creates a tournament easier than using Showdown commands
* tourlog: Gets a register of the must recent tournaments
*/

'use strict';
Expand Down Expand Up @@ -40,21 +41,25 @@ function botCanHtml(room, App) {
return (roomData && roomData.users[botid] && App.parser.equalOrHigherGroup({ group: roomData.users[botid] }, 'bot'));
}

const MAX_POOL_OPTIONS = 10;

module.exports = {
newtour: 'tour',
tour: function (App) {
this.setLangFile(Lang_File);
if (!this.can('tour', this.room)) return this.replyAccessDenied('tour');
if (!this.arg) {
return this.errorReply(this.usage(
{ desc: this.usageTrans('format') },
{ desc: this.mlt('formatorpoll') },
{ desc: 'type=' + 'elim|rr|double-elim|double-rr', optional: true },
{ desc: 'auto-start=' + this.mlt('autostart'), optional: true },
{ desc: 'dq=' + this.mlt('autodq'), optional: true },
{ desc: 'users=' + this.mlt('maxusers'), optional: true },
{ desc: 'rounds=' + this.mlt('rounds'), optional: true },
{ desc: 'name=' + this.mlt('name'), optional: true },
{ desc: 'rules=' + this.mlt('rules'), optional: true }
{ desc: 'rules=' + this.mlt('rules'), optional: true },
{ desc: 'poll-options=' + this.mlt('polloptions'), optional: true },
{ desc: 'poll-time=' + this.mlt('polltime'), optional: true }
));
}
const Mod = App.modules.tourcmd.system;
Expand All @@ -79,6 +84,10 @@ module.exports = {
rounds: null,
rules: null,
name: null,
isPoll: false,
pollSet: "",
pollTime: Config.pollTime || 3,
pollMaxOptions: Config.pollMaxOptions || 4,
};
if (this.arg && this.arg.length) {
let args = this.args;
Expand All @@ -92,6 +101,8 @@ module.exports = {
timer: null,
rules: "",
name: null,
pollTime: null,
pollMaxOptions: null,
};
let splArg;
let isRules = false;
Expand Down Expand Up @@ -173,6 +184,13 @@ module.exports = {
}
isRules = true;
break;
case "polltime":
params.pollTime = valueArg;
break;
case "polloptions":
case "pollmaxoptions":
params.pollMaxOptions = valueArg;
break;
default:
if (isRules) {
if (params.rules) {
Expand All @@ -182,33 +200,41 @@ module.exports = {
}
} else {
return this.reply(this.mlt('param') + ' ' + idArg + ' ' +
this.mlt('paramhelp') + ": tier, autostart, dq, users, rounds, type, scout, timer");
this.mlt('paramhelp') + ": tier, autostart, dq, users, rounds, type, scout, timer, name, rules, poll-time");
}
}
}
}
if (params.format) {
let formatOptions = (params.format + "").split("|").filter(a => {
return !!(a.trim());
});
if (formatOptions.length > 0) {
const parsedFormats = [];
for (let formatOption of formatOptions) {
let format = parseAliases(formatOption, App);
if (!App.bot.formats[format]) {
let inexact = Inexact.safeResolve(App, format, { formats: 1, others: 0 });
return this.reply(this.mlt('e31') + ' "' + format + '" ' + this.mlt('e33') +
(inexact ? (". " + this.mlt('inexact') + " " + Chat.italics(inexact) + "?") : ""));
}
if (App.bot.formats[format].disableTournaments) {
return this.reply(this.mlt('e31') + ' ' + Chat.italics(App.bot.formats[format].name) +
' ' + this.mlt('e32'));
const formatTrim = (params.format + "").trim().toLowerCase().replace(/\s/g, "");

if (formatTrim === "poll" || formatTrim.startsWith("poll(")) {
// Is a pool
details.isPoll = true;
details.pollSet = Text.toId(formatTrim.substring("poll(".length));
} else {
let formatOptions = (params.format + "").split("|").filter(a => {
return !!(a.trim());
});
if (formatOptions.length > 0) {
const parsedFormats = [];
for (let formatOption of formatOptions) {
let format = parseAliases(formatOption, App);
if (!App.bot.formats[format]) {
let inexact = Inexact.safeResolve(App, format, { formats: 1, others: 0 });
return this.reply(this.mlt('e31') + ' "' + format + '" ' + this.mlt('e33') +
(inexact ? (". " + this.mlt('inexact') + " " + Chat.italics(inexact) + "?") : ""));
}
if (App.bot.formats[format].disableTournaments) {
return this.reply(this.mlt('e31') + ' ' + Chat.italics(App.bot.formats[format].name) +
' ' + this.mlt('e32'));
}

parsedFormats.push(format);
}

parsedFormats.push(format);
details.format = parsedFormats[Math.floor(Math.random() * parsedFormats.length)];
}

details.format = parsedFormats[Math.floor(Math.random() * parsedFormats.length)];
}
}
if (params.timeToStart) {
Expand All @@ -229,6 +255,16 @@ module.exports = {
details.autodq = dq;
}
}
if (params.pollTime) {
let pollTime = parseFloat(params.pollTime);
if (!pollTime || pollTime < 0) return this.reply(this.mlt('e9'));
details.pollTime = pollTime;
}
if (params.pollMaxOptions) {
let maxOptions = parseInt(params.pollMaxOptions);
if (!maxOptions || maxOptions < 2 || maxOptions > MAX_POOL_OPTIONS) return this.reply(this.mlt('e10'));
details.pollMaxOptions = maxOptions;
}
if (params.maxUsers) {
if (Text.toId(params.maxUsers) === 'off' || Text.toId(params.maxUsers) === 'infinite') {
details.maxUsers = null;
Expand Down Expand Up @@ -293,6 +329,21 @@ module.exports = {
details.rules = params.rules;
}
}

if (details.isPoll) {
const pollSet = details.pollSet;
const formatOptions = Mod.getPollSet(pollSet).slice(0, Math.min(details.pollMaxOptions, MAX_POOL_OPTIONS));

if (formatOptions.length > 0) {
Mod.setupPoolWait(this.room, details);

// Prepare command
return this.send('/poll create ' + this.mlt('polltitle') + "," + formatOptions.join(","), this.room);
} else {
return this.reply(this.mlt('e5'));
}
}

Mod.newTour(this.room, details);
},

Expand Down
16 changes: 16 additions & 0 deletions src/bot-modules/tour-cmd/commands.translations
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ $e4 = Time to start is not a valid time
$e5 = Autodq is not a valid time
$e6 = Max users number is not valid
$e7 = Tour type is not valid. Valid types are: elimination, roundrobin, double-elimination, double-roundrobin
$e8 = The list of format for the poll is empty
$e9 = The poll time is invalid
$e10 = The poll options limit is not valid
$notstarted = Error: the tournament did not start, probably because I do not have permission to create tournaments or commands were changed.
$param = Parameter
$paramhelp = not found, valid parameters are
Expand All @@ -29,6 +32,11 @@ $maxusers = Max users
$rounds = Rounds
$name = Name
$rules = Rules (Split by commas)
$formatorpoll = Format or Poll (group)
$polloptions = Number of options for the poll
$polltime = Poll time (mins)

$polltitle = Which format do you prefer for the next tournament?

$agob =
$ago = ago
Expand Down Expand Up @@ -62,6 +70,9 @@ $e4 = El tiempo para iniciar no es válido
$e5 = El AutoDq no es válido
$e6 = El máximo numero de users no es válido
$e7 = El tipo de torneo no es válido. Usa alguno de estos: elimination, roundrobin, double-elimination, double-roundrobin
$e8 = La lista de formatos para la encuesta se encuentra vacía
$e9 = El tiempo de encuesta no es válido
$e10 = La cantidad de opciones de la encuesta no es válida
$notstarted = Error: El torneo no ha empezado, probablemente por un cambio en los permisos o en los comandos
$param = Parámetro
$paramhelp = no encontrado, Los parámetros correctos son
Expand All @@ -72,6 +83,11 @@ $maxusers = Usuarios máximos
$rounds = Rondas
$name = Nombre
$rules = Reglas (separadas por comas)
$formatorpoll = Formato o Poll (grupo)
$polloptions = Cantidad de opciones de la encuesta
$polltime = Tiempo de la encuesta (mins)

$polltitle = ¿Qué formato prefieres para el siguiente torneo?

$room = Sala
$nf = no encontrada
Expand Down
2 changes: 2 additions & 0 deletions src/bot-modules/tour-cmd/errors.translations
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ $0 = Error: Tournaments are disabled in this room
$1 = Error: I have not permission to create a tournament
$2 = Error: The server is in lockdown, so tournaments cannot be started
$3 = Error: Could not set custom rules
$4 = Error: Could not start the poll

%spanish

$0 = Error: Los torneos están desactivados en esta sala
$1 = Error: No tengo los permisos necesarios para crear un torneo
$2 = Error: El server se está reiniciando, por lo que no se pueden crear torneos
$3 = Error: No se pudieron configurar las reglas personalizadas.
$4 = Error: No se pudo iniciar la encuesta
Loading

0 comments on commit 1446ff9

Please sign in to comment.