forked from benjamw/webrisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
review_ajax_helper.php
125 lines (92 loc) · 2.86 KB
/
review_ajax_helper.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
<?php
$GLOBALS['NODEBUG'] = true;
$GLOBALS['AJAX'] = true;
require_once 'includes/inc.global.php';
// if we are debugging, change some things for us
// (although REQUEST_METHOD may not always be valid)
if (('GET' === $_SERVER['REQUEST_METHOD']) && DEBUG) {
$GLOBALS['NODEBUG'] = false;
$GLOBALS['AJAX'] = false;
$_POST = $_GET;
$DEBUG = true;
call('REVIEW AJAX HELPER');
call($_POST);
}
if (array_key_exists('file', $_REQUEST)) {
$_SESSION['game_file'] = $_REQUEST['file'];
}
if (empty($_SESSION['game_file'])) {
echo json_encode(array('msg' => 'RELOAD'));
exit;
}
if (array_key_exists('step', $_REQUEST)) {
$_SESSION['step'] = (int) $_REQUEST['step'];
}
if (empty($_SESSION['step'])) {
$_SESSION['step'] = 0;
}
// init our game
$Review = new Review($_SESSION['game_file'], $_SESSION['step']);
// run the card clicks
if (isset($_POST['cardcheck'])) {
try {
echo $Review->get_cards($_POST['id']);
}
catch (MyException $e) {
echo 'ERROR';
}
exit;
}
// run the game review button clicks
// actually... if this is hit, they've already been handled
// just return stuff to display
$players = $Review->get_players( );
$colors = array( );
foreach ($players as $key => $player) {
$colors[$player['color']] = htmlentities($GLOBALS['_PLAYERS'][$key]).' ['.$key.']';
}
try {
$board = strip_whitespace(board($Review));
call($board);
$players = strip_whitespace($Review->draw_players( ));
call($players);
$game_info = game_info($Review); // don't strip_whitespace, it breaks the JS included
call($game_info);
$move_info = nl2br(trim(trim($Review->get_move_info( ), " -=+")));
// wrap the player name in a class of the players color
foreach ($colors as $color => $player) {
if (false !== strpos($move_info, $player)) {
$move_info = str_replace($player, '<span class="'.substr($color, 0, 3).'">'.$player.'</span>', $move_info);
}
}
call($move_info);
$dice = '';
$move = $Review->get_step( );
if ('A' === $move{0}) {
list($type, $action) = explode(' ', $move);
$action = explode(':', $action);
$rolls = explode(',', $action[4]);
$players_array = $Review->get_players( );
$attack_class = substr($players_array[$action[0]]['color'], 0, 3);
$defend_class = substr($players_array[$action[2]]['color'], 0, 3);
$dice .= '<div class="attack">';
foreach (str_split($rolls[0]) as $die) {
$dice .= '<div class="'.$attack_class.' dc'.$die.'">'.$die.'</div>';
}
$dice .= '</div><div class="defend">';
foreach (str_split($rolls[1]) as $die) {
$dice .= '<div class="'.$defend_class.' dc'.$die.'">'.$die.'</div>';
}
$dice .= '</div>';
}
call($dice);
$trade = $Review->get_trade_value( );
call($trade);
echo json_encode(compact('board', 'dice', 'game_info', 'move_info', 'players', 'trade'));
}
catch (MyException $e) {
echo 'ERROR: '.$e->outputMessage( );
}
function strip_whitespace($string) {
return preg_replace('%[\s]+%', ' ', $string);
}