From afbf39be530e8f0aef051187a062465d427dc628 Mon Sep 17 00:00:00 2001 From: muktar1907 Date: Tue, 30 Apr 2024 04:00:29 -0500 Subject: [PATCH] Show changes made by other players --- html/index_1.html | 40 +++++++++++++++++++++--------- src/main/java/uta/cse3310/App.java | 25 +++++-------------- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/html/index_1.html b/html/index_1.html index 5b9dfb9..a85661c 100644 --- a/html/index_1.html +++ b/html/index_1.html @@ -71,10 +71,20 @@

Timer: 30 seconds

+
+ + +

The Word Search Game

-
@@ -134,6 +144,7 @@

Leaderboard

player=""; cell=-1; action=-1; + playerColor=""; } @@ -144,6 +155,7 @@

Leaderboard

var clicked=0;//helps with selection of letters var attemptArr;//array of cell numbers in an attempt var score; + var playerColor=""; socket.onopen = function(evt) { console.log("Open"); requestGameList(); @@ -154,6 +166,7 @@

Leaderboard

let messageData = JSON.parse(evt.data); console.log("Message received: " + messageData); const type= messageData.type; + console.log(messageData.color); if(type==="JoinGame") { let game; @@ -161,6 +174,7 @@

Leaderboard

let playerData; console.log(nick+"has joined"); + console.log("My color is: "+playerColor); console.log(messageData.ready==="true"); console.log("My game ID: "+ gameId +"\ngame joined: "+ messageData.gameId); if("ready" in messageData && messageData.ready==="true" && messageData.gameId==gameId) @@ -171,7 +185,7 @@

Leaderboard

{ //store all letters inside grid variable grid=JSON.parse(messageData.grid); - + } document.getElementById("lobbyContainer").style.display="none"; @@ -191,11 +205,12 @@

Leaderboard

if(messageData.valid==="true") { score=messageData.score; + player.score=score; console.log(messageData.score); attemptArr= JSON.parse(messageData.attempt); for(let i=0;iLeaderboard console.log("Game id from options: "+gameId); console.log("GameId after confirmation: "+gameId); var mode = document.getElementById('modeSelect').value; + playerColor = document.getElementById('playerColor').value; + player.nick=nick; + if (!nick) { alert('Please enter a nickname.'); @@ -387,7 +405,7 @@

Leaderboard

{ buttonEvent(this.getAttribute("id"),0); - document.getElementById(this.getAttribute("id")).style.backgroundColor="blue"; + document.getElementById(this.getAttribute("id")).style.backgroundColor=playerColor; clicked++; console.log("clicked="+clicked); }); @@ -396,14 +414,14 @@

Leaderboard

if(clicked>0) { buttonEvent(this.getAttribute("id"),1); - document.getElementById(this.getAttribute("id")).style.backgroundColor="blue"; + document.getElementById(this.getAttribute("id")).style.backgroundColor=playerColor; } }); cells[i].addEventListener("mouseup",function() { buttonEvent(this.getAttribute("id"),2); - document.getElementById(this.getAttribute("id")).style.backgroundColor="blue"; + document.getElementById(this.getAttribute("id")).style.backgroundColor=playerColor; clicked--; console.log("clicked="+clicked); }); @@ -414,22 +432,20 @@

Leaderboard

function buttonEvent(cell,action) { let button= document.getElementById(cell); - //button.style.backgroundColor="blue"; + U = new UserEvent(); U.cell=parseInt(cell); U.action=action; U.player=player; U.gameId=gameId; + U.playerColor= playerColor; var data = { type: "UpdateGame", events: U }; - if(socket.readyState===1) - { - socket.send(JSON.stringify(data)); - } - console.log(JSON.stringify(U)); + socket.send(JSON.stringify(data)); + console.log(JSON.stringify(data)); resetTimer(); } diff --git a/src/main/java/uta/cse3310/App.java b/src/main/java/uta/cse3310/App.java index e954b8d..d4e7ae6 100644 --- a/src/main/java/uta/cse3310/App.java +++ b/src/main/java/uta/cse3310/App.java @@ -129,7 +129,7 @@ public void onClose(WebSocket conn, int code, String reason, boolean remote) { @Override public void onMessage(WebSocket conn, String message) { // Logic for handling websocket message event - System.out.println(conn +": " +message); + //System.out.println(conn +": " +message); //Bring in data from the webpage //Take in userEvents and make into an arrayList @@ -218,7 +218,7 @@ public void updateLobby(Gson gson,JsonObject jsonObject) { { for(String part : mapParts) { - if(part!="") + if(part!="" && part!=null) { String[] temp = part.split(":"); keys[i]=temp[0].trim(); @@ -480,7 +480,7 @@ public void messageHandler(Gson gson, String jsonString, WebSocket conn) j++; } String jsonGameList= gson.toJson(activeGameArr.toString()); - System.out.println(activeGameArr.toString()); + //System.out.println(activeGameArr.toString()); jsonObject.addProperty("gameList",jsonGameList); conn.send(jsonObject.toString()); break; @@ -499,21 +499,6 @@ public void messageHandler(Gson gson, String jsonString, WebSocket conn) break; case("updateLobby"): updateLobby(gson,jsonObject); - /*ArrayListlobbyData=new ArrayList(); - ArrayList players=new ArrayList(); - for(Game i : activeGames) - { - gameId=i.getGameID(); - lobbyData.add(gameId); - players=i.getPlayersList(); - for(Player j :players) - { - lobbyData.add(j.getNick()); - } - } - jsonObject.addProperty("type","updateLobby"); - jsonObject.addProperty("lobbyData",lobbyData.toString()); - broadcast(jsonObject.toString());*/ break; default: System.out.println("Unexpected message"); @@ -529,7 +514,8 @@ public void messageHandler(Gson gson, String jsonString, WebSocket conn) public void updateHandler(Gson gson, JsonObject message, WebSocket conn) { String evtMessage=message.toString(); - System.out.println(evtMessage); + System.out.println("Evt Message"+evtMessage); + UserEvent U = gson.fromJson(message,UserEvent.class);//turn message into userEvent instance U.action = Integer.valueOf(message.get("action").getAsString()); U.cell = Integer.valueOf(message.get("cell").getAsString()); @@ -580,6 +566,7 @@ public void updateHandler(Gson gson, JsonObject message, WebSocket conn) jsonObject.addProperty("attempt",gson.toJson(cells)); jsonObject.addProperty("valid",String.valueOf(valid)); jsonObject.addProperty("score",String.valueOf(U.player.getScore())); + jsonObject.addProperty("color",message.get("playerColor").getAsString()); conn.send(jsonObject.toString()); //search through the game's player list ArrayList playerList=new ArrayList();