Skip to content

Commit

Permalink
Merge pull request #26 from utastudents/main
Browse files Browse the repository at this point in the history
update
  • Loading branch information
adrianm104 authored May 1, 2024
2 parents 6b20ddf + 548a53e commit e4794ae
Show file tree
Hide file tree
Showing 10 changed files with 466 additions and 424 deletions.
119 changes: 68 additions & 51 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ <h1>The Word Search Game</h1>
Button = -1;
PlayerIdx = 0;
GameId = 0;
GameMode = 0;
Command = "User"; // User is default, "Grid" to get the grid
}
var connection = null;
Expand All @@ -109,14 +110,13 @@ <h1>The Word Search Game</h1>
selectedLetters += cell.textContent.trim();
});
var highlightedLetters = {
type: "word-selection",
text: selectedLetters,
username: document.getElementById("usernameInput").value,
coordinates: [startPoint, endPoint]
};
type: "word-selection",
text: selectedLetters,
username: document.getElementById("usernameInput").value,
coordinates: [startPoint, endPoint]
};
connection.send(JSON.stringify(highlightedLetters)); // Send the username to the server
console.log(JSON.stringify(highlightedLetters));

}

function getRowCol(index) {
Expand Down Expand Up @@ -260,6 +260,7 @@ <h1>The Word Search Game</h1>
}

var username = document.getElementById("usernameInput").value;
// p[username] = p.index;
if (username.trim() !== "") {
// Check if the username is already taken
var usernames = document.querySelectorAll("#waiting-players-list li");
Expand Down Expand Up @@ -295,56 +296,67 @@ <h1>The Word Search Game</h1>

// Listen for messages from the server
connection.onmessage = function (event) {
var message = JSON.parse(event.data);
var message = JSON.parse(event.data);

if (message.type === "wordCoordinates"){
console.log("[" + message.coordinates[0] + "] [" + message.coordinates[1] +"]");
console.log("Start idx: " + getIdx(message.coordinates[0]) + " End idx: " + getIdx(message.coordinates[1]));
selectRange(getIdx(message.coordinates[0]), getIdx(message.coordinates[1]), "used");
if ('YouAre' in message) {
if (message.YouAre == "PLAYERONE") {
PlayerIdx = 0;
}

console.log("Message received: " + event.data);
if ("PlayerUserNames" in message) {
var playerUserNames = message["PlayerUserNames"];

for (var i = 0; i < playerUserNames.length; i++) {
addPlayerToList(playerUserNames[i]);
}
else {
PlayerIdx = 1;
}

if (message.type === "chat") {
addChatMessage(message.text, message.username);
} else if (message.type === "new_player") {
var playerName = message.username;
addPlayerToList(playerName);
}
gameid = message.GameId;
}

if ("ver" in message) {
document.title = "TWSG " + message.ver;
}
if (message.type === "wordCoordinates"){
console.log("[" + message.coordinates[0] + "] [" + message.coordinates[1] +"]");
console.log("Start idx: " + getIdx(message.coordinates[0]) + " End idx: " + getIdx(message.coordinates[1]));
selectRange(getIdx(message.coordinates[0]), getIdx(message.coordinates[1]), "used");
}

console.log("Message received: " + event.data);
if ("PlayerUserNames" in message) {
var playerUserNames = message["PlayerUserNames"];

for (var i = 0; i < playerUserNames.length; i++) {
addPlayerToList(playerUserNames[i]);
}
}

if (document.getElementById('grid').childNodes.length <= 0) {
if(message.wordGrid != null && message.wordGrid.Grid.length > 0) {
var size = message.wordGrid.Grid.length;
const grid = document.getElementById('grid');
if (message.type === "chat") {
addChatMessage(message.text, message.username);
} else if (message.type === "new_player") {
var playerName = message.username;
addPlayerToList(playerName);
}

if ("ver" in message) {
document.title = "TWSG " + message.ver;
}

// create cells in the format <div class="cell">A</div>
grid.innerHTML = "";
for(var row=0; row<size; row++)
if (document.getElementById('grid').childNodes.length <= 0) {
if(message.wordGrid != null && message.wordGrid.Grid.length > 0) {
var size = message.wordGrid.Grid.length;
const grid = document.getElementById('grid');

// create cells in the format <div class="cell">A</div>
grid.innerHTML = "";
for(var row=0; row<size; row++)
{
for(var col=0; col<size; col++)
{
for(var col=0; col<size; col++)
{
var temp = message.wordGrid.Grid[row][col];
const cell = document.createElement('div')
cell.classList.add('cell')
cell.textContent = temp;
// cell.textContent = characters.charAt((i + j) % charactersLength); // A-Z in order
grid.appendChild(cell);
}
var temp = message.wordGrid.Grid[row][col];
const cell = document.createElement('div')
cell.classList.add('cell')
cell.textContent = temp;
// cell.textContent = characters.charAt((i + j) % charactersLength); // A-Z in order
grid.appendChild(cell);
}
selectCells();
}
selectCells();
}
}
};

function addPlayerToList(playerName){
Expand All @@ -359,9 +371,6 @@ <h1>The Word Search Game</h1>
}
}

function gridStart(){

}
</script>
</div>

Expand Down Expand Up @@ -442,9 +451,17 @@ <h3 style="margin-top: 0px">Score</h3>

<script>
function selectPlayers(numPlayers) {
console.log(numPlayers + " players selected.");
document.getElementById("lobby-page").style.display = "none";
document.getElementById("game-page").style.display = "block"; // Show game
console.log(numPlayers + " players selected.");

var selectPlayerNum = {
type: "num-players",
text: numPlayers
};
connection.send(JSON.stringify(selectPlayerNum));
console.log(JSON.stringify(selectPlayerNum));

document.getElementById("lobby-page").style.display = "none";
document.getElementById("game-page").style.display = "block"; // Show game
}
</script>
</div>
Expand Down
20 changes: 10 additions & 10 deletions html/tttsample.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@
if (gameid == obj.GameId) {
// button state to display values

document.getElementById("b1").value = ButtonStateToDisplay.get(obj.Button[0]);
document.getElementById("b2").value = ButtonStateToDisplay.get(obj.Button[1]);
document.getElementById("b3").value = ButtonStateToDisplay.get(obj.Button[2]);
document.getElementById("b4").value = ButtonStateToDisplay.get(obj.Button[3]);
document.getElementById("b5").value = ButtonStateToDisplay.get(obj.Button[4]);
document.getElementById("b6").value = ButtonStateToDisplay.get(obj.Button[5]);
document.getElementById("b7").value = ButtonStateToDisplay.get(obj.Button[6]);
document.getElementById("b8").value = ButtonStateToDisplay.get(obj.Button[7]);
document.getElementById("b9").value = ButtonStateToDisplay.get(obj.Button[8]);
// document.getElementById("b1").value = ButtonStateToDisplay.get(obj.Button[0]);
// document.getElementById("b2").value = ButtonStateToDisplay.get(obj.Button[1]);
// document.getElementById("b3").value = ButtonStateToDisplay.get(obj.Button[2]);
// document.getElementById("b4").value = ButtonStateToDisplay.get(obj.Button[3]);
// document.getElementById("b5").value = ButtonStateToDisplay.get(obj.Button[4]);
// document.getElementById("b6").value = ButtonStateToDisplay.get(obj.Button[5]);
// document.getElementById("b7").value = ButtonStateToDisplay.get(obj.Button[6]);
// document.getElementById("b8").value = ButtonStateToDisplay.get(obj.Button[7]);
// document.getElementById("b9").value = ButtonStateToDisplay.get(obj.Button[8]);


// the message line
document.getElementById("topMessage").innerHTML = obj.Msg[idx];
// document.getElementById("topMessage").innerHTML = obj.Msg[idx];
}
}
}
Expand Down
63 changes: 46 additions & 17 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public class App extends WebSocketServer {
// the vector ActiveGames
private Vector<Game> ActiveGames = new Vector<Game>();

private int GameId = 1;
private int GameId = 0;

private int GameMode;

private int connectionId = 0;

Expand All @@ -91,32 +93,48 @@ public App(int port, Draft_6455 draft) {

@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {

connectionId++;

System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " connected");
}

public void startGame(WebSocket conn, int GameMode) {
ServerEvent E = new ServerEvent();

// search for a game needing a player
Game G = null;
System.err.println("ActiveGames size: " + ActiveGames.size());
for (Game i : ActiveGames) {
if (i.Players == uta.cse3310.PlayerType.PLAYERONE ||
i.Players == uta.cse3310.PlayerType.PLAYERTWO ||
i.Players == uta.cse3310.PlayerType.PLAYERTHREE) {
G = i;
System.out.println("found a match");
System.err.println("for loop for active games fired.");

boolean openSpots = false;
if (i.GameMode == GameMode) {
for (PlayerType p : i.Players) {
if (p.equals(uta.cse3310.PlayerType.NOPLAYER)) {
openSpots = true;
}
}

if (openSpots) {
G = i;
System.out.println("found a match");
}
}
}

// No matches ? Create a new Game.
// No matches ? Create a new Game.
// No matches ? Create a new Game.
PlayerType currPlayer = uta.cse3310.PlayerType.NOPLAYER;
if (G == null) {
G = new Game(stats);
G = new Game(stats, 1);
G.GameId = GameId;
G.GameMode = GameMode;
GameId++;

// Add the first player
G.Players = PlayerType.PLAYERONE;
G.Players[0] = uta.cse3310.PlayerType.PLAYERONE;
currPlayer = uta.cse3310.PlayerType.PLAYERONE;

ActiveGames.add(G);
System.out.println("creating a new Game");

Expand All @@ -141,13 +159,22 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) {
} else {
// join an existing game
System.out.println("not a new game");
G.Players = PlayerType.values()[G.Players.ordinal() + 1];
G.StartGame();
for (int i = 0; i < G.Players.length; i++) {
if (G.Players[i].equals(uta.cse3310.PlayerType.NOPLAYER)) {
G.Players[i] = PlayerType.values()[i + 2];
currPlayer = PlayerType.values()[i + 2];
}
}

if (!G.Players[G.Players.length - 1].equals(uta.cse3310.PlayerType.NOPLAYER)) {
G.StartGame();
}
}

// create an event to go to only the new player
E.YouAre = G.Players;
E.YouAre = currPlayer;
E.GameId = G.GameId;
E.GameMode = G.GameMode;

// allows the websocket to give us the Game when a message arrives..
// it stores a pointer to G, and will give that pointer back to us
Expand All @@ -162,8 +189,7 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) {
System.out
.println("> " + Duration.between(startTime, Instant.now()).toMillis() + " " + connectionId + " "
+ escape(jsonString));


System.out.println("connection id: " + connectionId);

// The state of the game has changed, so lets send it to everyone
jsonString = gson.toJson(G);
Expand All @@ -177,7 +203,7 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) {

@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
System.out.println(conn + " has closed");
System.out.println(conn + " has closed");
// Retrieve the game tied to the websocket connection
Game G = conn.getAttachment();
G = null;
Expand All @@ -192,7 +218,7 @@ public void onMessage(WebSocket conn, String message) {
try{
UserEvent U = gson.fromJson(message, UserEvent.class);
Game G = conn.getAttachment();
G.Update(U);
// G.Update(U);

System.err.println("message: " + message + message.contains("username") + U.GameId + G.GameId);
if (message.contains("username")){ // && U.GameId == G.GameId
Expand All @@ -208,6 +234,9 @@ public void onMessage(WebSocket conn, String message) {
System.err.println("chat message broadcasted");
return;
}
if ("num-players".equals(U.type)) {
startGame(conn, Integer.parseInt(U.text));
}
if ("word-selection".equals(U.type)) {
// word selection
// Send the selection to everyone
Expand Down
Loading

0 comments on commit e4794ae

Please sign in to comment.