add leaderboard

This commit is contained in:
biglyderv 2024-11-25 14:14:21 -05:00
parent dc26c4281b
commit dbab510358
9 changed files with 1575 additions and 8 deletions

25
game.js
View file

@ -1,12 +1,18 @@
import GameBasic from "./common/game_basic.js";
import NPC from "./common/npc.js";
import initDb from './db.js';
import { createHash } from "crypto";
let db = await initDb();
class Game extends GameBasic {
constructor() {
super();
this.ws = [];
}
sync(full = false) {
async sync(full = false) {
let onScreen = this.entities.filter(x => x && x.health > 0).length;
onScreen += 4;
@ -31,11 +37,24 @@ class Game extends GameBasic {
if (entList.length == 0) return;
for (let client of this.ws) {
if (!client.active) continue;
let wsEnt = client.ent;
if (!wsEnt) continue;
if (full && wsEnt.health < 1 && !wsEnt.picked) {
wsEnt.picked = true;
const hash = createHash('sha256');
hash.update(client.ip || '');
await db.run('INSERT INTO stats (username, ip, ko) VALUES (?,?,?)', [
wsEnt.you,
hash.digest('hex'),
wsEnt.headCount
]);
}
if (!client.active) continue;
wsEnt.isYou = true;