-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.php
94 lines (80 loc) · 2.38 KB
/
chess.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
<?php
require_once "lib/dbconnect.php";
require_once "lib/board.php";
require_once "lib/game.php";
require_once "lib/users.php";
$method = $_SERVER['REQUEST_METHOD'];
$request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
$input = json_decode(file_get_contents('php://input'),true);
if($input==null) {
$input=[];
}
if(isset($_SERVER['HTTP_X_TOKEN'])) {
$input['token']=$_SERVER['HTTP_X_TOKEN'];
} else {
$input['token']='';
}
// header("Content-Type: text/plain");
// print "method=$method\n";
// print "Path_info=".$_SERVER['PATH_INFO']."\n";
// print_r($request);
switch ($r=array_shift($request)) {
case 'board' :
switch ($b=array_shift($request)) {
case '':
case null: handle_board($method,$input);
break;
case 'piece': handle_piece($method, $request[0],$request[1],$input);
break;
}
break;
case 'status':
if(sizeof($request)==0) {handle_status($method);}
else {header("HTTP/1.1 404 Not Found");}
break;
case 'players': handle_player($method, $request,$input);
break;
default: header("HTTP/1.1 404 Not Found");
exit;
}
function handle_board($method,$input) {
if($method=='GET') {
show_board($input);
} else if ($method=='POST') {
reset_board();
show_board($input);
} else {
header('HTTP/1.1 405 Method Not Allowed');
}
}
function handle_piece($method, $x,$y,$input) {
if($method=='GET') {
show_piece($x,$y);
} else if ($method=='PUT') {
move_piece($x,$y,$input['x'],$input['y'],
$input['token']);
}
}
function handle_player($method, $p,$input) {
switch ($b=array_shift($p)) {
// case '':
// case null: if($method=='GET') {show_users($method);}
// else {header("HTTP/1.1 400 Bad Request");
// print json_encode(['errormesg'=>"Method $method not allowed here."]);}
// break;
case 'B':
case 'W': handle_user($method, $b,$input);
break;
default: header("HTTP/1.1 404 Not Found");
print json_encode(['errormesg'=>"Player $b not found."]);
break;
}
}
function handle_status($method) {
if($method=='GET') {
show_status();
} else {
header('HTTP/1.1 405 Method Not Allowed');
}
}
?>