/** * This class implements the stats interface * * @author Adrien Martel * @version 0.1 26/12/2006 * */ public class Stats extends Thread { private Ball ball; private double shotsOnTargetT0; private double shotsOnTargetT1; private double shotsOffTargetT0; private double shotsOffTargetT1; private double numOfKicksT0; private double numOfKicksT1; private double firstThirdBallCount; private double secondThirdBallCount; private double thirdThirdBallCount; private double numOffsideT0; private double numOffsideT1; private double numCornersT0; private double numCornersT1; private int scoreT0; private int scoreT1; public GUI gui; public DetailedStats ds; /** * Constructor stats, builds a new, empty stats object. */ public Stats(Ball b, DetailedStats ds) { ball = b; this.ds = ds; shotsOnTargetT0 = 0; shotsOnTargetT1 = 0; shotsOffTargetT0 = 0; shotsOffTargetT1 = 0; numOfKicksT0 = 1; numOfKicksT1 = 1; firstThirdBallCount = 0; secondThirdBallCount = 0; thirdThirdBallCount = 0; numOffsideT0 = 0; numOffsideT1 = 0; this.start(); } /** * Run method. Will continuously loop throughout the match to compute the * statistical data into meaningful information for others to use. At every * iteration, an update to the GUI is made displaying the latest data. */ public void run() { while (true) { try { // sleep 2.5 secs before polling posi sleep(1000); double pos = ball.getPosx(); if (pos >= 17.7) thirdThirdBallCount++; else if (pos >= -17.7) secondThirdBallCount++; else firstThirdBallCount++; } catch (InterruptedException e) { e.printStackTrace(); } //update DetailedStats ds.updateThirds(getPercentFirstThird(), getPercentSecondThird(), getPercentThirdThird()); updateGUI(); } } // shots on target team 0 public int getShotsOnTargetT0() { return (int) shotsOnTargetT0; } // shots on target team 1 public int getShotsOnTargetT1() { return (int) shotsOnTargetT1; } // shots off target team 0 public int getShotsOffTargetT0() { return (int) shotsOffTargetT0; } // shots off target team 1 public int getShotsOffTargetT1() { return (int) shotsOffTargetT1; } // total shots team 0 public int getTotalShotsT0() { return (int) (shotsOffTargetT0 + shotsOnTargetT0); } // total shots team 1 public int getTotalShostsT1() { return (int) (shotsOffTargetT1 + shotsOnTargetT1); } // possession (%) team 0 public int getPossT0() { double totalKicks = (numOfKicksT0 + numOfKicksT1); if (numOfKicksT0 > numOfKicksT1) return toPerc(numOfKicksT1 / totalKicks); else if (numOfKicksT0 < numOfKicksT1) return toPerc(numOfKicksT0 / totalKicks); else return 50; } // possession (5) team 1 public int getPossT1() { double totalKicks = (numOfKicksT0 + numOfKicksT1); if (numOfKicksT0 > numOfKicksT1) return toPerc(numOfKicksT0 / totalKicks); else if (numOfKicksT0 < numOfKicksT1) return toPerc(numOfKicksT1 / totalKicks); else return 50; } // % of time the ball was in leftmost third of pitch public int getPercentFirstThird() { return toPerc(firstThirdBallCount / (firstThirdBallCount + secondThirdBallCount + thirdThirdBallCount)); } // % of time the ball was in the middle third of pitch public int getPercentSecondThird() { return toPerc(secondThirdBallCount / (firstThirdBallCount + secondThirdBallCount + thirdThirdBallCount)); } // % of time the ball was in the rightmost third of pitch public int getPercentThirdThird() { return toPerc(thirdThirdBallCount / (firstThirdBallCount + secondThirdBallCount + thirdThirdBallCount)); } // Method to correctly round for percentage calculations private int toPerc(double val) { return (int) Math.round(val * 100); } // number of offsides for team 0 public int getNumOffsidesT0() { return (int) numOffsideT0; } // number of offsides for team 1 public int getNumOffsidesT1() { return (int) numOffsideT1; } // -- Settor methods -- // add a shot on target to team 0 public void addShotOnTargetTeam0() { shotsOnTargetT0++; } // add a shot on target to team 1 public void addShotOnTargetTeam1() { shotsOnTargetT1++; } // add a shot off target to team 0 public void addShotOffTargetTeam0() { shotsOffTargetT0++; } // add a shot off target to team 1 public void addShotOffTargetTeam1() { shotsOffTargetT1++; } // add a kick team0 public void addKickTeam0() { numOfKicksT0++; } // add a kick team1 public void addKickTeam1() { numOfKicksT1++; } // add an offside team0 public void addOffsideTeam0() { numOffsideT0++; } // add an offside team1 public void addOffsideTeam1() { numOffsideT1++; } // add a corner to T0 public void addCornerTeam0() { numCornersT0++; } // add a corner to T0 public void addCornerTeam1() { numCornersT1++; } //sets the GUI object public void setGUI(GUI gui) { this.gui = gui; } //sets the latest score to the stats public void score(int left, int right) { scoreT0 = left; scoreT1 = right; } //updates all the relevant GUI components public void updateGUI() { gui.getSOffT1().setText(getShotsOffTargetT0() + ""); gui.getSOffT2().setText(getShotsOffTargetT1() + ""); gui.getSOnT1().setText(getShotsOnTargetT0() + ""); gui.getSOnT2().setText(getShotsOnTargetT1() + ""); gui.getOffsideT1().setText((int) numOffsideT0 + ""); gui.getOffsideT2().setText((int) numOffsideT1 + ""); gui.getPosT1().setText(getPossT0() + "%"); gui.getPosT2().setText(getPossT1() + "%"); gui.getConersT1().setText("" + (int) numCornersT0); gui.getCornersT2().setText("" + (int) numCornersT1); gui.getScore().setText(scoreT0 + ":" + scoreT1); } }