<?php
/*
* @ServerMon v1.1
* @Developed by Maksa988
* @www.Server-Mon.ru
* @Copyright (C) 2014
*/
 if(!defined('SMON'))
	die('Hacking attempt!');
	
class servers extends Controller {
	public function __construct() {
		global $db;
		$this->header();
		
		$this->model('games');
		$this->model('servers');
		$this->model('serverStats');
		
		$param = explode(".", $this->GET());
		
		if($param[0] == 'all'){
			if($param[1] == $this->cfg('token')){
				$servers = $this->serversModel->getServers();
				foreach($servers as $item){

						$queryLib = new Query($item['game_code']);
						$queryLib->connect($item['server_ip'], $item['server_port']);
						$query = $queryLib->getInfo();
						$queryLib->disconnect();
						
						if($query['maxplayers'] >= '1'){
							$this->serversModel->updateServer($item['server_id'], array(
								$query['hostname'], 
								$query['players'],
								$query['maxplayers'],
								$query['mapname'],
								$query['gamemode']
							));
							
							$this->serverStatsModel->addStats($item['server_id'], $query['players']);
							$this->serversModel->updateServerStatus($item['server_id'], '2');
						} else {
							$this->serverStatsModel->addStats($item['server_id'], '0');
							$this->serversModel->updateServerStatus($item['server_id'], '1');
						}
				}
			} 
			else {
				exit('Error #009');
			}
		} elseif($param[0] == 'game') {
			if($param[2] == $this->cfg('token')){
				if($param[1] >= '1'){
					$game = $this->gamesModel->checkGame($param[1]);
					
					if($game){
						$servers = $this->serversModel->getServers("WHERE `game_code` = '{$game['game_code']}'");
						foreach($servers as $item){

								$queryLib = new Query($item['game_code']);
								$queryLib->connect($item['server_ip'], $item['server_port']);
								$query = $queryLib->getInfo();
								$queryLib->disconnect();
								
								if($query['maxplayers'] >= '1'){
									$this->serversModel->updateServer($item['server_id'], array(
										$query['hostname'], 
										$query['players'],
										$query['maxplayers'],
										$query['mapname'],
										$query['gamemode']
									));
									
									$this->serverStatsModel->addStats($item['server_id'], $query['players']);
									$this->serversModel->updateServerStatus($item['server_id'], '2');
								} else {
									$this->serverStatsModel->addStats($item['server_id'], '0');
									$this->serversModel->updateServerStatus($item['server_id'], '1');
								}
						}
					} else {
						exit('Error #007');
					}
				} else {
					exit('Error #005');
				}
			} 
			else {
				exit('Error #009');
			}
		} elseif($param[0] == 'server') {
			if($param[2] == $this->cfg('token')){
				if($param[1] >= '1'){
					if($server = $this->serversModel->getServerInfo($param[1])){
					
							$queryLib = new Query($server['game_code']);
							$queryLib->connect($server['server_ip'], $server['server_port']);
							$query = $queryLib->getInfo();
							$queryLib->disconnect();
							
							if($query['maxplayers'] >= '1'){
								$this->serversModel->updateServer($server['server_id'], array(
									$query['hostname'], 
									$query['players'],
									$query['maxplayers'],
									$query['mapname'],
									$query['gamemode']
								));
								
								$this->serverStatsModel->addStats($server['server_id'], $query['players']);
								$this->serversModel->updateServerStatus($server['server_id'], '2');
							} else {
								$this->serverStatsModel->addStats($server['server_id'], '0');
								$this->serversModel->updateServerStatus($server['server_id'], '1');
							}
					} else {
						exit('Error #006');
					}
				} else {
					exit('Error #005');
				}
			} 
			else {
				exit('Error #009');
			}
		}
	}
}