parent
1b09e466e2
commit
b8fffcdd25
6 changed files with 104 additions and 99 deletions
@ -0,0 +1,98 @@ |
||||
import {Account} from "./account"; |
||||
import {getMySQLConnection} from "./util"; |
||||
import {RowDataPacket} from "mysql2/promise"; |
||||
import {Collection} from "discord.js"; |
||||
|
||||
export class DataManager { |
||||
async updateAllAccounts(){ |
||||
let accounts = await this.getProgressedAccountList(); |
||||
for (let account of accounts){ |
||||
await account.update(); |
||||
} |
||||
} |
||||
|
||||
async addAccount(gameName: string, tagLine: string) { |
||||
let account = await Account.fetchFromRiotID(gameName, tagLine); |
||||
if (!account) |
||||
return; |
||||
|
||||
if (!await account.isAdded()){ |
||||
await account.addToDB(); |
||||
await account.update(); |
||||
return account; |
||||
} else { |
||||
console.error(`Account ${account} already added.`); |
||||
return; |
||||
} |
||||
} |
||||
|
||||
async removeAccount(gameName: string, tagLine: string) { |
||||
let account = await Account.fetchFromRiotID(gameName, tagLine); |
||||
if (!account) |
||||
return; |
||||
|
||||
if (await account.isAdded()){ |
||||
await account.removeFromDB(); |
||||
return account; |
||||
} else { |
||||
console.error(`Account ${account} was not added.`); |
||||
return; |
||||
} |
||||
} |
||||
|
||||
async getAccountMap(){ |
||||
const conn = await getMySQLConnection(); |
||||
|
||||
const [sqlAccounts] = await conn.query<RowDataPacket[]>(`SELECT * FROM accounts`); |
||||
|
||||
let accounts = new Collection<string, Account>(); |
||||
|
||||
for (const sqlAccount of sqlAccounts){ |
||||
let account = new Account(sqlAccount["puuid"]); |
||||
account.gameName = sqlAccount["gameName"]; |
||||
account.tagLine = sqlAccount["tagLine"]; |
||||
accounts.set(account.puuid, account); |
||||
} |
||||
|
||||
return accounts; |
||||
} |
||||
|
||||
async getProgressedAccountList() { |
||||
const conn = await getMySQLConnection(); |
||||
|
||||
const [[, starts, ends]] = await conn.query<RowDataPacket[][]>(` |
||||
CREATE TEMPORARY TABLE limits AS |
||||
SELECT MIN(date) AS start, MAX(date) AS end, accountId, gameName, tagLine, puuid |
||||
FROM accounts JOIN elo_entries ON id = accountId |
||||
GROUP BY accountId, gameName, tagLine, puuid; |
||||
|
||||
SELECT tier, \`rank\`, points, gameName, tagLine, puuid
|
||||
FROM elo_entries JOIN limits ON elo_entries.date = start AND limits.accountId = elo_entries.accountId; |
||||
|
||||
SELECT tier, \`rank\`, points, gameName, tagLine, puuid
|
||||
FROM elo_entries JOIN limits ON elo_entries.date = end AND limits.accountId = elo_entries.accountId; |
||||
`);
|
||||
|
||||
let accounts = await this.getAccountMap(); |
||||
|
||||
for (const start of starts){ |
||||
let account = accounts.get(start["puuid"]) as Account; |
||||
account.startElo = { |
||||
rank: start["rank"], |
||||
tier: start["tier"], |
||||
points: start["points"] |
||||
}; |
||||
} |
||||
|
||||
for (const end of ends){ |
||||
let account = accounts.get(end["puuid"]) as Account; |
||||
account.currentElo = { |
||||
rank: end["rank"], |
||||
tier: end["tier"], |
||||
points: end["points"] |
||||
}; |
||||
} |
||||
|
||||
return Array.from(accounts.values()); |
||||
} |
||||
} |
Loading…
Reference in new issue