Skip to content

Commit

Permalink
Chat messages
Browse files Browse the repository at this point in the history
  • Loading branch information
muktar1907 committed May 1, 2024
1 parent e469266 commit 32a7084
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
33 changes: 24 additions & 9 deletions html/oldIndex.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ <h1>Welcome to The Word Search Game</h1>
<form id="nickForm">
<label for="nickInput">Enter your nickname:</label>
<input type="text" id="nickInput" placeholder="Nickname" required>
<button type="submit">Enter Lobby</button>
<p id="error" style="color: red; display: none;">Your nick is already taken, please reenter.</p>
</form>
</div>
Expand Down Expand Up @@ -91,15 +90,15 @@ <h1>The Word Search Game</h1>
</div>
<div id="gameListSection">
<p style="display: inline;">Game1</p>
<div class="Room" id="Game1" style="display: block;">text</div>
<div class="Room" id="Game1" style="display: block;"></div>
<p style="display: inline;">Game2</p>
<div class="Room" id="Game2" style="display: block;">text</div>
<div class="Room" id="Game2" style="display: block;"></div>
<p style="display: inline;">Game3</p>
<div class="Room" id="Game3" style="display: block;">text</div>
<div class="Room" id="Game3" style="display: block;"></div>
<p style="display: inline;">Game4</p>
<div class="Room" id="Game4" style="display: block;">text</div>
<div class="Room" id="Game4" style="display: block;"></div>
<p style="display: inline;">Game5</p>
<div class="Room" id="Game5" style="display: block;">text</div>
<div class="Room" id="Game5" style="display: block;"></div>
</div>


Expand Down Expand Up @@ -157,6 +156,8 @@ <h2>Leaderboard</h2>
var attemptArr;//array of cell numbers in an attempt
var score;
var playerColor="";
var message="";
var allMessages=new Array();
socket.onopen = function(evt) {
console.log("Open");
requestGameList();
Expand Down Expand Up @@ -254,11 +255,16 @@ <h2>Leaderboard</h2>
document.getElementsByClassName("Room").innerHTML="";
for(i=0;i<keys.length;i++)
{
lobbyInfo = "Slots filled: \n"

document.getElementById(values[i]).innerHTML=keys[i]+"\n";
}

}
else if(type==="message")
{
allMessages.push(messageData.player+": "+ messageData.message);
console.log(allMessages);
}

};

Expand Down Expand Up @@ -309,7 +315,6 @@ <h2>Leaderboard</h2>
console.log("GameId after confirmation: "+gameId);
var mode = document.getElementById('modeSelect').value;
playerColor = document.getElementById('playerColor').value;

player.nick=nick;

if (!nick)
Expand All @@ -322,7 +327,7 @@ <h2>Leaderboard</h2>
alert('Please select a game and mode.');
return;
}

sendChat(nick,document.getElementById('nickInput').value.trim());
joinGame(nick, gameIndex, mode);

});
Expand Down Expand Up @@ -539,5 +544,15 @@ <h2>Leaderboard</h2>
socket.send(JSON.stringify(data));

}

function sendChat(nick,message)
{
var data={
type: "message",
message: message,
player: nick
}
socket.send(JSON.stringify(data));
}
</script>

14 changes: 12 additions & 2 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,15 @@ public void toLobby() {
// Logic for navigating to the lobby
}

public void globalChat() {
// Logic for handling global chat
public void globalChat(Gson gson, JsonObject object)
{
// Logic for handling local chat
JsonObject jsonObject=new JsonObject();
jsonObject.addProperty("type","message");
jsonObject.addProperty("message",object.get("message").getAsString());
jsonObject.addProperty("player",object.get("player").getAsString());

broadcast(jsonObject.toString());
}

public void getPlayerColor() {
Expand Down Expand Up @@ -507,6 +514,9 @@ public void messageHandler(Gson gson, String jsonString, WebSocket conn)
case("updateLobby"):
updateLobby(gson,jsonObject);
break;
case("message"):
globalChat(gson,object);
break;
default:
System.out.println("Unexpected message");

Expand Down

0 comments on commit 32a7084

Please sign in to comment.