aboutsummaryrefslogtreecommitdiff
path: root/src/main.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.js')
-rw-r--r--src/main.js116
1 files changed, 73 insertions, 43 deletions
diff --git a/src/main.js b/src/main.js
index a0ec6bc..7e9f988 100644
--- a/src/main.js
+++ b/src/main.js
@@ -2,14 +2,24 @@ import "notyf/notyf.min.css";
import "bulma/css/bulma.css";
import "@fortawesome/fontawesome-free/css/all.css";
import "./style.css";
-import { Manager, Timer } from "./utils";
-import { CourtManager, PlayerManager } from "./manage";
+import { Notyf } from "notyf";
+import { Timer } from "./utils";
import { Team, Game } from "./data";
+import { Manager, CourtManager, PlayerManager, GuestManager } from "./manage";
+
+export function getNotifier(seconds) {
+ return new Notyf({
+ duration: seconds * milliSecond,
+ position: { x: "center", y: "top" },
+ dismissible: true,
+ });
+}
export class GameManager extends Manager {
- t = new Timer();
+ timer = new Timer();
courts = new CourtManager();
players = new PlayerManager();
+ guests = new GuestManager();
constructor() {
super("Game", 12);
@@ -32,30 +42,23 @@ export class GameManager extends Manager {
return new Game(id, status, teamOne, teamTwo);
}
addBehaviour() {
- let description = this.isConfirmed() ? "Round " + this.round : "Proposal";
+ let confirmedStatus = this.data[0].status;
+ let description = confirmedStatus ? "Round " + this.round : "Proposal";
document.getElementById("Game_description").innerHTML = description;
}
- isConfirmed() {
- return this.data[0].status;
- }
- getBlockedGames() {
+ addBlockedGames() {
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"));
+ this.data.push(this.createItem(court.id, "RESERVED"));
}
- return games;
}
- inactive(courts) {
- let games = [];
+ addInactiveGames(courts) {
for (let court of courts) {
- games.push(this.createItem(court.id));
+ this.data.push(this.createItem(court.id));
}
- return games;
}
- active(courts, players) {
- let games = [];
+ addActiveGames(courts, players) {
for (let index = 0; index < courts.length; index++) {
const startIndex = 4 * index;
const endIndex = startIndex + 4;
@@ -64,33 +67,67 @@ export class GameManager extends Manager {
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);
+ this.data.push(game);
}
- return games;
+ }
+ getPossibleGameCount() {
+ let activeRegularCount = this.players.active().length;
+ let activeGuestCount = this.guests.active().length;
+ let activePlayerCount = activeRegularCount + activeGuestCount;
+ console.log("There are a total of " + activePlayerCount + " players available.");
+ let activeCourtCount = this.courts.active().length;
+ console.log("There are a total of " + activeCourtCount + " courts available.");
+ let possibleMatches = Math.min(Math.floor(activePlayerCount / 4), activeCourtCount);
+ return possibleMatches;
+ }
+ selectPlayers(count) {
+ console.log("Required players are " + count);
+ let activePlayers = this.players.active();
+ const activeGuests = this.guests.active();
+ activePlayers = activePlayers.concat(activeGuests);
+ console.log("Active players are:");
+ console.log(
+ activePlayers
+ .map((i) => {
+ return i.name;
+ })
+ .join(",")
+ );
+ const shuffledPlayers = shuffle(activePlayers);
+ shuffledPlayers.sort((a, b) => a.totalPlayed - b.totalPlayed);
+ const selectedPlayers = shuffledPlayers.slice(0, count);
+ selectedPlayers.sort((a, b) => a.level - b.level);
+ console.log("Selected players are:");
+ console.log(
+ selectedPlayers
+ .map((i) => {
+ return i.name;
+ })
+ .join(",")
+ );
+ return selectedPlayers;
}
propose() {
console.log("Proposing new games!");
- let possibleMatches = getMatchCount(
- this.players.data.concat(this.players.g.data),
- this.courts.data
- );
+ let possibleMatches = this.getPossibleGameCount();
console.log("Total possible matches are " + possibleMatches + ".");
- this.data = this.getBlockedGames();
+ this.data = [];
+ this.addBlockedGames();
const activeCourts = this.courts.active();
const unusedCourts = activeCourts.slice(possibleMatches + 1);
- this.data = this.data.concat(this.inactive(unusedCourts));
+ this.addInactiveGames(unusedCourts);
const usedCourts = activeCourts.slice(0, possibleMatches);
let requiredPlayerCount = possibleMatches * 4;
- const selectedPlayers = this.players.propose(requiredPlayerCount);
+ const selectedPlayers = this.selectPlayers(requiredPlayerCount);
console.log("The matches are:");
- this.data = this.data.concat(this.active(usedCourts, selectedPlayers));
+ this.addActiveGames(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!");
+ let confirmedStatus = this.data[0].status;
+ if (confirmedStatus) {
+ getNotifier().error("Create a proposal first!");
return;
}
console.log("Confirm the proposal for round " + this.round + ".");
@@ -116,19 +153,12 @@ export class GameManager extends Manager {
}
}
-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 shuffle(array) {
+ for (let i = array.length - 1; i > 0; i--) {
+ const j = Math.floor(Math.random() * (i + 1));
+ [array[i], array[j]] = [array[j], array[i]];
+ }
+ return array;
}
function addGameBehaviour(g) {