From e244da094247fa615a71a13a8fcd88bc4402bfec Mon Sep 17 00:00:00 2001 From: KGrzeg Date: Mon, 18 Oct 2021 02:33:29 +0200 Subject: [PATCH] expand Justice --- game/src/api.ts | 5 ++++- game/src/scenes/GameOverScene.ts | 4 ++++ game/src/scenes/StartScene.ts | 10 +++++++++- server/.env.example | 1 + server/Auth.js | 7 +++++-- server/Database.js | 18 ++++++++++++++++++ server/JusticeGuard.js | 29 ++++++++++++++++++++--------- server/server.js | 8 ++++++++ 8 files changed, 69 insertions(+), 13 deletions(-) diff --git a/game/src/api.ts b/game/src/api.ts index a9cf376..560e649 100644 --- a/game/src/api.ts +++ b/game/src/api.ts @@ -1,4 +1,3 @@ - const baseApiPath = "http://localhost:3000" export default { @@ -70,5 +69,9 @@ export default { async top() { return this.get("top") + }, + + async start() { + return this.get("start") } } diff --git a/game/src/scenes/GameOverScene.ts b/game/src/scenes/GameOverScene.ts index fb7b047..33d8e12 100644 --- a/game/src/scenes/GameOverScene.ts +++ b/game/src/scenes/GameOverScene.ts @@ -22,6 +22,10 @@ export default class PlayScene extends Phaser.Scene { if (window.myStuff.token) { const response = await API.record(pts, shts, time) + if (response.error) { + console.log("%c" + response.error, "color:red") + } + if (response.rank) { rank = response.rank await window.updateTopList() diff --git a/game/src/scenes/StartScene.ts b/game/src/scenes/StartScene.ts index 7b3ea89..9e4485d 100644 --- a/game/src/scenes/StartScene.ts +++ b/game/src/scenes/StartScene.ts @@ -1,4 +1,5 @@ import Phaser from 'phaser' +import API from "../api" export default class StartScene extends Phaser.Scene { @@ -42,8 +43,15 @@ export default class StartScene extends Phaser.Scene { color: 'yellow' }).setOrigin(0.5, 0.5) - this.input.on('pointerup', () => { + this.input.on('pointerup', async () => { window.freezeGui() + if (window.myStuff.token) { + try { + await API.start() + } catch (error) { + console.log("oops", error) + } + } this.scene.start('play-scene'); }); } diff --git a/server/.env.example b/server/.env.example index 16890b7..65735ce 100644 --- a/server/.env.example +++ b/server/.env.example @@ -1 +1,2 @@ secret = oIMzR4YvM9x9NoPrQfk4 +enableJusticeGuard = 1 diff --git a/server/Auth.js b/server/Auth.js index 6fa3ed6..dd5d542 100644 --- a/server/Auth.js +++ b/server/Auth.js @@ -23,6 +23,7 @@ export default { name, password: password, record: 0, + lastPlayed: 0, }) return { @@ -32,7 +33,7 @@ export default { token } }, - + login(password) { console.log("Logging user"); const user = db.getUserByPassword(password) @@ -47,8 +48,10 @@ export default { record: user.record, }, process.env.secret) + const { lastPlayed, ...strippedUser } = user; + return { - ...user, + ...strippedUser, token } } diff --git a/server/Database.js b/server/Database.js index f010544..e4c878e 100644 --- a/server/Database.js +++ b/server/Database.js @@ -28,6 +28,10 @@ class Database { return user } + getUserByName(username) { + return this.db.data.users.find(user => user.name === username) + } + getUserByPassword(password) { return this.db.data.users.find(user => user.password === password) } @@ -74,6 +78,20 @@ class Database { return 0 } + + getLastPlayed(username) { + const user = this.getUserByName(username) + + return user.lastPlayed + } + + async setLastPlayedToNow(username) { + const user = this.getUserByName(username) + user.lastPlayed = Date.now() + + await this.db.write() + return user.lastPlayed + } } export default new Database diff --git a/server/JusticeGuard.js b/server/JusticeGuard.js index 5703786..896eee2 100644 --- a/server/JusticeGuard.js +++ b/server/JusticeGuard.js @@ -2,34 +2,45 @@ import db from './Database.js' export default function (req, res, next) { + if (!process.env.enableJusticeGuard) { + return next() + } + const { points, shoots, time } = req.body if (points > shoots) return res.status(400).json({ - error: "Stop that" + error: "Stop that, Rule #1", }) //FIXES https://discord.com/channels/762566311930101761/892788974647656500/893963260045455410 const foreseeRank = db.foreseeRank(req.body.points) if (foreseeRank == 1 && req.user.name == 'mw') return res.status(400).json({ - error: "Stop that" + error: "Stop that, Rule #4" }) //ship can shoot only 5 shoots per second //and one bullet is worth max 1 point - const maxPossiblePointsByTime = time * 0.2 - if (points >= maxPossiblePointsByTime) + const maxPossiblePointsByTime = time * 5 + if (points > maxPossiblePointsByTime) return res.status(400).json({ - error: "Stop that" + error: "Stop that, Rule #9" //yeah, greater numbers will scare hacker }) - //nobody will play for over hour, I assure you - if (time > 1000 * 60 * 60) + //nobody will play for over an hour, I assure you + if (time > 60 * 60) return res.status(400).json({ - error: "Stop that" + error: "Stop that, Rule #13" }) + //try to detect time manipulation + const startTimeOnServer = db.getLastPlayed(req.user.name) + const elapsedTimeOnServer = (Date.now() - startTimeOnServer) / 1000 + if (elapsedTimeOnServer * 1.2 < time) + return res.status(400).json({ + error: "Stop that, Rule #19" + }) - next() + return next() } diff --git a/server/server.js b/server/server.js index 8b67213..713d437 100644 --- a/server/server.js +++ b/server/server.js @@ -95,6 +95,14 @@ app.get("/top", (req, res) => { }) }) +app.get("/start", protect, async (req, res) => { + await db.setLastPlayedToNow(req.user.name) + + res.json({ + status: "ok" + }) +}) + app.use(function (err, req, res, next) { if (err.name === 'UnauthorizedError') { res.status(401).send('invalid token...');