-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeadjury.php
275 lines (251 loc) · 6.59 KB
/
deadjury.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
define('DEADJURY_FILES_PREFIX', 'deadjury-game-');
if(isset($_POST['reset']))
{
$game = $_POST['game'];
foreach(glob('{'.DEADJURY_FILES_PREFIX.$game.'-}*txt', GLOB_BRACE) as $filename)
{
unlink($filename);
}
$json['response'] = 1;
}
elseif(isset($_POST['guess']))
{
$guess = intval($_POST['guess']);
$number = $_POST['number'];
$game = $_POST['game'];
$player = $_POST['player'];
$new = setGame($guess, $number, $game);
if($new)
{
$json = array(
'response' => 1,
'game' => $new['game'],
'player' => $new['player']
);
canGuess($new['game'], $new['player'], 0);
canGuess($new['game'], $new['player'] == 1 ? 2 : 1);
}
else
{
$filename = getFile($game, $player, $number);
if($filename)
{
file_put_contents($filename, $guess);
canGuess($game, $player, 0);
canGuess($game, $player == 1 ? 2 : 1);
$json['response'] = 1;
}
}
}
elseif(isset($_GET['number']))
{
$number = intval($_GET['number']);
$game = $_GET['game'];
$player = $_GET['player'];
$guess = getGuess($game, $player, $number);
$allowed = isAllowedToGuess($game, $player);
if($guess)
{
$opponentNumber = getOpponentNumber($game, $player == 1 ? 2 : 1);
if($opponentNumber)
{
$results = compare($guess, $opponentNumber);
$winner = getWinner($game);
if($results)
{
$json = array(
'response' => 1,
'allowed' => $allowed,
'results' => $results
);
if($winner)
{
$json['winner'] = $winner;
}
elseif($results['dead'] == 4)
{
setWinner($game, $number);
$json['winner'] = $number;
}
}
}
}
}
if(!isset($json))
{
$json['response'] = 0;
}
echo json_encode($json);
/**************************************
***************Functions***************
***************************************/
/**
* Gets the file associated with a player
*
* @param string $game, the Id of the game
* @param string $player, the Id of the player
* @param string $number, the number chosen by the player
*
* @return string|false
*/
function getFile($game, $player, $number)
{
$filename = DEADJURY_FILES_PREFIX.$game.'-'.$player.'-'.$number.'.txt';
return is_file($filename) ? $filename : false;
}
/**
* Gets the last guess of the player
*
* @param string $game, the Id of the game
* @param string $player, the Id of the player
* @param string $number, the number chosen by the player
*
* @return string|false
*/
function getGuess($game, $player, $number)
{
$filename = DEADJURY_FILES_PREFIX.$game.'-'.$player.'-'.$number.'.txt';
return is_file($filename) ? trim(file_get_contents($filename)) : false;
}
/**
* Gets the opponent's number
*
* @param string $game, the Id of the game
* @param string $player, the Id of the player
*
* @return string|false
*/
function getOpponentNumber($game, $player)
{
$search = glob('{'.DEADJURY_FILES_PREFIX.'}'.$game.'-'.$player.'*.txt', GLOB_BRACE);
foreach($search as $filename) {
if(preg_match('/([0-9]{4})\.txt$/', $filename, $matches))
{
return $matches[1];
}
}
return false;
}
/**
* Gets the winner of the game
*
* @param string $game, the Id of the game
*
* @return string|false
*/
function getWinner($game)
{
$filename = DEADJURY_FILES_PREFIX.$game.'-winner.txt';
return is_file($filename) ? trim(file_get_contents($filename)) : false;
}
/**
* Sets the winner of the game
*
* @param string $game, the Id of the game
* @param string $number, the winner's number
*/
function setWinner($game, $number)
{
$filename = DEADJURY_FILES_PREFIX.$game.'-winner.txt';
file_put_contents($filename, $number);
}
/**
* Determines if the player if allowed to play
*
* @param string $game, the Id of the game
* @param string $player, the Id of the player
*
* @return string|false
*/
function isAllowedToGuess($game, $player)
{
$filename = DEADJURY_FILES_PREFIX.$game.'-'.$player.'-guess.txt';
return is_file($filename) ? intval(file_get_contents($filename)) : false;
}
/**
* Sets a file to allow the player to input his guess
*
* @param string $game, the Id of the game
* @param string $player, the Id of the player
* @param integer $canGuess, 1 => can guess, 2 => can't guess
*
* @return boolean
*/
function canGuess($game, $player, $canGuess = 1)
{
$filename = DEADJURY_FILES_PREFIX.$game.'-'.$player.'-guess.txt';
file_put_contents($filename, $canGuess);
}
/**
* Compares a guess to the number chosen by the opponent
*
* @param string $guess, the number guessed by the player
* @param string $to, the number chosen by the opponent
*
* @return array
*/
function compare($guess, $to)
{
$dead = 0;
$injured = 0;
for($i = 0; $i < strlen($guess); $i++)
{
if($guess[$i] == $to[$i])
{
$dead++;
}
elseif(strpos($to, $guess[$i]))
{
$injured++;
}
}
return array(
'dead' => $dead,
'injured' => $injured
);
}
/**
* Creates the file associated with a given player
* deadjury-game-[gameId]-[playerId]-[chosenNumber].txt
*
* @param string $guess, the number guessed by the player
* @param string $number, the number chosen by the player
* @param string $game, the id of the game
*/
function setGame($guess = '', $number = '', $gameId = '')
{
if(!$gameId)
{
$games = glob('{'.DEADJURY_FILES_PREFIX.'}*.txt', GLOB_BRACE);
$current = 0; // The newest game
$player = 1; // The number of players in that game
foreach($games as $game)
{
if(preg_match('/game-([0-9]+).+[0-9]{4}\.txt$/', $game, $matches))
{
$num = intval($matches[1]);
if($num > $current)
{
$current = $num;
$player = 2;
}
elseif($num == $current)
{
$player = 1;
}
}
}
if($player == 1)
{
$current++;
}
file_put_contents(DEADJURY_FILES_PREFIX.$current.'-'.$player.'-'.$number.'.txt', $guess);
return array(
'game' => $current,
'player' => $player
);
}
return false;
}
?>