stjernan

asd

Jan 30th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.44 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Model;
  4.  
  5. use Tmvc;
  6.  
  7. //use App;
  8.  
  9. class servers {
  10.  
  11.     private $c;
  12.  
  13.     public function __construct() {
  14.         $this->c = Tmvc\Container::get();
  15.     }
  16.  
  17.     //gameQ biblioteket som hamnar i DiC (Anonym funktions lagring)
  18.     public function gameQ() {
  19.         include "/gameq/GameQ.php";
  20.         return new \GameQ;
  21.     }
  22.  
  23.     public function fetchServers() {
  24.         return $this->c->Database->
  25.                         prepare('
  26.                 SELECT * FROM warzone.servers
  27.                 ORDER BY ID DESC')
  28.                         ->execute()
  29.                         ->fetchAll();
  30.     }
  31.  
  32.     public function fetchServerbyId($id) {
  33.         if (empty($id))
  34.             return false;
  35.         return $this->c->Database->
  36.                         prepare('
  37.                 SELECT * FROM warzone.servers
  38.                 WHERE ID = ' . $id)
  39.                         ->execute()
  40.                         ->fetchAll();
  41.     }
  42.  
  43.     public function insert($data) {
  44.         $this->c->pdo->
  45.                 prepare('
  46.         INSERT INTO warzone.servers (alias,ip,type)
  47.                VALUES (:alias,:ip,:type)')
  48.                 ->execute(array(
  49.                     ':alias' => $data['alias'],
  50.                     ':ip' => $data['ip'],
  51.                     ':type' => $data['type']
  52.         ));
  53.     }
  54.  
  55.     public function update($data) {
  56.         $this->c->pdo->
  57.                 prepare('
  58.                    UPDATE warzone.servers
  59.                    SET alias=:alias, ip=:ip, type=:type
  60.                    WHERE id=:id')
  61.                 ->execute(array(
  62.                     ':alias' => $data['alias'],
  63.                     ':ip' => $data['ip'],
  64.                     ':type' => $data['type'],
  65.                     ':id' => $data['id']
  66.         ));
  67.     }
  68.  
  69.     public function delete($data) {
  70.         $this->c->pdo->
  71.                 prepare('
  72.                    DELETE FROM warzone.servers
  73.                    WHERE id =:id')
  74.                 ->execute(array(
  75.                     ':id' => $data['id']
  76.         ));
  77.     }
  78.  
  79.     public function listServers($id = false) {
  80.         $gq = $this->gameQ();
  81.  
  82.         $fetch_servers = $this->fetchServers();
  83.         if (!$fetch_servers)
  84.             return false;
  85.         $result = array();
  86.  
  87.         foreach ($fetch_servers as $server_data) {
  88.             $ip_data = explode(":", $server_data["ip"]);
  89.             $servers[] = array($server_data["type"], $ip_data[0], $ip_data[1]);
  90.         }
  91.  
  92.         $gq->addServers($servers);
  93.  
  94.         $reqeust = $gq->requestData($servers);
  95.  
  96.         foreach ($reqeust as $server_item) {
  97.             $result[$server_item["game_descr"]][] = array(
  98.                 "ip" => $server_item["gq_address"] . ":" . $server_item["gq_port"],
  99.                 "server_name" => $server_item["hostname"],
  100.                 "map" => $server_item["map"],
  101.                 "num_players" => $server_item["num_players"],
  102.                 "max_players" => $server_item["max_players"],
  103.                 "game_name" => $server_item["game_descr"]
  104.             );
  105.         }
  106.         return $result;
  107.     }
  108.  
  109.     public function listGames() {
  110.         $return = array();
  111.         $ini = parse_ini_file('/gameq/gameq/games.ini', true);
  112.         foreach ($ini as $key => &$entry)
  113.             $entry['id'] = $key;
  114.  
  115.  
  116.         sort($ini);
  117.  
  118.         foreach ($ini as $key => $entry) {
  119.             $return[] = array("id" => $entry['id'], "name" => htmlentities($entry['name']));
  120.         }
  121.         return $return;
  122.     }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment