refactors, fix username check
This commit is contained in:
parent
00a8490082
commit
e33faddb20
4 changed files with 196 additions and 166 deletions
|
@ -50,59 +50,30 @@ class Player extends Entity {
|
||||||
|
|
||||||
player.ticks = 0;
|
player.ticks = 0;
|
||||||
}
|
}
|
||||||
handleTick(game) {
|
handleInt(target) {
|
||||||
|
if (target.you == this.you) return;
|
||||||
|
|
||||||
let { entities, width, height } = game;
|
let dist = distF(this, target);
|
||||||
|
|
||||||
let ent = this;
|
let dp = (Math.sin(this.rot) * (this.pos.x - target.pos.x))
|
||||||
ent.headCount += 1/200;
|
- (Math.cos(this.rot) * (this.pos.y - target.pos.y));
|
||||||
|
|
||||||
ent.headCount = Math.round(ent.headCount * 1000) / 1000;
|
|
||||||
|
|
||||||
if (ent.health <= 0) return;
|
|
||||||
|
|
||||||
ent.pos.x += ent.vel.x;
|
|
||||||
ent.pos.y += ent.vel.y;
|
|
||||||
|
|
||||||
ent.pos.x = Math.max(Math.min(ent.pos.x, width / 2), - width / 2);
|
|
||||||
ent.pos.y = Math.max(Math.min(ent.pos.y, height / 2), - height / 2);
|
|
||||||
|
|
||||||
ent.bounce(game);
|
|
||||||
|
|
||||||
ent.vel.x *= 0.9;
|
|
||||||
ent.vel.y *= 0.9;
|
|
||||||
|
|
||||||
ent.rot += 0.03 * ent.dir;
|
|
||||||
ent.rot = ent.rot % (Math.PI * 10);
|
|
||||||
|
|
||||||
ent.camera.x = -ent.pos.x * 0.1 + ent.camera.x * 0.9;
|
|
||||||
ent.camera.y = -ent.pos.y * 0.1 + ent.camera.y * 0.9;
|
|
||||||
|
|
||||||
ent.ticks++;
|
|
||||||
|
|
||||||
let velness = Math.sqrt(ent.vel.x ** 2 + ent.vel.y ** 2);
|
|
||||||
|
|
||||||
for (let target of entities) {
|
|
||||||
if (target.you == ent.you) continue;
|
|
||||||
|
|
||||||
let dist = distF(ent, target);
|
|
||||||
|
|
||||||
let dp = (Math.sin(ent.rot) * (ent.pos.x - target.pos.x))
|
|
||||||
- (Math.cos(ent.rot) * (ent.pos.y - target.pos.y));
|
|
||||||
|
|
||||||
dp /= Math.sqrt(dist + 0.1);
|
dp /= Math.sqrt(dist + 0.1);
|
||||||
|
|
||||||
if (Math.sqrt(dist) < 128 && 1 / dp < -0.2) {
|
if (!(Math.sqrt(dist) < 128 && 1 / dp < -0.2)) return;
|
||||||
if (target.type == 'NPC' && velness > 0.2 && ent.ticks >= 15) {
|
|
||||||
ent.isMenu = true;
|
let velness = Math.sqrt(this.vel.x ** 2 + this.vel.y ** 2);
|
||||||
ent.rot = 1.2;
|
|
||||||
|
if (target.type == 'NPC' && velness > 0.2 && this.ticks >= 15) {
|
||||||
|
this.isMenu = true;
|
||||||
|
this.rot = 1.2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (target.type == 'Shooter') {
|
if (target.type == 'Shooter') {
|
||||||
this.health -= 1;
|
this.health -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (target.immortal) continue;
|
if (target.immortal) return;
|
||||||
|
|
||||||
let oldHealth = target.health
|
let oldHealth = target.health
|
||||||
|
|
||||||
|
@ -110,16 +81,46 @@ class Player extends Entity {
|
||||||
|
|
||||||
target.health += Math.floor((dp - 0.001) * 10);
|
target.health += Math.floor((dp - 0.001) * 10);
|
||||||
|
|
||||||
target.vel.x += (target.pos.x - ent.pos.x) * 0.1;
|
target.vel.x += (target.pos.x - this.pos.x) * 0.1;
|
||||||
target.vel.y += (target.pos.y - ent.pos.y) * 0.1;
|
target.vel.y += (target.pos.y - this.pos.y) * 0.1;
|
||||||
|
|
||||||
if (target.health <= 0 && oldHealth > 0) {
|
if (target.health <= 0 && oldHealth > 0) {
|
||||||
console.log(`Player ${target.you} died to a player ${ent.you}`);
|
console.log(`Player ${target.you} died to a player ${this.you}`);
|
||||||
ent.health -= dmg;
|
this.health -= dmg;
|
||||||
if (ent.health > 100) ent.health = 100;
|
if (this.health > 100) this.health = 100;
|
||||||
ent.headCount += 500;
|
this.headCount += 500;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
handleTick(game) {
|
||||||
|
let { entities, width, height } = game;
|
||||||
|
|
||||||
|
this.headCount += 1 / 200;
|
||||||
|
|
||||||
|
this.headCount = Math.round(this.headCount * 1000) / 1000;
|
||||||
|
|
||||||
|
if (this.health <= 0) return;
|
||||||
|
|
||||||
|
this.pos.x += this.vel.x;
|
||||||
|
this.pos.y += this.vel.y;
|
||||||
|
|
||||||
|
this.pos.x = Math.max(Math.min(this.pos.x, width / 2), - width / 2);
|
||||||
|
this.pos.y = Math.max(Math.min(this.pos.y, height / 2), - height / 2);
|
||||||
|
|
||||||
|
this.bounce(game);
|
||||||
|
|
||||||
|
this.vel.x *= 0.9;
|
||||||
|
this.vel.y *= 0.9;
|
||||||
|
|
||||||
|
this.rot += 0.03 * this.dir;
|
||||||
|
this.rot = this.rot % (Math.PI * 10);
|
||||||
|
|
||||||
|
this.camera.x = -this.pos.x * 0.1 + this.camera.x * 0.9;
|
||||||
|
this.camera.y = -this.pos.y * 0.1 + this.camera.y * 0.9;
|
||||||
|
|
||||||
|
this.ticks++;
|
||||||
|
|
||||||
|
for (let target of entities) {
|
||||||
|
this.handleInt(target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
35
game.js
35
game.js
|
@ -1,6 +1,7 @@
|
||||||
import GameBasic from "./common/game_basic.js";
|
import GameBasic from "./common/game_basic.js";
|
||||||
import NPC from "./common/npc.js";
|
import NPC from "./common/npc.js";
|
||||||
import Shooter from "./common/shooter.js";
|
import Shooter from "./common/shooter.js";
|
||||||
|
import fetch from "node-fetch";
|
||||||
|
|
||||||
import initDb from './db.js';
|
import initDb from './db.js';
|
||||||
|
|
||||||
|
@ -115,6 +116,40 @@ class Game extends GameBasic {
|
||||||
|
|
||||||
setInterval(function () { that.sync(false) }, 1000 / 10);
|
setInterval(function () { that.sync(false) }, 1000 / 10);
|
||||||
setInterval(function () { that.sync(true) }, 1000);
|
setInterval(function () { that.sync(true) }, 1000);
|
||||||
|
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}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
24
index.js
24
index.js
|
@ -3,7 +3,6 @@ import expressW from "express-ws";
|
||||||
import Game from "./game.js";
|
import Game from "./game.js";
|
||||||
import NPC from "./common/npc.js";
|
import NPC from "./common/npc.js";
|
||||||
import Player from "./common/player.js";
|
import Player from "./common/player.js";
|
||||||
import fetch from "node-fetch";
|
|
||||||
|
|
||||||
import initDb from "./db.js";
|
import initDb from "./db.js";
|
||||||
|
|
||||||
|
@ -13,7 +12,6 @@ var app = express();
|
||||||
expressW(app);
|
expressW(app);
|
||||||
|
|
||||||
var game = new Game();
|
var game = new Game();
|
||||||
|
|
||||||
game.init();
|
game.init();
|
||||||
|
|
||||||
app.use('/crypto.js', express.static('./common/crypto.js'));
|
app.use('/crypto.js', express.static('./common/crypto.js'));
|
||||||
|
@ -49,27 +47,7 @@ app.ws('/', function (ws, req) {
|
||||||
let type = data[0];
|
let type = data[0];
|
||||||
|
|
||||||
if (type == 'AUTH') {
|
if (type == 'AUTH') {
|
||||||
ws.token = data[1];
|
game.authUser(ws,data[1]);
|
||||||
|
|
||||||
let f = new FormData();
|
|
||||||
f.append('token', ws.token);
|
|
||||||
|
|
||||||
(async function () {
|
|
||||||
let j = await fetch("https://bg.xuyezo.net/api/form/auth_api/",
|
|
||||||
{
|
|
||||||
"method": "post",
|
|
||||||
"body": f
|
|
||||||
}
|
|
||||||
);
|
|
||||||
j = await j.json();
|
|
||||||
if (game.ws.findIndex(x => x.username == j.username && x.health >= 0) != -1) {
|
|
||||||
console.log(`Player ${player.you} uses username ${x.username} illegally`)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ws.ent.username = ws.username = j.username;
|
|
||||||
|
|
||||||
console.log(`Player ${player.you} uses username ${ws.username}`)
|
|
||||||
})()
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,29 +51,10 @@ class Game extends GameBasic {
|
||||||
this.ctx = canvas.getContext("2d");
|
this.ctx = canvas.getContext("2d");
|
||||||
this.assetsIn = assetsIn;
|
this.assetsIn = assetsIn;
|
||||||
}
|
}
|
||||||
render() {
|
renderEnt(ent, textArgs) {
|
||||||
let { ctx, assetsIn, entities, player, width, height } = this;
|
let { ctx, assetsIn } = this;
|
||||||
|
|
||||||
ctx.clearRect(0, 0, cs, cs);
|
if (ent.health <= 0) return;
|
||||||
|
|
||||||
ctx.strokeStyle = "rgb(255,255,255)";
|
|
||||||
ctx.lineWidth = "8";
|
|
||||||
ctx.textAlign = "center";
|
|
||||||
ctx.textBaseline = "bottom";
|
|
||||||
ctx.lineCap = "round";
|
|
||||||
ctx.lineJoin = "round";
|
|
||||||
ctx.font = "bold 16px sans-serif";
|
|
||||||
|
|
||||||
ctx.save();
|
|
||||||
|
|
||||||
ctx.translate(player.camera.x + cs / 2, player.camera.y + cs / 2);
|
|
||||||
|
|
||||||
ctx.drawImage(assetsIn[2], -width / 2, -height / 2, width, height);
|
|
||||||
|
|
||||||
let textArgs = [];
|
|
||||||
|
|
||||||
for (let ent of entities) {
|
|
||||||
if (ent.health <= 0) continue;
|
|
||||||
|
|
||||||
if (ent.type == 'Player') {
|
if (ent.type == 'Player') {
|
||||||
ctx.save();
|
ctx.save();
|
||||||
|
@ -102,19 +83,43 @@ class Game extends GameBasic {
|
||||||
let a = assetsIn[bodies[ent.type]]
|
let a = assetsIn[bodies[ent.type]]
|
||||||
ctx.drawImage(a, ent.pos.x - 64 / 2, ent.pos.y - 64 / 2, 64, 64);
|
ctx.drawImage(a, ent.pos.x - 64 / 2, ent.pos.y - 64 / 2, 64, 64);
|
||||||
|
|
||||||
if (ent.type != 'Player') continue;
|
if (ent.type != 'Player') return;
|
||||||
|
|
||||||
textArgs = [`HP: ${ent.health} PT: ${ent.headCount}`, ent.pos.x, ent.pos.y - 64 / 2];
|
Object.assign(textArgs, [`HP: ${ent.health} PT: ${ent.headCount}`, ent.pos.x, ent.pos.y - 64 / 2]);
|
||||||
|
|
||||||
ctx.strokeText(...textArgs);
|
ctx.strokeText(...textArgs);
|
||||||
ctx.fillText(...textArgs);
|
ctx.fillText(...textArgs);
|
||||||
|
|
||||||
ctx.textBaseline = "top";
|
ctx.textBaseline = "top";
|
||||||
textArgs = [`${ent.username || 'Guest'}`, ent.pos.x, ent.pos.y + 64 / 2];
|
Object.assign(textArgs, [`${ent.username || 'Guest'}`, ent.pos.x, ent.pos.y + 64 / 2]);
|
||||||
|
|
||||||
ctx.strokeText(...textArgs);
|
ctx.strokeText(...textArgs);
|
||||||
ctx.fillText(...textArgs);
|
ctx.fillText(...textArgs);
|
||||||
}
|
}
|
||||||
|
render() {
|
||||||
|
let { ctx, assetsIn, entities, player, width, height } = this;
|
||||||
|
|
||||||
|
ctx.clearRect(0, 0, cs, cs);
|
||||||
|
|
||||||
|
ctx.strokeStyle = "rgb(255,255,255)";
|
||||||
|
ctx.lineWidth = "8";
|
||||||
|
ctx.textAlign = "center";
|
||||||
|
ctx.textBaseline = "bottom";
|
||||||
|
ctx.lineCap = "round";
|
||||||
|
ctx.lineJoin = "round";
|
||||||
|
ctx.font = "bold 16px sans-serif";
|
||||||
|
|
||||||
|
ctx.save();
|
||||||
|
|
||||||
|
ctx.translate(player.camera.x + cs / 2, player.camera.y + cs / 2);
|
||||||
|
|
||||||
|
ctx.drawImage(assetsIn[2], -width / 2, -height / 2, width, height);
|
||||||
|
|
||||||
|
let textArgs = [];
|
||||||
|
|
||||||
|
for (let ent of entities) {
|
||||||
|
this.renderEnt(ent, textArgs);
|
||||||
|
}
|
||||||
|
|
||||||
ctx.restore();
|
ctx.restore();
|
||||||
|
|
||||||
|
@ -179,14 +184,12 @@ class Game extends GameBasic {
|
||||||
|
|
||||||
this.ws.send(JSON.stringify(p));
|
this.ws.send(JSON.stringify(p));
|
||||||
}
|
}
|
||||||
recv({ data }) {
|
entMapper(j, i, entList) {
|
||||||
|
let that = this;
|
||||||
|
|
||||||
let { player } = this;
|
let { player } = this;
|
||||||
let you = player.you;
|
let you = player.you;
|
||||||
|
|
||||||
let that = this;
|
|
||||||
|
|
||||||
let entList = JSON.parse(data);
|
|
||||||
entList = entList.map((j, i) => {
|
|
||||||
if (i == entList.length - 1) {
|
if (i == entList.length - 1) {
|
||||||
that.width = j[0];
|
that.width = j[0];
|
||||||
that.height = j[1];
|
that.height = j[1];
|
||||||
|
@ -207,7 +210,8 @@ class Game extends GameBasic {
|
||||||
x[props[i]] = y[i];
|
x[props[i]] = y[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
x.handleTick = type.prototype.handleTick;
|
let { handleTick, bounce, handleInt } = type.prototype;
|
||||||
|
Object.assign(x, { handleTick, bounce, handleInt });
|
||||||
x.bounce = type.prototype.bounce;
|
x.bounce = type.prototype.bounce;
|
||||||
if (x.r != 1 && type == Player) {
|
if (x.r != 1 && type == Player) {
|
||||||
let a = new Audio(`sfx/${emojis[x.r]}.wav`);
|
let a = new Audio(`sfx/${emojis[x.r]}.wav`);
|
||||||
|
@ -224,7 +228,15 @@ class Game extends GameBasic {
|
||||||
a.play();
|
a.play();
|
||||||
}
|
}
|
||||||
return x;
|
return x;
|
||||||
})
|
}
|
||||||
|
recv({ data }) {
|
||||||
|
let { player } = this;
|
||||||
|
let you = player.you;
|
||||||
|
|
||||||
|
let that = this;
|
||||||
|
|
||||||
|
let entList = JSON.parse(data);
|
||||||
|
entList = entList.map((j, i) => that.entMapper(j, i, entList))
|
||||||
|
|
||||||
entList = entList.filter(x => x);
|
entList = entList.filter(x => x);
|
||||||
|
|
||||||
|
@ -246,7 +258,7 @@ class Game extends GameBasic {
|
||||||
|
|
||||||
if (this.entities.length == 0) this.entities = [this.player];
|
if (this.entities.length == 0) this.entities = [this.player];
|
||||||
}
|
}
|
||||||
async init() {
|
init() {
|
||||||
let u = new URL(window.location);
|
let u = new URL(window.location);
|
||||||
let tok = u.searchParams.get('token');
|
let tok = u.searchParams.get('token');
|
||||||
if (!tok) tok = window.localStorage.getItem('tok');
|
if (!tok) tok = window.localStorage.getItem('tok');
|
||||||
|
@ -275,6 +287,10 @@ class Game extends GameBasic {
|
||||||
|
|
||||||
game.canvas.onclick = () => that.click();
|
game.canvas.onclick = () => that.click();
|
||||||
|
|
||||||
|
this.handleScores();
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleScores() {
|
||||||
let jason = await(fetch('/leaderboard').then(x => x.json()))
|
let jason = await(fetch('/leaderboard').then(x => x.json()))
|
||||||
|
|
||||||
let scores = {};
|
let scores = {};
|
||||||
|
|
Loading…
Reference in a new issue