From baf74969b43a92b12bcca2bb34ad12e59e00075e Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Sun, 9 Jan 2022 01:21:00 -0600 Subject: [PATCH] Begin work on the Game class --- server/src/objects/Game.ts | 81 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 server/src/objects/Game.ts diff --git a/server/src/objects/Game.ts b/server/src/objects/Game.ts new file mode 100644 index 0000000..447099f --- /dev/null +++ b/server/src/objects/Game.ts @@ -0,0 +1,81 @@ +import { determineDirection, FuelCard } from "common"; +import { promises as fs } from "fs"; +import { v4 as uuid } from "uuid"; +import { Player } from "./Player"; +import { Logger } from "tslog"; +import { Deck } from "./Deck"; +import { log } from "../main"; +import path from "path"; + +export class Game { + readonly id: string; + readonly host: Player; + + private log: Logger; + private _deck: Deck; + + private board: (Player|null)[]; + private _players: Player[]; + + + constructor(host: Player) { + + // Setup the board + this.board = new Array(55).fill(null); + this.board[26] = null; + this.board[36] = null; + + // Init the player data + this.host = host; + this._players = [ host ]; + + // Instantiate the deck + this.loadDeck(); + + this.id = uuid(); + + this.log = log.getChildLogger({ + name: this.id, + displayLoggerName: true, + }); + }; + + /** + * Loads the deck from the data file which contains an array of card + * definitions. + */ + private async loadDeck() { + let cards = JSON.parse( + await fs.readFile( + path.join(process.cwd(), "cards.json"), + `utf-8` + ) + ); + this._deck = new Deck(cards); + }; + + + /** The deck of the fuel cards */ + get deck() { return this._deck; }; + + /** The players in the game */ + get players() { return this._players }; + + /** + * The algorithm to determine which direction the closest ship is, this + * uses an integer that can be multiplied by the player's fuel card + * magnitude in order to determine the delta for the board index. + * + * --- + * + * Possible Return Values: + * - `-1` = Away from the Warp Gate + * - `0` = Not moving + * - `1` = Towards the Warp Gate + */ + public movementDirection(player: Player): number { + let location = this.board.indexOf(player); + this.log.debug(`Calculating movement direction for ${player.name}`); + return determineDirection(this.board, location); + }; +}; \ No newline at end of file