From 5c8d5a63d8589c5795dc1247e86601ac90cef043 Mon Sep 17 00:00:00 2001 From: Guangcong Luo Date: Fri, 1 Sep 2017 06:45:13 -0400 Subject: [PATCH] Refactor some ladder functions to async/await --- ladders-matchmaker.js | 27 ++++++++++++--------------- ladders.js | 42 ++++++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/ladders-matchmaker.js b/ladders-matchmaker.js index 28b063c1c3..ac1341c4d1 100644 --- a/ladders-matchmaker.js +++ b/ladders-matchmaker.js @@ -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); diff --git a/ladders.js b/ladders.js index be77558f75..7b1c1aa387 100644 --- a/ladders.js +++ b/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 = `

${name} Top 100

`; - buf += ``; - buf += ``; - for (let i = 0; i < ladder.length; i++) { - let row = ladder[i]; - buf += ``; - } - return [formatid, buf]; - }); + const ladder = await this.ladder; + let buf = `

${name} Top 100

`; + buf += `
` + ['', 'Username', 'Elo', 'W', 'L', 'T'].join(``) + `
` + [ - i + 1, row[2], `${Math.round(row[1])}`, row[3], row[4], row[5], - ].join(``) + `
`; + buf += ``; + for (let i = 0; i < ladder.length; i++) { + let row = ladder[i]; + buf += ``; + } + 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]); } /**
` + ['', 'Username', 'Elo', 'W', 'L', 'T'].join(``) + `
` + [ + i + 1, row[2], `${Math.round(row[1])}`, row[3], row[4], row[5], + ].join(``) + `