mirror of
https://github.com/smogon/pokemon-showdown.git
synced 2026-08-28 03:56:35 -05:00
Refactor some ladder functions to async/await
This commit is contained in:
@@ -58,27 +58,24 @@ class Matchmaker {
|
||||
return true;
|
||||
}
|
||||
|
||||
searchBattle(user, formatid) {
|
||||
async searchBattle(user, formatid) {
|
||||
if (!user.connected) return;
|
||||
formatid = Dex.getFormat(formatid).id;
|
||||
|
||||
return Promise.all([
|
||||
Promise.resolve(user.userid),
|
||||
user.prepBattle(formatid, 'search', null),
|
||||
Ladders(formatid).getRating(user.userid),
|
||||
]).then(([userId, validTeam, rating]) => {
|
||||
if (userId !== user.userid) return;
|
||||
return this.finishSearchBattle(user, formatid, validTeam, rating);
|
||||
}, err => {
|
||||
let oldUserid = user.userid;
|
||||
let validTeam, 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.
|
||||
});
|
||||
}
|
||||
|
||||
finishSearchBattle(user, formatid, validTeam, rating) {
|
||||
return;
|
||||
}
|
||||
if (oldUserid !== user.userid) return;
|
||||
if (validTeam === false) return;
|
||||
|
||||
const search = new Search(user.userid, validTeam, rating);
|
||||
|
||||
42
ladders.js
42
ladders.js
@@ -128,41 +128,39 @@ class Ladder {
|
||||
* ladder toplist, to be displayed directly in the ladder tab of the
|
||||
* client.
|
||||
*/
|
||||
getTop() {
|
||||
async getTop() {
|
||||
let formatid = this.formatid;
|
||||
let name = Dex.getFormat(formatid).name;
|
||||
return this.ladder.then(ladder => {
|
||||
let buf = `<h3>${name} Top 100</h3>`;
|
||||
buf += `<table>`;
|
||||
buf += `<tr><th>` + ['', 'Username', '<abbr title="Elo rating">Elo</abbr>', 'W', 'L', 'T'].join(`</th><th>`) + `</th></tr>`;
|
||||
for (let i = 0; i < ladder.length; i++) {
|
||||
let row = ladder[i];
|
||||
buf += `<tr><td>` + [
|
||||
i + 1, row[2], `<strong>${Math.round(row[1])}</strong>`, row[3], row[4], row[5],
|
||||
].join(`</td><td>`) + `</td></tr>`;
|
||||
}
|
||||
return [formatid, buf];
|
||||
});
|
||||
const ladder = await this.ladder;
|
||||
let buf = `<h3>${name} Top 100</h3>`;
|
||||
buf += `<table>`;
|
||||
buf += `<tr><th>` + ['', 'Username', '<abbr title="Elo rating">Elo</abbr>', 'W', 'L', 'T'].join(`</th><th>`) + `</th></tr>`;
|
||||
for (let i = 0; i < ladder.length; i++) {
|
||||
let row = ladder[i];
|
||||
buf += `<tr><td>` + [
|
||||
i + 1, row[2], `<strong>${Math.round(row[1])}</strong>`, row[3], row[4], row[5],
|
||||
].join(`</td><td>`) + `</td></tr>`;
|
||||
}
|
||||
return [formatid, buf];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Promise for the Elo rating of a user
|
||||
*/
|
||||
getRating(userid) {
|
||||
async getRating(userid) {
|
||||
let formatid = this.formatid;
|
||||
let user = Users.getExact(userid);
|
||||
if (Ladders.disabled === true || Ladders.disabled === 'db' && (!user || !user.mmrCache[formatid])) {
|
||||
return Promise.reject(new Error(`Ladders are disabled.`));
|
||||
throw new Error(`Ladders are disabled.`);
|
||||
}
|
||||
if (user && user.mmrCache[formatid]) {
|
||||
return Promise.resolve(user.mmrCache[formatid]);
|
||||
return user.mmrCache[formatid];
|
||||
}
|
||||
return this.ladder.then(() => {
|
||||
if (user.userid !== userid) return Promise.reject(`Expired rating for ${userid}`);
|
||||
let index = this.indexOfUser(userid);
|
||||
if (index < 0) return (user.mmrCache[formatid] = 1000);
|
||||
return (user.mmrCache[formatid] = this.loadedLadder[index][1]);
|
||||
});
|
||||
await this.ladder;
|
||||
if (user.userid !== userid) throw new Error(`Expired rating for ${userid}`);
|
||||
let index = this.indexOfUser(userid);
|
||||
if (index < 0) return (user.mmrCache[formatid] = 1000);
|
||||
return (user.mmrCache[formatid] = this.loadedLadder[index][1]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user