5
5
use \Psr \Http \Message \ResponseInterface as Response ;
6
6
7
7
use \BotBattle \BotBattle ;
8
+ use \BotBattle \Config ;
9
+
10
+ use \BotBattle \Models \Error ;
11
+ use \BotBattle \Models \Game ;
8
12
9
13
session_start ();
10
14
15
+ // Load the config and app
16
+ $ config = new Config (include ('config.php ' ));
17
+ $ botBattle = new BotBattle ($ config );
18
+
11
19
// Setup Slim
12
20
$ configuration = [
13
21
'settings ' => [
17
25
$ container = new \Slim \Container ($ configuration );
18
26
$ app = new \Slim \App ($ container );
19
27
20
- $ app ->group ("/user " , function () {
21
- $ this ->post ('[/] ' , function (Request $ request , Response $ response ) {
28
+ /**
29
+ * User requests
30
+ */
31
+ $ app ->group ("/users " , function () use ($ botBattle ) {
32
+ $ this ->post ('[/] ' , function (Request $ request , Response $ response ) use ($ botBattle ) {
22
33
$ json = $ request ->getBody ();
34
+
35
+ // TODO: Deserialize to a request type
23
36
$ data = json_decode ($ json , true );
24
37
25
38
// Check data is given
26
39
if (empty ($ data )) {
27
- error_log ("Empty Data " );
28
- return $ response ->withStatus (400 );
40
+ return $ response ->withJson (new Error ('No user data sent ' ), 400 );
29
41
}
30
42
31
- $ _SESSION [ ' username ' ] = $ data ['username ' ];
43
+ $ user = $ botBattle -> userService -> login ( $ data ['username ' ]) ;
32
44
33
- $ json = ['username ' => $ _SESSION ['username ' ]];
45
+ return $ response ->withJson ($ user );
46
+ });
47
+
48
+ $ this ->get ('[/] ' , function (Request $ request , Response $ response ) use ($ botBattle ) {
49
+ $ user = $ botBattle ->userService ->getCurrentUser ();
34
50
35
- return $ response ->withJson ($ json );
51
+ return $ response ->withJson ($ user );
36
52
});
37
53
38
- $ this ->get ('[/] ' , function (Request $ request , Response $ response ) {
39
- $ json = ['username ' => $ _SESSION ['username ' ]];
54
+ $ this ->get ('/{id} ' , function (Request $ request , Response $ response ) use ($ botBattle ) {
55
+ $ id = $ request ->getAttribute ('id ' );
56
+ $ user = $ botBattle ->userService ->getUser ($ id );
57
+
58
+ // User not found
59
+ if ($ user === null ) {
60
+ return $ response ->withJson (new Error ('User not found ' ), 404 );
61
+ }
40
62
41
- return $ response ->withJson ($ json );
63
+ return $ response ->withJson ($ user );
42
64
});
43
65
});
44
66
45
- $ app ->group ("/action " , function () {
46
- $ this ->post ('[/] ' , function (Request $ request , Response $ response ) {
67
+ /**
68
+ * Game requests
69
+ */
70
+ $ app ->group ("/games " , function () use ($ botBattle ) {
71
+ /**
72
+ * Create a new game or get an open matching one
73
+ */
74
+ $ this ->post ('[/] ' , function (Request $ request , Response $ response ) use ($ botBattle ) {
47
75
$ json = $ request ->getBody ();
76
+
77
+ // TODO: Deserialize to a request type
48
78
$ data = json_decode ($ json , true );
49
79
50
80
// Check data is given
51
81
if (empty ($ data )) {
52
- error_log ("Empty Data " );
53
- return $ response ->withStatus (400 );
82
+ return $ response ->withJson (new Error ('No game data sent ' ), 400 );
83
+ }
84
+
85
+ // Create the game
86
+ $ game = $ botBattle ->gameService ->createGame ($ data ['difficulty ' ]);
87
+
88
+ return $ response ->withJson ($ game );
89
+ });
90
+
91
+
92
+ /**
93
+ * Get current games
94
+ */
95
+ $ this ->get ('[/] ' , function (Request $ request , Response $ response ) use ($ botBattle ) {
96
+ // TODO: List games
97
+ return $ response ->withJson (new Error ('Method not implemented ' ), 400 );
98
+ });
99
+
100
+ /**
101
+ * Get a game by ID
102
+ */
103
+ $ this ->get ('/{id} ' , function (Request $ request , Response $ response ) use ($ botBattle ) {
104
+ $ id = $ request ->getAttribute ('id ' );
105
+ $ game = $ botBattle ->gameService ->getGame ($ id );
106
+
107
+ // Game not found
108
+ if ($ game === null ) {
109
+ return $ response ->withJson (new Error ('Game not found ' ), 404 );
110
+ }
111
+
112
+ return $ response ->withJson ($ game );
113
+ });
114
+
115
+ /**
116
+ * Join a game
117
+ */
118
+ $ this ->post ('/{id}/players ' , function (Request $ request , Response $ response ) use ($ botBattle ) {
119
+ $ user = $ botBattle ->userService ->getCurrentUser ();
120
+ if ($ user == null ) {
121
+ return $ response ->withJson (new Error ('Not logged in ' ), 401 );
122
+ }
123
+
124
+ $ id = $ request ->getAttribute ('id ' );
125
+ $ game = $ botBattle ->gameService ->getGame ($ id );
126
+
127
+ // Game not found
128
+ if ($ game === null ) {
129
+ return $ response ->withJson (new Error ('Game not found ' ), 404 );
54
130
}
55
131
56
- switch ($ data ['action ' ]) {
57
- case 'left ' :
58
- break ;
59
- case 'right ' :
60
- break ;
61
- case 'up ' :
62
- break ;
63
- case 'down ' :
64
- break ;
65
- default :
66
- // noop
67
- break ;
132
+ $ player = $ game ->getUserPlayer ($ user );
133
+ if ($ player == null ) {
134
+ $ player = $ botBattle ->gameService ->joinGame ($ game , $ user );
68
135
}
69
136
70
- // Return the current state
71
- $ state = new BotBattle ();
137
+ return $ response ->withJson ($ player );
138
+ });
139
+
140
+ /**
141
+ * Make a move in a game
142
+ */
143
+ $ this ->post ('/{id}/moves ' , function (Request $ request , Response $ response ) use ($ botBattle ) {
144
+ $ user = $ botBattle ->userService ->getCurrentUser ();
145
+ if ($ user == null ) {
146
+ return $ response ->withJson (new Error ('Not logged in ' ), 401 );
147
+ }
148
+
149
+ $ id = $ request ->getAttribute ('id ' );
150
+ $ game = $ botBattle ->gameService ->getGame ($ id );
151
+
152
+ // Game not found
153
+ if ($ game === null ) {
154
+ return $ response ->withJson (new Error ('Game not found ' ), 404 );
155
+ }
156
+
157
+ if ($ game ->state === Game::STATE_WAITING ) {
158
+ return $ response ->withJson (new Error ('Game hasn \'t started ' ), 400 );
159
+ }
160
+ if ($ game ->state === Game::STATE_DONE ) {
161
+ return $ response ->withJson (new Error ('Game has ended ' ), 400 );
162
+ }
163
+
164
+ $ json = $ request ->getBody ();
165
+
166
+ // TODO: Deserialize to a request type
167
+ $ data = json_decode ($ json , true );
168
+
169
+ // Check data is given
170
+ if (empty ($ data )) {
171
+ return $ response ->withJson (new Error ('No move data sent ' ), 400 );
172
+ }
173
+
174
+ // Get the user's player
175
+ $ user = $ botBattle ->userService ->getCurrentUser ();
176
+ $ player = $ game ->getUserPlayer ($ user );
177
+
178
+ if ($ player === null ) {
179
+ return $ response ->withJson (new Error ('Current user is not a player in the game ' ), 400 );
180
+ }
181
+
182
+ $ action = intval ($ data ['action ' ]);
183
+ $ move = $ botBattle ->gameService ->makeMove ($ game , $ player , $ action );
184
+
185
+ // Move failed
186
+ if ($ move === null ) {
187
+ return $ response ->withJson (new Error ('Invalid move action ' ), 400 );
188
+ }
72
189
73
- return $ response ->withJson ($ state );
190
+ return $ response ->withJson ($ move );
74
191
});
75
192
});
76
193
77
- $ app ->get ('[/] ' , function (Request $ request , Response $ response ) {
78
- $ state = new BotBattle ();
79
- return $ response ->withJson ($ state );
194
+ $ app ->get ('[/] ' , function (Request $ request , Response $ response ) use ($ botBattle ) {
195
+ return $ response ->withJson (['BATTLEBOTS ' ]);
80
196
});
81
197
82
198
$ app ->run ();
0 commit comments