diff options
Diffstat (limited to 'src/main.js')
| -rw-r--r-- | src/main.js | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/src/main.js b/src/main.js new file mode 100644 index 0000000..cb7ec8e --- /dev/null +++ b/src/main.js @@ -0,0 +1,184 @@ +import "notyf/notyf.min.css"; +import "bulma/css/bulma.css"; +import "@fortawesome/fontawesome-free/css/all.css"; +import "./style.css"; +import { Notyf } from "notyf"; +import { Timer } from "./timer"; +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 { + timer = new Timer(); + courts = new CourtManager(); + players = new PlayerManager(); + guests = new GuestManager(); + + 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 confirmedStatus = this.data[0].status; + let description = confirmedStatus ? "Round " + this.round : "Proposal"; + document.getElementById("Game_description").innerHTML = description; + } + addBlockedGames() { + const inactiveCourts = this.courts.inactive(); + console.log("Total reserved courts are " + inactiveCourts.length + "."); + for (let court of inactiveCourts) { + this.data.push(this.createItem(court.id, "RESERVED")); + } + } + addInactiveGames(courts) { + for (let court of courts) { + this.data.push(this.createItem(court.id)); + } + } + addActiveGames(courts, players) { + for (let index = 0; index < courts.length; index++) { + const startIndex = 4 * index; + const endIndex = startIndex + 4; + const courtPlayers = players.slice(startIndex, endIndex); + const teamOne = new Team(courtPlayers[0].name, courtPlayers[3].name); + const teamTwo = new Team(courtPlayers[1].name, courtPlayers[2].name); + const game = new Game(courts[index].id, false, teamOne, teamTwo); + game.print(); + this.data.push(game); + } + } + 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 = this.getPossibleGameCount(); + console.log("Total possible matches are " + possibleMatches + "."); + this.data = []; + this.addBlockedGames(); + const activeCourts = this.courts.active(); + const unusedCourts = activeCourts.slice(possibleMatches + 1); + this.addInactiveGames(unusedCourts); + const usedCourts = activeCourts.slice(0, possibleMatches); + let requiredPlayerCount = possibleMatches * 4; + const selectedPlayers = this.selectPlayers(requiredPlayerCount); + console.log("The matches are:"); + this.addActiveGames(usedCourts, selectedPlayers); + this.data.sort((a, b) => a.id - b.id); + this.render(); + } + confirm() { + let confirmedStatus = this.data[0].status; + if (confirmedStatus) { + getNotifier().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; + } + // Increment the number of games played + this.store(); + this.render(); + document.getElementById("timer-start").click(); + } + reset() { + localStorage.removeItem("round"); + localStorage.removeItem(this.description); + localStorage.removeItem(this.courts.description); + // Clear the number of games played + localStorage.removeItem(this.guests.description); + document.getElementById("timer-stop").click(); + window.location.reload(); + } +} + +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) { + document.getElementById("propose").addEventListener("click", () => { + g.propose(); + }); + document.getElementById("confirm").addEventListener("click", () => { + g.confirm(); + getNotifier().success("Round confirmed!"); + }); + document.getElementById("reset").addEventListener("click", () => { + g.reset(); + getNotifier().error("Session has been reset!"); + }); +} + +const g = new GameManager(); |
