unibutton/game.js

254 lines
7 KiB
JavaScript
Raw Normal View History

2024-11-25 14:14:20 -05:00
import GameBasic from "./common/game_basic.js";
import NPC from "./common/npc.js";
2024-11-25 14:14:21 -05:00
import Shooter from "./common/shooter.js";
2024-11-25 14:14:21 -05:00
import fetch from "node-fetch";
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:21 -05:00
import initDb from './db.js';
import { createHash } from "crypto";
let db = await initDb();
2024-11-25 14:14:21 -05:00
function sround(n) {
return Math.round(n * 1000) / 1000;
}
2024-11-25 14:14:20 -05:00
class Game extends GameBasic {
constructor() {
super();
2024-11-25 14:14:20 -05:00
this.ws = [];
2024-11-25 14:14:20 -05:00
}
2024-11-25 14:14:21 -05:00
async updateLeaderboard() {
2024-11-25 14:14:21 -05:00
let jason = await db.all('SELECT * FROM stats ORDER BY CAST(ko AS REAL) DESC LIMIT 1000');
2024-11-25 14:14:21 -05:00
2024-11-25 14:14:21 -05:00
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));
}
2024-11-25 14:14:21 -05:00
let s = Object.entries(scores).sort((a, b) => b[1] - a[1]).filter(a => a[1] > 5);
2024-11-25 14:14:21 -05:00
2024-11-25 14:14:21 -05:00
for (let x in s) {
2024-11-25 14:14:21 -05:00
let jk = jason.filter(y => y.ip == s[x][0]).map(y => y.ko).sort((a,b) => b - a);
2024-11-25 14:14:21 -05:00
s[x].push(jk.join(', '));
}
this.scores = s;
2024-11-25 14:14:21 -05:00
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);
2024-11-25 14:14:21 -05:00
if (!process.env.HOOK) return;
2024-11-25 14:14:21 -05:00
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: []
}),
}
);
}
2024-11-25 14:14:21 -05:00
async sync(full = false) {
2024-11-25 14:14:20 -05:00
let onScreen = this.entities.filter(x => x && x.health > 0).length;
onScreen += 4;
this.width = (this.width * 0.99) + (Math.sqrt(onScreen) * 16);
this.height = (this.height * 0.99) + (Math.sqrt(onScreen) * 16);
2024-11-25 14:14:20 -05:00
let { entities } = this;
let entList = [];
for (let entity of entities) {
2024-11-25 14:14:21 -05:00
entity.camera = entity.camera || false;
2024-11-25 14:14:20 -05:00
let props = entity.serverProps;
let basic = props.map(prop => entity[prop]);
2024-11-25 14:14:21 -05:00
for (let posn in basic) {
let posp = basic[posn];
if (!posp) continue;
if (posp.x && posp.y) {
posp.x = sround(posp.x);
posp.y = sround(posp.y);
}
if (typeof posp == 'number') {
basic[posn] = sround(posp);
}
}
let str = JSON.stringify(basic,null);
2024-11-25 14:14:20 -05:00
entList.push({ entity, str, basic });
2024-11-25 14:14:20 -05:00
}
if (entList.length == 0) return;
for (let client of this.ws) {
2024-11-25 14:14:20 -05:00
let wsEnt = client.ent;
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
if (!wsEnt) continue;
2024-11-25 14:14:21 -05:00
2024-11-25 14:14:21 -05:00
if (full && wsEnt.health < 1 && !wsEnt.picked) {
wsEnt.picked = true;
2024-11-25 14:14:21 -05:00
let hash = client.username;
if (!hash) {
hash = createHash('sha256');
2024-11-25 14:14:21 -05:00
hash.update((+new Date) + '');
//hash.update(client.ip || '');
2024-11-25 14:14:21 -05:00
hash = hash.digest('hex');
}
2024-11-25 14:14:21 -05:00
await db.run('INSERT INTO stats (username, ip, ko) VALUES (?,?,?)', [
wsEnt.you,
2024-11-25 14:14:21 -05:00
hash,
2024-11-25 14:14:21 -05:00
wsEnt.headCount
]);
2024-11-25 14:14:21 -05:00
this.updateLeaderboard();
2024-11-25 14:14:21 -05:00
}
if (!client.active) continue;
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
wsEnt.isYou = true;
2024-11-25 14:14:20 -05:00
let filtered;
if (!full) {
2024-11-25 14:14:20 -05:00
filtered = entList.filter(({ entity: ent }) => {
return Math.sqrt((ent.pos.x - wsEnt.pos.x) ** 2 + (ent.pos.y - wsEnt.pos.y) ** 2) < 777
}
2024-11-25 14:14:20 -05:00
);
} else {
filtered = entList;
}
2024-11-25 14:14:20 -05:00
let na = [];
for (let i in filtered) {
let fi = filtered[i]
na.push(fi.str)
if (fi.entity.isYou) {
2024-11-25 14:14:21 -05:00
let narr = [...fi.basic];
2024-11-25 14:14:20 -05:00
narr[fi.entity.serverProps.indexOf('isYou')] = 'true';
na[na.length - 1] = JSON.stringify(narr);
}
}
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
na.push(JSON.stringify([this.width, this.height]))
2024-11-25 14:14:20 -05:00
2024-11-25 14:14:20 -05:00
client.send(`[${na.join(',')}]`);
2024-11-25 14:14:20 -05:00
wsEnt.isYou = false;
2024-11-25 14:14:20 -05:00
}
for (let entity of entities) {
entity.r = 1;
}
2024-11-25 14:14:20 -05:00
}
init() {
super.init();
2024-11-25 14:14:21 -05:00
this.updateLeaderboard();
2024-11-25 14:14:20 -05:00
let that = this;
2024-11-25 14:14:20 -05:00
that.entities = [];
2024-11-25 14:14:20 -05:00
for (let i = 0; i < 20; i++) {
2024-11-25 14:14:21 -05:00
that.entities.push(new NPC(false, that))
}
2024-11-25 14:14:21 -05:00
for (let i = 0; i < 35; i++) {
2024-11-25 14:14:21 -05:00
that.entities.push(new Shooter(false, that))
2024-11-25 14:14:21 -05:00
}
2024-11-25 14:14:20 -05:00
setInterval(function () { that.sync(false) }, 1000 / 10);
setInterval(function () { that.sync(true) }, 1000);
2024-11-25 14:14:21 -05:00
setInterval(function () { that.clean() }, 1000);
}
clean() {
for (let ent in this.entities) {
let x = this.entities[ent];
2024-11-25 14:14:21 -05:00
if (x.health > 0 || x.type != 'Player') return;
2024-11-25 14:14:21 -05:00
this.entites[ent] = false;
}
}
async authUser(ws, data) {
ws.token = data;
let f = new FormData();
f.append('token', ws.token);
2024-11-25 14:14:21 -05:00
let j = await fetch("https://bg.dervland.net/api/form/auth_api/",
2024-11-25 14:14:21 -05:00
{
"method": "post",
"body": f
}
);
2024-11-25 14:14:21 -05:00
j = await j.text();
try {
j = JSON.parse(j);
} catch (err) {
j = {};
}
2024-11-25 14:14:21 -05:00
if (j.username == '!nobody' || !j.username) return;
if (this.ws.findIndex(x => x.username == j.username && x.ent.health > 0) != -1) {
console.log(`Player ${ws.ent.you} uses username ${j.username} illegally`)
return;
}
ws.ent.username = ws.username = j.username;
console.log(`Player ${ws.ent.you} uses username ${ws.username}`)
2024-11-25 14:14:20 -05:00
}
}
export default Game;