-
Notifications
You must be signed in to change notification settings - Fork 0
/
service
403 lines (354 loc) · 13.4 KB
/
service
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
// The Green Spirit's Angry Birds Vuela Tazos service file
// This file handles most of the game's mechanics.
// Because why not?
// start database stuff
$host = "pma.ct8.pl";
$user = "m37488_default";
$pass = "thegreenspiritisc00L";
$db = 'm37488_angrybirds_pepsicomx';
// init PDO
try {
$pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
// users table functions
function addUserToDatabase($pdo, $nickName, $password, $id, $age, $fullName, $zipcode, $gender, $subscribed) {
$sql = "INSERT INTO users (nickName, password, id, age, fullName, zipcode, gender, subscribed) VALUES (:nickName, :password, :id, :age, :fullName, :zipcode, :gender, :subscribed)";
try {
$stmt = $pdo->prepare($sql);
$stmt->execute([
'nickName'=>$nickName,
'password'=>$password,
'id'=>$id,
'age'=>$age,
'fullName'=>$fullName,
'zipcode'=>$zipcode,
'gender'=>$gender,
'subscribed'=>$subscribed,
]);
} catch (Exception $e) {
echo $e;
}
}
function getUserFromDatabase($pdo, $id) {
$sql = "SELECT * FROM users WHERE id=:id";
try {
$stmt = $pdo->prepare($sql);
$stmt->execute([
'id'=>$id,
]);
$data = $stmt->fetch(PDO::FETCH_ASSOC);
return $data;
} catch (Exception $e) {
echo $e;
}
}
function checkNickNameHasBeenTaken($pdo, $nickName) {
$sql = "SELECT * FROM users WHERE nickName=:nickName";
try {
$stmt = $pdo->prepare($sql);
$stmt->execute(['nickName'=>$nickName]);
$data = $stmt->fetch(PDO::FETCH_ASSOC);
if ($data) {
return true;
}
} catch (Exception $e) {
echo $e;
}
}
// leveldata table functions
function addUserToLevelDatabase($pdo, $id, $level1score, $level2score, $level3score, $level4score, $level5score, $level6score, $level4unlocked, $level5unlocked, $level6unlocked) {
$sql = "INSERT INTO leveldata (id, level1score, level2score, level3score, level4score, level5score, level6score, level4unlocked, level5unlocked, level6unlocked) VALUES (:id, :level1score, :level2score, :level3score, :level4score, :level5score, :level6score, :level4unlocked, :level5unlocked, :level6unlocked)";
try {
$stmt = $pdo->prepare($sql);
$stmt->execute([
'id'=>$id,
'level1score'=>$level1score,
'level2score'=>$level2score,
'level3score'=>$level3score,
'level4score'=>$level4score,
'level5score'=>$level5score,
'level6score'=>$level6score,
'level4unlocked'=>$level4unlocked,
'level5unlocked'=>$level5unlocked,
'level6unlocked'=>$level6unlocked,
]);
} catch (Exception $e) {
echo $e;
}
}
function unlockLevel($pdo, $id, $levelId) {
if ($levelId == '1-4') {
$levelToUnlock = 'level4unlocked';
}
else if ($levelId == '1-5') {
$levelToUnlock = 'level5unlocked';
}
else if ($levelId == '1-6') {
$levelToUnlock = 'level6unlocked';
}
$sql = "UPDATE leveldata SET {$levelToUnlock}=:{$levelToUnlock} WHERE id=:id";
try {
$stmt = $pdo->prepare($sql);
$stmt->execute([
'id'=>$id,
"$levelToUnlock"=>true,
]);
$data = $stmt->fetch(PDO::FETCH_ASSOC);
return $data;
} catch (Exception $e) {
echo $e;
}
}
function submitScore($pdo, $id, $levelId, $score) {
if ($levelId == '1-1') {
$levelToSubmit = 'level1score';
}
else if ($levelId == '1-2') {
$levelToSubmit = 'level2score';
}
else if ($levelId == '1-3') {
$levelToSubmit = 'level3score';
}
else if ($levelId == '1-4') {
$levelToSubmit = 'level4score';
}
else if ($levelId == '1-5') {
$levelToSubmit = 'level5score';
}
else if ($levelId == '1-6') {
$levelToSubmit = 'level6score';
}
$sql = "UPDATE leveldata SET {$levelToSubmit}=:{$levelToSubmit} WHERE id=:id";
try {
$stmt = $pdo->prepare($sql);
$stmt->execute([
'id'=>$id,
"$levelToSubmit"=>$score,
]);
$data = $stmt->fetch(PDO::FETCH_ASSOC);
return $data;
} catch (Exception $e) {
echo $e;
}
}
function getUserFromLevelDatabase($pdo, $id) {
$sql = "SELECT * FROM leveldata WHERE id=:id";
try {
$stmt = $pdo->prepare($sql);
$stmt->execute([
'id'=>$id,
]);
$data = $stmt->fetch(PDO::FETCH_ASSOC);
return $data;
} catch (Exception $e) {
echo $e;
}
}
function getAllUsersFromLevelDatabase($pdo) {
$sql = "SELECT * FROM leveldata";
try {
$stmt = $pdo->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $data;
} catch (Exception $e) {
echo $e;
}
}
// end database stuff
// do not display php errors or else it will cause issues!!
ini_set('display_errors', 0);
// start the actual function of this file (todo: find out if this is needed)
session_start();
// Decode base64-encrypted json data that the game sends
$requestData = $_POST['json'];
$requestData = base64_decode($requestData);
$requestData = json_decode($requestData, true);
// if type is registerPlayer, then add player to database
if ($requestData['type'] == 'registerPlayer') {
// Add placeholder values if optional information isn't there (except subscribing)
// Define full name
if ($requestData['player']['name']) {
$fullName = $requestData['player']['name'];
}
else {
$fullName = "unspecified";
}
// Define zipcode
if ($requestData['player']['zipcode']) {
$zipcode = $requestData['player']['zipcode'];
}
else {
$zipcode = "unspecified";
}
// Define genders
if ($requestData['player']['gender'] == 'm') {
$gender = 'male';
}
else if ($requestData['player']['gender'] == 'f') {
$gender = 'female';
}
else {
$gender = "unspecified";
}
$checker = checkNickNameHasBeenTaken($pdo, $requestData['player']['nickName']);
$playerData = getUserFromDatabase($pdo, $requestData['player']['id']);
// if nickname is taken, then send an error telling that the nickname is taken
if ($checker) {
echo json_encode(array('error' => 'FIELD_NOT_VALID', 'field' => 'nickName'));
}
// if email is taken, then send an error telling that the email is taken
else if ($playerData) {
echo json_encode(array('error' => 'PLAYER_ID_USED'));
}
else {
addUserToDatabase($pdo, $requestData['player']['nickName'], $requestData['player']['password'], $requestData['player']['id'], $requestData['player']['age'], $fullName, $zipcode, $gender, $requestData['player']['marketing']);
addUserToLevelDatabase($pdo, $requestData['player']['id'], 0, 0, 0, 0, 0, 0, false, false, false);
echo json_encode(array('security' => 'success'));
}
}
// if type is login, then try to log player in
else if ($requestData['type'] == 'login') {
$playerData = getUserFromDatabase($pdo, $requestData['player']['id']);
$playerLevelData = getUserFromLevelDatabase($pdo, $requestData['player']['id']);
if ($playerData['password'] == $requestData['player']['password']) {
// Update level data after login
$levelProfile = array();
// since levels 1-1 to 1-3 are already unlocked in base game, we can just set unlocked to true
$level1 = array('levelId' => '1-1', 'unlocked' => true, 'score' => $playerLevelData['level1score']);
array_push($levelProfile, $level1);
$level2 = array('levelId' => '1-2', 'unlocked' => true, 'score' => $playerLevelData['level2score']);
array_push($levelProfile, $level2);
$level3 = array('levelId' => '1-3', 'unlocked' => true, 'score' => $playerLevelData['level3score']);
array_push($levelProfile, $level3);
$level4 = array('levelId' => '1-4', 'unlocked' => $playerLevelData['level4unlocked'], 'score' => $playerLevelData['level4score']);
array_push($levelProfile, $level4);
$level5 = array('levelId' => '1-5', 'unlocked' => $playerLevelData['level5unlocked'], 'score' => $playerLevelData['level5score']);
array_push($levelProfile, $level5);
$level6 = array('levelId' => '1-6', 'unlocked' => $playerLevelData['level6unlocked'], 'score' => $playerLevelData['level6score']);
array_push($levelProfile, $level6);
echo json_encode(array('security' => 'success', 'nickName' => $playerData['nickName'], 'id' => $requestData['player']['id'], 'levelProfile' => $levelProfile));
}
else {
echo json_encode(array('error' => 'PLAYER_INVALID_LOGIN'));
}
}
// if type is assignCode, then try to unlock the level
else if ($requestData['type'] == 'assignCode') {
$playerId = $requestData['player']['id'];
$levelId = $requestData['levelId'];
$playerData = getUserFromDatabase($pdo, $playerId);
$playerLevelData = getUserFromLevelDatabase($pdo, $playerId);
// Update level data
$levelProfile = array();
// since levels 1-1 to 1-3 are already unlocked in base game, we can just set unlocked to true
$level1 = array('levelId' => '1-1', 'unlocked' => true, 'score' => $playerLevelData['level1score']);
array_push($levelProfile, $level1);
$level2 = array('levelId' => '1-2', 'unlocked' => true, 'score' => $playerLevelData['level2score']);
array_push($levelProfile, $level2);
$level3 = array('levelId' => '1-3', 'unlocked' => true, 'score' => $playerLevelData['level3score']);
array_push($levelProfile, $level3);
$level4 = array('levelId' => '1-4', 'unlocked' => $playerLevelData['level4unlocked'], 'score' => $playerLevelData['level4score']);
$level5 = array('levelId' => '1-5', 'unlocked' => $playerLevelData['level5unlocked'], 'score' => $playerLevelData['level5score']);
$level6 = array('levelId' => '1-6', 'unlocked' => $playerLevelData['level6unlocked'], 'score' => $playerLevelData['level6score']);
if ($requestData['code']/* == ''*/ and $levelId == '1-4') {
// Update level 4 to unlocked state
$level4 = array('levelId' => $levelId, 'unlocked' => true, 'score' => $playerLevelData['level4score']);
// Push all of the special levels to levelProfile
array_push($levelProfile, $level4);
array_push($levelProfile, $level5);
array_push($levelProfile, $level6);
unlockLevel($pdo, $playerId, $levelId);
// Finalize data
echo json_encode(array('security' => $requestData['player']['security'], 'nickName' => $playerData['nickName'], 'id' => $playerId, 'levelProfile' => $levelProfile));
}
else if ($requestData['code']/* == ''*/ and $levelId == '1-5') {
// Update level 5 to unlocked state
$level5 = array('levelId' => $levelId, 'unlocked' => true, 'score' => $playerLevelData['level5score']);
// Push all of the special levels to levelProfile
array_push($levelProfile, $level4);
array_push($levelProfile, $level5);
array_push($levelProfile, $level6);
unlockLevel($pdo, $playerId, $levelId);
// Finalize data
echo json_encode(array('security' => $requestData['player']['security'], 'nickName' => $playerData['nickName'], 'id' => $playerId, 'levelProfile' => $levelProfile));
}
else if ($requestData['code']/* == ''*/ and $levelId == '1-6') {
// Update level 6 to unlocked state
$level6 = array('levelId' => $levelId, 'unlocked' => true, 'score' => $playerLevelData['level6score']);
// Push all of the special levels to levelProfile
array_push($levelProfile, $level4);
array_push($levelProfile, $level5);
array_push($levelProfile, $level6);
unlockLevel($pdo, $playerId, $levelId);
// Finalize data
echo json_encode(array('security' => $requestData['player']['security'], 'nickName' => $playerData['nickName'], 'id' => $playerId, 'levelProfile' => $levelProfile));
}
else {
echo json_encode(array('error' => 'CODE_NOT_EXIST'));
}
}
// if type is highscore, then load scores
else if ($requestData['type'] == 'highscore') {
$highscores = array();
$allUsers = getAllUsersFromLevelDatabase($pdo);
// Get scores from level 5, 6 and 7 (because 1, 2, 3 does not show highscores)
foreach ($allUsers as $key => $userData) {
if ($requestData['levelId'] == '1-4') {
$playerData = getUserFromDatabase($pdo, $userData['id']);
$playerScore = array('nickName' => $playerData['nickName'], 'score' => $userData['level4score']);
array_push($highscores, $playerScore);
}
else if ($requestData['levelId'] == '1-5') {
$playerData = getUserFromDatabase($pdo, $userData['id']);
$playerScore = array('nickName' => $playerData['nickName'], 'score' => $userData['level5score']);
array_push($highscores, $playerScore);
}
else if ($requestData['levelId'] == '1-6') {
$playerData = getUserFromDatabase($pdo, $userData['id']);
$playerScore = array('nickName' => $playerData['nickName'], 'score' => $userData['level6score']);
array_push($highscores, $playerScore);
}
}
// Sort highscores
function highscoreSort($a, $b){
if($a['score'] == $b['score']) return 0;
return $a['score'] > $b['score'] ? -1 : 1;
}
usort($highscores, 'highscoreSort');
echo json_encode($highscores);
}
// if type is updateLevelProfile, then submit score
else if ($requestData['type'] == 'updateLevelProfile') {
$playerId = $requestData['player']['id'];
$levelId = $requestData['levelProfile']['levelId'];
$score = $requestData['levelProfile']['score'];
$playerLevelData = getUserFromLevelDatabase($pdo, $playerId);
if ($levelId == '1-1') {
$levelToSubmit = 'level1score';
}
else if ($levelId == '1-2') {
$levelToSubmit = 'level2score';
}
else if ($levelId == '1-3') {
$levelToSubmit = 'level3score';
}
else if ($levelId == '1-4') {
$levelToSubmit = 'level4score';
}
else if ($levelId == '1-5') {
$levelToSubmit = 'level5score';
}
else if ($levelId == '1-6') {
$levelToSubmit = 'level6score';
}
if ($playerLevelData[$levelToSubmit] < $score) {
submitScore($pdo, $playerId, $levelId, $score);
}
}
?>