1 package cz.cuni.amis.pogamut.ut2004.tournament.generator;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.io.PrintWriter;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.Comparator;
9 import java.util.List;
10
11 public class UT2004DMTableGenerator {
12
13 public static final String FILE_SEPARATOR = System.getProperty("file.separator");
14
15
16 String map = "DM-1on1-Roughinery-FPS";
17 String ut2004HomeDir = "d:\\Games\\UT2004-Devel";
18 int fragLimit = 10;
19 int timeLimitMins = 10;
20 String resultDir = "..\\Round-01-Results";
21
22
23
24
25 String executor = "../ut2004-tournament-3.5.2-SNAPSHOT.one-jar.jar";
26
27
28 String sourceDir = "D:/Workspaces/Pogamut-Trunk/Competitions/PogamutCup/2013/Tournament/Bots/";
29
30
31 String targetDir = "D:/Workspaces/Pogamut-Trunk/Competitions/PogamutCup/2013/Tournament/Round-01";
32
33
34
35 private String removeImplicitDirs(String dir) {
36 StringBuffer result = new StringBuffer(dir.length());
37
38 for (int i = 0; i < dir.length(); ++i) {
39 String c1 = String.valueOf(dir.charAt(i));
40 if (c1.equals(FILE_SEPARATOR)) {
41 if (i+2 < dir.length()) {
42 String c2 = String.valueOf(dir.charAt(i+1));
43 String c3 = String.valueOf(dir.charAt(i+2));
44 if (c2.equals(".") && c3.equals(FILE_SEPARATOR)) {
45 result.append(FILE_SEPARATOR);
46 ++i;
47 ++i;
48 continue;
49 }
50 }
51 }
52 result.append(c1);
53 }
54
55 return result.toString();
56 }
57
58
59
60
61
62
63 private String relativePath(File directory, File findRelative) {
64 String result = null;
65
66 if (findRelative.getAbsolutePath().contains(directory.getAbsolutePath())) {
67
68
69 result = ".";
70 while (!directory.getAbsolutePath().equals(findRelative.getAbsolutePath())) {
71 String lastDir = findRelative.getAbsolutePath().substring(findRelative.getAbsolutePath().lastIndexOf(FILE_SEPARATOR)+1);
72 findRelative = findRelative.getParentFile();
73 result += lastDir + FILE_SEPARATOR + result;
74 }
75 result += "." + FILE_SEPARATOR + result;
76 if (result.equals("." + FILE_SEPARATOR + ".")) return ".";
77 } else
78
79 if (directory.getAbsolutePath().contains(findRelative.getAbsolutePath())) {
80
81
82
83 while (!findRelative.getAbsolutePath().equals(directory.getAbsolutePath())) {
84 directory = findRelative.getParentFile();
85 result += ".." + FILE_SEPARATOR + result;
86 }
87 result += "." + FILE_SEPARATOR + result;
88 } else {
89
90 File commonParent = findRelative.getParentFile();
91 if (commonParent == null) {
92
93 return null;
94 }
95
96 while (!directory.getAbsolutePath().contains(commonParent.getAbsolutePath())) {
97 commonParent = findRelative.getParentFile();
98 if (commonParent == null) {
99
100 return null;
101 }
102 }
103
104
105
106
107
108
109 result = ".";
110
111
112
113 while (!commonParent.getAbsolutePath().equals(directory.getAbsolutePath())) {
114 directory = directory.getParentFile();
115 result = ".." + FILE_SEPARATOR + result;
116 }
117
118
119
120 String resultRest = ".";
121
122 while (!commonParent.getAbsolutePath().equals(findRelative.getAbsolutePath())) {
123 String lastDir = findRelative.getAbsolutePath().substring(findRelative.getAbsolutePath().lastIndexOf(FILE_SEPARATOR)+1);
124 findRelative = findRelative.getParentFile();
125 resultRest = lastDir + FILE_SEPARATOR + resultRest;
126 }
127
128 result = result + FILE_SEPARATOR + resultRest;
129 }
130
131 if (result.equals("." + FILE_SEPARATOR + ".")) return ".";
132
133 result = removeImplicitDirs(result);
134
135 return result;
136 }
137
138 public void generate() {
139 File sourceDirFile = new File(sourceDir);
140 File targetDirFile = new File(targetDir);
141
142 if (!targetDirFile.exists()) {
143 if (!targetDirFile.mkdirs()) {
144 throw new RuntimeException("Failed to create directory chain towards: " + targetDirFile.getAbsolutePath());
145 }
146 }
147
148 String sourceDirRelative = relativePath(targetDirFile, sourceDirFile);
149 if (sourceDirRelative == null) sourceDirRelative = sourceDirFile.getAbsolutePath();
150
151
152 try {
153
154 FileWriter writer = new FileWriter(new File(targetDirFile, "const.bat"));
155 PrintWriter print = new PrintWriter(writer);
156 print.println("REM All match-batch-files uses following constants");
157 print.println("");
158 print.println("REM Unreal Tournament 2004 home directory");
159 print.println("SET UT2004_HOME=" + ut2004HomeDir);
160 print.println("");
161 print.println("REM UT2004DeathMatchConsole executable jar");
162 print.println("SET MATCH_EXECUTOR_JAR=" + executor);
163 print.println("");
164 print.println("REM Directory where to output match results");
165 print.println("SET MATCH_RESULT_DIR=" + resultDir);
166 print.println("");
167 print.println("REM Name of the UT2004 map to be played");
168 print.println("SET MATCH_MAP=" + map);
169 print.println("");
170 print.println("REM Target frag limit for the match");
171 print.println("SET MATCH_FRAG_LIMIT=" + fragLimit);
172 print.println("");
173 print.println("REM Time limit for the match in minutes");
174 print.println("SET MATCH_TIME_LIMIT=" + timeLimitMins);
175 print.println("");
176 print.close();
177 writer.close();
178
179 } catch (Exception e) {
180 throw new RuntimeException(e);
181 }
182
183 List<File> jars = gatherJarFiles(sourceDirFile);
184 Collections.sort(jars, new Comparator<File>() {
185
186 @Override
187 public int compare(File o1, File o2) {
188 return o1.getAbsolutePath().compareTo(o2.getAbsolutePath());
189 }
190
191 });
192
193 List<String> botIds = extractBotIds(jars);
194
195 List<String> jarsRelative = new ArrayList<String>(jars.size());
196 for (File jar : jars) {
197 String file = jar.getAbsolutePath().substring(jar.getAbsolutePath().lastIndexOf(FILE_SEPARATOR)+1);
198 String relative = sourceDirRelative + FILE_SEPARATOR + file;
199 relative = removeImplicitDirs(relative);
200 jarsRelative.add(relative);
201 }
202
203 int maxMatchNum = botIds.size() * (botIds.size()-1) / 2;
204 int matchNum = 0;
205
206 for (int i = 0; i < botIds.size(); ++i) {
207
208 String jar1 = jarsRelative.get(i);
209 String botId1 = botIds.get(i);
210
211 for (int j = i+1; j < jarsRelative.size(); ++j) {
212
213 String jar2 = jarsRelative.get(j);
214 String botId2 = botIds.get(j);
215
216 ++matchNum;
217
218 String strMatchNum = prefixNum(matchNum, maxMatchNum);
219
220 try {
221
222 FileWriter writer = new FileWriter(new File(targetDirFile, "match-" + strMatchNum + "-" + botId1 + "-vs-" + botId2 + ".bat"));
223 PrintWriter print = new PrintWriter(writer);
224
225 print.println("call const.bat");
226 print.println("");
227
228 print.println("java -jar %MATCH_EXECUTOR_JAR% ^");
229
230 print.println("-u %UT2004_HOME% ^");
231
232 print.println("-a " + jar1 + ";" + jar2 + " ^");
233 print.println("-b " + botId1 + ";" + botId2 + " ^");
234
235 print.println("-m %MATCH_MAP% ^");
236 print.println("-r %MATCH_RESULT_DIR% ^");
237 print.println("-n " + "Match" + strMatchNum + "-" + botId1 + "-vs-" + botId2 + " ^");
238 print.println("-s " + "DMServer-" + strMatchNum + " ^");
239
240 print.println("-f %MATCH_FRAG_LIMIT% ^");
241 print.println("-t %MATCH_TIME_LIMIT%");
242
243 print.close();
244 writer.close();
245
246 } catch (Exception e) {
247 throw new RuntimeException(e);
248 }
249
250 }
251 }
252
253 try {
254
255 FileWriter writer = new FileWriter(new File(targetDirFile, "execute-all.bat"));
256 PrintWriter print = new PrintWriter(writer);
257
258 matchNum = 0;
259
260 for (int i = 0; i < jars.size(); ++i) {
261
262 String botId1 = botIds.get(i);
263
264 for (int j = i+1; j < jars.size(); ++j) {
265 String botId2 = botIds.get(j);
266
267 ++matchNum;
268 String strMatchNum = prefixNum(matchNum, maxMatchNum);
269
270 print.println("call match-" + strMatchNum + "-" + botId1 + "-vs-" + botId2 + ".bat");
271 }
272 }
273
274 print.close();
275 writer.close();
276
277 } catch (Exception e) {
278 throw new RuntimeException(e);
279 }
280
281 System.out.println("MATCH BATCH FILES GENERATED!");
282
283 }
284
285 private String prefixNum(int num, int maxNum) {
286
287 String strNum = String.valueOf(num);
288 String strMaxNum = String.valueOf(maxNum);
289
290 while (strNum.length() < strMaxNum.length()) {
291 strNum = "0" + strNum;
292 }
293
294 return strNum;
295 }
296
297 private List<String> extractBotIds(List<File> jars) {
298 List<String> result = new ArrayList<String>(jars.size());
299
300 for (File jar : jars) {
301 String file = jar.getAbsolutePath().substring(jar.getAbsolutePath().lastIndexOf(FILE_SEPARATOR)+1);
302 String id = file.substring(0, file.lastIndexOf("."));
303 result.add(id);
304 }
305
306 return result;
307 }
308
309 private List<File> gatherJarFiles(File sourceDirFile) {
310 List<File> result = new ArrayList<File>();
311 for (File file : sourceDirFile.listFiles()) {
312 if (file.getAbsolutePath().toLowerCase().endsWith(".jar")) {
313 result.add(file);
314 }
315 }
316 return result;
317 }
318
319 public static void main(String[] args) {
320 UT2004DMTableGenerator generator = new UT2004DMTableGenerator();
321 generator.generate();
322 }
323
324 }