-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.ts
220 lines (178 loc) · 6.46 KB
/
example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Battle bot example
"use strict";
import dotenv from "dotenv";
dotenv.config();
import { PokemonShowdownBot } from "@asanrom/ps-bot-lib";
import { Log } from "./log";
import {
DefaultBattleAnalyzerFactory,
GenericNPCDecisionAlgorithm,
PokemonShowdownBattleBot,
TopDamageDecisionAlgorithm,
toId,
toRoomId,
PokemonTeam,
parsePokemonTeam,
isBattle,
simplifyBattleId
} from "..";
import Path from "path";
import { existsSync, readFileSync, readdirSync, statSync } from "fs";
import { BattleLogger } from "./battle-logger";
function main() {
const username = process.env.ACCOUNT_NAME;
const password = process.env.ACCOUNT_PASSWORD;
if (!username || !password) {
Log.error("You must set both ACCOUNT_NAME and ACCOUNT_PASSWORD for this example to work.");
Log.error("Set the environment variables in the .env file or by any other method.");
return;
}
// Configuration
const hasChallengeWhiteList = !!process.env.CHALLENGE_WHITELIST;
const challengeWhiteList = (process.env.CHALLENGE_WHITELIST || "").split(",").map(toId).filter(m => {
return !!m;
});
// Instantiate decision algorithms we want to use
const exampleDefaultAlgorithm = new GenericNPCDecisionAlgorithm();
const exampleTopDamageAlgorithm = new TopDamageDecisionAlgorithm();
// Load teams
const teams = new Map<string, PokemonTeam[]>();
const teamsFolder = Path.resolve(process.env.TEAMS_PATH || "teams");
let teamsCount = 0;
if (existsSync(teamsFolder)) {
const subPaths = readdirSync(teamsFolder);
for (const subPath of subPaths) {
const isDir = statSync(Path.resolve(teamsFolder, subPath)).isDirectory();
if (!isDir) {
continue;
}
const formatId = toId(subPath);
if (!formatId) {
continue;
}
const teamList: PokemonTeam[] = [];
teams.set(formatId, teamList);
const teamsFiles = readdirSync(Path.resolve(teamsFolder, subPath));
for (const teamFile of teamsFiles) {
if (!teamFile.endsWith(".txt")) {
continue;
}
const team = parsePokemonTeam(readFileSync(Path.resolve(teamsFolder, subPath, teamFile)).toString());
if (team && team.length > 0) {
teamList.push(team);
teamsCount++;
}
}
}
}
if (teamsCount > 0) {
Log.info(`Loaded ${teamsCount} teams for ${teams.size} different formats.`);
}
let battleLogger: BattleLogger;
if (process.env.LOG_BATTLES === "YES") {
battleLogger = new BattleLogger(process.env.LOGS_PATH || "logs");
}
// Battle bot
const battleBot = new PokemonShowdownBattleBot({
configFunc: battleDetails => {
if (toId(battleDetails.format).includes("challengecup1v1") || toId(battleDetails.format).includes("challengecup2v2")) {
// For 1v1 and 2v2 random, the best algorithm is just using the most damaging move
return {
algorithm: exampleTopDamageAlgorithm,
analyzerFactory: DefaultBattleAnalyzerFactory,
};
}
// Default
return {
algorithm: exampleDefaultAlgorithm,
analyzerFactory: DefaultBattleAnalyzerFactory,
};
},
teams: teams,
autoSetTimer: process.env.AUTO_SET_TIMER === "YES",
maxBattles: parseInt(process.env.MAX_BATTLES || "0", 10) || 0,
acceptChallenges: process.env.ACCEPT_CHALLENGES === "YES",
acceptChallengeFunc: user => {
if (!hasChallengeWhiteList) {
return true;
}
return challengeWhiteList.includes(toId(user));
},
autoLadder: process.env.AUTO_LADDER,
autoLadderCheckDelay: parseInt(process.env.AUTO_LADDER_DELAY || "0", 10) || 0,
joinAbandonedBattles: true,
leaveAfterBattleEnds: true,
});
battleBot.on("error", err => {
Log.error(err);
});
battleBot.on("debug", (battle, msg) => {
Log.debug(`[BATTLE-BOT-DEBUG] [${battle}] ${msg}`);
if (battleLogger && Log.LOG_DEBUG) {
battleLogger.log(battle, "|debug|" + msg);
}
});
const bot = new PokemonShowdownBot({
host: process.env.SERVER_HOST || 'sim3.psim.us',
port: parseInt(process.env.SERVER_PORT || '443', 10),
secure: process.env.SERVER_SSL !== 'NO',
serverId: process.env.SERVER_ID || 'showdown',
});
battleBot.on("send", (room, msg) => {
// When the battle bot wants to send a message, send it to the server
bot.sendTo(room, msg);
});
bot.on("connected", () => {
Log.info("Connected to the server!");
});
bot.on("can-login", () => {
bot.rename(username, password);
});
bot.on("sent", msg => {
Log.trace(`>>> ${msg}`);
});
bot.on("error", err => {
Log.error(err);
});
bot.on("renamed", () => {
if (process.env.ROOMS_JOIN) {
const cmdList = (process.env.ROOMS_JOIN || "").split(",").map(toRoomId).filter(m => {
return !!m;
}).map(m => {
return "/join " + m;
});
bot.sendToGlobal(cmdList);
}
});
bot.on("disconnected", err => {
Log.warning(`Disconnected. Code: ${err.code}, Reason: ${err.reason}`);
// Clear battle bot
battleBot.clear();
});
bot.on("line", (room, line, spl, isInit) => {
Log.trace(`[${room}] ${line}`);
// Send to the battle bot all the received lines
battleBot.receive(room, line, spl, isInit);
switch (spl[0]) {
case "init":
if (spl[1] === "battle" && battleLogger) {
// Battle started
battleLogger.initBattle(simplifyBattleId(room));
}
break;
case "deinit":
if (isBattle(room) && battleLogger) {
// Battle ended
battleLogger.close(simplifyBattleId(room));
}
break;
default:
if (isBattle(room) && battleLogger) {
// Battle line
battleLogger.log(simplifyBattleId(room), line);
}
}
});
bot.connect();
}
main();