diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c5f3f6b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "interactive" +} \ No newline at end of file diff --git a/html/index.html b/html/index.html index 25d9e18..44953ee 100644 --- a/html/index.html +++ b/html/index.html @@ -15,12 +15,11 @@

The WordSearch Game

Welcome Everyone

-

Rules...

User Login

- +
diff --git a/html/main.js b/html/main.js index 1ecf7c1..f015451 100644 --- a/html/main.js +++ b/html/main.js @@ -1,417 +1,59 @@ -var gridsize = 30; -var isReady = false; // Global variable to keep track of the player's ready status -// Variables for player information and WebSocket connection -var username; -var connection; +var serverUrl = "ws://sp24.cse3310.org:" + (Number(location.port) + 100); +var connection = new WebSocket(serverUrl); -// Wait for the DOM to fully load before running the script -document.addEventListener("DOMContentLoaded", function() { - // Initialize WebSocket connection - initializeWebSocket(); - // Attach event listener to the login form - var loginForm = document.getElementById('login-form'); - if (loginForm) { - loginForm.addEventListener('submit', function(event) { - event.preventDefault(); - username = document.getElementById('username').value.trim(); - - // Send username to the server - if (connection && connection.readyState === WebSocket.OPEN) { - connection.send(JSON.stringify({ type: "login", username: username })); - } - - // Update UI to show the lobby page - document.getElementById('loginPage').style.display = 'none'; - document.getElementById('lobbyPage').style.display = 'block'; - document.getElementById('chatArea').style.display = 'block'; - updatePlayerList([{name: username, isReady: false}]); // Add player to the list - }); - } else { - console.error('Login form not found!'); - } -}); - -// Initialize WebSocket and setup handlers -function initializeWebSocket() { - // Assuming you're running the WebSocket server locally on port 8080 - var serverUrl = "ws://localhost:8080"; - console.log("Connecting to WebSocket server at " + serverUrl); - - connection = new WebSocket(serverUrl); - - connection.onopen = function () { - console.log("Connected to the server."); - // Possibly update the UI to show connection status - }; - - // When a message from the server is received -connection.onmessage = function (event) { - var data = JSON.parse(event.data); - console.log("Message received:", data); - - // Handle different types of messages - switch (data.type) { - case 'playerListUpdate': - updatePlayerList(data.players); - break; - case 'gameStarted': - startGame(); - break; - case 'chat': - displayChatMessage(data.username, data.message); - break; - - // If the message type indicates an update to the player list - //if (data.type === 'playerListUpdate') { - // updateLobbyWithPlayers(data.players); // data.players should be an array of player objects - //} -}; - - connection.onerror = function (error) { - console.error("WebSocket Error:", error); - // Possibly update the UI to reflect the error status without using alert - }; - - connection.onclose = function () { - console.log("Connection closed by the server."); - // Update the UI to show the connection is not currently active - // Avoid using alert here; instead, you might want to show a message in the UI - }; -} -} - -// Handle different types of messages from the server -function handleServerMessages(data) { - switch (data.type) { - case 'playerListUpdate': - updatePlayerList(data.players); // Use this function to update the lobby - break; - case 'gameStarted': - startGame(); - break; - // Add more cases as needed - } -} - -function sendMessage() { - var input = document.getElementById('chatInput'); - var message = input.value.trim(); - if (message) { - if (connection && connection.readyState === WebSocket.OPEN) { - connection.send(JSON.stringify({ type: "chat", username: username, message: message })); - } - displayChatMessage(username, message); // Ensure this is called - input.value = ''; // Clear the input field after sending - } -} - -function displayChatMessage(username, message) { - const chatDisplay = document.getElementById('chatDisplay'); - const messageElem = document.createElement('p'); - messageElem.innerHTML = `${username}: ${message}`; - chatDisplay.appendChild(messageElem); - chatDisplay.scrollTop = chatDisplay.scrollHeight; // Automatically scroll to the latest message -} - - - - -// Function to update the player list in the lobby -function updatePlayerList(players) { - const playerListDiv = document.getElementById('playerList'); - playerListDiv.innerHTML = ''; // Clear the existing list - players.forEach(player => { - const playerElem = document.createElement('div'); - playerElem.innerHTML = `${player.name} - ${player.isReady ? 'Ready' : 'Not Ready'}`; - playerListDiv.appendChild(playerElem); - }); - checkReadyPlayers(); // Check if the start condition is met after updating the list -} - - - - - //update player status - function updateStatus(status) { - // Make sure the WebSocket is connected before sending a message - if (connection.readyState === WebSocket.OPEN) { - const message = { type: 'updateStatus', status: status }; - connection.send(JSON.stringify(message)); - } else { - console.log("WebSocket is not yet open. Waiting to send the status update."); - // Optionally, wait and try again or handle according to your needs - } +function UserEvent(type, eventData){ + this.type = type; + this.eventData = eventData; } +connection.onopen = function(event){ + console.log("Websocket open"); +}; - function changeLobby(lobbySize) { - const message = { type: 'changeLobby', lobbySize: lobbySize }; - connection.send(JSON.stringify(message)); - } - -// Start the game -function startGame() { - if (!isReady) { // Check if the player is ready before starting the game - alert("Please mark yourself as ready before starting the game!"); - return; - } - document.getElementById('lobbyPage').style.display = 'none'; - document.getElementById('gamePage').style.display = 'block'; - document.getElementById('chatArea').style.display = 'block'; - initializeGrid(); - loadWordList(); - console.log('Game started.'); -} - -function loadWordList() { - fetch('http://yourserver.com/wordlist.txt') - .then(response => response.text()) - .then(data => { - const words = data.split(/\r?\n/); // Split the text file into an array of words - displayWords(words); - }) - .catch(error => console.error('Error loading word list:', error)); - } - - function displayWords(words) { - const wordListContainer = document.getElementById('wordList'); - wordListContainer.innerHTML = ''; // Clear existing content - words.forEach(word => { - if (word.trim().length > 0) { // Avoid empty lines - const wordElement = document.createElement('li'); - wordElement.textContent = word; - wordListContainer.appendChild(wordElement); - } - }); - } - -let startPoint = null; - -//create grid - -function initializeGrid() { - const grid = document.getElementById("grid"); - for (let i = 0; i < gridsize; i++) { - const row = grid.insertRow(); - for (let j = 0; j < gridsize; j++) { - const cell = row.insertCell(); - cell.id = `cell-${i}-${j}`; - cell.addEventListener("click", function() { handleCellClick(i, j); }); - // Placeholder for letters, replace with actual game data - cell.textContent = String.fromCharCode(65 + Math.floor(Math.random() * 26)); - } - } -} +connection.onclose = function(event){ + console.log("Websocket closed"); +}; -function handleCellClick(row, col) { - const cellId = `cell-${row}-${col}`; - const cell = document.getElementById(cellId); - if (!startPoint) { - // Mark the start point - startPoint = { row, col }; - cell.style.backgroundColor = "yellow"; // Highlight starting cell - } else { - // Determine the direction and highlight the path - highlightPath(startPoint, { row, col }); - startPoint = null; // Reset start point for next word - } -} +connection.onmessage = function(event){ + //read in event + var message = event.data; -function highlightPath(start, end) { - if (start.row === end.row) { - // Horizontal path - for (let j = Math.min(start.col, end.col); j <= Math.max(start.col, end.col); j++) { - document.getElementById(`cell-${start.row}-${j}`).style.backgroundColor = "lightgreen"; - } - } else if (start.col === end.col) { - // Vertical path - for (let i = Math.min(start.row, end.row); i <= Math.max(start.row, end.row); i++) { - document.getElementById(`cell-${i}-${start.col}`).style.backgroundColor = "lightgreen"; - } - } else { - // Diagonal path - const rowIncrement = start.row < end.row ? 1 : -1; - const colIncrement = start.col < end.col ? 1 : -1; - let row = start.row; - let col = start.col; - while (row !== end.row + rowIncrement && col !== end.col + colIncrement) { - document.getElementById(`cell-${row}-${col}`).style.backgroundColor = "lightgreen"; - row += rowIncrement; - col += colIncrement; - } - } -} + //parse message for object + var data = JSON.parse(message); -function updateScoreboard(players) { - const scoreboard = document.getElementById('scoreBoard'); - scoreboard.innerHTML = ''; // Clear existing scoreboard - - players.forEach(player => { - const scoreItem = document.createElement('li'); - scoreItem.classList.add('playerScore'); - scoreItem.textContent = `${player.name}: ${player.score}`; - scoreItem.style.color = player.color; // Assign color dynamically - scoreboard.appendChild(scoreItem); - }); - } - - - function ready() { - isReady = !isReady; - // If the WebSocket is not open, wait for a bit and then try to update the status - if (connection.readyState !== WebSocket.OPEN) { - console.log("Waiting for the WebSocket to open..."); - setTimeout(() => { - updateStatus(isReady); - }, 1000); // Wait for 1 second before trying again - } else { - updateStatus(isReady); + if(data == "loginSuccess"){ + showLobby(); } - updateLocalPlayerReadyStatus(); -} - -function checkReadyPlayers() { - const listId = `${requiredPlayers}PlayerList`; - const players = Array.from(document.querySelectorAll(`#${listId} > div`)); - const readyCount = players.reduce((count, player) => { - return count + (player.textContent.includes('Ready') ? 1 : 0); - }, 0); - - const startButton = document.getElementById('startGameButton'); - startButton.disabled = readyCount < requiredPlayers; -} - - +}; -function updateStatus(isReady) { - // Construct the message to send the updated status to the server - const message = { type: 'updateStatus', username: username, isReady: isReady }; - connection.send(JSON.stringify(message)); -} +function login(){ + var username = document.getElementById("username").value; -function updateLocalPlayerReadyStatus() { - const playerListDiv = document.getElementById('playerList'); - // Assuming each player's entry is wrapped in a div, and username is directly within this div - Array.from(playerListDiv.children).forEach(child => { - if (child.textContent.includes(username)) { - // Correctly update the innerHTML to reflect the new status - child.innerHTML = `${username} - ${isReady ? 'Ready' : 'Not Ready'}`; + var data = { + type: "login", + eventData: { + username : "username" } - }); -} - -function setPlayerCount(playerCount) { - requiredPlayers = playerCount; - - // Define the lists for different modes - const lists = { - 2: document.getElementById('twoPlayerList'), - 3: document.getElementById('threePlayerList'), - 4: document.getElementById('fourPlayerList') }; - // Hide all lists and then display the selected one - Object.values(lists).forEach(list => list.style.display = 'none'); - lists[requiredPlayers].style.display = 'block'; - - // Clear the selected list and move players from the main list to the selected one - var players = Array.from(document.querySelectorAll('#playerList > div')); - var selectedListDiv = lists[requiredPlayers]; - selectedListDiv.innerHTML = ''; // Clear the existing list - - // Move players to the selected list (assuming all players are moved for simplicity) - players.forEach(player => { - selectedListDiv.appendChild(player.cloneNode(true)); // Clone the player element - }); - - // Check the readiness of players based on the selected mode - checkReadyPlayers(); -} - -// Now use this function for the buttons in HTML -// For 2 players: onclick="setPlayerCount(2)" -// For 3 players: onclick="setPlayerCount(3)" -// For 4 players: onclick="setPlayerCount(4)" - - - - -function twoplayers() { - requiredPlayers = 3; - updatePlayerMode('twoPlayerList'); -} - - -function threeplayers() { - requiredPlayers = 3; - updatePlayerMode('threePlayerList'); -} - -function fourplayers() { - requiredPlayers = 4; - updatePlayerMode('fourPlayerList'); -} - -function updatePlayerMode(modeListId) { - // Hide all player mode lists - ['twoPlayerList', 'threePlayerList', 'fourPlayerList'].forEach(listId => { - document.getElementById(listId).style.display = 'none'; - }); - - // Get the selected mode's list and display it - var selectedModeList = document.getElementById(modeListId); - selectedModeList.innerHTML = `

${modeListId.split('PlayerList')[0]} Mode

`; - selectedModeList.style.display = 'block'; - - // Move players to the selected mode list - var players = document.querySelectorAll('#playerList > div'); - players.forEach(player => { - // Clone the node to avoid removing it from the original playerList - selectedModeList.appendChild(player.cloneNode(true)); - }); - - // Update the start button state based on the new mode - checkReadyPlayers(); -} - + connection.send(JSON.stringify(data)); +}; -function back() { - // Check if the user is currently on the game page - if (document.getElementById('gamePage').style.display === 'block') { - // Hide the game page and show the lobby - document.getElementById('gamePage').style.display = 'none'; - document.getElementById('lobbyPage').style.display = 'block'; - // Reset the game state here if needed - resetGameState(); - } - // Check if the user is currently on the lobby page - else if (document.getElementById('lobbyPage').style.display === 'block') { - // Hide the lobby and show the login page - document.getElementById('lobbyPage').style.display = 'none'; - document.getElementById('loginPage').style.display = 'block'; - // Reset any lobby specific states or data - resetLobbyState(); - } +function showLogin(){ + document.getElementById('lobbyPage').style.display = 'none'; + document.getElementById('gamePage').style.display = 'none'; } -// Function to reset the game state -function resetGameState() { - // Clear the game grid or any game-related data - const grid = document.getElementById("grid"); - while (grid.firstChild) { - grid.removeChild(grid.firstChild); - } - // Reset any other game-specific variables +function showLobby(){ + document.getElementById('loginPage').style.display = 'none'; + document.getElementById('lobbyPage').style.display = 'block'; + document.getElementById('gamePage').style.display = 'none'; } -// Function to reset lobby state, like clearing the player list or other lobby data -function resetLobbyState() { - // Reset username or other sensitive data - username = ""; // Reset the username global variable - document.getElementById('username').value = ""; // Clear the input field - // Clear the player list displayed in the lobby - const playerListDiv = document.getElementById('playerList'); - playerListDiv.innerHTML = ''; +function showGame(){ + document.getElementById("loginPage").style.display = "none"; + document.getElementById("lobbyPage").style.display = "none"; + document.getElementById("gamePage").style.display = "block"; } \ No newline at end of file diff --git a/src/main/java/uta/cse3310/App.java b/src/main/java/uta/cse3310/App.java index 435cc0f..d52fb65 100644 --- a/src/main/java/uta/cse3310/App.java +++ b/src/main/java/uta/cse3310/App.java @@ -8,6 +8,8 @@ import java.nio.ByteBuffer; import java.util.Vector; import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + import java.util.ArrayList; @@ -17,6 +19,7 @@ public class App extends WebSocketServer { private int GameId = 1; private int connectionId = 0; private MainLobby mainLobby = new MainLobby(); + private Event eventMaker = new Event(); public App(int port) { super(new InetSocketAddress(port)); @@ -24,36 +27,35 @@ public App(int port) { @Override public void onOpen(WebSocket conn, ClientHandshake handshake) { - if(mainLobby.addPlayerToMainLobby(conn, "random")){ - conn.send("Welcome"); - } - else{ - conn.send("Game is full"); - conn.close(); - } + System.out.println("New connection: " + conn.getRemoteSocketAddress()); } @Override public void onClose(WebSocket conn, int code, String reason, boolean remote) { System.out.println("Closed connection: " + conn.getRemoteSocketAddress()); - mainLobby.removePlayerFromMainLobby(conn); + mainLobby.logOff(conn); broadcast("Player has left"); } @Override public void onMessage(WebSocket conn, String message) { - System.out.println("Recieved message from " + conn.getRemoteSocketAddress()); - - JsonObject json = new JsonObject(); + + //Parse JSON string + JsonObject json = JsonParser.parseString(message).getAsJsonObject(); + //Process the type of request String type = json.get("type").getAsString(); - String username = json.get("username").getAsString(); if(type == "login"){ - Player player = new Player(username, conn); - - mainLobby.addPlayerToMainLobby(conn, username); - System.out.println("players: " + mainLobby); + //Parse JSON string for event data (username) + JsonObject eventData = json.getAsJsonObject("eventData"); + String username = eventData.get("username").getAsString(); + + //add new player to mainLobby - returns true if successfulky added + if(mainLobby.logIn(conn, username)){ + eventMaker.loginSuccess(conn); // send json message back to JS + } + } } diff --git a/src/main/java/uta/cse3310/Event.java b/src/main/java/uta/cse3310/Event.java index 1d274d4..5171f6c 100644 --- a/src/main/java/uta/cse3310/Event.java +++ b/src/main/java/uta/cse3310/Event.java @@ -1,7 +1,26 @@ package uta.cse3310; +import com.google.gson.JsonObject; +import org.java_websocket.WebSocket; + public class Event { private int Game_ID; private String User_ID; private int button; + + + + public void loginSuccess(WebSocket connection){ + JsonObject json = new JsonObject(); + json.addProperty("type", "loginSuccess"); + connection.send(json.toString()); + } + + public void Error(WebSocket connection, String message){ + JsonObject json = new JsonObject(); + json.addProperty("type", "error"); + json.addProperty("message", message); + connection.send(json.toString()); + } + } diff --git a/src/main/java/uta/cse3310/MainLobby.java b/src/main/java/uta/cse3310/MainLobby.java index b97f793..cd47394 100644 --- a/src/main/java/uta/cse3310/MainLobby.java +++ b/src/main/java/uta/cse3310/MainLobby.java @@ -132,21 +132,30 @@ void startGame(SubLobby lobby){ private static final int MAX_PLAYERS = 20; private ArrayList players = new ArrayList<>(); - public boolean addPlayerToMainLobby(WebSocket conn, String name){ + + // add new players to main lobby + public boolean logIn(WebSocket conn, String name){ + //check for unique username + for(Player player : players){ + if(player.getName() == name){ + return false; + } + } + if(players.size() < MAX_PLAYERS){ Player player = new Player(name, conn); players.add(player); - return true; + return true; //if # of players is not max, create player } else{ - return false; + return false; // if # of players is max, don't create player } } - public void removePlayerFromMainLobby(WebSocket conn){ + public void logOff(WebSocket conn){ Player remove = null; for(Player player : players){ - if(player.getConn().equals(conn)){ + if(player.getConn() == conn){ remove = player; break; }