PollingThread.java (5922B)
1 /* 2 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 3 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template 4 */ 5 package com.groupproject.tictactoeclient; 6 7 import com.groupproject.tictactoeclient.ContentPanes.MainContentPanel; 8 import java.util.logging.Level; 9 import java.util.logging.Logger; 10 import javax.swing.JOptionPane; 11 12 /** 13 * 14 * @author William 15 */ 16 public class PollingThread implements Runnable { 17 18 private TicTacToeClient client; 19 private HelperMethods helperMethods; 20 21 //flag used to keep track if game is over so the dialog box doesnt keep appearing 22 private boolean gameOver = false; 23 24 public PollingThread(TicTacToeClient client) { 25 this.client = client; 26 helperMethods = new HelperMethods(client); 27 } 28 29 /** 30 * TicTacToe polling thread. 31 * Calls on TicTacToeClient to update the currently open CustomPanel, extending JPanel 32 * Also used to periodically fetch new data from server (once per second) 33 */ 34 @Override 35 public void run() { 36 while (true) { 37 try { 38 Thread.sleep(1000); 39 40 if (client.UID == null || client.UID.isEmpty() || Integer.parseInt(client.UID) == -1) { 41 continue; 42 } 43 44 client.BOARD = helperMethods.GetBoard(); 45 46 client.openGames = client.proxy.showOpenGames(); 47 client.refreshCurrentPanel(); 48 System.out.println("open games panel resetting\n"); 49 50 // if there is no game id, dont run code below 51 if (client.GID.isBlank() || client.GID.isEmpty()) { 52 continue; 53 } 54 55 // if there are no moves, dont run code below 56 if (client.NUM_OF_MOVES < 2) { 57 continue; 58 } 59 60 //call the web server to check if the game has been won or not yet using the WS checkWin function 61 String result = client.proxy.checkWin(Integer.parseInt(client.GID)); 62 System.out.println("checkWin: " + result); 63 //if the game is not over check the result of the game 64 switch (result) { 65 66 case "1": 67 // Player 1 wins 68 //Show on both player's frames player 1 has won 69 JOptionPane.showMessageDialog(client.frame, "Player 1 wins!"); 70 //Set the gamestate to 1 (player 1 has won) 71 client.proxy.setGameState(Integer.parseInt(client.GID), 1); 72 //Show opions - main menu or quit 73 helperMethods.showOptions(); 74 gameOver = true; 75 break; 76 case "2": 77 // Player 2 wins, show on both player's frame player 2 has won 78 JOptionPane.showMessageDialog(client.frame, "Player 2 wins!"); 79 //Set the game state to 2 indicating player 2 has won the game 80 client.proxy.setGameState(Integer.parseInt(client.GID), 2); 81 //Show options - main menu or quit 82 helperMethods.showOptions(); 83 gameOver = true; 84 break; 85 case "3": 86 // It's a draw, show on both players screen its a draw 87 JOptionPane.showMessageDialog(client.frame, "It's a draw!"); 88 //Set the gamestate to 3 indicating game is a draw 89 client.proxy.setGameState(Integer.parseInt(client.GID), 3); 90 //Show options - main menu or quit 91 helperMethods.showOptions(); 92 gameOver = true; 93 break; 94 case "0": 95 // Game continues 96 break; 97 default: 98 JOptionPane.showMessageDialog(client.frame, "Error checking the game status."); 99 100 } 101 102 //call the web server to check if the game has been won or not yet if the player is forfeiting 103 String gameState = client.proxy.getGameState(Integer.parseInt(client.GID)); 104 switch (gameState) { 105 106 case "1": 107 // Player 1 wins 108 // This message appears on opponents screen indicating the other player has forfeited 109 JOptionPane.showMessageDialog(client.frame, "The other player has forfeited, you win"); 110 // Opponent is presented with option to go to main menu or quit 111 helperMethods.showOptions(); 112 gameOver = true; 113 break; 114 case "2": 115 // Player 2 wins 116 //This message appears on the opponents screen indicating the other player has forfeited 117 JOptionPane.showMessageDialog(client.frame, "The other player has forfeited, you win"); 118 //Opponent presented with option to go to main menu or quit 119 helperMethods.showOptions(); 120 gameOver = true; 121 break; 122 case "3": 123 // Player 2 wins 124 JOptionPane.showMessageDialog(client.frame, "It's a draw!"); 125 //Opponent presented with option to go to main menu or quit 126 helperMethods.showOptions(); 127 gameOver = true; 128 break; 129 } 130 } catch (Exception e) { 131 e.printStackTrace(); 132 } 133 } 134 } 135 }