Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
TresEberle authored Apr 30, 2024
1 parent 493832c commit e62ed66
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 393 deletions.
383 changes: 93 additions & 290 deletions html/index.html

Large diffs are not rendered by default.

100 changes: 83 additions & 17 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.java_websocket.WebSocket;
import org.java_websocket.drafts.Draft;
Expand Down Expand Up @@ -113,9 +114,7 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) {
conn.send(jsonString);
System.out.println("sending " + jsonString);

// delete me String playerName = handshake.getFieldValue("playerName");

// delete me System.out.println("the player name is "+ playerName);


// Player newPlayer = new Player(playerName);

Expand Down Expand Up @@ -155,10 +154,12 @@ public void onClose(WebSocket conn, int code, String reason, boolean remote) {
@Override
public void onMessage(WebSocket conn, String message)
{
Game G = new Game(stats);
System.out.println("Received message from client: " + message);

// Parse the incoming message to extract sender and content
Gson gson = new Gson();


// broadcast the recieved message
// broadcast(message);
Expand All @@ -177,6 +178,7 @@ public void onMessage(WebSocket conn, String message)

// Broadcast lobby information to all clients
// broadcast(gson.toJson(lobby));
/*
if (false) {
NewNameEvent N = gson.fromJson(message, NewNameEvent.class);
// Lobby lobbys = conn.getAttachment();
Expand All @@ -198,8 +200,9 @@ public void onMessage(WebSocket conn, String message)
broadcast(chatMessage);
// broadcast(message);
}

}
*/

// if (message.contains("content")) {
// Use JSON parsing to extract sender and content

Expand All @@ -217,31 +220,94 @@ public void onMessage(WebSocket conn, String message)
L = createNewLobby();
}

// each kind of message is processed here
//Checking if the message is a newPlayer message
if (message.indexOf("newPlayer") > 0) {

// this is of type
// Received message from client:
// {"Type":"newPlayer","playerName":"aaaa","ConnectioID":1}

// Received message from client: {"type":"newPlayer","playerName":"aaaa","ConnectioID":1}
NewNameEvent N = gson.fromJson(message,NewNameEvent.class);
System.out.println("the name is " + N.playerName + " " + N.ConnectionID);
// new code added//
// Player playername = new Player(N.ConnectionID, N.playerName, 0, "");
// Checking to see if players names are being added to the list
Player P = new Player(N.ConnectionID,N.playerName,0,"green");
System.out.println("The name is: " + N.playerName + ", The Connection ID is: " + N.ConnectionID + ", The color is: " + N.playerColor) ;

Player P = new Player(N.ConnectionID,N.playerName,0,N.playerColor);
L.addPlayer(P);
// since the lobby has changed, let's send it out to everyone

String jsonString = gson.toJson(L.Players);
// since the lobby has changed, let's send it out to everyone

String jsonString = gson.toJson(L);
//conn.send(jsonString);
broadcast(jsonString);
// Player.printPlayerList();
// broadcastPlayerList();
// }
}

//Checking if the message is a player readying up
if (message.indexOf("readyPlayer") > 0) {
NewNameEvent N = gson.fromJson(message,NewNameEvent.class);
L.addToReadyQueue(L.getPlayerByName(N.playerName));

// since the lobby has changed, let's send it out to everyone

String jsonString = gson.toJson(L);
broadcast(jsonString);
}
}

if (message.startsWith("GamePlayer") == true) {
StringTokenizer string = new StringTokenizer(message," ");
string.nextToken();
String name1 = string.nextToken();
String name2 = string.nextToken();

if(L.getReadyQueueSize() < 2)
{
//ADD SOMETHING HERE TO DISPLAY NOT ENOUGH READY ON HTML
System.out.println("not enough to start");
}else{
L.removeFromReadyQueue(L.getPlayerByName(name1));
L.removeFromReadyQueue(L.getPlayerByName(name2));
String jsonString = gson.toJson(L);
broadcast(jsonString);

//THIS PART NEEDS ADJUSTMENTS


Message GridMessage = new Message(G.grid.WordSearchGrid);
String GridJSONString = gson.toJson(GridMessage);
conn.send(GridJSONString);

//Sending WordBank to server
Message WordBank = new Message(G.grid.WordsUsed);
String WordBankJSONString = gson.toJson(WordBank);
conn.send(WordBankJSONString);
}
}

if(message.startsWith("WordCheck") == true)
{

int j = 0;
Boolean Found = false;
StringTokenizer string = new StringTokenizer(message," ");
string.nextToken();
String test = string.nextToken();
for (String i : G.grid.WordsUsedLocations)
{
if(test.equals(i)){
System.out.println("ITS ACTUALLY A WORD!");
Message FoundWord = new Message(j);
String FoundWordJSONString = gson.toJson(FoundWord);
conn.send(FoundWordJSONString);
Found = true;
break;
}
j++;
}
if(Found == false){
Message FoundWord = new Message(-1);
String FoundWordJSONString = gson.toJson(FoundWord);
conn.send(FoundWordJSONString);
}
}
}
// private void broadcastPlayerList() {
// List<String> playerNames = Player.getPlayerList();
// Gson gson = new Gson();
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/uta/cse3310/Game.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package uta.cse3310;

import com.google.gson.Gson;
//import com.google.gson.Gson;

public class Game {

Expand All @@ -19,7 +19,6 @@ public class Game {
Stats = s;
Button = new PlayerType[9];


grid = new Grid();

// initialize it
Expand All @@ -36,6 +35,16 @@ public class Game {
Msg[1] = "THE WORD SEARCH GAME";
}

public void SetBoard(PlayerType p, int[] b) {
// this method is only used for testing purposes
// p is the player to give the square to, and b
// is an array of button numbers
for (int i : b) {
Button[i] = p;
}

}

public void StartGame() {

}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/uta/cse3310/Lobby.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Lobby {
private List<Player> readyQueue;
private PlayerType gameController; // Added field to track game controller
private final int MAX_PLAYERS_PER_GAME = 4; // Maximum players per game
private String Type = "Lobby";
private String type = "Lobby";

public Lobby() {
players = new ArrayList<>();
Expand Down Expand Up @@ -56,7 +56,7 @@ public synchronized List<Player> getPlayers() {
}

public synchronized void addToReadyQueue(Player player) {
if (!(readyQueue.contains(player))) {
if (!readyQueue.contains(player)) {
readyQueue.add(player);
}
// Start the game if the game controller is ready and there are enough players
Expand All @@ -73,6 +73,10 @@ public synchronized List<Player> getReadyQueue() {
return new ArrayList<>(readyQueue);
}

public synchronized int getReadyQueueSize(){
return readyQueue.size();
}

public synchronized void startGame() {
// Logic to start the game
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/uta/cse3310/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Message {
private String content;

public Message() {
// Default constructor
this.type = "Basic";
String content;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/uta/cse3310/NewNameEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public class NewNameEvent {
String Type;
String playerName;
String playerColor;
int ConnectionID;
}
10 changes: 5 additions & 5 deletions src/main/java/uta/cse3310/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ public Player(int connectionID, String PlayerName, int PlayerPoints, String Play

//Getter for player name
public String getName(){

return PlayerName;
}

//Getter for player points
public int getPoints(){

return 1;
return PlayerPoints;
}
//Getter for player color
public String getColor(){

return "player color";
return PlayerColor;
}
public int getConnectionID(){
return connectionID;
}

//getter for player list
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/uta/cse3310/PlayerType.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
// A player can be an X or an O

public enum PlayerType {
NOPLAYER, XPLAYER, OPLAYER, PLAYER1, PLAYER2, PLAYER3, PLAYER4
NOPLAYER, XPLAYER, OPLAYER
}

2 changes: 1 addition & 1 deletion src/main/java/uta/cse3310/ServerEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public class ServerEvent {
PlayerType YouAre; // Either an XPLAYER or a YPLAYER
int GameId;
int ConnectionID;
String Type ="ID";
String type = "ID";
}
Loading

0 comments on commit e62ed66

Please sign in to comment.