Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
RamonTorresUta committed Aug 4, 2024
1 parent 6afd9f0 commit f472df4
Show file tree
Hide file tree
Showing 28 changed files with 622 additions and 467,495 deletions.
Binary file modified src/.DS_Store
Binary file not shown.
Binary file modified src/main/.DS_Store
Binary file not shown.
90 changes: 52 additions & 38 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,48 +35,52 @@ public App(int port, Draft_6455 draft) {
super(new InetSocketAddress(port), Collections.<Draft>singletonList(draft));
}

@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
connectionId++;
System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " connected");

ServerEvent event = new ServerEvent();
Game game = null;

for (Game g : activeGames) {
if (g.getPlayers().size() < 4) { // Assuming a maximum of 4 players per game
game = g;
System.out.println("found a match");
break;
}
public Vector<Game> getGames() {
return activeGames;
}

if (game == null) {
List<Player> players = new ArrayList<>();
players.add(new Player("Player1")); // Initialize at least one player
try {
game = new Game(players, "src/main/resources/words.txt", "src/main/resources/stakes.txt", new Statistics());
} catch (IOException e) {
e.printStackTrace();
return;
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
connectionId++;
System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " connected");

ServerEvent event = new ServerEvent(PlayerType.NOPLAYER, gameId);
Game game = null;

for (Game g : activeGames) {
if (g.getPlayers().size() < 4) { // Assuming a maximum of 4 players per game
game = g;
System.out.println("Found a match");
break;
}
}
game.setGameId(gameId++);
activeGames.add(game);
System.out.println("creating a new Game");
} else {
System.out.println("joining an existing game");
}

conn.setAttachment(game); // This line ensures that the game is attached to the connection
Gson gson = new Gson();
String jsonString = gson.toJson(event);
conn.send(jsonString);
System.out.println("> " + jsonString);
if (game == null) {
List<Player> players = new ArrayList<>();
players.add(new Player("Player" + connectionId, PlayerType.HUMAN)); // Initialize at least one player
try {
game = new Game(players, "src/main/resources/words.txt", "src/main/resources/stakes.txt", new Statistics());
} catch (IOException e) {
e.printStackTrace();
return;
}
game.setGameId(gameId++);
activeGames.add(game);
System.out.println("Creating a new Game");
} else {
System.out.println("Joining an existing game");
}

jsonString = gson.toJson(game);
System.out.println("< " + jsonString);
broadcast(jsonString);
}
conn.setAttachment(game); // Attach the game to the connection
Gson gson = new Gson();
String jsonString = gson.toJson(event);
conn.send(jsonString);
System.out.println("> " + jsonString);

jsonString = gson.toJson(game);
System.out.println("< " + jsonString);
broadcastToGame(game, jsonString); // Broadcast to the specific game
}

@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
Expand All @@ -101,7 +105,7 @@ public void onMessage(WebSocket conn, String message) {
game.update(event);
String jsonString = gson.toJson(game);
System.out.println("> " + jsonString);
broadcast(jsonString);
broadcastToGame(game, jsonString); // Broadcast to the specific game
}
}

Expand All @@ -122,6 +126,14 @@ public void onStart() {
System.out.println("WebSocket server started successfully");
}

private void broadcastToGame(Game game, String message) {
for (WebSocket conn : getConnections()) { // Changed from connections() to getConnections()
if (conn.getAttachment() == game) {
conn.send(message);
}
}
}

public static void main(String[] args) {
String httpPortEnv = System.getenv("HTTP_PORT");
int httpPort = (httpPortEnv != null) ? Integer.parseInt(httpPortEnv) : 9005;
Expand All @@ -143,3 +155,5 @@ public static void main(String[] args) {





79 changes: 51 additions & 28 deletions src/main/java/uta/cse3310/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import java.util.List;
import org.java_websocket.WebSocket;

public class Game {

public class Game {
private static final int MAX_PLAYERS = 4;

private List<Player> players;
private List<Round> rounds;
private int currentRoundIndex;
Expand All @@ -21,13 +20,11 @@ public Game(List<Player> players, String wordFilePath, String stakeFilePath, Sta
this.rounds = new ArrayList<>();
this.currentRoundIndex = 0;
this.isGameActive = true;
this.stats = stats;
this.stats = stats; // Keep the passed stats
this.gameId = 0;
stats = new Statistics();

// Initialize rounds
for (int i = 0; i < 3; i++) { // Assuming a game consists of 3 rounds
rounds.add(new Round(players, wordFilePath, stakeFilePath));

for (int i = 0; i < 3; i++) {
rounds.add(new Round(players, wordFilePath, stakeFilePath));//error with this function
}
}

Expand All @@ -42,10 +39,10 @@ public void setGameId(int gameId) {
public boolean addPlayer(Player player) {
if (players.size() < MAX_PLAYERS) {
players.add(player);
return true; // Player added successfully
return true;
} else {
System.out.println("Cannot add player: maximum number of players reached.");
return false; // Player not added
return false;
}
}

Expand All @@ -54,13 +51,23 @@ public void removePlayer(WebSocket conn) {
}

public void update(UserEvent event) {
switch (event.getAction()) {
if (event == null) {
System.err.println("Received null event");
return;
}

String action = event.getAction();
if (action == null) {
System.err.println("Event action is null");
return;
}

switch (action) {
case "PLAY":
playRound();
break;
// Add cases for other actions as needed
default:
System.out.println("Unknown action: " + event.getAction());
System.out.println("Unknown action: " + action);
}
}

Expand All @@ -72,17 +79,27 @@ public void startGame() {
}
System.out.println("Game started with " + players.size() + " players.");
isGameActive = true;
startNextRound();
}


public void startNextRound() {
if (currentRoundIndex >= rounds.size()) {
System.out.println("All rounds completed.");
for(int i =0; i < 3; i++)
{
startNextRound();
if(i ==2)
{
isGameActive = false;
determineWinner();
return;
System.out.println("All round complete");

}
}
//startNextRound();
}

public void startNextRound() {
//if (currentRoundIndex >= rounds.size()) {
//System.out.println("All rounds completed.");
//isGameActive = false;
//determineWinner();
// return;
//}

Round currentRound = rounds.get(currentRoundIndex);
currentRound.startRound();
Expand All @@ -94,15 +111,14 @@ public void playRound() {
System.out.println("Game is not active.");
return;
}
Round currentRound = rounds.get(currentRoundIndex - 1); // -1 because we incremented in startNextRound

Round currentRound = rounds.get(currentRoundIndex - 1);
while (currentRound.isRoundActive()) {
currentRound.nextTurn();
}
startNextRound();

if (!currentRound.isRoundActive()) {
//startNextRound();
if (currentRoundIndex >= rounds.size()) {
System.out.println("All rounds completed.");
isGameActive = false;
Expand All @@ -119,7 +135,12 @@ public void endGame(Player player) {
public Statistics getStatistics() {
return stats;
}
private void determineWinner() {

public int getCurrentRoundIndex() {
return currentRoundIndex;
}

public void determineWinner() {
Player winner = null;
int highestScore = 0;
for (Player player : players) {
Expand All @@ -128,9 +149,9 @@ private void determineWinner() {
winner = player;
}
}

if (winner != null) {
winner.setWinner(true); // Ensure that the winner is marked correctly
winner.setWinner(true);
System.out.println("The winner is " + winner.getName() + " with a score of " + highestScore);
stats.updateWinner(winner);
} else {
Expand Down Expand Up @@ -160,3 +181,5 @@ public boolean isGameActive() {





17 changes: 9 additions & 8 deletions src/main/java/uta/cse3310/GameTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@

public class GameTimer {
private transient Timer timer;
// private Timer timer;
private long startTime;
private long elapsedTime;
private boolean isRunning;
private TimerTask timerTask;

public GameTimer() {

this.timer = new Timer();
this.elapsedTime = 0;
this.isRunning = false;
this.timerTask = new TimerTask() {
@Override
public void run() {
elapsedTime = System.currentTimeMillis() - startTime;
}
};
}

public void start() {
Expand All @@ -23,12 +28,7 @@ public void start() {
}
this.startTime = System.currentTimeMillis();
this.isRunning = true;
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
elapsedTime = System.currentTimeMillis() - startTime;
}
}, 0, 1000); // Update elapsed time every second
timer.scheduleAtFixedRate(timerTask, 0, 1000); // Update elapsed time every second
}

public void stop() {
Expand Down Expand Up @@ -57,3 +57,4 @@ public boolean isRunning() {
}



2 changes: 1 addition & 1 deletion src/main/java/uta/cse3310/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class HttpServer {

private static final String HTML = "./html";
int port = 9105;
int port = 80;
String dirname = HTML;

public HttpServer(int portNum, String dirName) {
Expand Down
40 changes: 27 additions & 13 deletions src/main/java/uta/cse3310/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ public class Player {
private String name;
private int score;
private boolean iswinner;
private GameTimer timer; // Updated reference
private GameTimer timer;
private List<Character> boughtVowels;
private List<Character> guessedConstants;
private List<Character> guessedConsonants;
private PlayerType playerType;


public Player(String name) {
public Player(String name, PlayerType playerType) {
this.id = UUID.randomUUID().toString();
this.name = name;
this.score = 1000;
this.timer = new GameTimer(); // Updated reference
this.timer = new GameTimer();
this.boughtVowels = new ArrayList<>();
this.guessedConstants = new ArrayList<>();
this.guessedConsonants = new ArrayList<>();
this.playerType = playerType;
}

public boolean isWinner() {
Expand Down Expand Up @@ -59,7 +60,7 @@ public GameTimer getTimer() {
return timer;
}

public boolean hasBoughtVowel(char vowel) {
public boolean hasBoughtVowel(char vowel) {
return this.boughtVowels.contains(vowel);
}

Expand All @@ -71,16 +72,29 @@ public List<Character> getBoughtVowels() {
return this.boughtVowels;
}

public void guessConstant(char constant) {
this.guessedConstants.add(constant);
public void guessConsonant(char consonant) {
this.guessedConsonants.add(consonant);
}

public List<Character> getGuessedConstants() {
return this.guessedConstants;
public List<Character> getGuessedConsonants() {
return this.guessedConsonants;
}

public boolean hasGuessedConstant(char constant) {
return this.guessedConstants.contains(constant);
public boolean hasGuessedConsonant(char consonant) {
return this.guessedConsonants.contains(consonant);
}

public PlayerType getPlayerType() {
return playerType;
}

public void setPlayerType(PlayerType playerType) {
this.playerType = playerType;
}
}






Loading

0 comments on commit f472df4

Please sign in to comment.