From 3dc01fbb930a4eacf416a9cc7911d90da75e6c33 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Sun, 9 Nov 2025 15:57:07 +0100 Subject: Moved a lot of the functionality --- assets/icon.ipe | 461 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 461 insertions(+) create mode 100644 assets/icon.ipe (limited to 'assets') diff --git a/assets/icon.ipe b/assets/icon.ipe new file mode 100644 index 0000000..d23d37f --- /dev/null +++ b/assets/icon.ipe @@ -0,0 +1,461 @@ + + + + + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-0.8 0 l +-1 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-0.8 0 l +-1 -0.333 l +h + + + + +0.6 0 0 0.6 0 0 e +0.4 0 0 0.4 0 0 e + + + + +0.6 0 0 0.6 0 0 e + + + + + +0.5 0 0 0.5 0 0 e + + +0.6 0 0 0.6 0 0 e +0.4 0 0 0.4 0 0 e + + + + + +-0.6 -0.6 m +0.6 -0.6 l +0.6 0.6 l +-0.6 0.6 l +h +-0.4 -0.4 m +0.4 -0.4 l +0.4 0.4 l +-0.4 0.4 l +h + + + + +-0.6 -0.6 m +0.6 -0.6 l +0.6 0.6 l +-0.6 0.6 l +h + + + + + +-0.5 -0.5 m +0.5 -0.5 l +0.5 0.5 l +-0.5 0.5 l +h + + +-0.6 -0.6 m +0.6 -0.6 l +0.6 0.6 l +-0.6 0.6 l +h +-0.4 -0.4 m +0.4 -0.4 l +0.4 0.4 l +-0.4 0.4 l +h + + + + + + +-0.43 -0.57 m +0.57 0.43 l +0.43 0.57 l +-0.57 -0.43 l +h + + +-0.43 0.57 m +0.57 -0.43 l +0.43 -0.57 l +-0.57 0.43 l +h + + + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-0.8 0 l +-1 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-0.8 0 l +-1 -0.333 l +h + + + + +-1 0.333 m +0 0 l +-1 -0.333 l + + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h +-1 0 m +-2 0.333 l +-2 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h +-1 0 m +-2 0.333 l +-2 -0.333 l +h + + + + +0.5 0 m +-0.5 0.333 l +-0.5 -0.333 l +h + + + + +0.5 0 m +-0.5 0.333 l +-0.5 -0.333 l +h + + + + +0.5 0 m +-0.5 0.333 l +-0.3 0 l +-0.5 -0.333 l +h + + + + +0.5 0 m +-0.5 0.333 l +-0.3 0 l +-0.5 -0.333 l +h + + + + +1 0 m +0 0.333 l +0 -0.333 l +h +0 0 m +-1 0.333 l +-1 -0.333 l +h + + + + +1 0 m +0 0.333 l +0 -0.333 l +h +0 0 m +-1 0.333 l +-1 -0.333 l +h + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +96 624 m +92 612 +88 600 +92 588 +96 576 c +96 576 +100 588 +104 600 +100 612 +96 624 c +h + + +96 624 m +92 612 +88 600 +92 588 +96 576 c +96 576 +100 588 +104 600 +100 612 +96 624 c +h + + +96 624 m +92 612 +88 600 +92 588 +96 576 c +96 576 +100 588 +104 600 +100 612 +96 624 c +h + + +96 624 m +92 612 +88 600 +92 588 +96 576 c +96 576 +100 588 +104 600 +100 612 +96 624 c +h + + +96 624 m +92 612 +88 600 +92 588 +96 576 c +96 576 +100 588 +104 600 +100 612 +96 624 c +h + + +92 620 m +4 0 0 -4 96 620 100 620 a +100 616 l +92 616 l +h + + +96 624 m +92 612 +88 600 +92 588 +96 576 c +96 576 +100 588 +104 600 +100 612 +96 624 c +h + + +96 624 m +92 612 +88 600 +92 588 +96 576 c +96 576 +100 588 +104 600 +100 612 +96 624 c +h + + +96 624 m +92 612 +88 600 +92 588 +96 576 c +96 576 +100 588 +104 600 +100 612 +96 624 c +h + + +96 624 m +92 612 +88 600 +92 588 +96 576 c +96 576 +100 588 +104 600 +100 612 +96 624 c +h + + +96 624 m +92 612 +88 600 +92 588 +96 576 c +96 576 +100 588 +104 600 +100 612 +96 624 c +h + + +92 620 m +4 0 0 -4 96 620 100 620 a +100 616 l +92 616 l +h + + + -- cgit v1.3.1 From 0e9e28c2595c1ec46d8601efa6f4d377ee70cb27 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Tue, 11 Nov 2025 17:50:02 +0100 Subject: First version of proposal --- assets/favicon.ipe | 325 +++++++++++++++++++++++++++++++++++++++++++++++++++++ assets/favicon.svg | 19 ++++ favicon.ico | Bin 0 -> 13054 bytes game.mjs | 60 ++++++++-- index.js | 16 +-- player.mjs | 7 +- utils.mjs | 31 ++--- 7 files changed, 425 insertions(+), 33 deletions(-) create mode 100644 assets/favicon.ipe create mode 100644 assets/favicon.svg create mode 100644 favicon.ico (limited to 'assets') diff --git a/assets/favicon.ipe b/assets/favicon.ipe new file mode 100644 index 0000000..5d79b35 --- /dev/null +++ b/assets/favicon.ipe @@ -0,0 +1,325 @@ + + + + + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-0.8 0 l +-1 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-0.8 0 l +-1 -0.333 l +h + + + + +0.6 0 0 0.6 0 0 e +0.4 0 0 0.4 0 0 e + + + + +0.6 0 0 0.6 0 0 e + + + + + +0.5 0 0 0.5 0 0 e + + +0.6 0 0 0.6 0 0 e +0.4 0 0 0.4 0 0 e + + + + + +-0.6 -0.6 m +0.6 -0.6 l +0.6 0.6 l +-0.6 0.6 l +h +-0.4 -0.4 m +0.4 -0.4 l +0.4 0.4 l +-0.4 0.4 l +h + + + + +-0.6 -0.6 m +0.6 -0.6 l +0.6 0.6 l +-0.6 0.6 l +h + + + + + +-0.5 -0.5 m +0.5 -0.5 l +0.5 0.5 l +-0.5 0.5 l +h + + +-0.6 -0.6 m +0.6 -0.6 l +0.6 0.6 l +-0.6 0.6 l +h +-0.4 -0.4 m +0.4 -0.4 l +0.4 0.4 l +-0.4 0.4 l +h + + + + + + +-0.43 -0.57 m +0.57 0.43 l +0.43 0.57 l +-0.57 -0.43 l +h + + +-0.43 0.57 m +0.57 -0.43 l +0.43 -0.57 l +-0.57 0.43 l +h + + + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-0.8 0 l +-1 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-0.8 0 l +-1 -0.333 l +h + + + + +-1 0.333 m +0 0 l +-1 -0.333 l + + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h +-1 0 m +-2 0.333 l +-2 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h +-1 0 m +-2 0.333 l +-2 -0.333 l +h + + + + +0.5 0 m +-0.5 0.333 l +-0.5 -0.333 l +h + + + + +0.5 0 m +-0.5 0.333 l +-0.5 -0.333 l +h + + + + +0.5 0 m +-0.5 0.333 l +-0.3 0 l +-0.5 -0.333 l +h + + + + +0.5 0 m +-0.5 0.333 l +-0.3 0 l +-0.5 -0.333 l +h + + + + +1 0 m +0 0.333 l +0 -0.333 l +h +0 0 m +-1 0.333 l +-1 -0.333 l +h + + + + +1 0 m +0 0.333 l +0 -0.333 l +h +0 0 m +-1 0.333 l +-1 -0.333 l +h + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +M + +8 840 m +8 824 l +24 824 l +24 840 l +h + + + diff --git a/assets/favicon.svg b/assets/favicon.svg new file mode 100644 index 0000000..ab0b740 --- /dev/null +++ b/assets/favicon.svg @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000..6cb5040 Binary files /dev/null and b/favicon.ico differ diff --git a/game.mjs b/game.mjs index 7b5d4c9..75c4906 100644 --- a/game.mjs +++ b/game.mjs @@ -1,3 +1,5 @@ +import { getMatchCount, shuffle } from "./utils.mjs"; + class Team { constructor(player_1, player_2) { this.player_1 = player_1; @@ -11,7 +13,7 @@ class Team { } } -class Game { +export class Game { constructor(court, team_1, team_2) { this.court_id = court.id; this.court_status = court.status; @@ -29,19 +31,61 @@ class Game { } } +export function generateDummyGames(courts) { + let team = new Team("---", "---"); + let games = []; + for (const court of courts ) { + let game = new Game(court, team, team); + games.push(game); + } + return games; +} + export class Round { - constructor(number, courts) { + constructor(number, games) { this.number = number; - this.courts = courts; + this.games = games; } - render() { - let output = `

Round ${this.number}

`; - let team = new Team("---", "---"); - for (const court of this.courts ) { - let game = new Game(court, team, team); + render(description) { + let heading = typeof description !== 'undefined' ? description : "Round " + this.number; + let output = `

${heading}

CourtTeam 1Team 2
`; + for (const game of this.games ) { output += game.render() } output += "
CourtTeam 1Team 2
"; return output; } +} + +export function proposeGames(players, courts) { + console.log("Proposing new games!"); + let possibleMatches = getMatchCount(players, courts); + let requiredPlayers = possibleMatches * 4; + console.log("Required players are " + requiredPlayers); + const activeCourts = courts.filter(function (c) { return c.status }); + const inactiveCourts = courts.filter(function (c) { return !c.status }); + const activePlayers = players.filter(function (p) { return p.status;}); + console.log("Total reserved courts are " + inactiveCourts.length + "."); + console.log("Total possible matches are " + possibleMatches + "."); + 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, requiredPlayers); + selectedPlayers.sort((a, b) => a.level - b.level); + console.log("Selected players are:"); + console.log(selectedPlayers.map((i) => {return i.name;}).join(',')); + console.log("The matches are:"); + let games = []; + for (let index = 0; index < possibleMatches; index++) { + const startIndex = 4 * index; + const endIndex = startIndex + 4; + const courtPlayers = selectedPlayers.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(activeCourts[index], team_1, team_2); + game.print(); + games.push(game); + } + return games; } \ No newline at end of file diff --git a/index.js b/index.js index 8b146a8..7f39140 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -import { Round } from "./game.mjs"; +import { Round, generateDummyGames, proposeGames } from "./game.mjs"; import { generateCourts } from "./court.mjs"; import { csvToPlayers, generatePlayers } from "./player.mjs"; import { addToggleBehaviour } from "./utils.mjs"; @@ -12,8 +12,8 @@ addToggleBehaviour(courts, "court_"); let regulars = generatePlayers(); addToggleBehaviour(regulars, "regular_"); const fileInput = document.querySelector("#player-load input[type=file]"); - if (fileInput !== null) { - fileInput.onchange = () => { +if (fileInput !== null) { + fileInput.onchange = () => { if (fileInput.files.length > 0) { const file = fileInput.files[0]; if (file) { @@ -27,12 +27,12 @@ const fileInput = document.querySelector("#player-load input[type=file]"); } } } -}; +} // Game handling -let games = new Round(0, courts); -let renderedHTML = games.render(); -document.getElementById("game_list").innerHTML = renderedHTML; +let round = new Round(0, generateDummyGames(courts)); +document.getElementById("game_list").innerHTML = round.render(); document.getElementById("propose").addEventListener("click", () => { - console.log("Propose new games!"); + round = new Round(0, proposeGames(regulars, courts)); + document.getElementById("game_list").innerHTML = round.render(); }) \ No newline at end of file diff --git a/player.mjs b/player.mjs index 75d31d8..91d146a 100644 --- a/player.mjs +++ b/player.mjs @@ -1,9 +1,10 @@ export class Player { - constructor(id, name, level, status) { + constructor(id, name, level, status, totalPlayed) { this.id = id; this.name = name; this.level = typeof level !== 'undefined' ? level : 1; this.status = typeof status !== 'undefined' ? status : false; + this.totalPlayed = typeof totalPlayed !== 'undefined' ? status : 0; } toggle() { this.status = !this.status; @@ -31,7 +32,7 @@ function randomStatus() { export function generatePlayers() { let players = []; for (let i = 0; i < 71; i++) { - players.push(new Player(i, "Player " + i, randomLevel(), randomStatus())); + players.push(new Player(i, "Player " + i, randomLevel(), randomStatus(), 0)); } return players; } @@ -41,7 +42,7 @@ function parseLineToPlayer(data) { var id = Number(info[0].trim()); var name = info[1].trim() + " " + info[2].trim(); var level = Number(info[3].trim()); - return new Player(id, name, level, false); + return new Player(id, name, level, false, 0); } export function csvToPlayers(csv) { diff --git a/utils.mjs b/utils.mjs index 968731c..308bba8 100644 --- a/utils.mjs +++ b/utils.mjs @@ -12,17 +12,20 @@ export function addToggleBehaviour(list, description) { } } -// document.querySelector("#player-load input[type=file]").onchange = () => { -// if (fileInput.files.length > 0) { -// const file = fileInput.files[0]; -// if (file) { -// const reader = new FileReader(); -// reader.onload = function(e) { -// const content = e.target.result; -// regulars = csvToPlayers(content); -// addToggleBehaviour(regulars, "regular_"); -// }; -// reader.readAsText(file); -// } -// } -// } \ No newline at end of file +export function getMatchCount(regulars, courts) { + const activeCourts = courts.filter(function (c) { return c.status }); + console.log("There are a total of " + activeCourts.length + " courts available."); + const activePlayers = regulars.filter(function (p) { return p.status;}); + console.log("There are a total of " + activePlayers.length + " players available."); + var possibleMatches = Math.floor(activePlayers.length / 4); + possibleMatches = possibleMatches > activeCourts.length ? activeCourts.length : possibleMatches; + return possibleMatches; +} + +export 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; +} \ No newline at end of file -- cgit v1.3.1 From 05d2f2f943921d6b5c921686c5edce6eb39d23b8 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Thu, 13 Nov 2025 11:58:42 +0100 Subject: Cleaned up the code quite a bit --- .gitlab-ci.yml | 2 +- assets/icon.ipe | 461 -------------------------------------------------------- game.mjs | 188 ++++++++++++----------- icon.png | Bin 0 -> 1453 bytes icon.svg | 41 ++++- index.html | 23 +-- index.js | 10 +- player.mjs | 37 ++++- timer.js | 8 +- utils.mjs | 32 ++-- 10 files changed, 207 insertions(+), 595 deletions(-) delete mode 100644 assets/icon.ipe create mode 100644 icon.png (limited to 'assets') diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 96fb3e5..3926782 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,4 +6,4 @@ create-pages: before_script: - mkdir public script: - - cp index.html index.css icon.svg *.js *.mjs public/ + - cp index.html index.css icon.png *.js *.mjs public/ diff --git a/assets/icon.ipe b/assets/icon.ipe deleted file mode 100644 index d23d37f..0000000 --- a/assets/icon.ipe +++ /dev/null @@ -1,461 +0,0 @@ - - - - - - - -0 0 m --1 0.333 l --1 -0.333 l -h - - - - -0 0 m --1 0.333 l --1 -0.333 l -h - - - - -0 0 m --1 0.333 l --0.8 0 l --1 -0.333 l -h - - - - -0 0 m --1 0.333 l --0.8 0 l --1 -0.333 l -h - - - - -0.6 0 0 0.6 0 0 e -0.4 0 0 0.4 0 0 e - - - - -0.6 0 0 0.6 0 0 e - - - - - -0.5 0 0 0.5 0 0 e - - -0.6 0 0 0.6 0 0 e -0.4 0 0 0.4 0 0 e - - - - - --0.6 -0.6 m -0.6 -0.6 l -0.6 0.6 l --0.6 0.6 l -h --0.4 -0.4 m -0.4 -0.4 l -0.4 0.4 l --0.4 0.4 l -h - - - - --0.6 -0.6 m -0.6 -0.6 l -0.6 0.6 l --0.6 0.6 l -h - - - - - --0.5 -0.5 m -0.5 -0.5 l -0.5 0.5 l --0.5 0.5 l -h - - --0.6 -0.6 m -0.6 -0.6 l -0.6 0.6 l --0.6 0.6 l -h --0.4 -0.4 m -0.4 -0.4 l -0.4 0.4 l --0.4 0.4 l -h - - - - - - --0.43 -0.57 m -0.57 0.43 l -0.43 0.57 l --0.57 -0.43 l -h - - --0.43 0.57 m -0.57 -0.43 l -0.43 -0.57 l --0.57 0.43 l -h - - - - - -0 0 m --1 0.333 l --1 -0.333 l -h - - - - -0 0 m --1 0.333 l --0.8 0 l --1 -0.333 l -h - - - - -0 0 m --1 0.333 l --0.8 0 l --1 -0.333 l -h - - - - --1 0.333 m -0 0 l --1 -0.333 l - - - - -0 0 m --1 0.333 l --1 -0.333 l -h --1 0 m --2 0.333 l --2 -0.333 l -h - - - - -0 0 m --1 0.333 l --1 -0.333 l -h --1 0 m --2 0.333 l --2 -0.333 l -h - - - - -0.5 0 m --0.5 0.333 l --0.5 -0.333 l -h - - - - -0.5 0 m --0.5 0.333 l --0.5 -0.333 l -h - - - - -0.5 0 m --0.5 0.333 l --0.3 0 l --0.5 -0.333 l -h - - - - -0.5 0 m --0.5 0.333 l --0.3 0 l --0.5 -0.333 l -h - - - - -1 0 m -0 0.333 l -0 -0.333 l -h -0 0 m --1 0.333 l --1 -0.333 l -h - - - - -1 0 m -0 0.333 l -0 -0.333 l -h -0 0 m --1 0.333 l --1 -0.333 l -h - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -92 620 m -4 0 0 -4 96 620 100 620 a -100 616 l -92 616 l -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -92 620 m -4 0 0 -4 96 620 100 620 a -100 616 l -92 616 l -h - - - diff --git a/game.mjs b/game.mjs index 45efb81..926d0eb 100644 --- a/game.mjs +++ b/game.mjs @@ -1,5 +1,6 @@ +import { Manager } from "./utils.mjs"; import { CourtManager } from "./court.mjs"; -import { getMatchCount, shuffle, Manager } from "./utils.mjs"; +import { PlayerManager, addLoadBehaviour } from "./player.mjs"; class Team { constructor(playerOne, playerTwo) { @@ -10,27 +11,30 @@ class Team { console.log(this.playerOne + " and " + this.playerTwo); } render() { - return `${this.playerOne}${this.playerTwo}`; + let p1 = `${this.playerOne}`; + let p2 = `${this.playerTwo}`; + return p1 + p2; } } class Game { - constructor(courtId, team_1, team_2) { - this.courtId = courtId; - this.team_1 = team_1; - this.team_2 = team_2; + constructor(id, teamOne, teamTwo, status) { + this.id = id; + this.teamOne = teamOne; + this.teamTwo = teamTwo; + this.status = typeof status !== "undefined" ? status : false; } print() { - let team_1 = this.team_1.playerOne + " and " + this.team_1.playerTwo; - let team_2 = this.team_2.playerOne + " and " + this.team_2.playerTwo; - console.log(team_1 + " is playing against " + team_2); + 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 type = - this.team_1.playerOne === "RESERVED" ? `class="is-danger is-dark"` : ``; + this.teamOne.playerOne === "RESERVED" ? `class="is-danger is-dark"` : ``; return `${ - this.courtId + 1 - }${this.team_1.render()}${this.team_2.render()}`; + this.id + 1 + }${this.teamOne.render()}${this.teamTwo.render()}`; } } @@ -45,10 +49,13 @@ function generateDummyGames(courts, description) { } export class GameManager extends Manager { + courts = new CourtManager(); + players = new PlayerManager(); + constructor() { super("game", []); this.round = Number(localStorage.getItem("round")) || 0; - this.courts = new CourtManager(); + addLoadBehaviour(this.players); console.log("Loading game data."); let data = JSON.parse(localStorage.getItem(this.description)); if (data === null || data.length === 0) { @@ -57,117 +64,130 @@ export class GameManager extends Manager { return; } for (let i = 0; i < data.length; i++) { - let courtId = Number(data[i].courtId); + let id = Number(data[i].id); let teamOne = new Team( - data[i].team_1.playerOne, - data[i].team_1.playerTwo + data[i].teamOne.playerOne, + data[i].teamOne.playerTwo ); let teamTwo = new Team( - data[i].team_2.playerOne, - data[i].team_2.playerTwo + data[i].teamTwo.playerOne, + data[i].teamTwo.playerTwo ); - this.listData.push(new Game(courtId, teamOne, teamTwo)); + let status = Boolean(data[i].status); + this.listData.push(new Game(id, teamOne, teamTwo, status)); } console.log("Loaded " + this.listData.length + " games."); this.render(); this.addDescription(); } + isConfirmed() { + return this.listData[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; + } + console.log(this.listData); this.store(); this.render(); this.addDescription(); } addDescription() { - let description = this.proposed ? "Proposal" : "Round " + this.round; + let description = this.isConfirmed() ? "Round " + this.round : "Proposal"; document.getElementById("game_description").innerHTML = description; } - propose(players) { - console.log("Proposing new games!"); - let possibleMatches = getMatchCount(players, this.courts.listData); - let requiredPlayers = possibleMatches * 4; - console.log("Required players are " + requiredPlayers); - const activeCourts = this.courts.listData.filter(function (c) { - return c.status; - }); - const inactiveCourts = this.courts.listData.filter(function (c) { - return !c.status; - }); - const activePlayers = players.filter(function (p) { - return p.status; - }); + getBlockedGames() { + const inactiveCourts = this.courts.inactive(); console.log("Total reserved courts are " + inactiveCourts.length + "."); - console.log("Total possible matches are " + possibleMatches + "."); - 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, requiredPlayers); - selectedPlayers.sort((a, b) => a.level - b.level); - console.log("Selected players are:"); - console.log( - selectedPlayers - .map((i) => { - return i.name; - }) - .join(",") - ); - console.log("The matches are:"); - this.listData = generateDummyGames( - activeCourts.slice(possibleMatches + 1), - "---" - ); - this.listData = this.listData.concat( - generateDummyGames(inactiveCourts, "RESERVED") - ); - for (let index = 0; index < possibleMatches; index++) { + 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 = selectedPlayers.slice(startIndex, endIndex); + 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(activeCourts[index].id, team_1, team_2); + const game = new Game(courts[index].id, team_1, team_2); game.print(); - this.listData.push(game); + games.push(game); } - this.listData.sort((a, b) => a.courtId - b.courtId); - this.proposed = true; + return games; + } + propose() { + console.log("Proposing new games!"); + let possibleMatches = getMatchCount(this.players.listData, this.courts.listData); + console.log("Total possible matches are " + possibleMatches + "."); + this.listData = this.getBlockedGames(); + const activeCourts = this.courts.active(); + const unusedCourts = activeCourts.slice(possibleMatches + 1); + this.listData = this.listData.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.active(usedCourts, selectedPlayers) + ); + this.listData.sort((a, b) => a.id - b.id); this.render(); this.addDescription(); } confirm() { - this.proposed = false; + 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.listData.length; i++) { + this.listData[i].status = true; + } // Need to update the number of games played here this.store(); this.addDescription(); document.getElementById("timer-start").click(); - const notification = new Notyf({ - duration: 30 * 1000, - position: { x: "center", y: "top" }, - types: [ - { - type: "success", - background: "green", - duration: 1000 * 1000, - dismissible: true, - }, - ], - }); + const notification = new Notyf(); notification.success("Round confirmed!"); } reset() { - localStorage.clear("round"); - localStorage.clear("round"); + 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(regulars, courts) { + const activeCourts = courts.filter(function (c) { + return c.status; + }); + console.log( + "There are a total of " + activeCourts.length + " courts available." + ); + console.log(regulars); + const activePlayers = regulars.filter(function (p) { + return p.status; + }); + console.log( + "There are a total of " + activePlayers.length + " players available." + ); + var possibleMatches = Math.floor(activePlayers.length / 4); + possibleMatches = + possibleMatches > activeCourts.length + ? activeCourts.length + : possibleMatches; + return possibleMatches; +} diff --git a/icon.png b/icon.png new file mode 100644 index 0000000..655915f Binary files /dev/null and b/icon.png differ diff --git a/icon.svg b/icon.svg index d2700a4..278a070 100644 --- a/icon.svg +++ b/icon.svg @@ -1,5 +1,33 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -12,4 +40,15 @@ + + + + + + + + + + + diff --git a/index.html b/index.html index 35d1b15..b3f2f0a 100644 --- a/index.html +++ b/index.html @@ -13,9 +13,12 @@