import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Set; /** * Goals * - Store for all the goals, which links into saving them to a file. * Also it links into showing them on the GUI. * @author Akbar Sherwani * @date 20th January * @version 0.1 */ public class Goals { private ArrayList goals; private HashMap playersgoal; public Logger log; InstantReplay in; /** * Goals Constructor */ public Goals() { goals = new ArrayList(); playersgoal = new HashMap(); in = new InstantReplay(); File file = new File("goals.txt"); // starts logger try { log = new Logger(file); } catch (NullPointerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * goalScored * @param time * @param player_no * @param player_pos * @param ball_pos * @param team */ public void goalScored(int time, int player_no,int distance, Position player_pos, Position ball_pos) { GoalDetails goal = new GoalDetails(time, player_no,distance, player_pos, ball_pos); goals.add(goal); int x = (int) player_pos.getPosx(); int y = (int) player_pos.getPosy(); int bx = (int) ball_pos.getPosx(); int by = (int) ball_pos.getPosy(); // Sent to GUI in.addGoalVisio(player_no, x, y, bx, by,distance); printDetails(goal); // getGoals(); // remove afterwards // Works out who scored and whether they have scored before and // there record is increased if(playersgoal.containsKey(player_no)) { int amount = playersgoal.get(player_no); amount++; playersgoal.put(player_no, amount); } else { playersgoal.put(player_no, 1); } } /** * getGoals * gets back all goals with details and mentions them */ public int getGoals() { Set set= playersgoal.keySet(); Iterator iter = set.iterator() ; Integer number; int player = 0; int amount = 0; // Go through each position while ( iter.hasNext () ) { number = (Integer) iter.next(); int a = playersgoal.get(number.intValue()); if(amount < a) { amount = a; player = number; } } return player; } /** * playerScored * Gets how many goals a player has scored * @param int player */ public int playerScored(int player) { int x = 0; int goal = 0; while(x < goals.size()) { GoalDetails detail = goals.get(x); if(player == detail.getPlayer_no()) { goal++; } x++; } return goal; } /** * printDetails * Saves goal details to txt file * @param detail */ public void printDetails(GoalDetails detail) { int player_no = detail.getPlayer_no(); int time = detail.getTime(); int distance = detail.getDistance(); Position ppos = detail.getPlayer_pos(); Position bpos = detail.getBall_pos(); double py = ppos.getPosy(); double px = ppos.getPosx(); double bally = bpos.getPosy(); log.write("At " + time + " " + player_no + "scores from " + distance + " Player pos " + px + "," + py + " Ball Pos y " + bally, time); } /** * Shows Goal Widget * Links to GUI so so the goal details in GUI is shown */ public void show() { in.showWidget(); } }