PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/servers_controller.php

http://skygames.googlecode.com/
PHP | 260 lines | 185 code | 71 blank | 4 comment | 26 complexity | 3763c74528864d9a96c0402e1ce8f3f3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, CC-BY-SA-3.0
  1. <?php
  2. class ServersController extends AppController {
  3. var $name = 'Servers';
  4. var $helpers = array('Html', 'Form', 'Ajax', 'Javascript');
  5. var $components = array('Lgsl');
  6. var $defaultGameId = 2;
  7. var $lgsl_types = array(
  8. 'Counter-Strike' => 'halflife',
  9. 'Counter-Strike: Source' => 'source',
  10. 'Call of Duty 4' => 'callofduty4',
  11. 'Warcraft 3' => ''
  12. );
  13. var $paginate = array('Servercomment' => array('limit' => 8, 'page' => 'last'));
  14. function grab_feed($url, $cacheTimeMinutes)
  15. {
  16. $fn = ROOT . DS . APP_DIR . DS . 'tmp' . DS . 'cache' . DS . 'servers' . DS . md5($url); // Final filename.
  17. if (file_exists($fn) && (time() - filemtime($fn) > $cacheTimeMinutes * 60))
  18. {
  19. return unserialize(file_get_contents($fn));
  20. }
  21. else
  22. {
  23. /* write the file to disk */
  24. $f = fopen($fn,"w");
  25. $curl = curl_init($url);
  26. curl_setopt ($curl, CURLOPT_FILE, $f);
  27. curl_setopt ($curl, CURLOPT_HEADER, 0);
  28. $curl_result = curl_exec($curl);
  29. curl_close($curl);
  30. fclose($f);
  31. /* Save the file to the valid array */
  32. return unserialize(file_get_contents($fn));
  33. }
  34. }
  35. function getLgslType($game_id)
  36. {
  37. $game = $this->Server->Game->read(null, $game_id);
  38. return $this->lgsl_types[$game['Game']['name']];
  39. }
  40. function index($id = null) {
  41. if (isset($this->data['Server']['game_id']))
  42. $game_id = $this->data['Server']['game_id'];
  43. else
  44. $game_id = 2;
  45. if (isset($this->data['Server']['sort']))
  46. $sort = $this->data['Server']['sort'];
  47. else
  48. $sort = "players";
  49. if (isset($this->data['Server']['refresh']))
  50. $refresh = $this->data['Server']['refresh'];
  51. else
  52. $refresh = 60;
  53. $this->useAjaxForMonitor = false;
  54. $this->monitor($game_id, $sort);
  55. $this->layout = 'skygames';
  56. $games = $this->Server->Game->find('list');
  57. $this->set('games', $games);
  58. $this->set('game_id', $game_id);
  59. $this->set('sort', $sort);
  60. $this->set('refresh', $refresh);
  61. $this->contentHelpers = false;
  62. }
  63. function monitor($game_id = null, $sort = null)
  64. {
  65. function cmpServersPlayers($a, $b) { return $b["Server"]["players"] - $a["Server"]["players"]; }
  66. function cmpServersPing($a, $b) {
  67. $i = 0;
  68. return $a["Server"]["ping"] - $b["Server"]["ping"];
  69. }
  70. $this->layout = 'ajax';
  71. Configure::write('debug', '0');
  72. if (!$game_id)
  73. $id = $this->defaultGameId;
  74. else
  75. $id = $game_id;
  76. $servers = $this->Server->findAll(array("game_id" => $id));
  77. foreach ($servers as $key => $srv)
  78. {
  79. //$url = "http://module.game-monitor.com/" . $srv["Server"]['ip'] . ":" . $srv["Server"]['port'] . "/data/server.php";
  80. //$server = $this->grab_feed($url, $srv["Server"]['cachetime']);
  81. echo $srv["Server"]['ip'];
  82. $server = $this->Lgsl->get_server($srv["Server"]['ip'], $srv["Server"]['port'], $this->getLgslType( $srv["Server"]['game_id']));
  83. if ($server['b']['status'])
  84. {
  85. $servers[$key]["Server"]["name"] = $server['s']['name'];
  86. $servers[$key]["Server"]["ping"] = $server['s']['query_time'];
  87. $servers[$key]["Server"]["maxplayers"] = $server['s']['playersmax'];
  88. $servers[$key]["Server"]["players"] = $server['s']['players'];
  89. $servers[$key]["Server"]["map"] = $server['s']['map'];
  90. }
  91. else
  92. unset($servers[$key]);
  93. }
  94. if ($sort == "players")
  95. {
  96. $i = 0;
  97. usort($servers, "cmpServersPlayers");
  98. }
  99. elseif ($sort == "ms")
  100. {
  101. $i = 0;
  102. usort($servers, "cmpServersPing");
  103. }
  104. $this->set('servers', $servers);
  105. }
  106. function addcomment()
  107. {
  108. if (!empty($this->data)) {
  109. $this->data['Servercomment']['user_id'] = $this->othAuth->user('id');
  110. $this->Server->Servercomment->create();
  111. if ($this->Server->Servercomment->save($this->data)) {
  112. $this->Session->setFlash(__('Comment has been submited', true));
  113. } else {
  114. $this->Session->setFlash(__('Error occured', true));
  115. }
  116. $this->redirect(array('action'=>'view', $this->data['Servercomment']['server_id']));
  117. exit();
  118. }
  119. }
  120. function view($id = null) {
  121. if (!$id) {
  122. $this->Session->setFlash(__('Invalid Server.', true));
  123. $this->redirect(array('action'=>'index'));
  124. }
  125. $server = $this->Server->read(null, $id);
  126. $info = $this->Lgsl->get_server($server["Server"]['ip'], $server["Server"]['port'], $this->getLgslType( $server["Server"]['game_id']));
  127. $players = $this->Lgsl->get_players($server["Server"]['ip'], $server["Server"]['port'], $this->getLgslType( $server["Server"]['game_id']));
  128. $comments = $this->paginate('Servercomment', array('server_id' => $id));
  129. $this->set('server', $server);
  130. $this->set('players', $players['p']);
  131. $this->set('info', $info);
  132. $this->set('comments', $comments);
  133. $this->titleForContent = '<b>' . $info["s"]["name"] . '</b>';
  134. }
  135. function admin_index() {
  136. $this->Server->recursive = 0;
  137. $this->set('servers', $this->paginate());
  138. }
  139. function admin_view($id = null) {
  140. if (!$id) {
  141. $this->Session->setFlash(__('Invalid Server.', true));
  142. $this->redirect(array('action'=>'index'));
  143. }
  144. $this->set('server', $this->Server->read(null, $id));
  145. }
  146. function admin_add() {
  147. if (!empty($this->data)) {
  148. $this->Server->create();
  149. if ($this->Server->save($this->data)) {
  150. $this->Session->setFlash(__('The Server has been saved', true));
  151. $this->redirect(array('action'=>'index'));
  152. } else {
  153. $this->Session->setFlash(__('The Server could not be saved. Please, try again.', true));
  154. }
  155. }
  156. $games = $this->Server->Game->find('list');
  157. $this->set(compact('games'));
  158. }
  159. function admin_edit($id = null) {
  160. if (!$id && empty($this->data)) {
  161. $this->Session->setFlash(__('Invalid Server', true));
  162. $this->redirect(array('action'=>'index'));
  163. }
  164. if (!empty($this->data)) {
  165. if ($this->Server->save($this->data)) {
  166. $this->Session->setFlash(__('The Server has been saved', true));
  167. $this->redirect(array('action'=>'index'));
  168. } else {
  169. $this->Session->setFlash(__('The Server could not be saved. Please, try again.', true));
  170. }
  171. }
  172. if (empty($this->data)) {
  173. $this->data = $this->Server->read(null, $id);
  174. }
  175. $games = $this->Server->Game->find('list');
  176. $this->set(compact('games'));
  177. }
  178. function admin_delete($id = null) {
  179. if (!$id) {
  180. $this->Session->setFlash(__('Invalid id for Server', true));
  181. $this->redirect(array('action'=>'index'));
  182. }
  183. if ($this->Server->del($id)) {
  184. $this->Session->setFlash(__('Server deleted', true));
  185. $this->redirect(array('action'=>'index'));
  186. }
  187. }
  188. }
  189. ?>