-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswtparser.php
251 lines (225 loc) · 7.79 KB
/
swtparser.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
<?php
/**
* SWT parser: Parsing binary Swiss Chess Tournament files
*
* Part of »Zugzwang Project«
* https://www.zugzwang.org/projects/swtparser
*
* @author Falco Nogatz, [email protected]
* @author Gustaf Mossakowski, [email protected]
* @author Jacob Roggon
* @copyright Copyright © 2012, 2016 Falco Nogatz
* @copyright Copyright © 2005, 2012-2014, 2016-2017, 2019, 2022 Gustaf Mossakowski
* @copyright Copyright © 2005 Jacob Roggon
* @license http://opensource.org/licenses/lgpl-3.0.html LGPL-3.0
*/
/*
SWT parser: parsing binary Swiss Chess Tournament files
Copyright (C) 2005, 2012 Gustaf Mossakowski, Jacob Roggon, Falco Nogatz
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// required files
require_once 'fileparsing.php';
/**
* Parses an SWT file from SwissChess and returns data in an array
* Parst ein SWT file aus SwissChess und gibt Daten in Liste zurück
*
* @param string $filename
* @param string $encoding output encoding, defaults to UTF-8, file is ISO-8859-1
* @return array
* array 'out' data for further processing
* array 'bin' data for marking up binary output
*/
function swtparser($filename, $encoding = 'UTF-8') {
if (!$filename) {
echo '<p>Please choose a filename! / Bitte wählen Sie einen Dateinamen aus!</p>';
return false;
}
$contents = file_get_contents($filename);
if (!$contents) return false;
define('START_FILEVERSION', 261);
define('END_FILEVERSION', 262);
define('FILEVERSION', hexdec(bin2hex(strrev(zzparse_binpos($contents, START_FILEVERSION, END_FILEVERSION)))));
define('ENCODING_OUT', $encoding);
// read common tournament data
// Allgemeine Turnierdaten auslesen
$tournament = zzparse_interpret($contents, 'general');
$structure = swtparser_get_structure();
// common data lengths
// Allgemeine Datenlängen
define('LEN_PAARUNG', $structure['length:pairing']);
define('START_PARSING', $structure['start:fixtures_players']);
define('LEN_SPIELER_KARTEI', $structure['length:player']);
define('LEN_MANNSCHAFT_KARTEI', $structure['length:team']);
// index card for teams
// Karteikarten Mannschaften
if ($tournament['out'][35]) {
list($tournament['out']['Teams'], $bin) = swtparser_records($contents, $tournament['out'], 'Teams');
$tournament['bin'] = array_merge($tournament['bin'], $bin);
}
// index card for players
// Karteikarten Spieler
list($tournament['out']['Spieler'], $bin) = swtparser_records($contents, $tournament['out'], 'Spieler');
$tournament['bin'] = array_merge($tournament['bin'], $bin);
// team fixtures, at least one round has to be fixed
// Mannschaftspaarungen
if ($tournament['out'][35] AND $tournament['out'][3]) {
list($tournament['out']['Mannschaftspaarungen'], $bin) = swtparser_fixtures($contents, $tournament['out'], 'Teams');
$tournament['bin'] = array_merge($tournament['bin'], $bin);
} else {
$tournament['out']['Mannschaftspaarungen'] = [];
}
// player fixtures, at least one round has to be fixed
// Einzelpaarungen
if ($tournament['out'][3]) {
list($tournament['out']['Einzelpaarungen'], $bin) = swtparser_fixtures($contents, $tournament['out'], 'Spieler');
$tournament['bin'] = array_merge($tournament['bin'], $bin);
} else {
$tournament['out']['Einzelpaarungen'] = [];
}
return $tournament;
}
/**
* Gets the structure information as key-value-pairs
*
* @return array
*/
function swtparser_get_structure() {
$filename = zzparse_get_filename('structure.csv');
$array = [];
$rows = file($filename);
for ($i = 0; $i < count($rows); $i++) {
$row = str_getcsv($rows[$i], "\t");
if (preg_match('/^\w/', $row[0])) $array[$row[0]] = $row[1];
}
return $array;
}
/**
* Parses record cards for single players and teams
*
* @param array $contents
* @param array $tournament
* @param string $type ('Spieler', 'Teams')
* @return array
*/
function swtparser_records($contents, $tournament, $type = 'Spieler') {
if ($tournament[3]) {
// there is at least one round fixed
if ($tournament[35]) {
$startval = (START_PARSING
+ ($tournament[4] * $tournament[1] * $tournament[33] * LEN_PAARUNG)
+ ($tournament[80] * $tournament[1] * $tournament[33] * LEN_PAARUNG));
} else {
$startval = (START_PARSING
+ ($tournament[4] * $tournament[1] * $tournament[33] * LEN_PAARUNG));
}
} else {
$startval = START_PARSING;
}
switch ($type) {
case 'Spieler':
$maxval = $tournament[4];
$structfile = 'player';
$len_kartei = LEN_SPIELER_KARTEI;
break;
case 'Teams':
$startval = ($startval + $tournament[4] * LEN_SPIELER_KARTEI);
$maxval = $tournament[80];
$structfile = 'team';
$len_kartei = LEN_MANNSCHAFT_KARTEI;
break;
}
$records = [];
$bin = [];
for ($i = 0; $i < $maxval; $i++) {
$data = zzparse_interpret($contents, $structfile, $startval + $i * $len_kartei, $len_kartei);
$bin = array_merge($bin, $data['bin']);
if ($type === 'Teams') {
$records[$data['out'][1019]] = $data['out'];
} else {
$records[$data['out'][2020]] = $data['out'];
}
}
return [$records, $bin];
}
/**
* Parses fixtures for single players and teams
*
* @param array $contents
* @param array $tournament
* @param string $type ('Spieler', 'Teams')
* @return array [player ID][round] = data
*/
function swtparser_fixtures($contents, $tournament, $type = 'Spieler') {
$fixtures = [];
$runde = 1;
$ids = array_keys($tournament[$type]);
$index = -1;
$startval = START_PARSING;
switch ($type) {
case 'Spieler':
$max_i = $tournament[1] * $tournament[33] * $tournament[4];
$structfile = 'individual-pairings';
$name_field = 2000;
$opponent_field = 4001;
break;
case 'Teams':
$startval += $tournament[1] * $tournament[33] * $tournament[4] * LEN_PAARUNG;
$max_i = $tournament[1] * $tournament[33] * $tournament[80];
$structfile = 'team-pairings';
$name_field = 1000;
$opponent_field = 3002;
break;
}
$bin = [];
for ($i = 0; $i < $max_i; $i++) {
// Teams, starting with index 0
// Mannschaften, beginnend mit Index 0
if ($runde == 1) $index++;
if (!isset($ids[$index])) continue;
$id = $ids[$index];
$pos = $startval + $i * LEN_PAARUNG;
$data = zzparse_interpret($contents, $structfile, $pos, LEN_PAARUNG);
$bin = array_merge($bin, $data['bin']);
if (isset($tournament[$type][$data['out'][$opponent_field]])) {
$data['out']['Gegner_lang'] = $tournament[$type][$data['out'][$opponent_field]][$name_field];
} elseif ($data['out'][$opponent_field] !== '00') {
$data['out']['Gegner_lang'] = 'UNKNOWN '.$data['out'][$opponent_field];
} else {
$data['out']['Gegner_lang'] = '';
}
$fixtures[$id][$runde] = $data['out'];
// increment round, when reaching maximum rounds, start over again
// Runde einen erhoehen, nach max. Rundenzahl wieder von vorne beginnen
if ($runde == $tournament[1]) $runde = 1;
else $runde++;
}
return [$fixtures, $bin];
}
/**
* Gets a list of field names by a given language
* Erzeugt eine Liste von Feldbezeichnern zu einer gegebenen Sprache
*
* @param array $language (two-letter language code)
* @return array field names
*/
function swtparser_get_field_names($language) {
$field_names = [];
$rows = file(__DIR__.'/definitions/field-names/'.$language.'.csv');
for ($i = 0; $i < count($rows); $i++) {
$row = str_getcsv($rows[$i], "\t");
if ($row[0] AND preg_match('/^\d/', $row[0])) {
$field_names[$row[0]] = $row[1];
}
}
return $field_names;
}