From 7e56f5f036c2ead7e16ae5e51cc0b93993251155 Mon Sep 17 00:00:00 2001 From: Zuxxied Date: Wed, 23 Oct 2024 18:44:19 -0400 Subject: [PATCH] add leaderboard announcement --- game.js | 72 +++++++++++++++++++++++++++++++++++++++++++++- index.js | 3 +- static/js/index.js | 18 ++---------- 3 files changed, 75 insertions(+), 18 deletions(-) diff --git a/game.js b/game.js index fb7b54c..3923aa1 100644 --- a/game.js +++ b/game.js @@ -14,6 +14,73 @@ class Game extends GameBasic { super(); this.ws = []; } + async updateLeaderboard() { + let jason = await db.all('SELECT * FROM stats ORDER BY ko DESC LIMIT 100'); + + let scoresOld = this.scores || []; + + let scores = {}; + + for (let e of jason) { + scores[e.ip] = scores[e.ip] || 0; + scores[e.ip] += e.ko ** 2; + } + + for (let score in scores) { + let s = scores[score]; + scores[score] = Math.round(Math.sqrt(s)); + } + + this.scores = Object.entries(scores).sort((a, b) => b[1] - a[1]); + + let s = this.scores; + + s = [...s]; + s.length = Math.min(s.length,10); + + let message = []; + + if (scoresOld.length == 0) return; + + for (let i = 0; i < this.scores.length || i < scoresOld.length; i++) { + let a = this.scores[i] ? this.scores[i][0] : '!nobody'; + let b = this.scores[i + 1] ? this.scores[i + 1][0] : '!nobody'; + + let c = scoresOld.findIndex(x => x[0] == a); + let d = scoresOld.findIndex(x => x[0] == b); + + if (a == '!nobody' || b == '!nobody' || d - c == 1 || (c != -1 && this.scores[i][1] == scoresOld[c][1])) continue; + + message.push(`${a} beat ${b} on the leaderboard!`); + } + + if (message.length == 0) return; + + message = message.join('\n'); + console.log(message); + + let h = await fetch( + process.env.HOOK, + { + method: 'post', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + username: 'UniButton', + content: message, + embeds: [ + { + color: 11730954, + title: 'Leaderboard', + description: s.map((x, i) => `**#${i + 1}**: ${x[0]} [${x[1]}]`).join('\n'), + }, + ], + files: [] + }), + } + ); + } async sync(full = false) { let onScreen = this.entities.filter(x => x && x.health > 0).length; @@ -60,6 +127,8 @@ class Game extends GameBasic { hash, wsEnt.headCount ]); + + this.updateLeaderboard(); } if (!client.active) continue; @@ -103,6 +172,7 @@ class Game extends GameBasic { init() { super.init(); + this.updateLeaderboard(); let that = this; that.entities = []; @@ -122,7 +192,7 @@ class Game extends GameBasic { clean() { for (let ent in this.entities) { let x = this.entities[ent]; - if ( x.health > 0 || x.type != 'Player') return; + if (x.health > 0 || x.type != 'Player') return; this.entites[ent] = false; } } diff --git a/index.js b/index.js index 1d590ba..feae123 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,8 @@ app.use('/js', express.static('./common')); app.use(express.static('./static')); app.get('/leaderboard', async function (ws, req) { - req.send(JSON.stringify(await db.all('SELECT * FROM stats ORDER BY ko DESC LIMIT 100'))); + let scores = game.scores || []; + req.send(scores.map((x,i) => `#${i+1}: ${x[0]} [${x[1]}]`).join('\n')); }) app.ws('/', function (ws, req) { diff --git a/static/js/index.js b/static/js/index.js index 4a7eff7..21698f6 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -291,23 +291,9 @@ class Game extends GameBasic { } async handleScores() { - let jason = await(fetch('/leaderboard').then(x => x.json())) + let jason = await (fetch('/leaderboard').then(x => x.text())) - let scores = {}; - - for (let e of jason) { - scores[e.ip] = scores[e.ip] || 0; - scores[e.ip] += e.ko ** 2; - } - - for (let score in scores) { - let s = scores[score]; - scores[score] = Math.round(Math.sqrt(s)); - } - - scores = Object.entries(scores).sort((a, b) => b[1] - a[1]); - - document.querySelector('.lb').textContent = scores.map(x => `${x[0]}: ${x[1]}`).join('\n'); + document.querySelector('.lb').textContent = jason; } }