-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathresult.php
More file actions
406 lines (345 loc) · 15.7 KB
/
result.php
File metadata and controls
406 lines (345 loc) · 15.7 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
class result
{
public static function getResultsForEvent($eventid)
{
//utils::rg2log("Get results for event ".$eventid);
$output = array();
$comments = 0;
$text = array();
// @ suppresses error report if file does not exist
if (($handle = @fopen(KARTAT_DIRECTORY."kommentit_".$eventid.".txt", "r")) !== false) {
while (($data = fgetcsv($handle, 0, "|")) !== false) {
if (count($data) >= 5) {
// remove null comments
if (!self::isDefaultComment($data[4])) {
$text[$comments]["resultid"] = $data[1];
$text[$comments]["comments"] = self::formatComments($data[4]);
$comments++;
}
}
}
fclose($handle);
}
$codes = array();
// initialise empty to deal with corrupt results files that occur sometimes
$variant = array();
if (event::isScoreEvent($eventid)) {
// read control locations visited: this includes start and finish
if (($handle = @fopen(KARTAT_DIRECTORY."ratapisteet_".$eventid.".txt", "r")) !== false) {
$row = 0;
while (($data = fgetcsv($handle, 0, "|")) !== false) {
$x = array();
$y = array();
$tempcodes = array();
// field is N separated and then comma separated
$pairs = explode("N", $data[1]);
for ($j = 0; $j < count($pairs); $j++) {
$xy = explode(";", $pairs[$j]);
// some courses seem to have nulls at the end so just ignore them
if ($xy[0] != "") {
$x[$j] = 1 * $xy[0];
// make it easier to draw map
$y[$j] = -1 * $xy[1];
// needs to be a string for javascript
$tempcodes[$j] = strval($j);
}
}
$variant[$row] = $data[0];
$xpos[$row] = $x;
$ypos[$row] = $y;
$codes[$row] = $tempcodes;
$sentalready[$row] = false;
$row++;
}
fclose($handle);
}
// read control codes visited
if (($handle = @fopen(KARTAT_DIRECTORY."hajontakanta_".$eventid.".txt", "r")) !== false) {
// if file exists then we can delete the old codes list and get the real one
unset($codes);
$codes = array();
$row = 0;
while (($data = fgetcsv($handle, 0, "|")) !== false) {
$allcodes = explode("_", $data[2]);
// add start at beginning of array
array_unshift($allcodes, "S");
// add finish at end of array
array_push($allcodes, "F");
$codes[$row] = $allcodes;
$row++;
}
fclose($handle);
}
}
// @ suppresses error report if file does not exist
if (($handle = @fopen(KARTAT_DIRECTORY."kilpailijat_".$eventid.".txt", "r")) !== false) {
$row = 0;
while (($data = fgetcsv($handle, 0, "|")) !== false) {
$detail = array();
$resultid = intval($data[0]);
$courseid = intval($data[1]);
// protect against corrupt/invalid files
// skip this record and go to next line
if (($resultid < 1) || ($courseid < 1)) {
continue;
}
$detail["resultid"] = $resultid;
$detail["courseid"] = $courseid;
$detail["coursename"] = utils::encode_rg_input($data[2]);
$detail["name"] = trim(utils::encode_rg_input($data[3]));
$detail["starttime"] = intval($data[4]);
$detail["position"] = '';
$detail["status"] = 'ok';
// look for RG2 extra fields in dbid
$databaseid = utils::encode_rg_input($data[5]);
$pos = strpos($databaseid, "_#");
if ($pos !== false) {
$extras = explode("#", substr($databaseid, $pos + 2));
if (count($extras) == 2) {
$detail["position"] = $extras[0];
$detail["status"] = $extras[1];
}
}
// score event check should be redundant but see issue #159
if (($data[6] != "") && event::isScoreEvent($eventid)) {
$detail["variant"] = intval($data[6]);
for ($i = 0; $i < count($variant); $i++) {
// only send course details the first time they occur: makes response a lot smaller for big (Jukola!) relays
if ($variant[$i] == $data[6]) {
if (!$sentalready[$i]) {
$detail["scorex"] = $xpos[$i];
$detail["scorey"] = $ypos[$i];
$detail["scorecodes"] = $codes[$i];
$sentalready[$i] = true;
}
}
}
}
list($detail["time"], $detail["secs"]) = utils::tidyTime($data[7]);
// trim trailing ;which create null fields when expanded
$temp = rtrim($data[8], ";");
// split array at ;and force to integers
$detail["splits"] = array_map('intval', explode(";", $temp));
//$detail["comments"] = "";
for ($i = 0; $i < $comments; $i++) {
if ($detail["resultid"] == $text[$i]["resultid"]) {
$detail["comments"] = $text[$i]["comments"];
}
}
$output[$row] = $detail;
$row++;
}
fclose($handle);
}
if (!event::hasResults($eventid)) {
// event with no results so need to sort times and add positions
// a lot easier to do this here in one place rather than when adding and deleting results
// avoids multiple rewrites of full results file plus need to manage route deletion
usort($output, "self::sortResultsByCourseThenTime");
$pos = 0;
$ties = 0;
$oldsecs = -1;
$courseid = -1;
for ($i = 0; $i < count($output); $i++) {
if ($courseid !== $output[$i]["courseid"]) {
// new course so reset
$pos = 1;
$courseid = $output[$i]["courseid"];
$ties = 0;
$oldsecs = -1;
} else {
// same course so check for ties
if ($oldsecs === $output[$i]["secs"]) {
$ties++;
} else {
$pos = $pos + 1 + $ties;
}
}
$output[$i]["position"] = $pos;
$oldsecs = $output[$i]["secs"];
}
}
return $output;
}
public static function formatComments($inputComments)
{
// replace carriage return and line break codes
$comments = utils::encode_rg_input($inputComments);
// RG1 uses #cr##nl# and #nl# to allow saving to text file
$comments = str_replace("#cr##nl#", "\n", $comments);
$comments = str_replace("#nl#", "\n", $comments);
$comments = str_replace("<br>", "\n", $comments);
return $comments;
}
private static function isDefaultComment($comment)
{
// returns true if the comment matches a list of possible default comments
// add new strings as necessary
// #310 would be better to read these from the language file but that is
// quite a lot of effort so keep adding here for now and put a filter on
// the front end as well
$defaults = array('Type your comment', 'Dein Kommentar', 'Kirjoita kommentti',
'Kirjoita kommentit tähän', 'Votre commentaire', 'Kommenter',
'Scrivi un tuo commento');
for ($i = 0; $i < count($defaults); $i++) {
if (strcmp($defaults[$i], $comment) == 0) {
return true;
}
}
return false;
}
public static function updateResults($eventid, $data){
$date = date("Y-m-dTHis");
$write["status_msg"] = "";
utils::rg2log("Updating result files for ".$eventid.". Old data will have datestamp: ".$date.".txt");
//Archive old kilpailijat file
rename(KARTAT_DIRECTORY."kilpailijat_".$eventid.".txt",
KARTAT_DIRECTORY."kilpailijat_".$eventid."_rm_".$date.".txt");
// create new kilpailijat file: results
for ($i = 0; $i < count($data->results); $i++) {
$a = $data->results[$i];
// save position and status if we got them
if (isset($a->position)) {
$position = $a->position;
} else {
$position = '';
}
if (isset($a->status)) {
$status = utils::abbreviateStatus($a->status);
} else {
$status = '';
}
// course provided by json is actually result class - get correct course name
// based on mapping file, and load to txt file if not "Do not save"
$coursename = "";
$courseid = "";
$fh = fopen(KARTAT_DIRECTORY."mappings_".$eventid.".txt", 'r');
while ($oldrow = fgets($fh)) {
$row = explode("|", $oldrow);
if (utils::encode_rg_output($a->course) == $row[0]) {
$coursename = $row[1];
$courseid = $row[2];
break;
}
}
if($coursename !== "Do not save"){
$result = ($i + 1)."|".trim($courseid)."|".utils::encode_rg_output($coursename);
$result .= "|".utils::encode_rg_output(trim($a->name))."|".$a->starttime."|";
// abusing dbid to save status and position
$result .= utils::encode_rg_output($a->dbid)."_#".$position."#".$status;
$result .= "|".$a->variantid."|".$a->time."|".$a->splits.PHP_EOL;
file_put_contents(KARTAT_DIRECTORY."kilpailijat_".$eventid.".txt", $result, FILE_APPEND);
}
}
// Get orig and new resultid so we can replace them in comments and routes files
// 1|1|C2|Bridget Anderson|35700|121_#1#OK||00:51:36|
$kilpailijat = array();
$fh = fopen(KARTAT_DIRECTORY."kilpailijat_".$eventid."_rm_".$date.".txt", 'r');
while ($oldrow = fgets($fh)) {
$old = explode("|", $oldrow);
$fh = fopen(KARTAT_DIRECTORY."kilpailijat_".$eventid.".txt", 'r');
while ($newrow = fgets($fh)) {
$new = explode("|", $newrow);
if ($new[3] == $old[3] && $new[2] == $old[2]){
$row = array();
$row["origresultid"] = $old[0];
$row["newresultid"] = $new[0];
$row["origcourseid"] = $old[1];
$row["newcourseid"] = $new[1];
$row["coursename"] = $old[2];
$row["name"] = $old[3];
$kilpailijat[] = $row;
break;
}
}
}
// Add GPS routes from old file
$fh = fopen(KARTAT_DIRECTORY."kilpailijat_".$eventid."_rm_".$date.".txt", 'r');
while ($oldrow = fgets($fh)) {
$old = explode("|", $oldrow);
if (strpos($old[3], 'GPS ') !== false){
// Get the current Id for the non-GPS record
foreach ($kilpailijat as $kil){
if (substr($old[3], 5) == $kil["name"] &&
$old[2] == $kil["coursename"]){
$row = array();
$row["origresultid"] = $old[0];
$row["newresultid"] = GPS_RESULT_OFFSET + $kil["newresultid"];
$row["origcourseid"] = $old[1];
$row["newcourseid"] = $kil["newcourseid"];
$row["coursename"] = $old[2];
$row["name"] = $old[3];
$kilpailijat[] = $row;
$result = $row["newresultid"]."|".$row["newcourseid"]."|".$row["coursename"];
$result .= "|".$row["name"]."|".$old[4]."|".$old[5]."|".$old[6]."|".$old[7]."|".$old[8]."|".$old[9];
file_put_contents(KARTAT_DIRECTORY."kilpailijat_".$eventid.".txt", $result, FILE_APPEND);
break;
}
}
}
}
if (file_exists(KARTAT_DIRECTORY."kommentit_".$eventid.".txt")){
// Recreate kommentit file
// Replace old course id and result id with ones from new kilpailijat file
// Kommentit format: 2|34|Jake Hanson||test
$updatedfile = array();
$fh = fopen(KARTAT_DIRECTORY."/kommentit_".$eventid.".txt", 'r');
while ($oldrow = fgets($fh)) {
$olddata = explode("|", $oldrow);
foreach ($kilpailijat as $k){
if ($k["origresultid"] == $olddata[1]){
$row = $k["newcourseid"]."|".$k["newresultid"]."|".$olddata[2]."|".$olddata[3]."|".$olddata[4];
$updatedfile[] = $row;
break;
}
}
}
//Archive old file
rename(KARTAT_DIRECTORY."kommentit_".$eventid.".txt",
KARTAT_DIRECTORY."kommentit_".$eventid."_rm_".$date.".txt");
//Write new file
file_put_contents(KARTAT_DIRECTORY."kommentit_".$eventid.".txt", $updatedfile);
utils::rg2log("Updated route comments file ");
}
if (file_exists(KARTAT_DIRECTORY."merkinnat_".$eventid.".txt")){
// Recreate merkinnat file
// Replace old course id and result id with ones from new kilpaijat file
// Merkinnat format: 2|34|Jake Hanson|null|Semicolon-separated results
$updatedfile = array();
$fh = fopen(KARTAT_DIRECTORY."merkinnat_".$eventid.".txt", 'r');
while ($oldrow = fgets($fh)) {
$olddata = explode("|", $oldrow);
foreach ($kilpailijat as $k){
if ($k["origresultid"] == $olddata[1]){
$row = $k["newcourseid"]."|".$k["newresultid"]."|".$olddata[2]."|".$olddata[2]."|".$olddata[4];
$updatedfile[] = $row;
break;
}
}
}
//Archive old file
rename(KARTAT_DIRECTORY."merkinnat_".$eventid.".txt",
KARTAT_DIRECTORY."merkinnat_".$eventid."_rm_".$date.".txt");
//Write updated file
file_put_contents(KARTAT_DIRECTORY."merkinnat_".$eventid.".txt", $updatedfile);
utils::rg2log("Updated routes file");
}
if ($write["status_msg"] == "") {
$write["ok"] = true;
$write["status_msg"] = "Results updated.";
} else {
$write["ok"] = false;
}
utils::rg2log("Updated results file ");
return $write;
utils::unlockDatabase();
}
private static function sortResultsByCourseThenTime($a, $b)
{
if (intval($a["courseid"]) !== intval($b["courseid"])) {
return (intval($a["courseid"]) - intval($b["courseid"]));
}
return (intval($a["secs"]) - intval($b["secs"]));
}
}