|
|
@ -5,7 +5,7 @@ import {Command, loadCommands} from "./command"; |
|
|
|
|
|
|
|
|
|
|
|
export class UEMEloBot extends Client { |
|
|
|
export class UEMEloBot extends Client { |
|
|
|
|
|
|
|
|
|
|
|
players: Player[] = [] |
|
|
|
servers = new Collection<string, Player[]>(); |
|
|
|
fileName = "players.json" |
|
|
|
fileName = "players.json" |
|
|
|
commands = new Collection<string, Command>(); |
|
|
|
commands = new Collection<string, Command>(); |
|
|
|
|
|
|
|
|
|
|
@ -35,11 +35,16 @@ export class UEMEloBot extends Client { |
|
|
|
if (fs.existsSync(this.fileName)){ |
|
|
|
if (fs.existsSync(this.fileName)){ |
|
|
|
let fileContent = fs.readFileSync(this.fileName).toString(); |
|
|
|
let fileContent = fs.readFileSync(this.fileName).toString(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
this.players = []; |
|
|
|
this.servers.clear(); |
|
|
|
for (let obj of JSON.parse(fileContent)){ |
|
|
|
for (let [serverID, players] of Object.entries(JSON.parse(fileContent))){ |
|
|
|
let p = Player.Load(obj); |
|
|
|
console.log(`Server: ${serverID}`); |
|
|
|
this.players.push(p); |
|
|
|
let playersList: Player[] = []; |
|
|
|
console.log(`Parsed player: ${p}`); |
|
|
|
for (let obj of (players as Object[])){ |
|
|
|
|
|
|
|
let p = Player.Load(obj); |
|
|
|
|
|
|
|
playersList.push(p); |
|
|
|
|
|
|
|
console.log(`Parsed player: ${p}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this.servers.set(serverID, playersList); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
} catch (error) { |
|
|
|
console.error(`Failed to parse players: ${error}`); |
|
|
|
console.error(`Failed to parse players: ${error}`); |
|
|
@ -48,39 +53,41 @@ export class UEMEloBot extends Client { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
savePlayersToFile(){ |
|
|
|
savePlayersToFile(){ |
|
|
|
fs.writeFileSync(this.fileName, JSON.stringify(this.players, null, 4)); |
|
|
|
fs.writeFileSync(this.fileName, JSON.stringify(Object.fromEntries(this.servers), null, 4)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async updatePlayers(){ |
|
|
|
async updatePlayers(){ |
|
|
|
for (let p of this.players){ |
|
|
|
for (let [_, players] of this.servers){ |
|
|
|
await p.updateFullName(); |
|
|
|
for (let p of players){ |
|
|
|
await p.updateCurrentElo(); |
|
|
|
await p.updateFullName(); |
|
|
|
if (!p.startElo && p.currentElo){ |
|
|
|
await p.updateCurrentElo(); |
|
|
|
p.startElo = p.currentElo; |
|
|
|
if (!p.startElo && p.currentElo){ |
|
|
|
console.log(`Updated start elo for ${p}`); |
|
|
|
p.startElo = p.currentElo; |
|
|
|
|
|
|
|
console.log(`Updated start elo for ${p}`); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
this.savePlayersToFile(); |
|
|
|
this.savePlayersToFile(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async addPlayer(gameName: string, tagLine: string){ |
|
|
|
async addPlayer(gameName: string, tagLine: string, serverID: string){ |
|
|
|
let player = await Player.TryCreateFrom(gameName, tagLine) as Player; |
|
|
|
let player = await Player.TryCreateFrom(gameName, tagLine) as Player; |
|
|
|
|
|
|
|
|
|
|
|
if (!player) |
|
|
|
if (!player) |
|
|
|
return; |
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
if (this.players.find(p => p.puuid === player.puuid)){ |
|
|
|
if (this.servers.ensure(serverID, () => []).find(p => p.puuid === player.puuid)) { |
|
|
|
console.error(`${player} already registered!`); |
|
|
|
console.error(`${player} already registered!`); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.players.push(player); |
|
|
|
this.servers.ensure(serverID, () => []).push(player); |
|
|
|
console.log(`Added ${player}!`); |
|
|
|
console.log(`Added ${player} on Server: ${serverID}!`); |
|
|
|
this.savePlayersToFile(); |
|
|
|
this.savePlayersToFile(); |
|
|
|
return player; |
|
|
|
return player; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async removePlayer(gameName: string, tagLine: string){ |
|
|
|
async removePlayer(gameName: string, tagLine: string, serverID: string){ |
|
|
|
let playerCopy = await Player.TryCreateFrom(gameName, tagLine); |
|
|
|
let playerCopy = await Player.TryCreateFrom(gameName, tagLine); |
|
|
|
|
|
|
|
|
|
|
|
if (!playerCopy){ |
|
|
|
if (!playerCopy){ |
|
|
@ -88,13 +95,16 @@ export class UEMEloBot extends Client { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let player = this.players.find(p => p.puuid === playerCopy?.puuid); |
|
|
|
let serverPlayers = this.servers.ensure(serverID, () => []); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let player = serverPlayers.find(p => p.puuid === playerCopy?.puuid); |
|
|
|
if (!player){ |
|
|
|
if (!player){ |
|
|
|
console.error(`Tried to remove non-added player ${playerCopy}!`); |
|
|
|
console.error(`Tried to remove non-added player ${playerCopy}!`); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.players.splice(this.players.indexOf(player), 1); |
|
|
|
serverPlayers.splice(serverPlayers.indexOf(player), 1); |
|
|
|
|
|
|
|
this.servers.set(serverID, serverPlayers); |
|
|
|
console.log(`Removed ${player}!`); |
|
|
|
console.log(`Removed ${player}!`); |
|
|
|
this.savePlayersToFile(); |
|
|
|
this.savePlayersToFile(); |
|
|
|
return player; |
|
|
|
return player; |
|
|
|