Skip to content

Commit

Permalink
Battle: Adapt decision maker to work without timer
Browse files Browse the repository at this point in the history
- Also add the option to decide if the bot should set the timer on or off
- Bump version to 2.13.0
  • Loading branch information
AgustinSRG committed Dec 25, 2024
1 parent 4ebde92 commit 0ad8bb8
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "showdown-chatbot",
"version": "2.12.2",
"version": "2.13.0",
"author": {
"name": "Agustin San Roman",
"email": "agustinsanromanguzman@gmail.com",
Expand Down
13 changes: 13 additions & 0 deletions src/bot-modules/battle/battle-ai/battle-majors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
},

Expand All @@ -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];
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions src/bot-modules/battle/battle-ai/battle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/bot-modules/battle/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ exports.setup = function (App) {
ladderBattles: 1,
joinTours: Object.create(null),
ignoreAbandonedbattles: false,
turnTimerOn: true,
maxTurns: 0,
};
}
Expand Down
2 changes: 2 additions & 0 deletions src/bot-modules/battle/server-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(',');
Expand Down Expand Up @@ -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'));
Expand Down
1 change: 1 addition & 0 deletions src/bot-modules/battle/templates/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<p><strong>Max number of battles of regular users</strong>:&nbsp;<input name="maxbattles" type="text" size="10" value="${MAXBATTLES}" /></p>
<p><strong>Max number of ladder battles</strong>:&nbsp;<input name="maxladder" type="text" size="10" value="${MAXLADDER}" /></p>
<p><strong>Max number of turns (0 = no limit)</strong>:&nbsp;<input name="maxturns" type="text" size="10" value="${MAXTURNS}" /></p>
<p><input type="checkbox" name="timeron" ${TIMER_ON} />&nbsp;Turn the battle timer on.</p>
<p><strong>Rooms to join tournaments</strong>:&nbsp;<input name="jointours" type="text" size="50" value="${JOINTOURS}" autocomplete="off" /> (separated by commas)</p>
<p><input type="checkbox" name="joinabandoned" ${JOIN_ABANDONED} />&nbsp;Join abandonned / pending battles.</p>
<p><strong>Battle Initial Messages</strong>:</p><p><textarea name="initmsg" cols="60" rows="3">${INITMSG}</textarea></p>
Expand Down

0 comments on commit 0ad8bb8

Please sign in to comment.