-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-easywk-to-json.php
109 lines (84 loc) · 2.6 KB
/
convert-easywk-to-json.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
<?php
function parseTime(string $i): float
{
$callback = 0.0;
$i = array_reverse(preg_split("/[\.,:]/", $i));
if (isset($i[0])) {
$callback += (int) $i[0] / 100;
}
if (isset($i[1])) {
$callback += (int) $i[1];
}
if (isset($i[2])) {
$callback += (int) $i[2] * 60;
}
return round($callback, 2);
}
if (sizeof($argv) < 3) {
echo "Usage ..php {wa|master} {input filename}\n";
exit(1);
}
$MODE = $argv[1];
$FILE = $argv[2];
if (!file_exists($FILE)) {
echo "the given file does not exists\n";
exit(1);
}
$inputData = file_get_contents($FILE);
$inputData = explode("\n", $inputData);
$baseTimes = [];
if (file_exists("./src/baseTimes.json")) {
$baseTimes = json_decode(file_get_contents("./src/baseTimes.json"), true);
}
foreach ($inputData as $line) {
$line = explode(";", $line);
switch ($MODE) {
case "wa":
if (sizeof($line) >= 8) {
$stroke = $line[4];
if ($line[3] === "4") {
$stroke = "4x " . $stroke;
}
switch ($line[5]) {
case "FREE":
$stroke = $stroke . "F";
break;
case "FLY":
$stroke = $stroke . "S";
break;
case "BREAST":
$stroke = $stroke . "B";
break;
case "BACK":
$stroke = $stroke . "R";
break;
case "MEDLEY":
$stroke = $stroke . "L";
break;
}
$year = $line[0];
$course = strtolower($line[1]);
$age = "all";
$gender = strtolower($line[2]);
$baseTimes[$MODE][$year][$course][$stroke][$age][$gender] = parseTime($line[6]);
}
break;
case "master":
if (sizeof($line) >= 6) {
$year = $line[4];
$course = strtolower($line[5]);
$age = $line[1];
$stroke = $line[0];
$baseTimes[$MODE][$year][$course][$stroke][$age]["m"] = parseTime($line[2]);
$baseTimes[$MODE][$year][$course][$stroke][$age]["f"] = parseTime($line[3]);
}
break;
}
}
if (isset($baseTimes["wa"])) {
ksort($baseTimes["wa"]);
}
if (isset($baseTimes["master"])) {
ksort($baseTimes["master"]);
}
file_put_contents("./src/baseTimes.json", json_encode($baseTimes, JSON_PRETTY_PRINT));