aboutsummaryrefslogtreecommitdiff
path: root/src/game.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.mjs')
-rw-r--r--src/game.mjs78
1 files changed, 22 insertions, 56 deletions
diff --git a/src/game.mjs b/src/game.mjs
index 864f255..9a2a921 100644
--- a/src/game.mjs
+++ b/src/game.mjs
@@ -2,48 +2,14 @@ import { Notyf } from "notyf";
import { Manager } from "./utils.mjs";
import { CourtManager } from "./court.mjs";
import { PlayerManager } from "./player.mjs";
+import { Team, Game } from "./data";
-class Team {
- constructor(playerOne, playerTwo) {
- this.playerOne = playerOne;
- this.playerTwo = playerTwo;
- }
- print() {
- console.log(this.playerOne + " and " + this.playerTwo);
- }
- render() {
- let p1 = `<td style="width: 23%; text-align: right;">${this.playerOne}</td>`;
- let p2 = `<td style="width: 23%; text-align: right;">${this.playerTwo}</td>`;
- return p1 + p2;
- }
-}
-
-class Game {
- constructor(id, teamOne, teamTwo, status) {
- this.id = id;
- this.teamOne = teamOne;
- this.teamTwo = teamTwo;
- this.status = typeof status !== "undefined" ? status : false;
- }
- print() {
- let teamOne = this.teamOne.playerOne + " and " + this.teamOne.playerTwo;
- let teamTwo = this.teamTwo.playerOne + " and " + this.teamTwo.playerTwo;
- console.log(teamOne + " is playing against " + teamTwo);
- }
- render() {
- let blockedState = this.teamOne.playerOne === "RESERVED";
- let type = blockedState ? `class="is-danger is-dark"` : ``;
- return `<tr ${type}><td style="width: 8%; text-align: center;">${
- this.id + 1
- }</td>${this.teamOne.render()}${this.teamTwo.render()}</tr>`;
- }
-}
function generateDummyGames(courts, description) {
let team = new Team(description, description);
let games = [];
for (const court of courts) {
- let game = new Game(court.id, team, team);
+ let game = new Game(court.id, false, team, team);
games.push(game);
}
return games;
@@ -54,10 +20,11 @@ export class GameManager extends Manager {
players = new PlayerManager();
constructor() {
- super("game", []);
+ super();
this.round = Number(localStorage.getItem("round")) || 0;
console.log("Loading game data.");
- let data = JSON.parse(localStorage.getItem(this.description));
+ let tempGame = new Game();
+ let data = JSON.parse(localStorage.getItem(tempGame.description));
if (data === null || data.length === 0) {
console.log("Game data unavailable: " + data);
this.generate();
@@ -74,29 +41,28 @@ export class GameManager extends Manager {
data[i].teamTwo.playerTwo
);
let status = Boolean(data[i].status);
- this.listData.push(new Game(id, teamOne, teamTwo, status));
+ this.data.push(new Game(id, status, teamOne, teamTwo));
}
- console.log("Loaded " + this.listData.length + " games.");
+ console.log("Loaded " + this.data.length + " games.");
this.render();
addGameBehaviour(this);
}
isConfirmed() {
- return this.listData[0].status;
+ return this.data[0].status;
}
generate() {
console.log("Generating the dummy round.");
- this.listData = generateDummyGames(this.courts.listData, "---");
- console.log("Loaded " + this.listData.length + " dummy round.");
- for (let i = 0; i < this.listData.length; i++) {
- this.listData[i].status = true;
+ this.data = generateDummyGames(this.courts.data, "---");
+ console.log("Loaded " + this.data.length + " dummy round.");
+ for (let i = 0; i < this.data.length; i++) {
+ this.data[i].status = true;
}
- console.log(this.listData);
this.store();
this.render();
}
addBehaviour() {
let description = this.isConfirmed() ? "Round " + this.round : "Proposal";
- document.getElementById("game_description").innerHTML = description;
+ document.getElementById("Game_description").innerHTML = description;
}
getBlockedGames() {
const inactiveCourts = this.courts.inactive();
@@ -114,7 +80,7 @@ export class GameManager extends Manager {
const courtPlayers = players.slice(startIndex, endIndex);
const team_1 = new Team(courtPlayers[0].name, courtPlayers[3].name);
const team_2 = new Team(courtPlayers[1].name, courtPlayers[2].name);
- const game = new Game(courts[index].id, team_1, team_2);
+ const game = new Game(courts[index].id, false, team_1, team_2);
game.print();
games.push(game);
}
@@ -123,22 +89,22 @@ export class GameManager extends Manager {
propose() {
console.log("Proposing new games!");
let possibleMatches = getMatchCount(
- this.players.listData.concat(this.players.g.listData),
- this.courts.listData
+ this.players.data.concat(this.players.g.data),
+ this.courts.data
);
console.log("Total possible matches are " + possibleMatches + ".");
- this.listData = this.getBlockedGames();
+ this.data = this.getBlockedGames();
const activeCourts = this.courts.active();
const unusedCourts = activeCourts.slice(possibleMatches + 1);
- this.listData = this.listData.concat(this.inactive(unusedCourts));
+ this.data = this.data.concat(this.inactive(unusedCourts));
const usedCourts = activeCourts.slice(0, possibleMatches);
let requiredPlayerCount = possibleMatches * 4;
const selectedPlayers = this.players.propose(requiredPlayerCount);
console.log("The matches are:");
- this.listData = this.listData.concat(
+ this.data = this.data.concat(
this.active(usedCourts, selectedPlayers)
);
- this.listData.sort((a, b) => a.id - b.id);
+ this.data.sort((a, b) => a.id - b.id);
this.render();
}
confirm() {
@@ -150,8 +116,8 @@ export class GameManager extends Manager {
console.log("Confirm the proposal for round " + this.round + ".");
this.round += 1;
localStorage.setItem("round", this.round);
- for (let i = 0; i < this.listData.length; i++) {
- this.listData[i].status = true;
+ for (let i = 0; i < this.data.length; i++) {
+ this.data[i].status = true;
}
this.store();
this.render();