forked from benjamw/battleship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax_helper.php
258 lines (197 loc) · 5.07 KB
/
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
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
<?php
$GLOBALS['NODEBUG'] = true;
$GLOBALS['AJAX'] = true;
// don't require log in when testing for used usernames and emails
if (isset($_POST['validity_test']) || (isset($_GET['validity_test']) && isset($_GET['DEBUG']))) {
define('LOGIN', false);
}
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']) && defined('DEBUG') && DEBUG) {
$GLOBALS['NODEBUG'] = false;
$GLOBALS['AJAX'] = false;
$_GET['token'] = $_SESSION['token'];
$_GET['keep_token'] = true;
$_POST = $_GET;
$DEBUG = true;
call('AJAX HELPER');
call($_POST);
}
// run the index page refresh checks
if (isset($_POST['timer'])) {
$message_count = (int) Message::check_new($_SESSION['player_id']);
$turn_count = (int) Game::check_turns($_SESSION['player_id']);
echo $message_count + $turn_count;
exit;
}
// run registration checks
if (isset($_POST['validity_test'])) {
# if (('email' == $_POST['type']) && ('' == $_POST['value'])) {
# echo 'OK';
# exit;
# }
$player_id = 0;
if ( ! empty($_POST['profile'])) {
$player_id = (int) $_SESSION['player_id'];
}
switch ($_POST['validity_test']) {
case 'username' :
case 'email' :
$username = '';
$email = '';
${$_POST['validity_test']} = sani($_POST['value']);
$player_id = (isset($_POST['player_id']) ? (int) $_POST['player_id'] : 0);
try {
Player::check_database($username, $email, $player_id);
}
catch (MyException $e) {
echo $e->getCode( );
exit;
}
break;
default :
break;
}
echo 'OK';
exit;
}
// run the in game chat
if (isset($_POST['chat'])) {
try {
if ( ! isset($_SESSION['game_id'])) {
$_SESSION['game_id'] = 0;
}
$Chat = new Chat((int) $_SESSION['player_id'], (int) $_SESSION['game_id']);
$Chat->send_message($_POST['chat'], isset($_POST['private']), isset($_POST['lobby']));
$return = $Chat->get_box_list(1);
$return = $return[0];
}
catch (MyException $e) {
$return['error'] = 'ERROR: '.$e->outputMessage( );
}
echo json_encode($return);
exit;
}
// run the invites stuff
if (isset($_POST['action']) && ('delete' == $_POST['action'])) {
try {
Game::delete($_POST['game_id']);
echo 'Game Deleted';
}
catch (MyEception $e) {
echo 'ERROR: Could not delete game';
}
exit;
}
// init our game
$Game = new Game((int) $_SESSION['game_id']);
// run the game refresh check
if (isset($_POST['refresh'])) {
echo $Game->last_move;
exit;
}
// run the ship count clicks
if (isset($_POST['shipcheck'])) {
try {
echo $Game->get_sunk_ships($_POST['id']);
}
catch (MyException $e) {
echo 'ERROR';
}
exit;
}
// do some more validity checking for the rest of the functions
if (empty($DEBUG) && empty($_POST['notoken'])) {
test_token( ! empty($_POST['keep_token']));
}
if ($_POST['game_id'] != $_SESSION['game_id']) {
echo 'ERROR: Incorrect game id given';
exit;
}
// run the board setup
if (isset($_POST['method'])) {
$return = array( );
try {
if (isset($_POST['done'])) {
$Game->setup_done( );
$return['redirect'] = (($Game->test_ready( )) ? 'game.php?id='.$Game->id : 'index.php');
}
else {
switch ($_POST['method']) {
case 'clear' :
$Game->setup_action('clear_board');
break;
case 'random' :
$Game->setup_action('random_board');
break;
case 'between' :
list($value1, $value2) = explode(':', $_POST['value']);
$Game->setup_action('boat_between', $value1, $value2);
break;
case 'random_boat' :
$Game->setup_action('random_boat', $_POST['value']);
break;
case 'remove' :
$Game->setup_action('remove_boat', $_POST['value']);
break;
} // end method switch
$return['board'] = $Game->get_board_html('first', true);
$return['boats'] = $Game->get_boats_html( );
}
}
catch (MyException $e) {
$return['error'] = 'ERROR: '.$e->outputMessage( );
}
echo json_encode($return);
exit;
}
// make sure we are the player we say we are
// unless we're an admin, then it's ok
$player_id = (int) $_POST['player_id'];
if (($player_id != $_SESSION['player_id']) && ! $GLOBALS['Player']->is_admin) {
echo 'ERROR: Incorrect player id given';
exit;
}
// run the 'Nudge' button
if (isset($_POST['nudge'])) {
$return = array( );
$return['token'] = $_SESSION['token'];
try {
$Game->nudge($player_id);
}
catch (MyException $e) {
$return['error'] = 'ERROR: '.$e->outputMessage( );
}
echo json_encode($return);
exit;
}
// run the 'Resign' button
if (isset($_POST['resign'])) {
$return = array( );
$return['token'] = $_SESSION['token'];
try {
$Game->resign($_SESSION['player_id']);
}
catch (MyException $e) {
$return['error'] = 'ERROR: '.$e->outputMessage( );
}
echo json_encode($return);
exit;
}
// run the shots
if (isset($_POST['shots'])) {
$return = array( );
$return['token'] = $_SESSION['token'];
// clean up the shots
$_POST['shots'] = explode(',', trim($_POST['shots'], ', '));
try {
$Game->do_shots($_POST['shots']);
$return['action'] = 'RELOAD';
}
catch (MyException $e) {
$return['error'] = 'ERROR: '.$e->outputMessage( );
}
echo json_encode($return);
exit;
}