diff --git a/src/account.ts b/src/account.ts index 5f1bca7..40a715e 100644 --- a/src/account.ts +++ b/src/account.ts @@ -63,7 +63,67 @@ export class Account { SET gameName = ?, tagLine = ? WHERE puuid = ? `, [this.gameName, this.tagLine, this.puuid]); - // TODO try add new elo entry to db + + const fetchedElo = await this.fetchCurrentElo(); + if (fetchedElo && (!this.currentElo || + this.currentElo.tier !== fetchedElo.tier || + this.currentElo.rank !== fetchedElo.rank || + this.currentElo.points !== fetchedElo.points + )){ + this.currentElo = fetchedElo; + const date = new Date(Date.now()).toISOString().slice(0, 19).replace('T', ' '); + + const [[row]] = await conn.execute(` + SELECT id + FROM accounts + WHERE puuid = ? + `, [this.puuid]); + const sqlAccountId = row["id"]; + + await conn.execute(` + INSERT INTO elo_entries + (accountId, date, tier, \`rank\`, points) + VALUES (?, ?, ?, ?, ?) + `, [sqlAccountId, date, this.currentElo.tier, this.currentElo.rank, this.currentElo.points]); + + console.log(`Added new elo entry for ${this}.`); + } + } + + async fetchCurrentElo() { + const tryRegion = async (region: RiotAPITypes.LoLRegion) => { + return await riotAPI.league.getEntriesBySummonerId({ + region: region, + summonerId: (await riotAPI.summoner.getByPUUID({ + region: region, + puuid: this.puuid + })).id + }); + }; + + let entries; + + try { + entries = await tryRegion(PlatformId.EUW1); + } catch (error){ + try { + entries = await tryRegion(PlatformId.EUNE1); + } catch (error){ + let response = error as Response; + console.error(await response.json()); + return; + } + } + + let soloQ = entries.find(e => e.queueType == "RANKED_SOLO_5x5"); + if (soloQ && ((soloQ.wins + soloQ.losses) >= 5)){ + return { + tier: soloQ.tier, + rank: soloQ.rank, + points: soloQ.leaguePoints + } as Elo; + } + } static async fetchFromRiotID(gameName: string, tagLine: string){ diff --git a/src/bot.ts b/src/bot.ts index 41b8e05..6f13786 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -17,8 +17,6 @@ export class UEMEloBot extends Client { this.commands = commands; const token = await this.login(); console.log(`Discord Token: ${token}`); - await this.addAccount("benjo", "tgm"); - await this.addAccount("ag infinity", "euw"); }); }