unibutton/game.js

121 lines
3.4 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: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: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 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:20 -05:00
entity.pos = { x: Math.round(entity.pos.x), y: Math.round(entity.pos.y) };
entity.vel = { x: Math.round(entity.vel.x), y: Math.round(entity.vel.y) };
2024-11-25 14:14:20 -05:00
entity.camera = entity.camera ? { x: Math.round(entity.camera.x), y: Math.round(entity.camera.y) } : false;
2024-11-25 14:14:20 -05:00
let props = entity.serverProps;
let basic = props.map(prop => entity[prop]);
let str = JSON.stringify(basic);
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
]);
}
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();
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 < 18; 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:20 -05:00
}
}
export default Game;