-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
100 lines (81 loc) · 2.21 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
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
<?php
header('Content-Type: text/html; charset=utf-8');
// Include the main Propel script
require_once 'propel/runtime/lib/Propel.php';
// Initialize Propel with the runtime configuration
Propel::init("seabattle/build/conf/seabattle-conf.php");
// Add the generated 'classes' directory to the include path
set_include_path("seabattle/build/classes" . PATH_SEPARATOR . get_include_path());
require_once 'lib/httpUtils.php';
require_once 'lib/gameException.php';
require_once 'controllers/game.php';
$auth = HttpUtils::getPost( 'auth', null );
$userId = HttpUtils::getPost( 'uid', null );
$method = HttpUtils::getPost( 'method', null );
$params = unserialize( HttpUtils::getPost( 'params', null ) );
if( !checkAuth( $auth, $method, $params ) )
die( "You aren't authorization" );
$res = array();
$data = array();
$game = Game::instance();
try
{
switch( $method )
{
// No parameters
case 'getRooms':
$rooms = $game->getRooms( $userId );
$selfRoomId = $game->findRoomByUserId( $userId );
$data = array(
'rooms' => serialize($rooms),
'selfRoomId' => $selfRoomId,
);
break;
// No parameters
case 'createRoom':
$game->createRoom( $userId );
break;
// roomId
case 'enterRoom':
if( !isset($params['roomId']) )
throw new GameException( 'Parameter RoomId not found' );
$roomId = $params['roomId'];
$game->enterRoom( $userId, $roomId );
break;
// roomId
case 'leaveRoom':
if( !isset($params['roomId']) )
throw new GameException( 'Parameter RoomId not found' );
$roomId = $params['roomId'];
$game->leaveRoom( $userId, $roomId );
break;
// field
case 'saveField':
if( !isset($params['field']) )
throw new GameException( 'Parameter Field not found' );
$field = $params['field'];
$game->saveField( $userId, $field );
break;
/*
// No parameters
case 'runRoom':
Game::instance()->runRoom( $userId );
break;
*/
default:
$res['error'] = 'API-method "'.$method.'" not found';
break;
}
}
catch( GameException $e )
{
$res['error'] = $e->getMessage();
}
$res['data'] = $data;
echo serialize($res);
function checkAuth( $auth, $method, $params )
{
$salt = 'darkair';
$hash = md5(md5($method.serialize($params).$salt));
return $hash == $auth;
}