add leaderboard announcement
This commit is contained in:
parent
e33faddb20
commit
7e56f5f036
3 changed files with 75 additions and 18 deletions
72
game.js
72
game.js
|
@ -14,6 +14,73 @@ class Game extends GameBasic {
|
||||||
super();
|
super();
|
||||||
this.ws = [];
|
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) {
|
async sync(full = false) {
|
||||||
let onScreen = this.entities.filter(x => x && x.health > 0).length;
|
let onScreen = this.entities.filter(x => x && x.health > 0).length;
|
||||||
|
|
||||||
|
@ -60,6 +127,8 @@ class Game extends GameBasic {
|
||||||
hash,
|
hash,
|
||||||
wsEnt.headCount
|
wsEnt.headCount
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
this.updateLeaderboard();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!client.active) continue;
|
if (!client.active) continue;
|
||||||
|
@ -103,6 +172,7 @@ class Game extends GameBasic {
|
||||||
init() {
|
init() {
|
||||||
super.init();
|
super.init();
|
||||||
|
|
||||||
|
this.updateLeaderboard();
|
||||||
let that = this;
|
let that = this;
|
||||||
that.entities = [];
|
that.entities = [];
|
||||||
|
|
||||||
|
@ -122,7 +192,7 @@ class Game extends GameBasic {
|
||||||
clean() {
|
clean() {
|
||||||
for (let ent in this.entities) {
|
for (let ent in this.entities) {
|
||||||
let x = this.entities[ent];
|
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;
|
this.entites[ent] = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
3
index.js
3
index.js
|
@ -19,7 +19,8 @@ app.use('/js', express.static('./common'));
|
||||||
app.use(express.static('./static'));
|
app.use(express.static('./static'));
|
||||||
|
|
||||||
app.get('/leaderboard', async function (ws, req) {
|
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) {
|
app.ws('/', function (ws, req) {
|
||||||
|
|
|
@ -291,23 +291,9 @@ class Game extends GameBasic {
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleScores() {
|
async handleScores() {
|
||||||
let jason = await(fetch('/leaderboard').then(x => x.json()))
|
let jason = await (fetch('/leaderboard').then(x => x.text()))
|
||||||
|
|
||||||
let scores = {};
|
document.querySelector('.lb').textContent = jason;
|
||||||
|
|
||||||
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');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue