1
2 package hoplugins.teamAnalyzer.report;
3
4 import hoplugins.commons.vo.MatchRating;
5
6 import hoplugins.teamAnalyzer.manager.PlayerDataManager;
7 import hoplugins.teamAnalyzer.vo.MatchDetail;
8 import hoplugins.teamAnalyzer.vo.PlayerPerformance;
9
10 import java.util.Iterator;
11 import java.util.LinkedHashMap;
12 import java.util.Map;
13
14
15 /***
16 * The main report containing all the data
17 *
18 * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
19 */
20 public class TeamReport {
21
22
23 /*** Map of SpotReport */
24 private Map spotReports;
25
26 /*** Match Ratings */
27 private MatchRating rating;
28
29 /*** Average stars */
30 private double averageStars;
31
32 /*** Number of matches considered */
33 private int matchNumber;
34
35
36
37 /***
38 * Creates a new TeamReport object.
39 */
40 public TeamReport() {
41 spotReports = new LinkedHashMap();
42 rating = new MatchRating();
43 matchNumber = 0;
44 averageStars = 0d;
45 }
46
47
48
49 /***
50 * DOCUMENT ME!
51 *
52 * @return
53 */
54 public MatchRating getRating() {
55 return rating;
56 }
57
58 /***
59 * Returns the spot report for the specified spot field
60 *
61 * @param spot the spot number we want
62 *
63 * @return SpotReport
64 */
65 public SpotReport getSpotReport(int spot) {
66 return (SpotReport) spotReports.get("" + spot);
67 }
68
69 /***
70 * Document me!
71 *
72 * @return
73 */
74 public double getStars() {
75 return averageStars;
76 }
77
78 /***
79 * Add a match to the report
80 *
81 * @param matchDetail Match to be analyzed
82 * @param showUnavailable consider also unavailable or not
83 */
84 public void addMatch(MatchDetail matchDetail, boolean showUnavailable) {
85 for (Iterator iter = matchDetail.getPerformances().iterator(); iter.hasNext();) {
86 addPerformance((PlayerPerformance) iter.next(), showUnavailable);
87 }
88
89 addRating(matchDetail.getRating());
90 addStars(matchDetail.getStars());
91 matchNumber++;
92 }
93
94 /***
95 * Add a performance to the correct SpotReport
96 *
97 * @param pp
98 * @param showUnavailable
99 */
100 private void addPerformance(PlayerPerformance pp, boolean showUnavailable) {
101 if ((!showUnavailable) && (pp.getStatus() != PlayerDataManager.AVAILABLE)) {
102 return;
103 }
104
105 SpotReport spotReport = getSpotReport(pp.getId());
106
107 if (spotReport == null) {
108 spotReport = new SpotReport(pp);
109 spotReports.put(pp.getId() + "", spotReport);
110 }
111
112 spotReport.addPerformance(pp);
113 }
114
115 /***
116 * Updated the ratings
117 *
118 * @param aRating new match ratings
119 */
120 private void addRating(MatchRating aRating) {
121 rating.setMidfield(updateAverage(rating.getMidfield(), aRating.getMidfield()));
122 rating.setLeftDefense(updateAverage(rating.getLeftDefense(), aRating.getLeftDefense()));
123 rating.setCentralDefense(updateAverage(rating.getCentralDefense(),
124 aRating.getCentralDefense()));
125 rating.setRightDefense(updateAverage(rating.getRightDefense(), aRating.getRightDefense()));
126 rating.setLeftAttack(updateAverage(rating.getLeftAttack(), aRating.getLeftAttack()));
127 rating.setCentralAttack(updateAverage(rating.getCentralAttack(), aRating.getCentralAttack()));
128 rating.setRightAttack(updateAverage(rating.getRightAttack(), aRating.getRightAttack()));
129 }
130
131 /***
132 * Updates the average stars
133 *
134 * @param stars new game stars
135 */
136 private void addStars(double stars) {
137 averageStars = updateAverage(averageStars, stars);
138 }
139
140 /***
141 * Generic calculate average method
142 *
143 * @param oldValue
144 * @param newValue
145 *
146 * @return the new average number
147 */
148 private double updateAverage(double oldValue, double newValue) {
149 double rat = ((oldValue * matchNumber) + newValue) / (matchNumber + 1);
150
151 return rat;
152 }
153 }