From cf7ef7350ecd63924fad5f6b58cd3a25531e197a Mon Sep 17 00:00:00 2001 From: spj2401Dev Date: Fri, 19 Jul 2024 21:16:38 +0200 Subject: [PATCH] #28 --- client/pages/setWords/index.html | 5 +++++ client/pages/setWords/script.js | 2 ++ src/controller/photoController.mjs | 6 ++++++ src/controller/wordController.mjs | 2 ++ src/models/game.mjs | 1 + src/services/playerService.mjs | 6 +++--- 6 files changed, 19 insertions(+), 3 deletions(-) diff --git a/client/pages/setWords/index.html b/client/pages/setWords/index.html index 55fb75f..fd3ae17 100644 --- a/client/pages/setWords/index.html +++ b/client/pages/setWords/index.html @@ -36,6 +36,11 @@

Set Prompts

+
+ + +
+
diff --git a/client/pages/setWords/script.js b/client/pages/setWords/script.js index 3ab1e43..3dd4a29 100644 --- a/client/pages/setWords/script.js +++ b/client/pages/setWords/script.js @@ -72,6 +72,7 @@ function submitPrompts() { const promptInputs = promptContainer.getElementsByTagName("input"); const time = document.getElementById("duration").value; const votes = document.getElementById("votes").value; + const penalty = document.getElementById("penalty").checked; const prompts = []; if (time < 1) { @@ -108,6 +109,7 @@ function submitPrompts() { wordsPerPlayer: wordsPerPlayer, time: time, votesPerPlayer: votes, + penalty: penalty, }; fetch("/words", { diff --git a/src/controller/photoController.mjs b/src/controller/photoController.mjs index b2ecb02..7fb5f01 100644 --- a/src/controller/photoController.mjs +++ b/src/controller/photoController.mjs @@ -3,10 +3,12 @@ import { AddImageToPlayer, GetFullPlayers, DeclinePhoto, + AddVoteToWord } from "../services/playerService.mjs"; import path from "path"; import webSocketService from "../services/webSocketService.mjs"; import fs from 'fs'; +import { game } from "../models/game.mjs"; const uploadDir = "data/photos/"; @@ -109,6 +111,10 @@ export const DeclinePhotoController = async (req, res) => { const word = req.body.word; DeclinePhoto(playername, word); + if (game.removePoints) { + AddVoteToWord(word, playername, -1); + } + webSocketService.broadcast("Decline"); res.status(200).send("Photo declined."); diff --git a/src/controller/wordController.mjs b/src/controller/wordController.mjs index 63ebbea..01901c3 100644 --- a/src/controller/wordController.mjs +++ b/src/controller/wordController.mjs @@ -13,6 +13,7 @@ export const setWords = async (req, res) => { wordsPerPlayer = req.body.wordsPerPlayer; var time = req.body.time; // Time in Minutes var votes = req.body.votesPerPlayer; + var penalty = req.body.penalty; if (words.length + wordlist.length < wordsPerPlayer) { return res.status(400).json({ @@ -28,6 +29,7 @@ export const setWords = async (req, res) => { userWordList = words; game.time = time; game.votesPerPlayer = votes; + game.removePoints = penalty; await changeGameStatus(GameStatus.STARTING) webSocketService.broadcast('Words'); diff --git a/src/models/game.mjs b/src/models/game.mjs index d7de383..7f5a89a 100644 --- a/src/models/game.mjs +++ b/src/models/game.mjs @@ -7,4 +7,5 @@ export const game = { endTime: new Date(), words: [], votesPerPlayer: 0, + removePoints: false, // If true, one point is removed from the player if one of their pictures gets deleted by an Admin }; diff --git a/src/services/playerService.mjs b/src/services/playerService.mjs index aa22c37..bf15f7b 100644 --- a/src/services/playerService.mjs +++ b/src/services/playerService.mjs @@ -109,7 +109,7 @@ export function GetWinner() { players.forEach((p) => { if (!rankList.some((player) => player.player === p.name)) { let completedPhotos = p.words.filter((w) => w.completed === true); - let votesScore = completedPhotos.reduce((acc, word) => acc + word.votes, 0); + let votesScore = p.words.reduce((acc, word) => acc + word.votes, 0); let totalScore = completedPhotos.length + votesScore; rankList.push({ @@ -157,7 +157,7 @@ export function VoteForPlayer(playername) { } } -export function AddVoteToWord(word, playername) { +export function AddVoteToWord(word, playername, amount = 1) { let player = players.find((p) => p.name === playername); if (!player) { return; @@ -166,7 +166,7 @@ export function AddVoteToWord(word, playername) { if (!targetWord) { return; } - targetWord.votes += 1; + targetWord.votes += amount; } export function getVoteAmount(playername) {