1 /*
2 * HalfLifeLoader.java
3 *
4 * Created on February 6, 2003, 8:53 AM
5 */
6
7 package com.mlw.fps.load.adapter;
8
9 import java.io.File;
10 import java.io.FileReader;
11 import java.io.FilenameFilter;
12 import java.io.IOException;
13 import java.io.LineNumberReader;
14 import java.net.DatagramPacket;
15 import java.net.MulticastSocket;
16 import java.text.SimpleDateFormat;
17 import java.util.Date;
18
19 import com.mlw.fps.load.DefaultLoader;
20 import com.mlw.fps.model.business.bean.SystemProperties;
21 import com.mlw.fps.model.business.vo.WeaponUsage;
22
23 /*** Loads logs from counter-strike 1.3 and up and dod 1.0 and up
24 *
25 * @author Matthew Wilson
26 */
27 public class HalfLifeLoader extends DefaultLoader implements FilenameFilter, Runnable
28 {
29 private String file = null;
30 private int lineNumber = 0;
31
32 private int index= 0;
33 private int port;
34 private String logDir;
35 private SimpleDateFormat format= new SimpleDateFormat("MM/dd/yyyy - kk:mm:ss");
36
37 //TODO: I don't like having this as a var!!
38 private WeaponUsage weaponUsage;
39
40 /*** Creates a new instance of HalfLifeLoader
41 * @param logDir directory containing the logs.
42 */
43 public HalfLifeLoader(String logDir)
44 {
45 this.logDir= logDir;
46 }
47
48 public HalfLifeLoader(int port)
49 {
50 this.port= port;
51 }
52
53 /*** @see com.mlw.fps.load.Loader
54 */
55 public void load() throws Exception
56 {
57
58 String line= new String();
59
60 File directory= new File(logDir);
61 parseDirectory(directory);
62
63 String port= SystemProperties.getProperty("port");
64 if (port != null && port.length() > 0)
65 {
66 this.port= Integer.parseInt(port);
67 Thread thread = new Thread(this);
68 thread.setDaemon(true);
69 thread.start();
70 }
71
72 }
73
74 public void parseDirectory(File directory) throws Exception
75 {
76 String line= new String();
77 File[] files= directory.listFiles(this);
78 for (int i= 0; i < files.length; i++)
79 {
80 if (files[i].isDirectory())
81 {
82 parseDirectory(files[i]);
83 }
84 else
85 {
86 lineNumber = 0;
87 file = files[i].getName();
88 FileReader reader= new FileReader(files[i]);
89 LineNumberReader lnr= new LineNumberReader(reader);
90 while ((line= lnr.readLine()) != null)
91 {
92 lineNumber++;
93 parseLine(line);
94 }
95 }
96 }
97 }
98
99 public void parseLine(String line)
100 {
101 try
102 {
103 Date date= format.parse(line.substring(2, 23));
104 line= line.substring(25, line.length()).trim();
105
106 if (line.startsWith("Started map"))
107 {
108 startMap(date, line.substring((index= (line.indexOf("\"") + 1)), line.indexOf("\"", index + 1)));
109 startGame(date);
110 }
111 else if (line.startsWith("Log file closed"))
112 {
113 endGame(date);
114 }
115 //L 05/01/2003 - 12:41:05: "Stink<2><4294967295><>" joined team "TERRORIST"
116 else if ((index= line.indexOf("\" joined team \"")) != -1)
117 {
118 String player = line.substring(line.indexOf("\"") + 1, line.indexOf("<"));
119
120 if (line.endsWith("\"TERRORIST\""))
121 {
122 setPlayerStatus(player, "TERRORIST", null);
123 }
124 else if (line.endsWith("\"CT\""))
125 {
126 setPlayerStatus(player, "CT", null);
127 }
128 }
129 else if ((index= line.indexOf("\" killed \"")) != -1)
130 {
131 String killer= line.substring(line.indexOf("\"") + 1, line.indexOf("<"));
132 String victim= line.substring((index= line.indexOf("\"", index + 1) + 1), line.indexOf("<", index));
133 String weapon= line.substring(line.lastIndexOf("\"", line.length() - 2) + 1, line.length() - 1);
134 addKill(date, killer, weapon, victim);
135 }
136 else if (line.startsWith("Team \"TERRORIST\" triggered"))
137 {
138 String event= line.substring(28, line.indexOf("\"", 29));
139 if ("Terrorists_Win".equals(event))
140 {
141 endRound("TERRORIST", date);
142 }
143 }
144 else if (line.startsWith("Team \"CT\" triggered"))
145 {
146 String event= line.substring(21, line.indexOf("\"", 22));
147 if ("CTs_Win".equals(event))
148 {
149 endRound("CT", date);
150 }
151 }
152 else if (line.startsWith("World triggered"))
153 {
154 String event= line.substring((index= (line.indexOf("\"") + 1)), line.indexOf("\"", index + 1));
155
156 if ("Round_Start".equals(event))
157 {
158 startRound(date);
159 }
160 else
161 {
162 // log.info("NOT PROCESSED: " + line);
163 }
164 }
165 else if (line.startsWith("[STATSME]"))
166 {
167 if (line.indexOf("Saving statsme rank") > 0)
168 {
169 }
170 else
171 {
172 //"weaponstats" (weapon "hegrenade") (shots "5") (hits "5") (kills "0") (headshots "0") (tks "0") (damage "128") (deaths "0")
173 //"weaponstats2" (weapon "hegrenade") (head "0") (chest "0") (stomach "0") (leftarm "0") (rightarm "0") (leftleg "0") (rightleg "0")
174 String player= line.substring(line.indexOf("\"") + 1, line.indexOf("<"));
175 String event= line.substring((index= (line.indexOf(" triggered ") + 12)), line.indexOf('"', index));
176
177 if ("weaponstats".equals(event))
178 {
179 weaponUsage= new WeaponUsage();
180
181 weaponUsage.setShots(Long.valueOf(parseStatsMeToken("shots", line)));
182 weaponUsage.setHits(Long.valueOf(parseStatsMeToken("hits", line)));
183 weaponUsage.setDamage(Long.valueOf(parseStatsMeToken("damage", line)));
184 //weaponUsage.setHitHead(Long.valueOf(parseStatsMeToken("headshots", line)));
185 }
186 else if ("weaponstats2".equals(event))
187 {
188 weaponUsage.getLocations().put("head", Long.valueOf(parseStatsMeToken("head", line)));
189 weaponUsage.getLocations().put("chest", Long.valueOf(parseStatsMeToken("chest", line)));
190 weaponUsage.getLocations().put("stomach", Long.valueOf(parseStatsMeToken("stomach", line)));
191 weaponUsage.getLocations().put("leftarm", Long.valueOf(parseStatsMeToken("leftarm", line)));
192 weaponUsage.getLocations().put("rightarm", Long.valueOf(parseStatsMeToken("rightarm", line)));
193 weaponUsage.getLocations().put("leftleg", Long.valueOf(parseStatsMeToken("leftleg", line)));
194 weaponUsage.getLocations().put("rightleg", Long.valueOf(parseStatsMeToken("rightleg", line)));
195
196 addWeaponUsage(player, parseStatsMeToken("weapon", line), weaponUsage);
197 }
198 }
199 }
200 }
201 catch (Exception e)
202 {
203 //e.printStackTrace();
204 //log.error("Error ("+file+")on line #" + lineNumber, e);
205 }
206 }
207
208 private String parseStatsMeToken(String key, String line)
209 {
210 key= " (" + key + " \"";
211 String value= line.substring(index= (line.indexOf(key) + key.length()), line.indexOf('"', index));
212 if ("hegrenade".equals(value))
213 return "grenade";
214 return value;
215 }
216
217 /*** Accept all files.
218 * @todo This should only accept *.log files.
219 */
220 public boolean accept(File file, String str)
221 {
222 return true;
223 }
224
225 /* (non-Javadoc)
226 * @see java.lang.Runnable#run()
227 */
228 public void run()
229 {
230 boolean running= true;
231
232 try
233 {
234 System.out.println("Opening socket on port: " + port);
235 MulticastSocket socket= new MulticastSocket(port);
236 while (running)
237 {
238
239 byte[] buf= new byte[256];
240 DatagramPacket recv= new DatagramPacket(buf, buf.length);
241 socket.receive(recv);
242 String line= new String(recv.getData());
243 System.out.println(line.trim().substring(8));
244 parseLine(line.trim().substring(8));
245 }
246 }
247 catch (IOException e)
248 {
249 // TODO Auto-generated catch block
250 e.printStackTrace();
251 }
252 }
253
254 }
This page was automatically generated by Maven