From 733766dd45bda98f87b219998d57af31531f3415 Mon Sep 17 00:00:00 2001 From: Stefan Brannfjell Date: Sun, 9 Mar 2014 04:42:28 +0100 Subject: [PATCH] Started with JSON API module system --- api/api.php | 30 ++++++++++++++++ api/index.php | 45 ++++++++++++++++++++++++ api/module.php | 3 ++ api/modules/highscores/topExperience.php | 23 ++++++++++++ api/modules/samples/blank.php | 44 +++++++++++++++++++++++ api/modules/towns/getTownNames.php | 15 ++++++++ config.php | 7 ++-- engine/function/general.php | 4 +-- 8 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 api/api.php create mode 100644 api/index.php create mode 100644 api/module.php create mode 100644 api/modules/highscores/topExperience.php create mode 100644 api/modules/samples/blank.php create mode 100644 api/modules/towns/getTownNames.php diff --git a/api/api.php b/api/api.php new file mode 100644 index 0000000..84e8e67 --- /dev/null +++ b/api/api.php @@ -0,0 +1,30 @@ + array( + 'znote' => $version, + 'ot' => $config['TFSVersion'] + ), +); + +if (isset($moduleVersion)) $response['version']['module'] = $moduleVersion; +function SendResponse($response) { + global $config; + if ($config['api']['debug']) data_dump($response, false, "Response (debug mode)"); + else echo json_encode($response); +} +?> \ No newline at end of file diff --git a/api/index.php b/api/index.php new file mode 100644 index 0000000..6f4763e --- /dev/null +++ b/api/index.php @@ -0,0 +1,45 @@ +isDot()) + continue; + $iterator = new DirectoryIterator($entity->getPathname()); + foreach($iterator as $entity) { + if($entity->isFile()) { + $file_extension = pathinfo($entity->getFilename(), PATHINFO_EXTENSION); + if ($file_extension == 'php') { + $path = explode('/', $entity->getPathname()); + if (count($path) === 1) $path = explode('\\', $entity->getPathname()); + $plugins[$path[1]] = $path[2]; + } + } + } +} + +$response['modules'] = $plugins; +$response['data']['title'] = $config['site_title']; +$response['data']['slogan'] = $config['site_title_context']; +$response['data']['time'] = getClock(time(), false, true); +$response['data']['time_formatted'] = getClock(time(), true, true); + +// Account count +$accounts = mysql_select_single("SELECT COUNT('id') AS `count` FROM `accounts`;"); +$response['data']['accounts'] = ($accounts !== false) ? $accounts['count'] : 0; +// Player count +$players = mysql_select_single("SELECT COUNT('id') AS `count` FROM `players`;"); +$response['data']['players'] = ($players !== false) ? $players['count'] : 0; +// online player count +if ($config['TFSVersion'] != 'TFS_10') $online = mysql_select_single("SELECT COUNT('id') AS `count` FROM `players` WHERE `status`='1';"); +else $online = mysql_select_single("SELECT COUNT('player_id') AS `count` FROM `players_online`;"); +$response['data']['online'] = ($online !== false) ? $online['count'] : 0; +$response['data']['client'] = $config['client']; +$response['data']['port'] = $config['port']; +$response['data']['guildwar'] = $config['guildwar_enabled']; +$response['data']['forum'] = $config['forum']['enabled']; + +SendResponse($response); +?> \ No newline at end of file diff --git a/api/module.php b/api/module.php new file mode 100644 index 0000000..1713af8 --- /dev/null +++ b/api/module.php @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/api/modules/highscores/topExperience.php b/api/modules/highscores/topExperience.php new file mode 100644 index 0000000..2ec4847 --- /dev/null +++ b/api/modules/highscores/topExperience.php @@ -0,0 +1,23 @@ + \ No newline at end of file diff --git a/api/modules/samples/blank.php b/api/modules/samples/blank.php new file mode 100644 index 0000000..31f8b45 --- /dev/null +++ b/api/modules/samples/blank.php @@ -0,0 +1,44 @@ +hasExpired()) { + $players = mysql_select_multi("SELECT `name`, `level`, `experience` FROM `players` ORDER BY `experience` DESC LIMIT 5;"); + + $cache->setContent($players); + $cache->save(); + } else { + $players = $cache->load(); + } + + -Functions found in general.php + :When fetching GET or POST from parameters, ALWAYS use getValue($value) + :Etc if you want to fetch character name from url, do it like this: + $playername = getValue($_GET['name']); + if ($playername !== false) { + // $playername either contains player name, or false if failed to fetch name from GET. + } + :getValue is often used in 3 ways: Fetch GET and POST values, or sanitize/secure any value you wish. + :Check ZnoteAAC\engine\function\general.php for full list of available functions. +*/ + +// Save the results of previous logic to the response +$response['data']['title'] = "The fabulous blank page!"; + +// Send the response through JSON API +SendResponse($response); +?> \ No newline at end of file diff --git a/api/modules/towns/getTownNames.php b/api/modules/towns/getTownNames.php new file mode 100644 index 0000000..186e620 --- /dev/null +++ b/api/modules/towns/getTownNames.php @@ -0,0 +1,15 @@ + \ No newline at end of file diff --git a/config.php b/config.php index dda7d26..49245f2 100644 --- a/config.php +++ b/config.php @@ -139,7 +139,6 @@ // -------------- \\ // WEBSITE STUFF \\ // -------------- \\ - // Highscore configuration $config['highscore'] = array( 'rows' => 100, @@ -232,7 +231,11 @@ //----------------\\ // ADVANCED STUFF \\ //----------------\\ - + // Api config + $config['api'] = array( + 'debug' => false, + ); + // Don't touch this unless you know what you are doing. (modifying this(key value) also requires modifications in OT files /XML/commands.xml). $config['ingame_positions'] = array( 1 => 'Player', diff --git a/engine/function/general.php b/engine/function/general.php index b671d17..d7efbaf 100644 --- a/engine/function/general.php +++ b/engine/function/general.php @@ -97,7 +97,6 @@ function url($path = false) { return $protocol . $domain . $folder . '/' . $path; } -// Get last cached function getCache() { $results = mysql_select_single("SELECT `cached` FROM `znote`;"); return ($results !== false) ? $results['cached'] : false; @@ -294,6 +293,7 @@ function getIP() { return $_SERVER['REMOTE_ADDR']; } +// Deprecated, just use count($array) instead. function array_length($ar) { $r = 1; foreach($ar as $a) { @@ -329,7 +329,6 @@ function online_id_to_name($id) { // Parameter: players.vocation_id. Returns: Configured vocation name. function vocation_id_to_name($id) { $vocations = config('vocations'); - return ($vocations[$id] >= 0) ? $vocations[$id] : false; } @@ -361,7 +360,6 @@ function skillid_to_name($skillid) { // Parameter: players.town_id. Returns: Configured town name. function town_id_to_name($id) { $towns = config('towns'); - return (array_key_exists($id, $towns)) ? $towns[$id] : 'Missing Town'; }