-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.php
72 lines (56 loc) · 2.12 KB
/
parser.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
<?php
error_reporting( E_ALL & ~E_WARNING & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
require 'vendor/autoload.php';
require 'config/config.php';
$parser = null;
$game_id = "0";
if ($config['url'] != "") {
if ($argv[1] !== null) {
// take game_id from command line instead of config
// http://www.j-archive.com/showgame.php?game_id=7093
$game_id = $argv[1];
$config['url'] = "http://www.j-archive.com/showgame.php?game_id=" . $game_id;
} else {
$game_id = explode("=", $config['url'])[1];
}
print $game_id . "\n";
$parser = new \Depotwarehouse\Jeopardy\Parser\WebParser($config['url']);
} else {
$parser = new \Depotwarehouse\Jeopardy\Parser\FileParser(file_get_contents($config['file']));
}
$game = $parser->parse();
if ($game !== null) {
if (is_array($game)) {
// As a hack for the web parser we return two game objects in an array representing each round
file_put_contents('output/questions-rd1-'.$game_id.'.json', json_encode(mergePlayersWithGame($game[0]->toArray(), $config)));
file_put_contents('output/questions-rd2-'.$game_id.'.json', json_encode(mergePlayersWithGame($game[1]->toArray(), $config)));
// combined game data
file_put_contents('output/'.$game_id.'.json', json_encode( [ mergeGame($game[0]->toArray()), mergeGame($game[1]->toArray()) ] ) );
} else {
file_put_contents('output/questions-rd1-'.$game_id.'.json', json_encode(mergePlayersWithGame($game->onlyRound(1)->toArray(), $config)));
file_put_contents('output/questions-rd2-'.$game_id.'.json', json_encode(mergePlayersWithGame($game->onlyRound(2)->toArray(), $config)));
}
}
function mergeGame(array $gameData)
{
$game = [];
foreach ($gameData as $key => $value) {
$game[$key] = $value;
}
return $game;
}
function mergePlayersWithGame(array $gameData, $config)
{
$game = [];
$game['contestants'] = [ ];
foreach ($config['players'] as $player) {
$game['contestants'][] = [
'name' => ucfirst(strtolower($player)),
'score' => 0
];
}
foreach ($gameData as $key => $value) {
$game[$key] = $value;
}
return $game;
}