-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.php
55 lines (47 loc) · 1.58 KB
/
routes.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
<?php
// Index home page and a test page for viewing the scores
$app->get('/', function(){
echo "My Game - home <br/>";
echo "Text database <a href='/index.php/testdb/'>Test database</a><br/>";
echo "Save score for user 1 <a href='/index.php/savescore/?id=1&game=3&score=100'>save</a><br/>";
echo "Save score for user 2 <a href='/index.php/savescore/?id=2&game=3&score=100'>save</a><br/>";
});
$app->get('/testdb/', function() use ($app){
echo "Doctrine library path: ".Doctrine::getPath()."<br/>";
echo "Tables: "; print_r($app->dbcon->execute("SHOW TABLES")->fetchAll()); echo "<br/>";
echo "Users: <br/>";
$users = Doctrine_Core::getTable('User')->findAll();
foreach($users as $user){
echo $user->id. " " .$user->name." <br/>";
if( count($user->Scores) ){
echo "<ul>";
foreach($user->Scores as $score){
echo "<li>" . $score->score . " in game id " . $score->game_id ." name: ". $score->Game->name ."</li><br/>";
}
echo "</ul>";
}
}
});
// API or remote functions for saving data
$app->map('/savescore/', function() use($app){
if ($app->request()->isGet()) {
$params = $app->request()->get();
}
elseif ($app->request()->isPost()){
$params = $app->request()->post();
}
else{ echo json_encode(array( 'status'=>'method error'));}
try{
saveScore($params);
echo json_encode($params);
}catch(Exception $e){
echo json_encode(array('status'=>$e->getMessage()));
}
})->via('GET', 'POST');
function saveScore($params){
$score = new Score();
$score->user_id = $params['id'];
$score->game_id = $params['game'];
$score->score = $params['score'];
$score->save();
}