-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.php
280 lines (240 loc) · 8.13 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* Example usage for SWT parser
* Beispiel für die Anwendung des SWT-Parsers
*
* Part of »Zugzwang Project«
* http://www.zugzwang.org/projects/swtparser
*
* @author Gustaf Mossakowski, [email protected]
* @author Jacob Roggon
* @copyright Copyright © 2005 Gustaf Mossakowski, Jacob Roggon
* @copyright Copyright © 2005, 2012, 2014, 2017, 2019 Gustaf Mossakowski
* @license http://opensource.org/licenses/lgpl-3.0.html LGPL-3.0
*/
?>
<!DOCTYPE html>
<title>Example for SWT Parser / Beispiel für SWT-Parser</title>
<style type="text/css">
dt { font-weight: bold; }
th { text-align: left; padding-right: .75em; vertical-align: top; }
.tabledata { max-width: 100%; overflow: auto; }
.nullbyte { color: #AAA; }
.code { font-family: monospace; }
.code .head { text-align: right; display: inline-block; width: 3em; padding: .15em 0; }
.code .bin, .code .bib
{ background: #c96b3d; }
.code .asc { background: #db9255; }
.code .b2a { background: #edc27c; }
.code .int, .code .inb, .code .in9
{ background: #f2dca9; }
.code .boo { background: #a8a889; }
.code .sel { background: #bbbbbb; }
.code em { font-style: normal; }
.data { border-collapse: collapse; }
.data thead th
{ font-size: 80%; border-bottom: 1px solid #999;
vertical-align: bottom; }
.data td { padding: .2em .75em .2em .2em; vertical-align: top;
white-space: nowrap; }
.data th { padding: .2em; }
.data tr.uneven td
{ background-color: #DDD; }
.data tr.even td
{ background-color: #EEE; }
</style>
<h1>Example for SWT Parser / Beispiel für SWT-Parser</h1>
<?php
if (!isset($dir)) {
$dir = 'data';
$files = swtparser_files($dir);
$filename = '';
if (!empty($_GET['file']) AND in_array($_GET['file'], $files)) {
$filename = $_GET['file'];
}
$own = 'example.php';
}
if (empty($filename)) {
// choose a file from existing files
// Datei aus den bestehenden Dateien auswählen
if ($files) {
?>
<p>Please choose from one of the following files: / Bitte wählen Sie eine
der folgenden Dateien aus: </p>
<ul>
<?php
foreach ($files as $file) {
echo '<li><a href="'.$own.'?file='.urlencode($file).'">'
.htmlspecialchars($file).'<a/></li>'."\n";
}
?>
</ul>
<?php
} else {
?>
<p>There are no files in the <code><?php echo $dir; ?></code>-directory to parse. /
In dem Verzeichnis <code><?php echo $dir; ?></code> gibt es keine SWT-Dateien zum auswerten.</p>
<?php
}
} else {
?>
<p>Current file is: <?php echo htmlspecialchars($filename); ?> /
Die aktuelle Datei ist: <?php echo htmlspecialchars($filename); ?></p>
<?php if ($dir === 'data') { ?>
<p><a href="<?php echo $own; ?>">Choose a different file / Eine andere Datei auswählen</a></p>
<?php } ?>
<ul>
<li><a href="<?php echo $own; ?>?file=<?php echo htmlspecialchars($filename); ?>&view=data">Data view / Datenansicht</a></li>
<li><a href="<?php echo $own; ?>?file=<?php echo htmlspecialchars($filename); ?>&view=binary">Binary view / Binäransicht</a></li>
</ul>
<?php
}
if (!empty($_GET['view'])) {
require_once 'swtparser.php';
$tournament = swtparser($dir.'/'.$filename);
switch ($_GET['view']) {
case 'binary':
?>
<h2>Binary view / Binäransicht</h2>
<?php
require_once 'filebinary.php';
echo filebinary($dir.'/'.$filename, $tournament['bin'], 'de');
break;
case 'data':
?>
<h2>Data view / Datenansicht</h2>
<ul>
<li><a href="#teams">Teams</a></li>
<li><a href="#mm-paarungen">Team fixtures / Mannschaftspaarungen</a></li>
<li><a href="#spieler">Players / Spieler</a></li>
<li><a href="#ez-paarungen">Player fixtures / Einzelpaarungen</a></li>
</ul>
<h2>Common information / Allgemeine Information</h2>
<?php
$language = 'de';
$field_names = swtparser_get_field_names($language);
if (count($field_names) == 0) {
echo 'Language <em>'.$language.'</em> currently not supported.<br><br>';
}
echo swtparser_out_tabular($tournament['out'], $field_names);
if ($tournament['out'][35]) {
echo '<h2 id="teams">Teams</h2>';
echo swtparser_out_info($tournament['out']['Teams'], $field_names);
echo swtparser_out_fixtures($tournament['out']['Mannschaftspaarungen'], 'Mannschaftspaarungen', 'mm-paarungen', $field_names);
}
echo '<h2 id="spieler">Players / Spieler</h2>';
echo swtparser_out_info($tournament['out']['Spieler'], $field_names);
echo swtparser_out_fixtures($tournament['out']['Einzelpaarungen'], 'Einzelpaarungen', 'ez-paarungen', $field_names);
break;
}
}
//
// Example functions / Beispielfunktionen
//
/**
* List all files ending .SWT in a given directory
*
* @param string $dir directory name
* @return array list of files
*/
function swtparser_files($dir) {
$files = [];
$handle = opendir($dir);
while ($file = readdir($handle)) {
if (substr($file, 0, 1) === '.') continue;
if (strtoupper(substr($file, -4)) !== '.SWT') continue;
$files[] = $file;
}
return $files;
}
/**
* Shows a list of keys and their values
* Zeigt eine Liste von Schlüsseln und ihren Werten
*
* @param array $tournament (returned array from swtparser())
* @param array $field_names (returned array from swtparser_get_field_names())
* @return string HTML output
* @see swtparser()
*/
function swtparser_out_tabular($tournament, $field_names) {
$output = '<div class="tabledata"><table class="data">';
$i = 0;
foreach (array_keys($tournament) as $key) {
$i++;
$output .= '<tr class="'.($i & 1 ? 'un' : '').'even"><th>'.(isset($field_names[$key]) ? $field_names[$key] : $key).'</th><td>';
if (!is_array($tournament[$key])) {
$output .= (preg_match('/^\d+\-\d+$/', $tournament[$key]) && $field_names[$tournament[$key]] ? $field_names[$tournament[$key]] : $tournament[$key]);
} else {
$output .= '(see below / siehe unten)';
}
$output .= '</td></tr>'."\n";
}
$output .= '</table></div>';
return $output;
}
/**
* Shows general information about players and teams
* Zeigt allgemeine Informationen über Spieler und Teams
*
* @param array $data (part of returned array from swtparser())
* @param array $field_names (returned array from swtparser_get_field_names())
* @return string HTML output
* @see swtparser()
*/
function swtparser_out_info($data, $field_names) {
if (!$data) return '<p>No data available. / Keine Daten vorhanden.</p>';
$output = '<div class="tabledata"><table class="data"><thead><th>ID</th>';
$head = reset($data);
foreach (array_keys($head) as $th) {
$output .= '<th><span>'.($field_names[$th] ? $field_names[$th] : $th).'</span></th>';
}
$output .= '</thead><tbody>';
$i = 0;
foreach (array_keys($data) as $id) {
$i++;
$output .= '<tr class="'.($i & 1 ? 'un' : '').'even"><th>'.$id.'</th>';
foreach (array_keys($data[$id]) as $key) {
$output .= '<td>'.$data[$id][$key].' </td>';
}
$output .= '</tr>';
}
$output .= '</tbody></table></div>';
return $output;
}
/**
* Shows all fixtures of a tournament
* Zeigt alle Paarungen eines Turniers
*
* @param array $fixtures (part of returned array from swtparser())
* @param string $title (optional, HTML heading)
* @param string $id (optional, HTML heading id attribute)
* @param array $field_names (returned array from swtparser_get_field_names())
* @return string HTML output
*/
function swtparser_out_fixtures($fixtures, $title = 'Paarungen', $id = 'paarungen', $field_names = []) {
if (!$fixtures) return '<p>No data available. / Keine Daten vorhanden.</p>';
$output = '<h2 id="'.$id.'">'.$title.'</h2>';
$output .= '<div class="tabledata"><table class="data"><thead><th>Round / Runde</th>';
$head = reset($fixtures);
$head = reset($head);
foreach (array_keys($head) as $th) {
$output .= '<th>'.(isset($field_names[$th]) ? $field_names[$th] : $th).'</th>';
}
$output .= '</thead>';
foreach ($fixtures AS $player => $rounds) {
$i = 0;
$output .= '<tr><td>'.$player.'</td><td></td></tr>';
// print first line with keys as head
foreach ($rounds as $round => $data) {
$i++;
$output .= '<tr class="'.($i & 1 ? 'un' : '').'even">';
$output .= '<th>'.$round.'</th>';
foreach (array_keys($data) as $key)
$output .= '<td>'.(preg_match('/^\d+\-\d+$/', $data[$key]) && isset($field_names[$data[$key]]) ? $field_names[$data[$key]] : $data[$key]).'</td>';
$output .= '</tr>';
}
$output .= '<tr><td> </td><td></td></tr>';
}
$output .= '</table></div>';
return $output;
}