From 29cd7f1b20852a032e59e5c0faa8e526bf52231d Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Sun, 9 May 2021 16:19:23 +0100 Subject: [PATCH] completely changed how that bad boy works --- client/public/scrabble/game.js | 40 ++++++++++++++++----- client/public/scrabble/index.html | 2 +- client/public/scrabble/network.js | 3 +- client/public/scrabble/ui.js | 59 +++++++++++++++++++------------ 4 files changed, 71 insertions(+), 33 deletions(-) diff --git a/client/public/scrabble/game.js b/client/public/scrabble/game.js index 98611ca..9a25e83 100644 --- a/client/public/scrabble/game.js +++ b/client/public/scrabble/game.js @@ -9,14 +9,16 @@ function computeTurn() GAMEUSERS OBJECT { uid: uid, - username: name, - turn: bool, - + name: name, + score: int, + me: bool, + turn: bool } NOTES - In play order */ -let Users = {}; +let Users = []; +// just shorthand, so long as i remember to keep it updated lmao let MyTurn = false; function initGame(boardstate, tileset, myplayer, players) @@ -49,12 +51,27 @@ function initGame(boardstate, tileset, myplayer, players) } addPiecesToDrawer(drawerStructure); + // populate Users + for (player of players) + { + Users.push({ + uid: player.uid, + name: player.name, + score: player.score, + me: myplayer.uid === player.uid, + turn: false + }); + } + Users[0].turn = true; + if (Users[0].me) + MyTurn = true; + + console.log(Users, MyTurn); + // construct UI initUI(); - - console.log(players); + setupUsersUI(Users, 0); - setupUsersUI(players, 0); return true; } @@ -64,11 +81,16 @@ function startMyTurn() } -function +function startOthersTurn(useruid) +{ + +} function playMyTurn(stagedpieces) { - if (!MyTurn) return; + if (!MyTurn) return false; + + return true; } function processOthersTurn() diff --git a/client/public/scrabble/index.html b/client/public/scrabble/index.html index bf8ed16..6fa0392 100644 --- a/client/public/scrabble/index.html +++ b/client/public/scrabble/index.html @@ -342,11 +342,11 @@ + - \ No newline at end of file diff --git a/client/public/scrabble/network.js b/client/public/scrabble/network.js index 4b115a5..97ba789 100644 --- a/client/public/scrabble/network.js +++ b/client/public/scrabble/network.js @@ -210,7 +210,8 @@ function onGameBegin(socket, args) { ConnectionState.forEach(e => { e.innerHTML = localeString('error-game-begin'); - }); return; + }); + return; } const status = initGame(boardstate, tileset, myplayer, players); diff --git a/client/public/scrabble/ui.js b/client/public/scrabble/ui.js index a3607e1..d752b0b 100644 --- a/client/public/scrabble/ui.js +++ b/client/public/scrabble/ui.js @@ -20,7 +20,7 @@ function initUI() ChatBox.value = ''; } -const UserUIReplacer = (p, n, s) => `
+const UserUIReplacer = (p, u, n, s) => `
${n}
Score:
${s}
`; @@ -31,7 +31,7 @@ function setupUsersUI(users, turn) let elements = []; for (const user in users) { - elements.push(UserUIReplacer(user, users[user].name, users[user].score)); + elements.push(UserUIReplacer(user, users[user].uid, users[user].name, users[user].score)); } IPlayerScores.forEach(e => { e.innerHTML += elements.join(''); @@ -39,28 +39,35 @@ function setupUsersUI(users, turn) console.log(users[turn].uid, JSON.parse(sessionStorage.getItem('user')).uid) - if (MyTurn) - { - document.querySelectorAll(`.p${turn}`).forEach(e => { - e.classList.toggle('myturn'); - }); - } else - { - document.querySelectorAll(`.p${turn}`).forEach(e => { - e.classList.toggle('theirturn'); - }); - } + updateUsersUI(); } -function updateUsersUI(users, turn) +// takes users object from +function updateUsersUI(users) { - -} - -function changeTurn() -{ - if (MyTurn) { - + for (const user of Users) + { + if (user.turn && user.me) + { + document.querySelectorAll(`.player${user.uid}`).forEach(e => { + e.classList.add('myturn'); + }); + } else if (user.turn) + { + document.querySelectorAll(`.player${user.uid}`).forEach(e => { + e.classList.add('theirturn'); + }); + } else + { + document.querySelectorAll(`.player${user.uid}`).forEach(e => { + if (e.classList.contains('myturn')) + e.classList.remove('myturn'); + if (e.classList.contains('myturnprocess')) + e.classList.remove('myturnprocess'); + if (e.classList.contains('theirturn')) + e.classList.remove('theirturn'); + }); + } } } @@ -95,7 +102,15 @@ function onPlayTurn() { // get all staged pieces const stagedPieces = getAllStagedPieces(); - playMyTurn(stagedPieces); + const status = playMyTurn(stagedPieces); + + if (!status) + { + alert('Invalid turn!') + } else + { + // switch state to processing + } } function onMessageSend()