From 40fb91d38a462d4d21c7d1b773276332802aefed Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sun, 16 Jan 2022 20:44:53 +0100 Subject: Fully switch stats over from the environment to the statTracker --- draughts/app.js | 28 ++++++++++++++-------------- draughts/environment.js | 7 +------ draughts/statTracker.js | 5 ++--- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/draughts/app.js b/draughts/app.js index 6d4901a..94dca46 100644 --- a/draughts/app.js +++ b/draughts/app.js @@ -1,14 +1,14 @@ const Http = require("http") const WebSocket = require("ws") const Express = require("express") +const { stat } = require("fs") const Game = require("./game") const Color = require("./public/javascripts/color") const Messages = require("./public/javascripts/messages") const Environment = require("./environment") const indexRouter = require("./routes/index") -const stats = require("./statTracker"); -const { stat } = require("fs") +const stats = require("./statTracker") /* Get the server port, if the user doesn't provide one then print an error to STDERR and exit */ const port = process.argv[2] @@ -24,7 +24,6 @@ app.use(Express.static(__dirname + "/public")) app.get("/", indexRouter) app.get("/play", indexRouter) - /* Initialize the server and websocket server */ const server = Http.createServer(app) const wss = new WebSocket.Server({ server }) @@ -48,8 +47,8 @@ wss.on("connection", ws => { game.ongoing = true game.messageClient({ head: Messages.WELCOME, body: Color.RED }, ws) game.messageOpponent({ head: Messages.START }, ws) - stats.totalGames++ stats.ongoingGames++ + stats.totalGames++ game.nextTurn() } @@ -59,9 +58,10 @@ wss.on("connection", ws => { * array. */ ws.on("close", () => { - if (game.ongoing) + if (game.ongoing) { game.messageOpponent({ head: Messages.DISCONNECT }, ws) - stats.ongoingGames-- + stats.ongoingGames-- + } stats.totalGames-- env.removeGame(game) }) @@ -89,18 +89,18 @@ wss.on("connection", ws => { }, ws) game.move(msg.body) - if (!game.nextTurn()){ - var totalMoves = game.history.length + if (!game.nextTurn()) { + const totalMoves = game.history.length + /* Update minimum amount of moves in stat tracker */ - if (totalMoves < stats.minimumMoves){ - stats.minimumMoves = totalMoves - } + stats.minimumMoves = Math.min(totalMoves, stats.minimumMoves) + /* Update average amount of moves in stat tracker */ - if (stats.averageMoves != Infinity){ + if (stats.averageMoves != Infinity) stats.averageMoves = (stats.averageMoves * (stats.totalGames - 1) + totalMoves) / stats.totalGames - } else { + else stats.averageMoves = totalMoves - } + /* Remove ongoing game */ stats.ongoingGames-- env.removeGame(game) diff --git a/draughts/environment.js b/draughts/environment.js index 7f1e997..7518185 100644 --- a/draughts/environment.js +++ b/draughts/environment.js @@ -1,10 +1,5 @@ -/* A class representing the complete environment. It holds all the games as well as some statistics - * to be displayed on the splash screen. - */ +/* A class representing the complete environment. It holds all the games currently being played. */ const Environment = function() { - this.minimumMoves = Infinity, - this.averageMoves = Infinity, - this.totalGames = 0, this.games = [] } diff --git a/draughts/statTracker.js b/draughts/statTracker.js index 4d16084..b5a09b9 100644 --- a/draughts/statTracker.js +++ b/draughts/statTracker.js @@ -1,9 +1,8 @@ var gameStatus = { - since: Date.now() /* since we keep it simple and in-memory, keep track of when this object was created */, minimumMoves: Infinity, averageMoves: Infinity, - totalGames: 0, - ongoingGames: 0 + ongoingGames: 0, + totalGames: 0 } module.exports = gameStatus; -- cgit v1.2.3