MainContentPanel.java (13226B)
1 package com.groupproject.tictactoeclient.ContentPanes; 2 3 import com.groupproject.tictactoeclient.TicTacToeClient; 4 import java.awt.*; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import javax.swing.*; 8 import javax.swing.event.ListSelectionListener; 9 10 /** 11 * 12 * @author William 13 */ 14 public class MainContentPanel extends CustomPanel { 15 16 TicTacToeClient client; 17 JList<String> openGamesList; 18 private JLabel gameTimerLabel; 19 private Timer timer; 20 private int remainingTime; 21 private TicTacToeGrid grid; 22 private JLabel gameStatusLabel; 23 private JLabel currentGameLabel; 24 private String[] previousRawGames; 25 26 public MainContentPanel(TicTacToeClient client) { 27 this.client = client; 28 29 // Set panel layout to SpringLayout 30 SpringLayout layout = new SpringLayout(); 31 setLayout(layout); 32 33 // Create panel components 34 JButton createGameButton = new JButton("New Game"); 35 JButton allscoreButton = new JButton("Scoreboard"); 36 JButton joinGameButton = new JButton("Join Game"); 37 JButton forfeitButton = new JButton("Forfeit Game"); 38 JButton logoutButton = new JButton("Logout"); 39 40 openGamesList = new JList<>(); 41 JScrollPane openGamesScrollPane = new JScrollPane(openGamesList); 42 grid = new TicTacToeGrid(client, 3, 3); // A 3x3 TicTacToe grid 43 gameStatusLabel = new JLabel(); 44 gameTimerLabel = new JLabel(); 45 currentGameLabel = new JLabel(); 46 JButton scoreButton = new JButton("View Personal Score"); 47 48 //For each user display their respective username on the main content panel 49 client.frame.setTitle("TicTacToe - Welcome " + client.username); 50 51 //Set the game timer label to 15m 52 gameTimerLabel.setText("15m 0s"); 53 54 // Add buttons to panel 55 add(allscoreButton); 56 add(createGameButton); 57 add(openGamesScrollPane); 58 add(joinGameButton); 59 add(grid); 60 add(gameStatusLabel); 61 add(gameTimerLabel); 62 add(scoreButton); 63 add(currentGameLabel); 64 add(forfeitButton); 65 add(logoutButton); 66 67 //create button constraint 68 layout.putConstraint(SpringLayout.WEST, createGameButton, 20, SpringLayout.WEST, this); 69 layout.putConstraint(SpringLayout.NORTH, createGameButton, 10, SpringLayout.NORTH, this); 70 71 //forfeit button 72 layout.putConstraint(SpringLayout.EAST, forfeitButton, -160, SpringLayout.EAST, grid); 73 layout.putConstraint(SpringLayout.SOUTH, forfeitButton, -20, SpringLayout.SOUTH, this); 74 75 //leaderboard constraint 76 layout.putConstraint(SpringLayout.WEST, allscoreButton, 20, SpringLayout.EAST, createGameButton); 77 layout.putConstraint(SpringLayout.NORTH, allscoreButton, 0, SpringLayout.NORTH, createGameButton); 78 79 //Open games constraint 80 layout.putConstraint(SpringLayout.WEST, openGamesScrollPane, 0, SpringLayout.WEST, createGameButton); 81 layout.putConstraint(SpringLayout.EAST, openGamesScrollPane, 0, SpringLayout.EAST, allscoreButton); 82 layout.putConstraint(SpringLayout.NORTH, openGamesScrollPane, 10, SpringLayout.SOUTH, createGameButton); 83 layout.putConstraint(SpringLayout.SOUTH, openGamesScrollPane, -10, SpringLayout.NORTH, joinGameButton); 84 85 //Join game button constraint 86 layout.putConstraint(SpringLayout.WEST, joinGameButton, 20, SpringLayout.WEST, this); 87 layout.putConstraint(SpringLayout.SOUTH, joinGameButton, -20, SpringLayout.SOUTH, this); 88 layout.putConstraint(SpringLayout.EAST, joinGameButton, 0, SpringLayout.EAST, allscoreButton); 89 90 //game status label constraint 91 layout.putConstraint(SpringLayout.WEST, gameStatusLabel, 0, SpringLayout.WEST, grid); 92 layout.putConstraint(SpringLayout.NORTH, gameStatusLabel, -20, SpringLayout.NORTH, grid); 93 94 //current game label constraint 95 layout.putConstraint(SpringLayout.WEST, currentGameLabel, 0, SpringLayout.WEST, grid); 96 layout.putConstraint(SpringLayout.NORTH, currentGameLabel, -35, SpringLayout.NORTH, grid); 97 98 //game timer label constraints 99 layout.putConstraint(SpringLayout.EAST, gameTimerLabel, 0, SpringLayout.EAST, grid); 100 layout.putConstraint(SpringLayout.NORTH, gameTimerLabel, -20, SpringLayout.NORTH, grid); 101 102 layout.putConstraint(SpringLayout.WEST, grid, 40, SpringLayout.EAST, allscoreButton); 103 layout.putConstraint(SpringLayout.NORTH, grid, 40, SpringLayout.NORTH, this); 104 layout.putConstraint(SpringLayout.SOUTH, grid, 240, SpringLayout.NORTH, this); 105 layout.putConstraint(SpringLayout.EAST, grid, -20, SpringLayout.EAST, this); 106 107 //logout button constraint 108 layout.putConstraint(SpringLayout.SOUTH, logoutButton, -80, SpringLayout.SOUTH, this); 109 layout.putConstraint(SpringLayout.EAST, logoutButton, -20, SpringLayout.EAST, this); 110 111 //score button constraint 112 layout.putConstraint(SpringLayout.SOUTH, scoreButton, -20, SpringLayout.SOUTH, this); 113 layout.putConstraint(SpringLayout.EAST, scoreButton, -20, SpringLayout.EAST, this); 114 115 openGamesList.addListSelectionListener(e -> { 116 if (!e.getValueIsAdjusting() && openGamesList.getSelectedValue() != null) { 117 String selectedGame = openGamesList.getSelectedValue(); 118 119 String[] parts = selectedGame.split(" "); 120 if (parts.length >= 2) { 121 String gameID = parts[1]; // The GID is the second part 122 // Debug: Print the extracted GID 123 124 // Store the game ID to use in the joinGame action 125 client.GID_Temp = gameID; 126 127 } else { 128 // Handle cases where the format doesn't match 129 } 130 } 131 }); 132 133 //Create new game button 134 createGameButton.addActionListener(e -> { 135 //If the user is already in a game they cannot create a new game 136 if (!client.HOST_UID.isEmpty()) { 137 JOptionPane.showMessageDialog(null, "You cannot create a new game since you already have one.", "NOO.", JOptionPane.INFORMATION_MESSAGE); 138 return; 139 } 140 141 //call the WS to create a new game using the client UID (user id) 142 client.GID = client.proxy.newGame(Integer.parseInt(client.UID)); 143 144 if (client.GID != null && !client.GID.isEmpty()) { 145 //assign the user's UID to the HOST User id 146 client.HOST_UID = client.UID; 147 //The user must move first 148 client.OPPONENTS_TURN = false; 149 String newGameData = "Game " + client.GID + " Host: " + client.UID; 150 if (client.openGames == null || client.openGames.isEmpty()) { 151 client.openGames = newGameData; 152 } else { 153 client.openGames += "\n" + newGameData; //seperates each new game 154 } 155 } 156 //Start the timer - as per assignment specification 157 startTimer(); 158 // refresh the list to show if there is a new game added 159 refresh(); 160 }); 161 162 joinGameButton.addActionListener(e -> { 163 // join the game using GID 164 if (client.GID_Temp != null) { 165 if (!client.UID.equals(client.HOST_UID)) { 166 client.resetGame(); 167 client.proxy.joinGame(Integer.parseInt(client.UID), Integer.parseInt(client.GID_Temp)); 168 client.GID = client.GID_Temp; 169 client.HOST_UID = ""; 170 } 171 } else { 172 System.out.println("no game selected."); 173 } 174 }); 175 176 //logout button 177 logoutButton.addActionListener(e -> { 178 179 //reset the client info 180 client.logout(); 181 182 //go back to the start panel 183 client.showPanel(new login(client)); 184 185 }); 186 187 //forfeit button action listener 188 forfeitButton.addActionListener(e -> { 189 //If the user is also the host 190 if (client.UID.equals(client.HOST_UID)) { 191 //Set the game state to 2 indicating that player 2 has won 192 client.proxy.setGameState(Integer.parseInt(client.GID), 2); 193 194 } else { 195 //Set the game state to 1 indicating that player 1 has won 196 client.proxy.setGameState(Integer.parseInt(client.GID), 1); 197 } 198 199 //Reset the game for the user after pressing forfeit 200 client.resetGame(); 201 //Go back to the main menu 202 client.showPanel(new MainContentPanel(client)); 203 refresh(); 204 205 }); 206 207 //Score button 208 scoreButton.addActionListener(e -> { 209 210 //Go to the clients score page 211 client.showPanel(new score(client)); 212 213 }); 214 215 //leaderboard button 216 allscoreButton.addActionListener(e -> { 217 218 //Go to the leaderboard page 219 client.showPanel(new allscore(client)); 220 221 }); 222 } 223 224 @Override 225 public void refresh() { 226 // System.out.println("Raw client.openGames data: " + client.openGames); // Debugging output 227 228 grid.refresh(); 229 230 if (client.OPPONENTS_TURN) { 231 gameStatusLabel.setText("Opponents turn!"); 232 } else { 233 gameStatusLabel.setText("Your turn!"); 234 } 235 236 // Check if openGames is empty/null, if there is nothin then display nothing in the box 237 if (client.openGames == null || client.openGames.isEmpty()) { 238 openGamesList.setListData(new String[0]); 239 } else { 240 String[] rawGames = client.openGames.split("\n"); 241 242 if (previousRawGames == null) { 243 previousRawGames = new String[0]; 244 } 245 246 String[] formattedGames = new String[rawGames.length]; 247 248 //extract the GID, Host, and time (mabye add time later or just data or just time) 249 for (int i = 0; i < rawGames.length; i++) { 250 String gameData = rawGames[i].trim(); 251 String[] parts = gameData.split(","); 252 253 if (parts.length == 3) { 254 String gameID = parts[0].trim(); 255 String hostID = parts[1].trim(); 256 String timestamp = parts[2].trim(); //use later mabye? 257 258 // formating the output in the box 259 formattedGames[i] = "Game " + gameID + " Host: " + hostID; 260 } else { 261 formattedGames[i] = " "; //set the box to nothing the format isnt right i.e if there is no host name 262 } 263 } 264 265 if (rawGames.length != previousRawGames.length) { 266 openGamesList.setListData(formattedGames); 267 } 268 previousRawGames = rawGames; 269 270 //set userNameLabel as the current user logged in 271 currentGameLabel.setText("Current Game : " + client.GID); 272 } 273 } 274 275 private void startTimer() { 276 //Remaining time is 15 minutes 277 //After 15 minutes the game should delete if nobody has joined 278 //Can comment out the line below for testing as its onlt 15 seconds 279 //remainingTime = 1 * 15; 280 281 //commented out as longer duration 282 remainingTime = 15 * 60; 283 284 // timer that updates every second 285 timer = new Timer(1000, new ActionListener() { 286 @Override 287 public void actionPerformed(ActionEvent e) { 288 289 //countdown remaining timer 290 remainingTime--; 291 292 //Minutes 293 int minutes = remainingTime / 60; 294 295 //seconds 296 int seconds = remainingTime % 60; 297 298 //Format the time 299 String time = String.format("%02d:%02d", minutes, seconds); 300 301 //Update the game timer label on main content page with time 302 gameTimerLabel.setText(time); 303 304 //Check if theres been 2 moves 305 if (client.NUM_OF_MOVES >= 2) { 306 //if there has stop the timer 307 timer.restart(); 308 timer.stop(); 309 String time2 = String.format("15m 0s"); 310 gameTimerLabel.setText(time2); 311 312 } 313 314 //If the remaining time reaches 0 315 if (remainingTime <= 0) { 316 317 //Stop the timer 318 timer.stop(); 319 gameTimerLabel.setText("No opponent, please try again"); 320 321 //delete the game using WS deleteGame() 322 String deleteGame = client.proxy.deleteGame(Integer.parseInt(client.GID), Integer.parseInt(client.UID)); 323 324 System.out.println(deleteGame); 325 //Reset the game 326 client.resetGame(); 327 328 //Put user back into main menu 329 client.showPanel(new MainContentPanel(client)); 330 refresh(); 331 332 } 333 } 334 335 }); 336 //start timer 337 timer.start(); 338 } 339 }