-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a lobby class and added a lobby to the html file
- Loading branch information
1 parent
a7d3251
commit 1fee75e
Showing
3 changed files
with
134 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,112 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
<label for="username">Username:</label> | ||
<input type="text" id="username" maxlength="20"> | ||
<button id="joinButton">Join Game</button> | ||
<div id="lobbyStatus">Waiting for players...</div> | ||
<div id="playersInLobby">Players in Lobby:</div> | ||
<label id="topMessage"></label> | ||
<table id="wordSearchBoard" style="display:none;"></table> | ||
<label id="bottomMessage"></label> | ||
<title>Multiplayer Word Search Game</title> | ||
<style> | ||
/* Add your CSS styles here */ | ||
body { | ||
font-family: Arial, sans-serif; | ||
background-color: #f0f0f0; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
|
||
<script> | ||
var gameId = -1; | ||
class UserEvent { | ||
Button = -1; | ||
Players = 0; | ||
GameId = 0; | ||
} | ||
#lobby-container { | ||
text-align: center; | ||
margin-top: 50px; | ||
} | ||
|
||
var connection = null; | ||
var serverUrl = "ws://" + window.location.hostname + ":9108"; | ||
connection = new WebSocket(serverUrl); | ||
|
||
connection.onopen = function () { | ||
console.log("Connected to server"); | ||
}; | ||
label { | ||
display: block; | ||
margin-bottom: 10px; | ||
} | ||
|
||
connection.onclose = function () { | ||
document.getElementById("topMessage").innerHTML = "Server Offline"; | ||
}; | ||
input[type="text"] { | ||
padding: 8px; | ||
font-size: 16px; | ||
border: 1px solid #ccc; | ||
border-radius: 5px; | ||
} | ||
|
||
connection.onmessage = function (evt) { | ||
var msg = JSON.parse(evt.data); | ||
if (msg.action === "updateLobby") { | ||
updateLobby(msg.players); | ||
document.getElementById("lobbyStatus").innerHTML = "Waiting for other players..."; | ||
button { | ||
padding: 10px 20px; | ||
font-size: 18px; | ||
background-color: #007bff; | ||
color: #fff; | ||
border: none; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
margin-top: 10px; | ||
} | ||
}; | ||
|
||
function updateLobby(players) { | ||
var playersList = "Players in Lobby: " + players.join(", "); | ||
document.getElementById("playersInLobby").innerHTML = playersList; | ||
|
||
} | ||
button:hover { | ||
background-color: #0056b3; | ||
} | ||
|
||
#lobby-status { | ||
margin-top: 20px; | ||
font-size: 18px; | ||
} | ||
|
||
document.getElementById("joinButton").addEventListener("click", function() { | ||
var username = document.getElementById("username").value.trim(); | ||
if (username) { | ||
var joinRequest = { | ||
action: "join", | ||
username: username | ||
}; | ||
connection.send(JSON.stringify(joinRequest)); | ||
document.getElementById("lobbyStatus").innerHTML = "Trying to join the game..."; | ||
} else { | ||
alert("Please enter a username."); | ||
#players-in-lobby { | ||
margin-top: 20px; | ||
font-size: 18px; | ||
} | ||
}); | ||
</style> | ||
</head> | ||
<body> | ||
<div id="lobby-container"> | ||
<label for="username">Username:</label> | ||
<input type="text" id="username" maxlength="20"> | ||
<button id="joinButton">Join Game</button> | ||
<div id="lobby-status">Waiting for players...</div> | ||
<div id="players-in-lobby">Players in Lobby:</div> | ||
<label id="topMessage"></label> | ||
<table id="wordSearchBoard" style="display:none;"></table> | ||
<label id="bottomMessage"></label> | ||
</div> | ||
|
||
<script> | ||
var gameId = -1; | ||
var connection = null; | ||
var serverUrl = "ws://" + window.location.hostname + ":9108"; | ||
connection = new WebSocket(serverUrl); | ||
|
||
connection.onopen = function () { | ||
console.log("Connected to server"); | ||
}; | ||
|
||
</script> | ||
</body> | ||
connection.onclose = function () { | ||
document.getElementById("topMessage").innerHTML = "Server Offline"; | ||
}; | ||
|
||
connection.onmessage = function (evt) { | ||
var msg = JSON.parse(evt.data); | ||
if (msg.action === "updateLobby") { | ||
updateLobby(msg.players); | ||
document.getElementById("lobby-status").innerHTML = "Waiting for other players..."; | ||
} | ||
}; | ||
|
||
function updateLobby(players) { | ||
var playersList = "Players in Lobby: " + players.join(", "); | ||
document.getElementById("players-in-lobby").innerHTML = playersList; | ||
} | ||
|
||
document.getElementById("joinButton").addEventListener("click", function() { | ||
var username = document.getElementById("username").value.trim(); | ||
if (username) { | ||
var joinRequest = { | ||
action: "join", | ||
username: username | ||
}; | ||
connection.send(JSON.stringify(joinRequest)); | ||
document.getElementById("lobby-status").innerHTML = "Trying to join the game..."; | ||
} else { | ||
alert("Please enter a username."); | ||
} | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package uta.cse3310; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Lobby { | ||
private List<PlayerType> players; | ||
|
||
public Lobby() { | ||
this.players = new ArrayList<>(); | ||
} | ||
|
||
// Method to add a player to the lobby | ||
public void addPlayer(PlayerType player) { | ||
players.add(player); | ||
} | ||
|
||
// Method to remove a player from the lobby | ||
public void removePlayer(PlayerType player) { | ||
players.remove(player); | ||
} | ||
|
||
// Method to check if the lobby is full | ||
public boolean isFull() { | ||
return players.size() >= 4; // Maximum of 4 players | ||
} | ||
|
||
// Method to get the list of players in the lobby | ||
public List<PlayerType> getPlayers() { | ||
return players; | ||
} | ||
|
||
// Method to reset the lobby | ||
public void reset() { | ||
players.clear(); | ||
} | ||
} |
Binary file not shown.