1
2 package hoplugins.teamAnalyzer.ht;
3
4 import hoplugins.Commons;
5
6 import hoplugins.teamAnalyzer.SystemManager;
7 import hoplugins.teamAnalyzer.manager.PlayerDataManager;
8 import hoplugins.teamAnalyzer.vo.PlayerInfo;
9
10 import org.w3c.dom.Document;
11 import org.w3c.dom.Node;
12
13 import plugins.IXMLParser;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18
19 /***
20 * Hattrick Download Helper class
21 *
22 * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
23 */
24 public class HattrickManager {
25
26
27 /***
28 * Method that download from Hattrick the available matches for the team
29 *
30 * @param teamId teamid to download matches for
31 */
32 public static void downloadMatches(int teamId) {
33 String xml = "";
34
35 try {
36 xml = Commons.getModel().getDownloadHelper().getHattrickXMLFile("/common/matches.asp?outputType=XML&actionType=view&TeamID="
37 + teamId);
38 } catch (Exception e) {
39 return;
40 }
41
42 IXMLParser parser = Commons.getModel().getXMLParser();
43 Document dom = parser.parseString(xml);
44 Node matchesList = dom.getElementsByTagName("MatchList").item(0);
45
46 for (int i = 0; i < matchesList.getChildNodes().getLength(); i++) {
47 String matchId = matchesList.getOwnerDocument().getElementsByTagName("MatchID").item(i)
48 .getFirstChild().getNodeValue();
49 String status = matchesList.getOwnerDocument().getElementsByTagName("Status").item(i)
50 .getFirstChild().getNodeValue();
51
52 if (status.equalsIgnoreCase("FINISHED")) {
53 Commons.getModel().getHelper().downloadMatchData(Integer.parseInt(matchId));
54 } else {
55 break;
56 }
57 }
58 }
59
60 /***
61 * Method that download from Hattrick the current players for the team
62 *
63 * @param teamId teamid to download players for
64 */
65 public static void downloadPlayers(int teamId) {
66 String xml = "";
67
68 try {
69 xml = Commons.getModel().getDownloadHelper().getHattrickXMLFile("/common/players.asp?outputType=XML&actionType=view&TeamID="
70 + teamId);
71 } catch (Exception e) {
72 return;
73 }
74
75 List players = new ArrayList();
76 IXMLParser parser = Commons.getModel().getXMLParser();
77 Document dom = parser.parseString(xml);
78 Node matchesList = dom.getElementsByTagName("PlayerList").item(0);
79
80 for (int i = 0; i < (matchesList.getChildNodes().getLength() / 2); i++) {
81 PlayerInfo player = new PlayerInfo();
82 player.setTeamId(teamId);
83
84 int id = getIntValue(matchesList, i, "PlayerID");
85 player.setPlayerId(id);
86
87 int card = getIntValue(matchesList, i, "Cards");
88 int injury = getIntValue(matchesList, i, "InjuryLevel");
89 int status = PlayerDataManager.AVAILABLE;
90
91 if (card == 3) {
92 status = PlayerDataManager.SUSPENDED;
93 }
94
95 if (injury > 0) {
96 status = PlayerDataManager.INJURED;
97 }
98
99 player.setStatus(status);
100
101 int se = getIntValue(matchesList, i, "Specialty");
102 player.setSpecialEvent(se);
103
104 int form = getIntValue(matchesList, i, "PlayerForm");
105 player.setForm(form);
106
107 int exp = getIntValue(matchesList, i, "Experience");
108 player.setExperience(exp);
109
110 int age = getIntValue(matchesList, i, "Age");
111 player.setAge(age);
112
113 int tsi = getIntValue(matchesList, i, "TSI");
114 player.setTSI(tsi);
115
116 String name = getValue(matchesList, i, "PlayerName");
117 player.setName(name);
118
119 players.add(player);
120 }
121
122 PlayerDataManager.update(players);
123 SystemManager.getPlugin().getMainPanel().getRosterPanel().reload();
124 }
125
126 /***
127 * Method that download from Hattrick the team name
128 *
129 * @param teamId Tteamid to download name for
130 *
131 * @return Team Name
132 *
133 * @throws Exception if error occurs
134 */
135 public static String downloadTeam(int teamId) throws Exception {
136 String xml = Commons.getModel().getDownloadHelper().getHattrickXMLFile("/common/teamDetails.asp?outputType=XML&TeamID="
137 + teamId);
138 IXMLParser parser = Commons.getModel().getXMLParser();
139 Document dom = parser.parseString(xml);
140 Document teamDocument = dom.getElementsByTagName("Team").item(0).getOwnerDocument();
141 String teamName = teamDocument.getElementsByTagName("TeamName").item(0).getFirstChild()
142 .getNodeValue();
143
144 return teamName;
145 }
146
147 /***
148 * TODO Missing Method Documentation
149 *
150 * @param node TODO Missing Method Parameter Documentation
151 * @param i TODO Missing Method Parameter Documentation
152 * @param tag TODO Missing Method Parameter Documentation
153 *
154 * @return TODO Missing Return Method Documentation
155 */
156 private static int getIntValue(Node node, int i, String tag) {
157 try {
158 String value = getValue(node, i, tag);
159 return Integer.parseInt(value);
160 } catch (NumberFormatException e) {
161 }
162
163 return 0;
164 }
165
166 /***
167 * TODO Missing Method Documentation
168 *
169 * @param node TODO Missing Method Parameter Documentation
170 * @param i TODO Missing Method Parameter Documentation
171 * @param tag TODO Missing Method Parameter Documentation
172 *
173 * @return TODO Missing Return Method Documentation
174 */
175 private static String getValue(Node node, int i, String tag) {
176 String value = node.getOwnerDocument().getElementsByTagName(tag).item(i).getFirstChild()
177 .getNodeValue();
178 return value;
179 }
180 }