-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
50 lines (43 loc) · 1.67 KB
/
api.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
<?php
require_once 'GameFunctions.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Check if 'method' is provided
if (!isset($_POST['method'])) {
echo "Error: 'method' parameter is required.";
exit;
}
$method = $_POST['method'];
try {
switch ($method) {
case 'initializeGame':
if (isset($_POST['player1']) && isset($_POST['player2'])) {
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];
initializeGame($player1, $player2);
} else {
echo "Error: Both 'player1' and 'player2' parameters are required.";
}
break;
case 'makeMove':
if (isset($_POST['game_id']) && isset($_POST['player_id']) &&
isset($_POST['piece_id']) && isset($_POST['startX']) && isset($_POST['startY'])) {
$gameId = $_POST['game_id'];
$playerId = $_POST['player_id'];
$pieceId = $_POST['piece_id'];
$startX = $_POST['startX'];
$startY = $_POST['startY'];
makeMove($gameId, $playerId, $pieceId, $startX, $startY);
} else {
echo "Error: Missing parameters. Required: 'game_id', 'player_id', 'piece_id', 'startX', 'startY'.";
}
break;
default:
echo "Error: Unknown method '$method'.";
break;
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
} else {
echo "Error: Invalid request method. Please use POST.";
}