parent
4a829081df
commit
194ea203c7
12 changed files with 343 additions and 268 deletions
@ -0,0 +1,127 @@ |
||||
import {PlatformId, RiotAPI, RiotAPITypes} from "@fightmegg/riot-api" |
||||
import {RowDataPacket} from "mysql2/promise"; |
||||
import {getMySQLConnection} from "./util"; |
||||
import AccountDTO = RiotAPITypes.Account.AccountDTO; |
||||
|
||||
const riotAPI = new RiotAPI(process.env["RIOT_API_KEY"] ?? ""); |
||||
|
||||
export class Account { |
||||
|
||||
puuid: string |
||||
gameName?: string |
||||
tagLine?: string |
||||
|
||||
currentElo?: Elo |
||||
startElo?: Elo |
||||
|
||||
constructor(puuid: string) { |
||||
this.puuid = puuid; |
||||
} |
||||
|
||||
getProgress(){ |
||||
return eloToNumber(this.currentElo) - eloToNumber(this.startElo); |
||||
} |
||||
|
||||
async isAdded() { |
||||
const conn = await getMySQLConnection(); |
||||
const [rows] = await conn.execute<RowDataPacket[]>(` |
||||
SELECT 1
|
||||
FROM accounts |
||||
WHERE puuid = ? |
||||
LIMIT 1 |
||||
`, [this.puuid]);
|
||||
return rows.length == 1; |
||||
} |
||||
|
||||
async addToDB(){ |
||||
const conn = await getMySQLConnection(); |
||||
await conn.execute(` |
||||
INSERT INTO accounts
|
||||
(puuid, gameName, tagLine) |
||||
VALUES (?, ?, ?) |
||||
`, [this.puuid, this.gameName, this.tagLine]);
|
||||
} |
||||
|
||||
async removeFromDB(){ |
||||
const conn = await getMySQLConnection(); |
||||
await conn.execute(` |
||||
DELETE FROM accounts
|
||||
WHERE puuid = ? |
||||
`, [this.puuid]);
|
||||
} |
||||
|
||||
async update(){ |
||||
const conn = await getMySQLConnection(); |
||||
const fetchedAccount = await riotAPI.account.getByPUUID({ |
||||
region: PlatformId.EUROPE, |
||||
puuid: this.puuid |
||||
}); |
||||
this.gameName = fetchedAccount.gameName; |
||||
this.tagLine = fetchedAccount.tagLine; |
||||
await conn.execute(` |
||||
UPDATE accounts |
||||
SET gameName = ?, tagLine = ? |
||||
WHERE puuid = ? |
||||
`, [this.gameName, this.tagLine, this.puuid]);
|
||||
// TODO try add new elo entry to db
|
||||
} |
||||
|
||||
static async fetchFromRiotID(gameName: string, tagLine: string){ |
||||
let fetchedAccount: AccountDTO; |
||||
try { |
||||
fetchedAccount = await riotAPI.account.getByRiotId({ |
||||
region: PlatformId.EUROPE, |
||||
gameName: gameName, |
||||
tagLine: tagLine |
||||
}); |
||||
} catch (error) { |
||||
console.error(`Riot ID not found: ${gameName}#${tagLine}!`); |
||||
return; |
||||
} |
||||
let account = new Account(fetchedAccount.puuid); |
||||
account.gameName = fetchedAccount.gameName; |
||||
account.tagLine = fetchedAccount.tagLine; |
||||
|
||||
return account; |
||||
} |
||||
|
||||
toString() { |
||||
return `${this.gameName}#${this.tagLine}`; |
||||
} |
||||
} |
||||
|
||||
interface Elo { |
||||
tier: string, |
||||
rank: string, |
||||
points: number |
||||
} |
||||
|
||||
export function eloToNumber(elo?: Elo){ |
||||
if (!elo) |
||||
return 0; |
||||
let tiers: Record<string, number> = { |
||||
"CHALLENGER": 2800, |
||||
"GRANDMASTER": 2800, |
||||
"MASTER": 2800, |
||||
"DIAMOND": 2400, |
||||
"EMERALD": 2000, |
||||
"PLATINUM": 1600, |
||||
"GOLD": 1200, |
||||
"SILVER": 800, |
||||
"BRONZE": 400, |
||||
"IRON": 0, |
||||
}; |
||||
|
||||
let ranks: Record<string, number> = { |
||||
"I": 300, |
||||
"II": 200, |
||||
"III": 100, |
||||
"IV": 0, |
||||
}; |
||||
|
||||
let tier = elo.tier; |
||||
if (tier === "MASTER" || tier === "GRANDMASTER" || tier === "CHALLENGER") |
||||
return tiers[tier] + elo.points; |
||||
|
||||
return tiers[tier] + ranks[elo.rank] + elo.points; |
||||
} |
@ -1,144 +0,0 @@ |
||||
import {PlatformId, RiotAPI, RiotAPITypes} from "@fightmegg/riot-api" |
||||
import AccountDTO = RiotAPITypes.Account.AccountDTO; |
||||
|
||||
const riotAPI = new RiotAPI(process.env["RIOT_API_KEY"] as string); |
||||
|
||||
export class Player { |
||||
|
||||
puuid: string |
||||
|
||||
gameName?: string |
||||
tagLine?: string |
||||
startElo?: Elo |
||||
currentElo?: Elo |
||||
|
||||
constructor(puuid: string) { |
||||
this.puuid = puuid; |
||||
} |
||||
|
||||
static Load(raw: any): Player { |
||||
let p = new Player(raw.puuid); |
||||
p.startElo = raw.startElo; |
||||
p.gameName = raw.gameName; |
||||
p.tagLine = raw.tagLine; |
||||
return p; |
||||
} |
||||
|
||||
static async TryCreateFrom(gameName: string, tagLine: string) { |
||||
let account: AccountDTO; |
||||
try { |
||||
account = await riotAPI.account.getByRiotId({ |
||||
region: PlatformId.EUROPE, |
||||
gameName: gameName, |
||||
tagLine: tagLine |
||||
}); |
||||
} catch (error) { |
||||
console.error(`Riot ID not found: ${gameName}#${tagLine}!`); |
||||
return; |
||||
} |
||||
let p = new Player(account.puuid); |
||||
p.gameName = account.gameName; |
||||
p.tagLine = account.tagLine; |
||||
await p.updateCurrentElo(); |
||||
p.startElo = p.currentElo; |
||||
return p; |
||||
} |
||||
|
||||
async updateFullName(){ |
||||
const account = await riotAPI.account.getByPUUID({ |
||||
region: PlatformId.EUROPE, |
||||
puuid: this.puuid |
||||
}); |
||||
this.gameName = account.gameName; |
||||
this.tagLine = account.tagLine; |
||||
} |
||||
|
||||
async updateCurrentElo() { |
||||
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)){ |
||||
this.currentElo = { |
||||
tier: soloQ.tier, |
||||
rank: soloQ.rank, |
||||
points: soloQ.leaguePoints |
||||
}; |
||||
} |
||||
} |
||||
|
||||
getProgress(){ |
||||
return eloToNumber(this.currentElo) - eloToNumber(this.startElo); |
||||
} |
||||
|
||||
toString() { |
||||
return `${this.gameName}#${this.tagLine}`; |
||||
} |
||||
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
toJSON(){ |
||||
return { |
||||
puuid: this.puuid, |
||||
gameName: this.gameName, |
||||
tagLine: this.tagLine, |
||||
startElo: this.startElo |
||||
} |
||||
} |
||||
} |
||||
|
||||
interface Elo { |
||||
tier: string, |
||||
rank: string, |
||||
points: number |
||||
} |
||||
|
||||
export function eloToNumber(elo: Elo | undefined){ |
||||
if (!elo) |
||||
return 0; |
||||
let tiers: Record<string, number> = { |
||||
"CHALLENGER": 2800, |
||||
"GRANDMASTER": 2800, |
||||
"MASTER": 2800, |
||||
"DIAMOND": 2400, |
||||
"EMERALD": 2000, |
||||
"PLATINUM": 1600, |
||||
"GOLD": 1200, |
||||
"SILVER": 800, |
||||
"BRONZE": 400, |
||||
"IRON": 0, |
||||
}; |
||||
|
||||
let ranks: Record<string, number> = { |
||||
"I": 300, |
||||
"II": 200, |
||||
"III": 100, |
||||
"IV": 0, |
||||
}; |
||||
|
||||
let tier = elo.tier; |
||||
if (tier === "MASTER" || tier === "GRANDMASTER" || tier === "CHALLENGER") |
||||
return tiers[tier] + elo.points; |
||||
|
||||
return tiers[tier] + ranks[elo.rank] + elo.points; |
||||
} |
Loading…
Reference in new issue