Skip to content

Commit

Permalink
Show changes made by other players
Browse files Browse the repository at this point in the history
  • Loading branch information
muktar1907 committed Apr 30, 2024
1 parent 4be41c6 commit afbf39b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
40 changes: 28 additions & 12 deletions html/index_1.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,20 @@ <h2>Timer: <span id="timer">30</span> seconds</h2>

</select>
</div>
<div>
<label for="playerColor">Player Color:</label>
<select id="playerColor">

<option value="red">red</option>
<option value="yellow">yellow</option>
<option value="purple">purple</option>
<option value="gold">gold</option>
<option value="pink">pink</option>
</select>
</div>
<div>
<button id="confirmButton" >CONFIRM</button>
<h1>The Word Search Game</h1>
<button id="startButton">START</button>
<button id="leaveButton">LEAVE</button>
<button id="refreshButton">Refresh</button>
</div>
Expand Down Expand Up @@ -134,6 +144,7 @@ <h2>Leaderboard</h2>
player="";
cell=-1;
action=-1;
playerColor="";
}


Expand All @@ -144,6 +155,7 @@ <h2>Leaderboard</h2>
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();
Expand All @@ -154,13 +166,15 @@ <h2>Leaderboard</h2>
let messageData = JSON.parse(evt.data);
console.log("Message received: " + messageData);
const type= messageData.type;
console.log(messageData.color);
if(type==="JoinGame")
{
let game;
let gameData;
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)
Expand All @@ -171,7 +185,7 @@ <h2>Leaderboard</h2>
{
//store all letters inside grid variable
grid=JSON.parse(messageData.grid);


}
document.getElementById("lobbyContainer").style.display="none";
Expand All @@ -191,11 +205,12 @@ <h2>Leaderboard</h2>
if(messageData.valid==="true")
{
score=messageData.score;
player.score=score;
console.log(messageData.score);
attemptArr= JSON.parse(messageData.attempt);
for(let i=0;i<attemptArr.length;i++)
{
document.getElementById(attemptArr[i].toString()).style.backgroundColor="red";
document.getElementById(attemptArr[i].toString()).style.backgroundColor=messageData.color;

}
}
Expand Down Expand Up @@ -287,7 +302,10 @@ <h2>Leaderboard</h2>
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.');
Expand Down Expand Up @@ -387,7 +405,7 @@ <h2>Leaderboard</h2>
{

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);
});
Expand All @@ -396,14 +414,14 @@ <h2>Leaderboard</h2>
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);
});
Expand All @@ -414,22 +432,20 @@ <h2>Leaderboard</h2>
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();
}
Expand Down
25 changes: 6 additions & 19 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -499,21 +499,6 @@ public void messageHandler(Gson gson, String jsonString, WebSocket conn)
break;
case("updateLobby"):
updateLobby(gson,jsonObject);
/*ArrayList<String>lobbyData=new ArrayList<String>();
ArrayList<Player> players=new ArrayList<Player>();
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");
Expand All @@ -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());
Expand Down Expand Up @@ -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<Player> playerList=new ArrayList<Player>();
Expand Down

0 comments on commit afbf39b

Please sign in to comment.