forked from smogon/pokemon-showdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathladders-matchmaker.js
285 lines (256 loc) · 7.54 KB
/
ladders-matchmaker.js
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* Matchmaker
* Pokemon Showdown - http://pokemonshowdown.com/
*
* This keeps track of challenges to battle made between users, setting up
* matches between users looking for a battle, and starting new battles.
*
* @License MIT License
*/
'use strict';
/** @type {number} */
const PERIODIC_MATCH_INTERVAL = 60 * 1000;
/**
* This represents a user's search for a battle under a format.
*/
class Search {
/**
* @param {string} userid
* @param {string} team
* @param {number} [rating = 1000]
*/
constructor(userid, team, rating = 1000) {
/** @type {string} */
this.userid = userid;
/** @type {string} */
this.team = team;
/** @type {number} */
this.rating = rating;
/** @type {number} */
this.time = Date.now();
}
}
/**
* This keeps track of searches for battles, creating a new battle for a newly
* added search if a valid match can be made, otherwise periodically
* attempting to make a match with looser restrictions until one can be made.
*/
class Matchmaker {
constructor() {
/**
* formatid:userid:Search
* @type {Map<string, Map<string, Search>>}
*/
this.searches = new Map();
/** @type {?NodeJS.Timer} */
this.periodicMatchInterval = setInterval(
() => this.periodicMatch(),
PERIODIC_MATCH_INTERVAL
);
}
/**
* @param {User} user
* @param {string} formatid
* @return {boolean}
*/
cancelSearch(user, formatid) {
formatid = toId(formatid);
const formatTable = this.searches.get(formatid);
if (!formatTable) return false;
if (!formatTable.has(user.userid)) return false;
formatTable.delete(user.userid);
user.updateSearch();
return true;
}
/**
* @param {User} user
* @return {number} cancel count
*/
cancelSearches(user) {
let cancelCount = 0;
for (let formatTable of this.searches.values()) {
const search = formatTable.get(user.userid);
if (!search) continue;
formatTable.delete(user.userid);
cancelCount++;
}
user.updateSearch();
return cancelCount;
}
/**
* @param {Search} search
* @param {string} formatid
*/
getSearcher(search, formatid) {
const user = Users.get(search.userid);
if (!user || !user.connected || user.userid !== search.userid) {
const formatTable = this.searches.get(formatid);
if (formatTable) formatTable.delete(search.userid);
if (user && user.connected) {
user.popup(`You changed your name and are no longer looking for a battle in ${formatid}`);
user.updateSearch();
}
return;
}
return user;
}
/**
* @param {User} user
*/
getSearches(user) {
let searches = [];
for (const [formatid, formatTable] of this.searches) {
if (formatTable.has(user.userid)) searches.push(formatid);
}
return searches;
}
/**
* @param {User} user
* @param {string} formatid
*/
hasSearch(user, formatid) {
const formatTable = this.searches.get(formatid);
if (!formatTable) return false;
return formatTable.has(user.userid);
}
/**
* Validates a user's team and fetches their rating for a given format
* before creating a search for a battle.
* @param {User} user
* @param {string} format
* @return {Promise<void>}
*/
async searchBattle(user, format) {
if (!user.connected) return;
let formatid = Dex.getFormat(format).id;
let oldUserid = user.userid;
let validTeam;
let rating;
try {
[validTeam, rating] = await Promise.all([
user.prepBattle(formatid, 'search', null),
Ladders(formatid).getRating(user.userid),
]);
} catch (e) {
// Rejects iff ladders are disabled, or if we
// retrieved the rating but the user had changed their name.
if (Ladders.disabled) return user.popup(`The ladder is currently disabled due to high server load.`);
// User feedback for renames handled elsewhere.
return;
}
if (oldUserid !== user.userid) return;
if (validTeam === false) return;
const search = new Search(user.userid, validTeam, rating);
this.addSearch(search, user, formatid);
}
/**
* Verifies whether or not a match made between two users is valid.
* @param {Search} search1
* @param {Search} search2
* @param {User=} user1
* @param {User=} user2
* @param {string} formatid
* @return {number | false | void}
*/
matchmakingOK(search1, search2, user1, user2, formatid) {
if (!user1 || !user2) {
// This should never happen.
return void require('./crashlogger')(new Error(`Matched user ${user1 ? search2.userid : search1.userid} not found`), "The main process");
}
// users must be different
if (user1 === user2) return false;
// users must have different IPs
if (user1.latestIp === user2.latestIp) return false;
// users must not have been matched immediately previously
if (user1.lastMatch === user2.userid || user2.lastMatch === user1.userid) return false;
// search must be within range
let searchRange = 100;
let elapsed = Date.now() - Math.min(search1.time, search2.time);
if (formatid === 'gen7ou' || formatid === 'gen7oucurrent' ||
formatid === 'gen7oususpecttest' || formatid === 'gen7randombattle') {
searchRange = 50;
}
searchRange += elapsed / 300; // +1 every .3 seconds
if (searchRange > 300) searchRange = 300 + (searchRange - 300) / 10; // +1 every 3 sec after 300
if (searchRange > 600) searchRange = 600;
if (Math.abs(search1.rating - search2.rating) > searchRange) return false;
user1.lastMatch = user2.userid;
user2.lastMatch = user1.userid;
return Math.min(search1.rating, search2.rating) || 1;
}
/**
* Atarts a search for a battle for a user under the given format.
* @param {Search} newSearch
* @param {User} user
* @param {string} formatid
*/
addSearch(newSearch, user, formatid) {
let formatTable = this.searches.get(formatid);
if (!formatTable) {
formatTable = new Map();
this.searches.set(formatid, formatTable);
}
if (formatTable.has(user.userid)) {
user.popup(`Couldn't search: You are already searching for a ${formatid} battle.`);
return;
}
// In order from longest waiting to shortest waiting
for (let search of formatTable.values()) {
const searcher = this.getSearcher(search, formatid);
if (!searcher) continue;
let minRating = this.matchmakingOK(search, newSearch, searcher, user, formatid);
if (minRating) {
formatTable.delete(search.userid);
Rooms.createBattle(formatid, {
p1: searcher,
p1team: search.team,
p2: user,
p2team: newSearch.team,
rated: !Ladders.disabled && minRating,
});
return;
}
}
formatTable.set(newSearch.userid, newSearch);
user.updateSearch();
}
/**
* Creates a match for a new battle for each format in this.searches if a
* valid match can be made. This is run periodically depending on
* PERIODIC_MATCH_INTERVAL.
*/
periodicMatch() {
// In order from longest waiting to shortest waiting
for (const [formatid, formatTable] of this.searches) {
let longestSearch, longestSearcher;
for (let search of formatTable.values()) {
if (!longestSearch) {
longestSearcher = this.getSearcher(search, formatid);
if (!longestSearcher) continue;
longestSearch = search;
continue;
}
let searcher = this.getSearcher(search, formatid);
if (!searcher) continue;
let minRating = this.matchmakingOK(search, longestSearch, searcher, longestSearcher, formatid);
if (minRating) {
formatTable.delete(search.userid);
formatTable.delete(longestSearch.userid);
Rooms.createBattle(formatid, {
p1: searcher,
p1team: search.team,
p2: longestSearcher,
p2team: longestSearch.team,
rated: !Ladders.disabled && minRating,
});
return;
}
}
}
}
}
module.exports = {
Search,
Matchmaker,
matchmaker: new Matchmaker(),
};