2024-09-29 03:16:15 -04:00
|
|
|
import GameBasic from "./common/game_basic.js";
|
2024-10-01 04:03:55 -04:00
|
|
|
import NPC from "./common/npc.js";
|
2024-10-18 19:43:40 -04:00
|
|
|
import Shooter from "./common/shooter.js";
|
2024-10-21 20:46:47 -04:00
|
|
|
import fetch from "node-fetch";
|
2024-09-29 03:16:15 -04:00
|
|
|
|
2024-10-08 20:48:38 -04:00
|
|
|
import initDb from './db.js';
|
|
|
|
|
|
|
|
import { createHash } from "crypto";
|
|
|
|
|
|
|
|
let db = await initDb();
|
|
|
|
|
2024-09-29 03:16:15 -04:00
|
|
|
class Game extends GameBasic {
|
|
|
|
constructor() {
|
|
|
|
super();
|
2024-09-29 03:55:22 -04:00
|
|
|
this.ws = [];
|
2024-09-29 03:16:15 -04:00
|
|
|
}
|
2024-10-08 20:48:38 -04:00
|
|
|
async sync(full = false) {
|
2024-10-03 05:16:40 -04: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-09-29 03:55:22 -04:00
|
|
|
let { entities } = this;
|
|
|
|
let entList = [];
|
|
|
|
for (let entity of entities) {
|
2024-10-02 04:14:56 -04: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-10-06 09:47:52 -04:00
|
|
|
entity.camera = entity.camera ? { x: Math.round(entity.camera.x), y: Math.round(entity.camera.y) } : false;
|
2024-10-04 12:29:34 -04:00
|
|
|
|
|
|
|
let props = entity.serverProps;
|
|
|
|
let basic = props.map(prop => entity[prop]);
|
|
|
|
let str = JSON.stringify(basic);
|
|
|
|
|
|
|
|
entList.push({ entity, str, basic });
|
2024-09-29 03:55:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entList.length == 0) return;
|
|
|
|
|
|
|
|
for (let client of this.ws) {
|
2024-10-02 04:14:56 -04:00
|
|
|
let wsEnt = client.ent;
|
2024-10-04 12:29:34 -04:00
|
|
|
|
2024-10-02 04:14:56 -04:00
|
|
|
if (!wsEnt) continue;
|
2024-10-19 16:03:38 -04:00
|
|
|
|
2024-10-08 20:48:38 -04:00
|
|
|
if (full && wsEnt.health < 1 && !wsEnt.picked) {
|
|
|
|
wsEnt.picked = true;
|
|
|
|
|
2024-10-19 16:03:38 -04:00
|
|
|
let hash = client.username;
|
|
|
|
|
|
|
|
if (!hash) {
|
|
|
|
hash = createHash('sha256');
|
2024-10-19 18:24:14 -04:00
|
|
|
hash.update((+new Date) + '');
|
|
|
|
//hash.update(client.ip || '');
|
2024-10-19 16:03:38 -04:00
|
|
|
hash = hash.digest('hex');
|
|
|
|
}
|
2024-10-08 20:48:38 -04:00
|
|
|
|
|
|
|
await db.run('INSERT INTO stats (username, ip, ko) VALUES (?,?,?)', [
|
|
|
|
wsEnt.you,
|
2024-10-19 16:03:38 -04:00
|
|
|
hash,
|
2024-10-08 20:48:38 -04:00
|
|
|
wsEnt.headCount
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!client.active) continue;
|
2024-10-02 04:14:56 -04:00
|
|
|
|
2024-10-04 12:29:34 -04:00
|
|
|
wsEnt.isYou = true;
|
|
|
|
|
2024-10-02 04:14:56 -04:00
|
|
|
let filtered;
|
|
|
|
|
|
|
|
if (!full) {
|
2024-10-04 12:29:34 -04: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-10-02 04:14:56 -04:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
filtered = entList;
|
|
|
|
}
|
|
|
|
|
2024-10-04 12:29:34 -04:00
|
|
|
let na = [];
|
|
|
|
for (let i in filtered) {
|
|
|
|
let fi = filtered[i]
|
|
|
|
|
|
|
|
na.push(fi.str)
|
|
|
|
if (fi.entity.isYou) {
|
2024-10-19 16:03:38 -04:00
|
|
|
let narr = [...fi.basic];
|
2024-10-04 12:29:34 -04:00
|
|
|
narr[fi.entity.serverProps.indexOf('isYou')] = 'true';
|
|
|
|
na[na.length - 1] = JSON.stringify(narr);
|
|
|
|
}
|
|
|
|
}
|
2024-10-02 04:14:56 -04:00
|
|
|
|
2024-10-04 12:29:34 -04:00
|
|
|
na.push(JSON.stringify([this.width, this.height]))
|
2024-10-03 05:16:40 -04:00
|
|
|
|
2024-10-04 12:29:34 -04:00
|
|
|
client.send(`[${na.join(',')}]`);
|
2024-10-02 04:22:32 -04:00
|
|
|
|
|
|
|
wsEnt.isYou = false;
|
2024-09-29 03:55:22 -04:00
|
|
|
}
|
2024-10-01 04:03:55 -04:00
|
|
|
|
|
|
|
for (let entity of entities) {
|
|
|
|
entity.r = 1;
|
|
|
|
}
|
2024-09-29 03:16:15 -04:00
|
|
|
}
|
|
|
|
init() {
|
|
|
|
super.init();
|
|
|
|
|
|
|
|
let that = this;
|
2024-09-29 03:55:22 -04:00
|
|
|
that.entities = [];
|
2024-09-29 03:16:15 -04:00
|
|
|
|
2024-10-04 02:56:58 -04:00
|
|
|
for (let i = 0; i < 20; i++) {
|
2024-10-19 16:03:38 -04:00
|
|
|
that.entities.push(new NPC(false, that))
|
2024-10-01 04:03:55 -04:00
|
|
|
}
|
|
|
|
|
2024-10-19 18:24:14 -04:00
|
|
|
for (let i = 0; i < 18; i++) {
|
2024-10-19 16:03:38 -04:00
|
|
|
that.entities.push(new Shooter(false, that))
|
2024-10-18 19:43:40 -04:00
|
|
|
}
|
|
|
|
|
2024-10-02 04:14:56 -04:00
|
|
|
setInterval(function () { that.sync(false) }, 1000 / 10);
|
|
|
|
setInterval(function () { that.sync(true) }, 1000);
|
2024-10-21 20:46:47 -04:00
|
|
|
setInterval(function () { that.clean() }, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
clean() {
|
|
|
|
for (let ent in this.entities) {
|
|
|
|
let x = this.entities[ent];
|
|
|
|
if ( x.health > 0 || x.type != 'Player') return;
|
|
|
|
this.entites[ent] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async authUser(ws, data) {
|
|
|
|
ws.token = data;
|
|
|
|
|
|
|
|
let f = new FormData();
|
|
|
|
f.append('token', ws.token);
|
|
|
|
|
|
|
|
let j = await fetch("https://bg.xuyezo.net/api/form/auth_api/",
|
|
|
|
{
|
|
|
|
"method": "post",
|
|
|
|
"body": f
|
|
|
|
}
|
|
|
|
);
|
|
|
|
j = await j.json();
|
|
|
|
|
|
|
|
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-09-29 03:16:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Game;
|