allscore.java (5451B)
1 package com.groupproject.tictactoeclient.ContentPanes; 2 3 import com.groupproject.tictactoeclient.TicTacToeClient; 4 import javax.swing.*; 5 import java.awt.*; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 import java.util.HashMap; 9 import java.util.Map; 10 11 /** 12 * 13 * Author: Luke 14 */ 15 public class allscore extends CustomPanel { 16 17 public allscore(TicTacToeClient client) { 18 19 SpringLayout layout = new SpringLayout(); 20 setLayout(layout); 21 22 // Text Area for displaying scores 23 JTextArea statsArea = new JTextArea(); 24 statsArea.setEditable(false); 25 statsArea.setFont(new Font("Monospaced", Font.PLAIN, 14)); 26 27 // Add Text Area inside a Scroll Pane 28 JScrollPane scrollPane = new JScrollPane(statsArea); 29 add(scrollPane); 30 31 // Back Button 32 JButton backButton = new JButton("Back"); 33 add(backButton); 34 35 // Set constraints for Scroll Pane 36 layout.putConstraint(SpringLayout.NORTH, scrollPane, 10, SpringLayout.NORTH, this); 37 layout.putConstraint(SpringLayout.WEST, scrollPane, 10, SpringLayout.WEST, this); 38 layout.putConstraint(SpringLayout.EAST, scrollPane, -10, SpringLayout.EAST, this); 39 layout.putConstraint(SpringLayout.SOUTH, scrollPane, -50, SpringLayout.SOUTH, this); 40 41 // Set constraints for Back Button 42 layout.putConstraint(SpringLayout.SOUTH, backButton, -10, SpringLayout.SOUTH, this); 43 layout.putConstraint(SpringLayout.WEST, backButton, 10, SpringLayout.WEST, this); 44 45 backButton.addActionListener(new ActionListener() { 46 @Override 47 public void actionPerformed(ActionEvent e) { 48 System.out.println("Hello, going back!"); 49 client.showPanel(new MainContentPanel(client)); 50 } 51 }); 52 53 // Populate stats area 54 try { 55 String leagueData = client.proxy.leagueTable(); 56 if (leagueData.equals("ERROR-NOGAMES")) { 57 statsArea.setText("No games found."); 58 return; 59 } else if (leagueData.equals("ERROR-DB")) { 60 statsArea.setText("Database error occurred."); 61 return; 62 } 63 64 // Split the result into rows 65 String[] games = leagueData.split("\n"); //splits the leagueTable into its games 66 Map<String, int[]> playerStats = new HashMap<>(); //The variable playerStats is of type Map<String, int[]> 67 68 for (String game : games) { 69 try { 70 String[] columns = game.split(","); //splits the games into the users ID and the game state 71 if (columns.length < 4) { 72 throw new IllegalArgumentException("Malformed row: " + game); 73 } 74 75 String player1UID = columns[1].trim(); // Player 1 UID 76 String player2UID = columns[2].trim(); // Player 2 UID 77 String gameState = columns[3].trim(); // Game state as a string 78 79 int gameStateInt = Integer.parseInt(gameState); //turns game state into an int for if statments 80 81 // update the stats for Player one 82 playerStats.putIfAbsent(player1UID, new int[3]); 83 playerStats.putIfAbsent(player2UID, new int[3]); 84 85 if (gameStateInt == 1) { // player 1 wins 86 playerStats.get(player1UID)[0]++; // increment wins for Player 1 87 playerStats.get(player2UID)[1]++; // increment losses for Player 2 88 } else if (gameStateInt == 2) { // player 2 wins 89 playerStats.get(player2UID)[0]++; // increment wins for Player 2 90 playerStats.get(player1UID)[1]++; // increment losses for Player 1 91 } else if (gameStateInt == 3) { // player 2 wins 92 playerStats.get(player2UID)[2]++; //draw 93 playerStats.get(player1UID)[2]++; //draw 94 } 95 96 } catch (Exception rowException) { 97 System.err.println("Error processing row: " + game); 98 rowException.printStackTrace(); 99 } 100 } 101 102 // Build the display string 103 StringBuilder statsBuilder = new StringBuilder(); 104 // statsBuilder.append(String.format("%-20s %-10s %-10s %-10s\n", "Username", "Wins", "Losses", "Draws"));//headers for cleaner UI 105 statsBuilder.append("-".repeat(50)).append("\n"); 106 107 for (Map.Entry<String, int[]> entry : playerStats.entrySet()) { 108 String username = entry.getKey(); //updates the wins, losses and draws for this specific user 109 int wins = entry.getValue()[0]; 110 int losses = entry.getValue()[1]; 111 int draws = entry.getValue()[2]; 112 statsBuilder.append(String.format("%-20s %-10d %-10d %-10d\n", username, wins, losses, draws)); //prints out all the wins losses and draws along with the username 113 } 114 // Set the stats to the JTextArea 115 statsArea.setText(statsBuilder.toString()); 116 117 } catch (Exception e) { 118 statsArea.setText("An error occurred while calculating player stats."); 119 e.printStackTrace(); 120 } 121 } 122 123 @Override 124 public void refresh() { 125 // not needed 126 } 127 }