sql-php-refactoring
Benjamin Kraft 3 months ago
parent 194ea203c7
commit 9471836129
  1. 62
      src/account.ts
  2. 2
      src/bot.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<RowDataPacket[]>(`
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){

@ -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");
});
}

Loading…
Cancel
Save