-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop.php
280 lines (254 loc) · 8.24 KB
/
stop.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
276
277
278
279
280
<?php
// stop.php
require 'db_connect.php';
// We'll return JSON responses
header('Content-Type: application/json');
// Only accept POST requests
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode([
'status' => 'error',
'message' => 'Invalid request method.'
]);
exit;
}
$game_id = $_POST['game_id'] ?? null;
$token = $_POST['token'] ?? null;
if (!$game_id || !$token) {
echo json_encode([
'status' => 'error',
'message' => 'Game ID and player token are required.'
]);
exit;
}
try {
// 1) Find player from the token
$stmt = $db->prepare("
SELECT id
FROM players
WHERE player_token = :token
");
$stmt->execute([
':token' => $token
]);
$player = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$player) {
echo json_encode([
'status' => 'error',
'message' => 'Invalid player token.'
]);
exit;
}
$player_id = $player['id'];
// 2) Check game status and turn
$stmt = $db->prepare("
SELECT
id,
status,
current_turn_player,
player1_id,
player2_id
FROM games
WHERE id = :game_id
");
$stmt->execute([':game_id' => $game_id]);
$game = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$game) {
echo json_encode([
'status' => 'error',
'message' => 'Invalid game ID.'
]);
exit;
}
if ($game['status'] !== 'in_progress') {
echo json_encode([
'status' => 'error',
'message' => 'Game is not in progress.'
]);
exit;
}
if ($game['current_turn_player'] != $player_id) {
echo json_encode([
'status' => 'error',
'message' => 'It is not your turn.'
]);
exit;
}
// We will collect messages about progress and columns won
$messages = [];
// 3) Merge from turn_markers into player_columns
$stmt = $db->prepare("
SELECT column_number, temp_progress
FROM turn_markers
WHERE game_id = :game_id
AND player_id = :player_id
");
$stmt->execute([
':game_id' => $game_id,
':player_id' => $player_id
]);
$tempMarkers = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($tempMarkers) {
foreach ($tempMarkers as $marker) {
$column_number = $marker['column_number'];
$temp_progress = (int)$marker['temp_progress'];
// Insert or update player's permanent progress
$stmtMerge = $db->prepare("
INSERT INTO player_columns (
game_id,
player_id,
column_number,
progress,
is_active
) VALUES (
:game_id,
:player_id,
:col_num,
:temp_progress,
0
)
ON DUPLICATE KEY UPDATE
progress = progress + VALUES(progress)
");
$stmtMerge->execute([
':game_id' => $game_id,
':player_id' => $player_id,
':col_num' => $column_number,
':temp_progress'=> $temp_progress
]);
// Check if that column reached max height
$stmtCheck = $db->prepare("
SELECT pc.progress, c.max_height
FROM player_columns pc
JOIN columns c ON c.column_number = pc.column_number
WHERE pc.game_id = :game_id
AND pc.player_id = :player_id
AND pc.column_number = :col_num
");
$stmtCheck->execute([
':game_id' => $game_id,
':player_id' => $player_id,
':col_num' => $column_number
]);
$row = $stmtCheck->fetch(PDO::FETCH_ASSOC);
if ($row) {
$currentProgress = (int)$row['progress'];
$maxHeight = (int)$row['max_height'];
if ($currentProgress >= $maxHeight) {
// Mark this column as won
$stmtWon = $db->prepare("
UPDATE player_columns
SET is_won = 1,
is_active = 0
WHERE game_id = :game_id
AND player_id = :player_id
AND column_number = :col_num
");
$stmtWon->execute([
':game_id' => $game_id,
':player_id' => $player_id,
':col_num' => $column_number
]);
// Print a friendly message that this player won the column
$messages[] = "Column $column_number is completed and now won by you!";
} else {
// The column is not yet completed, so print partial progress
$messages[] = "Column $column_number progress increased to $currentProgress.";
}
}
}
}
// 4) Clear turn_markers now that we’ve merged them
$stmt = $db->prepare("
DELETE FROM turn_markers
WHERE game_id = :game_id
AND player_id = :player_id
");
$stmt->execute([
':game_id' => $game_id,
':player_id' => $player_id
]);
// 5) Deactivate columns for the player (end of turn)
$stmt = $db->prepare("
UPDATE player_columns
SET is_active = 0
WHERE game_id = :game_id
AND player_id = :player_id
");
$stmt->execute([
':game_id' => $game_id,
':player_id' => $player_id
]);
// 6) Check if the player has now won 3 columns
$stmt = $db->prepare("
SELECT COUNT(*)
FROM player_columns
WHERE game_id = :game_id
AND player_id = :player_id
AND is_won = 1
");
$stmt->execute([
':game_id' => $game_id,
':player_id' => $player_id
]);
$wonCount = (int)$stmt->fetchColumn();
if ($wonCount >= 3) {
// Gather all the columns the player won
$stmtColumnsWon = $db->prepare("
SELECT column_number
FROM player_columns
WHERE game_id = :game_id
AND player_id = :player_id
AND is_won = 1
");
$stmtColumnsWon->execute([
':game_id' => $game_id,
':player_id' => $player_id
]);
$columnsWonList = $stmtColumnsWon->fetchAll(PDO::FETCH_COLUMN);
// Mark the game completed
$stmtEnd = $db->prepare("
UPDATE games
SET status = 'completed',
winner_id = :player_id
WHERE id = :game_id
");
$stmtEnd->execute([
':player_id' => $player_id,
':game_id' => $game_id
]);
// Override the entire final message with the victory announcement
echo json_encode([
'status' => 'success',
'message' => "You have won the game with columns "
. implode(', ', $columnsWonList)
. "! Congratulations!"
]);
exit;
}
// 7) Switch turn if no winner yet
$stmt = $db->prepare("
UPDATE games
SET current_turn_player = CASE
WHEN current_turn_player = player1_id THEN player2_id
ELSE player1_id
END
WHERE id = :game_id
");
$stmt->execute([':game_id' => $game_id]);
// 8) Print normal success message if the user has not yet won
// (Includes partial progress and newly-won column messages if any)
$joinedMessages = empty($messages)
? "Turn ended. Your progress is locked in, and it's now the other player's turn."
: implode(' ', $messages)
. " Turn ended. Your progress is locked in, and it's now the other player's turn.";
echo json_encode([
'status' => 'success',
'message' => $joinedMessages
]);
} catch (PDOException $e) {
echo json_encode([
'status' => 'error',
'message' => 'Database error: ' . $e->getMessage()
]);
}
?>