aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/court.js (renamed from src/player.mjs)100
-rw-r--r--src/court.mjs32
-rw-r--r--src/game.mjs164
-rw-r--r--src/guest.mjs48
-rw-r--r--src/main.js143
-rw-r--r--src/utils.js (renamed from src/utils.mjs)25
6 files changed, 222 insertions, 290 deletions
diff --git a/src/player.mjs b/src/court.js
index 06b1abd..31e828d 100644
--- a/src/player.mjs
+++ b/src/court.js
@@ -1,53 +1,61 @@
-import { GuestManager } from "./guest.mjs";
-import { shuffle, Manager, randomLevel, randomStatus } from "./utils.mjs";
-import { Player } from "./data";
+import { Court, Guest, Player } from "./data";
+import { shuffle, Manager, randomLevel, randomStatus } from "./utils";
+export class CourtManager extends Manager {
+ constructor() {
+ super("Court", 12);
+ }
+ createItem(i) {
+ return new Court(i, randomStatus());
+ }
+ loadItem(data) {
+ let id = Number(data.id);
+ let status = Boolean(Number(data.status));
+ return new Court(id, status);
+ }
+}
-function parseLineToPlayer(data) {
- var info = data.split(",");
- var id = Number(info[0].trim());
- var name = info[1].trim() + " " + info[2].trim();
- var level = Number(info[3].trim());
- return new Player(id, true, level, 0, name);
+export class GuestManager extends Manager {
+ constructor() {
+ super("Guest", 12);
+ }
+ createItem(i) {
+ return new Guest(i, randomStatus());
+ }
+ loadItem(data) {
+ let id = Number(data.id);
+ let status = Boolean(Number(data.status));
+ let games = data.games;
+ return new Guest(id, status, games);
+ }
+ active(factor) {
+ factor = typeof factor !== "undefined" ? factor : 1;
+ let data = super.active();
+ console.log("Normalize skill by a factor of " + factor + ".");
+ let players = data.map(function (p) {
+ p.skill = Math.ceil(p.skill / factor);
+ return p;
+ });
+ return players;
+ }
}
export class PlayerManager extends Manager {
- g = new GuestManager();
-
constructor() {
- super();
- console.log("Loading player data.");
- let tempPlayer = new Player();
- let data = JSON.parse(localStorage.getItem(tempPlayer.description));
- if (data === null || data.length === 0) {
- console.log("Player data unavailable: " + data);
- this.generate();
- return;
- }
- for (let i = 0; i < data.length; i++) {
- let id = Number(data[i].id);
- let name = data[i].name;
- let level = Number(data[i].level);
- let status = Boolean(Number(data[i].status));
- let games = Number(data[i].games);
- this.data.push(new Player(id, status, level, games, name));
- }
- console.log("Loaded " + this.data.length + " players.");
- this.render();
- this.addBehaviour();
+ super("Regular", 71);
addLoadBehaviour(this);
+ this.g = new GuestManager();
}
- generate() {
- console.log("Generating player data.");
- let maxPlayerCount = 71;
- for (let i = 0; i < maxPlayerCount; i++) {
- let p = new Player(i, randomStatus(), randomLevel());
- console.log(p);
- this.data.push(p);
- }
- console.log("Generated data for " + this.data.length + " players");
- this.store();
- this.render();
+ createItem(i) {
+ return new Player(i, randomStatus(), randomLevel());
+ }
+ loadItem(data) {
+ let id = Number(data.id);
+ let name = data.name;
+ let level = Number(data.level);
+ let status = Boolean(Number(data.status));
+ let games = Number(data.games);
+ return new Player(id, status, level, games, name);
}
import(data) {
var lines = data.split("\n");
@@ -107,6 +115,14 @@ export class PlayerManager extends Manager {
}
}
+function parseLineToPlayer(data) {
+ var info = data.split(",");
+ var id = Number(info[0].trim());
+ var name = info[1].trim() + " " + info[2].trim();
+ var level = Number(info[3].trim());
+ return new Player(id, true, level, 0, name);
+}
+
function addLoadBehaviour(p) {
const f = document.querySelector("#player-load input[type=file]");
f.addEventListener("change", () => {
diff --git a/src/court.mjs b/src/court.mjs
deleted file mode 100644
index d85f05e..0000000
--- a/src/court.mjs
+++ /dev/null
@@ -1,32 +0,0 @@
-import { Court } from "./data";
-import { Manager } from "./utils.mjs";
-
-export class CourtManager extends Manager {
- maxCourtCount = 12;
-
- constructor() {
- super();
- console.log("Loading court data.");
- let tempCourt = new Court();
- let data = JSON.parse(localStorage.getItem(tempCourt.description));
- if (data === null || data.length != this.maxCourtCount) {
- this.generate();
- return;
- }
- for (let i = 0; i < this.maxCourtCount; i++) {
- let id = Number(data[i].id);
- let status = Boolean(Number(data[i].status));
- this.data.push(new Court(id, status));
- }
- console.log("Loaded " + this.data.length + " courts.");
- this.render();
- }
- generate() {
- for (let i = 0; i < this.maxCourtCount; i++) {
- this.data.push(new Court(i, true));
- }
- console.log("Generated data for " + this.data.length + " courts");
- this.store();
- this.render();
- }
-}
diff --git a/src/game.mjs b/src/game.mjs
deleted file mode 100644
index 9a2a921..0000000
--- a/src/game.mjs
+++ /dev/null
@@ -1,164 +0,0 @@
-import { Notyf } from "notyf";
-import { Manager } from "./utils.mjs";
-import { CourtManager } from "./court.mjs";
-import { PlayerManager } from "./player.mjs";
-import { Team, Game } from "./data";
-
-
-function generateDummyGames(courts, description) {
- let team = new Team(description, description);
- let games = [];
- for (const court of courts) {
- let game = new Game(court.id, false, team, team);
- games.push(game);
- }
- return games;
-}
-
-export class GameManager extends Manager {
- courts = new CourtManager();
- players = new PlayerManager();
-
- constructor() {
- super();
- this.round = Number(localStorage.getItem("round")) || 0;
- console.log("Loading game data.");
- 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();
- return;
- }
- for (let i = 0; i < data.length; i++) {
- let id = Number(data[i].id);
- let teamOne = new Team(
- data[i].teamOne.playerOne,
- data[i].teamOne.playerTwo
- );
- let teamTwo = new Team(
- data[i].teamTwo.playerOne,
- data[i].teamTwo.playerTwo
- );
- let status = Boolean(data[i].status);
- this.data.push(new Game(id, status, teamOne, teamTwo));
- }
- console.log("Loaded " + this.data.length + " games.");
- this.render();
- addGameBehaviour(this);
- }
- isConfirmed() {
- return this.data[0].status;
- }
- generate() {
- console.log("Generating the dummy round.");
- 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;
- }
- this.store();
- this.render();
- }
- addBehaviour() {
- let description = this.isConfirmed() ? "Round " + this.round : "Proposal";
- document.getElementById("Game_description").innerHTML = description;
- }
- getBlockedGames() {
- const inactiveCourts = this.courts.inactive();
- console.log("Total reserved courts are " + inactiveCourts.length + ".");
- return generateDummyGames(inactiveCourts, "RESERVED");
- }
- inactive(courts) {
- return generateDummyGames(courts, "---");
- }
- active(courts, players) {
- let games = [];
- for (let index = 0; index < courts.length; index++) {
- const startIndex = 4 * index;
- const endIndex = startIndex + 4;
- 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, false, team_1, team_2);
- game.print();
- games.push(game);
- }
- return games;
- }
- propose() {
- console.log("Proposing new games!");
- let possibleMatches = getMatchCount(
- this.players.data.concat(this.players.g.data),
- this.courts.data
- );
- console.log("Total possible matches are " + possibleMatches + ".");
- this.data = this.getBlockedGames();
- const activeCourts = this.courts.active();
- const unusedCourts = activeCourts.slice(possibleMatches + 1);
- 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.data = this.data.concat(
- this.active(usedCourts, selectedPlayers)
- );
- this.data.sort((a, b) => a.id - b.id);
- this.render();
- }
- confirm() {
- if (this.isConfirmed()) {
- const notification = new Notyf();
- notification.error("Create a proposal first!");
- return;
- }
- console.log("Confirm the proposal for round " + this.round + ".");
- this.round += 1;
- localStorage.setItem("round", this.round);
- for (let i = 0; i < this.data.length; i++) {
- this.data[i].status = true;
- }
- this.store();
- this.render();
- document.getElementById("timer-start").click();
- const notification = new Notyf();
- notification.success("Round confirmed!");
- }
- reset() {
- localStorage.removeItem("round");
- localStorage.removeItem(this.description);
- localStorage.removeItem(this.courts.description);
- document.getElementById("timer-stop").click();
- window.location.reload();
- const notification = new Notyf();
- notification.error("Session has been reset!");
- }
-}
-
-function getMatchCount(players, courts) {
- courts = courts.filter(function (c) {
- return c.status;
- });
- console.log("There are a total of " + courts.length + " courts available.");
- console.log(players);
- players = players.filter(function (p) {
- return p.status;
- });
- console.log("There are a total of " + players.length + " players available.");
- var possibleMatches = Math.floor(players.length / 4);
- possibleMatches = Math.min(possibleMatches, courts.length);
- return possibleMatches;
-}
-
-function addGameBehaviour(g) {
- document.getElementById("propose").addEventListener("click", () => {
- g.propose();
- });
- document.getElementById("confirm").addEventListener("click", () => {
- g.confirm();
- });
- document.getElementById("reset").addEventListener("click", () => {
- g.reset();
- });
-}
diff --git a/src/guest.mjs b/src/guest.mjs
deleted file mode 100644
index 57eb237..0000000
--- a/src/guest.mjs
+++ /dev/null
@@ -1,48 +0,0 @@
-import { Manager } from "./utils.mjs";
-import { Guest } from "./data";
-
-export class GuestManager extends Manager {
- constructor() {
- super("guest", []);
- console.log("Loading guest data.");
- let tempGuest = new Guest();
- let data = JSON.parse(localStorage.getItem(tempGuest.description));
- if (data === null || data.length === 0) {
- console.log("Guest data unavailable: " + data);
- this.generate();
- return;
- }
- console.log(tempGuest.description);
- for (let i = 0; i < data.length; i++) {
- console.log(data[i]);
- let id = Number(data[i].id);
- let status = Boolean(Number(data[i].status));
- let games = data[i].games;
- this.data.push(new Guest(id, status, games));
- }
- console.log("Loaded " + this.data.length + " players.");
- this.render();
- }
- generate() {
- console.log("Generating guest data.");
- const tempGuest = new Guest(0, false);
- const totalGuestCount = tempGuest.maxCount * tempGuest.categoryCount;
- for (let i = 0; i < totalGuestCount; i++) {
- let p = new Guest(i, false, 0);
- this.data.push(p);
- }
- console.log("Generated data for " + this.data.length + " guest");
- this.store();
- this.render();
- }
- active(factor) {
- factor = typeof factor !== "undefined" ? factor : 1;
- let data = super.active();
- console.log("Normalize skill by a factor of " + factor + ".");
- let players = data.map(function (p) {
- p.skill = Math.ceil(p.skill / factor);
- return p;
- });
- return players;
- }
-}
diff --git a/src/main.js b/src/main.js
index 0e05e0d..d66ddb6 100644
--- a/src/main.js
+++ b/src/main.js
@@ -2,8 +2,145 @@ import "notyf/notyf.min.css";
import "bulma/css/bulma.css";
import "@fortawesome/fontawesome-free/css/all.css";
import "./style.css";
-import { GameManager } from "./game.mjs";
-import { Timer } from "./utils.mjs";
+import { Manager, Timer } from "./utils";
+import { CourtManager, PlayerManager } from "./court";
+import { Team, Game } from "./data";
+
+export class GameManager extends Manager {
+ t = new Timer();
+ courts = new CourtManager();
+ players = new PlayerManager();
+
+ constructor() {
+ super("Game", 12);
+ this.round = Number(localStorage.getItem("round")) || 0;
+ this.render();
+ addGameBehaviour(this);
+ }
+ createItem(i, description) {
+ if (typeof description === "undefined") {
+ description = "---";
+ }
+ let team = new Team(description, description);
+ return new Game(i, true, team, team);
+ }
+ loadItem(data) {
+ let id = Number(data.id);
+ let teamOne = new Team(data.teamOne.playerOne, data.teamOne.playerTwo);
+ let teamTwo = new Team(data.teamTwo.playerOne, data.teamTwo.playerTwo);
+ let status = Boolean(data.status);
+ return new Game(id, status, teamOne, teamTwo);
+ }
+ addBehaviour() {
+ let description = this.isConfirmed() ? "Round " + this.round : "Proposal";
+ document.getElementById("Game_description").innerHTML = description;
+ }
+ isConfirmed() {
+ return this.data[0].status;
+ }
+ getBlockedGames() {
+ const inactiveCourts = this.courts.inactive();
+ console.log("Total reserved courts are " + inactiveCourts.length + ".");
+ let games = [];
+ for (let court of inactiveCourts) {
+ games.push(this.createItem(court.id, "RESERVED"));
+ }
+ return games;
+ }
+ inactive(courts) {
+ let games = [];
+ for (let court of courts) {
+ games.push(this.createItem(court.id));
+ }
+ return games;
+ }
+ active(courts, players) {
+ let games = [];
+ for (let index = 0; index < courts.length; index++) {
+ const startIndex = 4 * index;
+ const endIndex = startIndex + 4;
+ 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, false, team_1, team_2);
+ game.print();
+ games.push(game);
+ }
+ return games;
+ }
+ propose() {
+ console.log("Proposing new games!");
+ let possibleMatches = getMatchCount(
+ this.players.data.concat(this.players.g.data),
+ this.courts.data
+ );
+ console.log("Total possible matches are " + possibleMatches + ".");
+ this.data = this.getBlockedGames();
+ const activeCourts = this.courts.active();
+ const unusedCourts = activeCourts.slice(possibleMatches + 1);
+ 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.data = this.data.concat(this.active(usedCourts, selectedPlayers));
+ this.data.sort((a, b) => a.id - b.id);
+ this.render();
+ }
+ confirm() {
+ if (this.isConfirmed()) {
+ const notification = new Notyf();
+ notification.error("Create a proposal first!");
+ return;
+ }
+ console.log("Confirm the proposal for round " + this.round + ".");
+ this.round += 1;
+ localStorage.setItem("round", this.round);
+ for (let i = 0; i < this.data.length; i++) {
+ this.data[i].status = true;
+ }
+ this.store();
+ this.render();
+ document.getElementById("timer-start").click();
+ const notification = new Notyf();
+ notification.success("Round confirmed!");
+ }
+ reset() {
+ localStorage.removeItem("round");
+ localStorage.removeItem(this.description);
+ localStorage.removeItem(this.courts.description);
+ document.getElementById("timer-stop").click();
+ window.location.reload();
+ const notification = new Notyf();
+ notification.error("Session has been reset!");
+ }
+}
+
+function getMatchCount(players, courts) {
+ courts = courts.filter(function (c) {
+ return c.status;
+ });
+ console.log("There are a total of " + courts.length + " courts available.");
+ console.log(players);
+ players = players.filter(function (p) {
+ return p.status;
+ });
+ console.log("There are a total of " + players.length + " players available.");
+ var possibleMatches = Math.floor(players.length / 4);
+ possibleMatches = Math.min(possibleMatches, courts.length);
+ return possibleMatches;
+}
+
+function addGameBehaviour(g) {
+ document.getElementById("propose").addEventListener("click", () => {
+ g.propose();
+ });
+ document.getElementById("confirm").addEventListener("click", () => {
+ g.confirm();
+ });
+ document.getElementById("reset").addEventListener("click", () => {
+ g.reset();
+ });
+}
-const t = new Timer();
const g = new GameManager();
diff --git a/src/utils.mjs b/src/utils.js
index 2fca3f5..a8d4f07 100644
--- a/src/utils.mjs
+++ b/src/utils.js
@@ -91,9 +91,32 @@ function addTimerBehaviour(t) {
}
export class Manager {
- constructor() {
+ constructor(description, maxCount) {
+ this.description = description;
+ this.maxCount = maxCount;
this.data = [];
this.count = this.data.length;
+ let data = JSON.parse(localStorage.getItem(this.description));
+ if (data === null || data.length === 0) {
+ this.generate();
+ return;
+ }
+ this.load(data);
+ }
+ generate() {
+ for (let i = 0; i < this.maxCount; i++) {
+ this.data.push(this.createItem(i));
+ }
+ console.log("Generated " + this.data.length + " " + this.description);
+ this.store();
+ this.render();
+ }
+ load(data) {
+ for (let i = 0; i < data.length; i++) {
+ this.data.push(this.loadItem(data[i]));
+ }
+ console.log("Loaded " + this.data.length + " " + this.description);
+ this.render();
}
store() {
let stringData = JSON.stringify(this.data);