aboutsummaryrefslogtreecommitdiff
path: root/src/main.js
diff options
context:
space:
mode:
authorKaran Jayachandra <mail@karanjayachandra.com>2026-08-30 22:43:21 +0200
committerKaran Jayachandra <mail@karanjayachandra.com>2026-08-30 22:43:21 +0200
commitfc8d8ab8d0da95ec79056f59e14a150df6fcea89 (patch)
tree33f8050b2f2920c0f1b884a205a8aa4c81d7687c /src/main.js
parentd37ebfda883bcd03b146a85bc3e5a4657d0abd73 (diff)
Moved to sveltemain
Diffstat (limited to 'src/main.js')
-rw-r--r--src/main.js181
1 files changed, 4 insertions, 177 deletions
diff --git a/src/main.js b/src/main.js
index 59f66d0..f6228c9 100644
--- a/src/main.js
+++ b/src/main.js
@@ -1,178 +1,5 @@
-import "./style.css"
-import { Timer } from "./timer";
-import { Team, Game, Regular } from "./data";
-import { Manager, CourtManager, RegularManager, GuestManager } from "./manage";
-
-
-export class GameManager extends Manager {
- timer = new Timer();
- courts = new CourtManager();
- regulars = new RegularManager();
- guests = new GuestManager();
-
- constructor() {
- super("Game", 12);
- this.round = Number(localStorage.getItem("round")) || 0;
- this.render();
- addGameBehaviour(this);
- }
- createItem(i, description, status) {
- if (typeof description === "undefined") {
- description = "---";
- }
- if (typeof status === "undefined") {
- status = true;
- }
- let player = new Regular(null, null, null, null, description, "");
- console.log(player);
- let team = new Team(player, player);
- return new Game(i, status, 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", false));
- }
- }
- addInactiveGames(possibleMatches) {
- const activeCourts = this.courts.active();
- const unusedCourts = activeCourts.slice(possibleMatches + 1);
- for (let court of unusedCourts) {
- this.data.push(this.createItem(court.id, "---", false));
- }
- }
- addActiveGames(possibleMatches) {
- const activeCourts = this.courts.active();
- const usedCourts = activeCourts.slice(0, possibleMatches);
- let requiredPlayerCount = possibleMatches * 4;
- const selectedPlayers = this.selectPlayers(requiredPlayerCount);
- for (let index = 0; index < usedCourts.length; index++) {
- const startIndex = 4 * index;
- const endIndex = startIndex + 4;
- const courtPlayers = selectedPlayers.slice(startIndex, endIndex);
- const teamOne = new Team(courtPlayers[0], courtPlayers[3]);
- const teamTwo = new Team(courtPlayers[1], courtPlayers[2]);
- const game = new Game(usedCourts[index].id, false, teamOne, teamTwo);
- this.data.push(game);
- }
- }
- getPossibleGameCount() {
- let activeRegularCount = this.regulars.active().length;
- let activeGuestCount = this.guests.active().length;
- let activePlayerCount = activeRegularCount + activeGuestCount;
- console.log("Total players available: " + activePlayerCount);
- let activeCourtCount = this.courts.active().length;
- console.log("There courts available: " + activeCourtCount);
- let possibleMatches = Math.min(
- Math.floor(activePlayerCount / 4),
- activeCourtCount
- );
- return possibleMatches;
- }
- selectPlayers(count) {
- console.log("Required players are " + count);
- let activePlayers = this.regulars.active();
- const activeGuests = this.guests.active();
- activePlayers = activePlayers.concat(activeGuests);
- console.log("Active players are:");
- activePlayers.map((p) => {
- p.print();
- });
- const shuffledPlayers = shuffle(activePlayers);
- shuffledPlayers.sort((a, b) => a.games - b.games);
- const selectedPlayers = shuffledPlayers.slice(0, count);
- selectedPlayers.sort((a, b) => a.level - b.level);
- console.log("Selected players are:");
- selectedPlayers.map((p) => {
- p.print();
- });
- return selectedPlayers;
- }
- propose() {
- console.log("Proposing new games!");
- this.data = [];
- this.addBlockedGames();
- let possibleMatches = this.getPossibleGameCount();
- console.log("Total possible matches are " + possibleMatches + ".");
- this.addInactiveGames(possibleMatches);
- this.addActiveGames(possibleMatches);
- console.log("The matches are:");
- this.data.sort((a, b) => a.id - b.id);
- this.data.map((g) => {
- g.print();
- })
- this.render();
- }
- confirm() {
- let confirmedStatus = this.data[0].status;
- if (confirmedStatus) {
- 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;
- }
- let selectedPlayers = [];
- for (let game of this.data) {
- console.log(game);
- if ( game.teamOne.playerOne.name !== "RESERVED" ){
- selectedPlayers.push(game.teamOne.playerOne.name);
- selectedPlayers.push(game.teamOne.playerTwo.name);
- selectedPlayers.push(game.teamTwo.playerOne.name);
- selectedPlayers.push(game.teamTwo.playerTwo.name);
- }
- }
- this.regulars.incrementGames(selectedPlayers);
- this.guests.incrementGames(selectedPlayers);
- this.store();
- this.render();
- document.getElementById("timer-start").click();
- }
- reset() {
- localStorage.removeItem("round");
- localStorage.removeItem(this.description);
- localStorage.removeItem(this.courts.description);
- localStorage.removeItem(this.guests.description);
- this.regulars.reset();
- 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();
- });
- document.getElementById("reset").addEventListener("click", () => {
- g.reset();
- });
-}
-
-const g = new GameManager();
+import "./style.css";
+import { mount } from "svelte";
+import App from "./App.svelte";
+mount(App, { target: document.getElementById("app") });