View Javadoc

1   // %3018375595:hoplugins.teamAnalyzer.manager%
2   package hoplugins.teamAnalyzer.manager;
3   
4   import hoplugins.Commons;
5   
6   import hoplugins.commons.vo.MatchRating;
7   
8   import hoplugins.teamAnalyzer.ht.CHPPManager;
9   import hoplugins.teamAnalyzer.util.MatchUtil;
10  import hoplugins.teamAnalyzer.vo.Match;
11  import hoplugins.teamAnalyzer.vo.MatchDetail;
12  import hoplugins.teamAnalyzer.vo.PlayerPerformance;
13  
14  import plugins.IMatchDetails;
15  import plugins.IMatchLineupPlayer;
16  import plugins.IMatchLineupTeam;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  
23  /***
24   * TODO Missing Class Documentation
25   *
26   * @author TODO Author Name
27   */
28  public class MatchPopulator {
29      //~ Static fields/initializers -----------------------------------------------------------------
30  
31      private static List analyzedMatch = new ArrayList();
32  
33      //~ Methods ------------------------------------------------------------------------------------
34  
35      /***
36       * TODO Missing Method Documentation
37       *
38       * @return TODO Missing Return Method Documentation
39       */
40      public static List getAnalyzedMatch() {
41          return analyzedMatch;
42      }
43  
44      /***
45       * TODO Missing Method Documentation
46       */
47      public static void clean() {
48          analyzedMatch = new ArrayList();
49      }
50  
51      /***
52       * TODO Missing Method Documentation
53       *
54       * @param matches TODO Missing Method Parameter Documentation
55       *
56       * @return TODO Missing Return Method Documentation
57       */
58      public List populate(List matches) {
59          List list = new ArrayList();
60  
61          analyzedMatch = new ArrayList();
62  
63          for (Iterator iter = matches.iterator(); iter.hasNext();) {
64              Match element = (Match) iter.next();
65  
66              if (!isMatchAvailable(element.getMatchId()) && CHPPManager.isDownloadAllowed(element)) {
67                  downloadMatch(element.getMatchId());
68              }
69  
70              try {
71                  MatchDetail md = populateMatch(element);
72  
73                  if (md != null) {
74                      list.add(md);
75                      analyzedMatch.add(md);
76                  }
77              } catch (RuntimeException e) {
78                  // DO NOTHING
79              }
80          }
81  
82          return list;
83      }
84  
85      /***
86       * TODO Missing Method Documentation
87       *
88       * @param matchId TODO Missing Method Parameter Documentation
89       *
90       * @return TODO Missing Return Method Documentation
91       */
92      private boolean isMatchAvailable(int matchId) {
93          return Commons.getModel().getHelper().existsMatchInDB(matchId);
94      }
95  
96      /***
97       * TODO Missing Method Documentation
98       *
99       * @param aMatchDetail TODO Missing Method Parameter Documentation
100      *
101      * @return TODO Missing Return Method Documentation
102      */
103     private static int getTacticLevel(IMatchDetails aMatchDetail) {
104         if (MatchUtil.isHome(aMatchDetail)) {
105             return aMatchDetail.getHomeTacticSkill();
106         } else {
107             return aMatchDetail.getGuestTacticSkill();
108         }
109     }
110 
111     /***
112      * TODO Missing Method Documentation
113      *
114      * @param aMatchDetail TODO Missing Method Parameter Documentation
115      *
116      * @return TODO Missing Return Method Documentation
117      */
118     private static int getTacticType(IMatchDetails aMatchDetail) {
119         if (MatchUtil.isHome(aMatchDetail)) {
120             return aMatchDetail.getHomeTacticType();
121         } else {
122             return aMatchDetail.getGuestTacticType();
123         }
124     }
125 
126     /***
127      * Gets a calculator for match ratings.
128      *
129      * @param aMatchDetail Match details
130      *
131      * @return Match ratings calculator
132      */
133     private MatchRating buildMatchRating(IMatchDetails aMatchDetail) {
134         MatchRating mr = new MatchRating();
135 
136         if (MatchUtil.isHome(aMatchDetail)) {
137             mr.setMidfield(aMatchDetail.getHomeMidfield());
138             mr.setLeftAttack(aMatchDetail.getHomeLeftAtt());
139             mr.setLeftDefense(aMatchDetail.getHomeLeftDef());
140             mr.setCentralAttack(aMatchDetail.getHomeMidAtt());
141             mr.setCentralDefense(aMatchDetail.getHomeMidDef());
142             mr.setRightAttack(aMatchDetail.getHomeRightAtt());
143             mr.setRightDefense(aMatchDetail.getHomeRightDef());
144         } else {
145             mr.setMidfield(aMatchDetail.getGuestMidfield());
146             mr.setLeftAttack(aMatchDetail.getGuestLeftAtt());
147             mr.setLeftDefense(aMatchDetail.getGuestLeftDef());
148             mr.setCentralAttack(aMatchDetail.getGuestMidAtt());
149             mr.setCentralDefense(aMatchDetail.getGuestMidDef());
150             mr.setRightAttack(aMatchDetail.getGuestRightAtt());
151             mr.setRightDefense(aMatchDetail.getGuestRightDef());
152         }
153 
154         return mr;
155     }
156 
157     /***
158      * TODO Missing Method Documentation
159      *
160      * @param matchId TODO Missing Method Parameter Documentation
161      *
162      * @return TODO Missing Return Method Documentation
163      */
164     private boolean downloadMatch(int matchId) {
165         return Commons.getModel().getHelper().downloadMatchData(matchId);
166     }
167 
168     /***
169      * TODO Missing Method Documentation
170      *
171      * @param aMatch TODO Missing Method Parameter Documentation
172      *
173      * @return TODO Missing Return Method Documentation
174      */
175     private MatchDetail populateMatch(Match aMatch) {
176         IMatchDetails tmpMatch = Commons.getModel().getMatchDetails(aMatch.getMatchId());
177 
178         MatchDetail matchDetail = new MatchDetail(aMatch);
179 
180         IMatchLineupTeam tmpLineupTeam = null;
181 
182         if (MatchUtil.isHome(tmpMatch)) {
183             tmpLineupTeam = Commons.getModel().getMatchLineup(aMatch.getMatchId()).getHeim();
184         } else {
185             tmpLineupTeam = Commons.getModel().getMatchLineup(aMatch.getMatchId()).getGast();
186         }
187 
188         double totStars = 0;
189 
190         for (int spot = 1; spot < 12; spot++) {
191             IMatchLineupPlayer mlp = tmpLineupTeam.getPlayerByPosition(spot);
192 
193             if (mlp.getSpielerId() > 0) {
194                 totStars += mlp.getRating();
195 
196                 PlayerPerformance pp = new PlayerPerformance(mlp);
197 
198                 pp.setStatus(PlayerDataManager.getLatestPlayerInfo(mlp.getSpielerId()).getStatus());
199 
200                 matchDetail.addMatchLineupPlayer(pp);
201             }
202         }
203 
204         MatchRating rating = buildMatchRating(tmpMatch);
205 
206         // Match is a WO skip it
207         if (rating.getHatStats() == 9) {
208             return null;
209         }
210 
211         matchDetail.setRating(rating);
212         matchDetail.setStars(totStars);
213         matchDetail.setTacticCode(getTacticType(tmpMatch));
214         matchDetail.setTacticLevel(getTacticLevel(tmpMatch));
215 
216         matchDetail.setFormation(tmpMatch.getFormation(MatchUtil.isHome(tmpMatch)));
217         NameManager.addNames(tmpMatch.getLineup(MatchUtil.isHome(tmpMatch)));
218 
219         return matchDetail;
220     }
221 }