From 0ad8bb86f6fa953f5063adf2b6657d0ee1a350e8 Mon Sep 17 00:00:00 2001 From: AgustinSRG Date: Wed, 25 Dec 2024 13:57:49 +0100 Subject: [PATCH] Battle: Adapt decision maker to work without timer - Also add the option to decide if the bot should set the timer on or off - Bump version to 2.13.0 --- package-lock.json | 4 ++-- package.json | 2 +- src/bot-modules/battle/battle-ai/battle-majors.js | 13 +++++++++++++ src/bot-modules/battle/battle-ai/battle.js | 14 ++++++++++++++ src/bot-modules/battle/main.js | 1 + src/bot-modules/battle/server-handler.js | 2 ++ src/bot-modules/battle/templates/config.html | 1 + 7 files changed, 34 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 25608fd..5fdf2e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "showdown-chatbot", - "version": "2.12.2", + "version": "2.13.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "showdown-chatbot", - "version": "2.12.2", + "version": "2.13.0", "license": "MIT", "dependencies": { "busboy": "1.6.0", diff --git a/package.json b/package.json index 2252d61..1276ba8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "showdown-chatbot", - "version": "2.12.2", + "version": "2.13.0", "author": { "name": "Agustin San Roman", "email": "agustinsanromanguzman@gmail.com", diff --git a/src/bot-modules/battle/battle-ai/battle-majors.js b/src/bot-modules/battle/battle-ai/battle-majors.js index 971be88..1504433 100644 --- a/src/bot-modules/battle/battle-ai/battle-majors.js +++ b/src/bot-modules/battle/battle-ai/battle-majors.js @@ -30,6 +30,8 @@ exports.setup = function (App, BattleData) { if (this.waitingForRequestToMove) { this.waitingForRequestToMove = false; this.makeDecision(); + } else { + this.decisionTimeout = setTimeout(this.onDecisionTimeout.bind(this), 1000); } }, @@ -45,6 +47,13 @@ exports.setup = function (App, BattleData) { this.makeDecision(); }, + upkeep: function () { + if (this.timer) return; + + this.checkTimer(); + this.makeDecision(); + }, + tier: function (args, kwargs) { if (!args[1]) args[1] = ''; for (let i in kwargs) args[1] += '[' + i + '] ' + kwargs[i]; @@ -122,6 +131,10 @@ exports.setup = function (App, BattleData) { inactiveoff: function (args, kwargs) { this.timer = false; + + if (this.turn > 0) { + this.timerExplicitlyTurnedOff = true; + } }, j: "join", diff --git a/src/bot-modules/battle/battle-ai/battle.js b/src/bot-modules/battle/battle-ai/battle.js index ceec931..3c129d5 100644 --- a/src/bot-modules/battle/battle-ai/battle.js +++ b/src/bot-modules/battle/battle-ai/battle.js @@ -174,6 +174,7 @@ exports.setup = function (App, CustomModules) { checkTimer() { if (!this.self || !this.request) return; // Not playing + if (Config.turnTimerOn === false || this.timerExplicitlyTurnedOff) return; // Configured to be turned off if (!this.timer) { if (this.sentTimerReq && Date.now() - this.sentTimerReq < MIN_TIME_LOCK) return; // Do not spam timer commands this.sentTimerReq = Date.now(); @@ -304,7 +305,16 @@ exports.setup = function (App, CustomModules) { return DecisionMaker.getDecisions(this, BattleData); } + onDecisionTimeout() { + this.decisionTimeout = null; + this.makeDecision(); + } + makeDecision(forced) { + if (this.decisionTimeout) { + clearTimeout(this.decisionTimeout); + this.decisionTimeout = null; + } if (!this.self) return; // Not playing this.debug(this.id + "->MakeDecision"); if (Config.maxTurns && this.turn > Config.maxTurns) { @@ -839,6 +849,10 @@ exports.setup = function (App, CustomModules) { } destroy() { + if (this.decisionTimeout) { + clearTimeout(this.decisionTimeout); + this.decisionTimeout = null; + } if (this.leaveInterval) { clearInterval(this.leaveInterval); this.leaveInterval = null; diff --git a/src/bot-modules/battle/main.js b/src/bot-modules/battle/main.js index f659e3e..084d94b 100644 --- a/src/bot-modules/battle/main.js +++ b/src/bot-modules/battle/main.js @@ -17,6 +17,7 @@ exports.setup = function (App) { ladderBattles: 1, joinTours: Object.create(null), ignoreAbandonedbattles: false, + turnTimerOn: true, maxTurns: 0, }; } diff --git a/src/bot-modules/battle/server-handler.js b/src/bot-modules/battle/server-handler.js index 5d69ca4..d2c9966 100644 --- a/src/bot-modules/battle/server-handler.js +++ b/src/bot-modules/battle/server-handler.js @@ -59,6 +59,7 @@ exports.setup = function (App) { if (!error) { Config.maxBattles = maxBattles; Config.ignoreAbandonedbattles = !context.post.joinabandoned; + Config.turnTimerOn = !!context.post.timeron; Config.maxTurns = maxTurns; Config.ladderBattles = ladderBattles; let joinTours = (context.post.jointours || "").split(','); @@ -89,6 +90,7 @@ exports.setup = function (App) { htmlVars.maxturns = Text.escapeHTML(Config.maxTurns || 0); htmlVars.jointours = Object.keys(Config.joinTours).join(', '); htmlVars.join_abandoned = (!Config.ignoreAbandonedbattles ? "checked=\"checked\"" : ""); + htmlVars.timer_on = (Config.turnTimerOn === false ? "" : "checked=\"checked\""); htmlVars.initmsg = Text.escapeHTML(Config.initBattleMsg.join('\n')); htmlVars.winmsg = Text.escapeHTML(Config.winmsg.join('\n')); htmlVars.losemsg = Text.escapeHTML(Config.losemsg.join('\n')); diff --git a/src/bot-modules/battle/templates/config.html b/src/bot-modules/battle/templates/config.html index e603ea7..ee7bfc0 100644 --- a/src/bot-modules/battle/templates/config.html +++ b/src/bot-modules/battle/templates/config.html @@ -2,6 +2,7 @@

Max number of battles of regular users

Max number of ladder battles

Max number of turns (0 = no limit)

+

 Turn the battle timer on.

Rooms to join tournaments (separated by commas)

 Join abandonned / pending battles.

Battle Initial Messages: