-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.php
More file actions
313 lines (252 loc) · 7.27 KB
/
handler.php
File metadata and controls
313 lines (252 loc) · 7.27 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
<?php
$row_size = intval($_POST['rowSize']);
$col_size = intval($_POST['colSize']);
$total_cell_count = 0;
define( 'JOINS_CHECK_INTERVAL', 1000000 );
define( 'GAMES_CHECK_INTERVAL', 100000 );
define( 'MESSAGE_DELIMITER', '#' );
define( 'KICKSTART_LENGTH', 500 );
define( 'PULSE_MESSAGE', '.' );
register_shutdown_function('handleShutdown');
// Kickstart
outputToBrowser( str_repeat( '.', KICKSTART_LENGTH ) );
//setup player info
$player_name= '';
$player_id= -1;
$turn_count= 0;
$game_id = -1;
$ActivePlayer = array(1,1,1);
//assign player position
first_load();
//Waiting for players loop
//this will check for players
while (1)
{
$new_player_count = check_player_count();
if ($new_player_count >= 3)
{
$msg = "start" . "," . getSizeAverage();
//message browser that its ready to start
//Vote Averge
outputMessage( $msg );
break;
}
// Pause before next iteration
usleep( JOINS_CHECK_INTERVAL );
//outputMessage("In wait loop");
#break; //debug usage
}
//Once exited waiting for players while loop goes into game updating loop
while(1){
//end if turns greater than 25
global $total_cell_count,$ActivePlayer, $game_id;
if($turn_count > $total_cell_count){
break;
}
//check for people exiting game;
if(handleShutdown()){
//$msg = "shutdown";
//outputMessage($msg);
PlayerGoneUpdatedDB();
exit();
}
// check if anyone is gone, then output shutdown msg with who dropped.
include "mysqlConnect.php";
$sql = mysql_query("SELECT Active FROM Players WHERE idGame = '$game_id'");
$currPlayer = 0;
while($row = mysql_fetch_array($sql)){
if ($ActivePlayer[$currPlayer] != $row['Active']){
$ActivePlayer[$currPlayer] = $row['Active'];
$playerDropped = $currPlayer+1;
$msg = "PlayerDropped" . "," . $playerDropped;
outputMessage($msg);
}
}
$sql = mysql_query("SELECT * FROM Game WHERE idGame = '$game_id'");
$row = mysql_fetch_array($sql);
if($row['Active'] == 0){
$msg = "shutdown" . "," . "dummymessage";
outputMessage($msg);
exit();
}
//get new turn count
$new_turn_count = get_turn_count();
//check if turn count has changed, if so push data
if($new_turn_count != $turn_count){
//update turn count
$turn_count = $new_turn_count;
//get pos, value, of latest
$play_pos = get_play_pos();
$play_value = get_play_value();
$msg = "";
if(gameover()){
$msg = "endOfGameTurn" ."," .$play_pos . "," . $play_value . "," . $turn_count;
outputMessage($msg);
exit();
}
else {
$msg = "turn" ."," .$play_pos . "," . $play_value . "," . $turn_count;
outputMessage($msg);
}
}
usleep( GAMES_CHECK_INTERVAL );
#break; //debug usage
}
function PlayerGoneUpdatedDB(){
include "mysqlConnect.php";
global $player_id;
$NotActive = 0;
$sql = mysql_query("UPDATE Players SET Active = '$NotActive' WHERE idPlayers = '$player_id'");
}
function gameover() {
global $row_size, $col_size;
$tiles = $row_size * $col_size;
//this will make sure that all players have fair number of turns.
if (get_turn_count() >= ($tiles - ($tiles % 3))){
return true;
}
else {
false;
}
}
function getSizeAverage(){
include "mysqlConnect.php";
//get player count;
#$game_id = intval($GLOBALS['game_id']);
global $game_id, $total_cell_count;
$sql = mysql_query("SELECT AVG(p.col_Choice) AS col_avg, AVG(p.row_Choice) AS row_avg FROM Players p WHERE p.game_ID = '$game_id'");
$rows = mysql_fetch_array($sql);
$total_cell_count = intval($rows['col_avg']) * intval($rows['row_avg']);
return strval(intval($rows['col_avg']) . "," . intval($rows['row_avg']));
}
function waited_for_play(){
$msg = "autoPlay";
outputMessage($msg);
}
#
# END OF SCRIPT - functions follow
#
function check_player_count()
{
include "mysqlConnect.php";
$count =0;
//get player count;
global $game_id;
#$game_id = intval($GLOBALS['game_id']);
$sql = mysql_query("SELECT * FROM Players WHERE game_ID = '$game_id'");
$count = mysql_num_rows($sql);
return $count;
}
function outputToBrowser($out)
{
echo $out;
ob_flush();
flush();
}
function outputMessage($msg)
{
outputToBrowser( $msg . MESSAGE_DELIMITER );
}
function handleShutdown()
{
$connectionStatus = connection_status();
if ( $connectionStatus == 1)
{
// User aborted - take appropriate action
return true;
}
elseif ( $connectionStatus == 2 )
{
return true;
}
else
{
return false;
}
}
function first_load(){
global $game_id;
$game_id = get_game_id();
//looking to db for player pos
$player_pos = get_player_pos();
#$GLOBALS['game_id'] = get_game_id();
//set play pos
$msg = "playerId" . "," . $player_pos . "," . $game_id;
outputMessage($msg);
}
function register_player(){
}
function get_player_pos()
{
include "mysqlConnect.php";
global $game_id,$row_size,$col_size,$player_id;
#$game_id = intval($GLOBALS['game_id']);
#$sql = mysql_query("SELECT * FROM Players WHERE game_ID = '$game_id'");
#$currentPlayerCount = mysql_num_rows($sql);
$Active = 1;
mysql_query("INSERT INTO Players (game_ID, row_Choice, col_Choice, Active) VALUES ($game_id,$row_size,$col_size,$Active)") or die(mysql_error());
$player_id = mysql_insert_id();
return check_player_count();
}
function get_turn_count()
{
include "mysqlConnect.php";
global $game_id;
#$game_id = intval($GLOBALS['game_id']);
$sql = mysql_query("SELECT * FROM Game WHERE idGame = '$game_id'");
$isGameExists = mysql_num_rows($sql);
if($isGameExists){
$row = mysql_fetch_array($sql);
return $row['turn_Count'];
}
return -1;
}
function get_play_pos()
{
include "mysqlConnect.php";
global $game_id;
#$game_id = intval($GLOBALS['game_id']);
$sql = mysql_query("SELECT * FROM Game WHERE idGame = '$game_id'");
$isGameExists = mysql_num_rows($sql);
if($isGameExists){
$row = mysql_fetch_array($sql);
return $row['lastPos'];
}
return -1;
}
function get_play_value()
{
include "mysqlConnect.php";
global $game_id;
#$game_id = intval($GLOBALS['game_id']);
$sql = mysql_query("SELECT * FROM Game WHERE idGame = '$game_id'");
$isGameExists = mysql_num_rows($sql);
if($isGameExists){
$row = mysql_fetch_array($sql);
return $row['last_Player'];
}
return -1;
}
function get_game_id()
{
include "mysqlConnect.php";
$sql = mysql_query("SELECT * FROM Game");
$GameCount = mysql_num_rows($sql);
if ($GameCount == 0 ){
makeNewGame();
return 1;
}
$sqlQueryLastGame = mysql_query("SELECT * FROM Players WHERE game_ID = '$GameCount'");
$playerCountOfTheGame = mysql_num_rows($sqlQueryLastGame);
if ($playerCountOfTheGame >= 3 ){
makeNewGame();
return $GameCount + 1;
}
return $GameCount;
}
function makeNewGame()
{
include "mysqlConnect.php";
mysql_query("INSERT INTO Game (turn_Count) VALUES (0)") or die(mysql_error());
}
?>