Begin work on the Game class

This commit is contained in:
Oliver-Akins 2022-01-09 01:21:00 -06:00
parent 1edd3a042e
commit baf74969b4

View file

@ -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<FuelCard>;
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);
};
};