Location: PHPKode > projects > ezStats for XBOX Live > ezStats2_xbl/classes/player.php
<?php
	class Player {
		private $sql = NULL;
		private $tpl = NULL;
		private $func = NULL;
		private $settings = NULL;
		
		public function __construct() {
			$this->sql  = new MySQL();
			$this->func = new Functions();
			$this->settings = new Settings();
			$this->tpl  = new Templates();
			$this->tpl->load_phrases("admin", true);
		}
		
		
		public function add($name) {
			$name = trim($name);
			
			// Test ob Name übergeben wurde 
			if ($name == "") {
				return $this->tpl->phrase("message_no_player_given");
			}
			
			// Name wurde übergeben
			else {
				// Überprüfen ob der Name in der Datenbank vorhanden ist
				$this->sql->query('SELECT id FROM '.$this->sql->prefix.'player WHERE name = "'.$name.'"');
				
				if($this->sql->count()) {
					// Name ist schon in der DB vorhanden / Message generieren / Spielername in die Phrase laden 
					$message = $this->tpl->phrase("message_player_already_added");
					eval ( "\$message = \"$message\";" );
					return $message;
				} 
				else {
					// Name ist noch nicht in der DB vorhanden / Spieler in die DB speichern
					$this->sql->query('INSERT INTO '.$this->sql->prefix.'player (name) VALUES ("'.$name.'")');
					$playerid = $this->sql->id;
					
					// Spieler updaten mit der Spieler-ID
					$result = $this->update($playerid, $name);
					
					// Daten der Aktualisierung in die Spielereigenschaften schreiben
					$this->sql->query('
						UPDATE `'.$this->sql->prefix.'player` SET
							`last_update` = "'.time().'"
						WHERE `id` = "'.$playerid.'"
					');
					
					// Message generieren / Spielername in die Phrase laden
					$message = $this->tpl->phrase("message_add_player");
					eval ( "\$message = \"$message\";" );
					return $message;
				}
			}
		}
		
		
		public function delete($playerid = NULL, $name = "NoName") {
			if ($playerid === NULL) {
				return "ERROR: No Player-ID given";
			} else {
				$this->sql->query('DELETE FROM '.$this->sql->prefix.'player WHERE id = "'.$playerid.'"');
				
				$message = $this->tpl->phrase("message_delete_player");
				eval ( "\$message = \"$message\";" );
				return $message;
			}
		}
		
		
		public function update($playerid, $name) {
			### STATS-DATEN ABRUFEN ###
			$api = new XboxApi();
			$profile = $api->fetch_profile($name);
			
			if (is_array($profile) AND isset($profile['error']['message'])) {
				$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `name` = "'.$name.'" WHERE `id` = "'.$playerid.'"');
				$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `state` = "'.$profile['error']['message'].'" WHERE `id` = "'.$playerid.'"');
				
				return true;
			} else if (is_object($profile) AND isset($profile->status) AND $profile->status != "error") {
				$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `state` = "'.$profile->status.'" WHERE `id` = "'.$playerid.'"');
				
				$profile = $profile->data;
				$games   = $api->fetch_games($name);
				if (isset($games->data)) $games = $games->data; else $games = new stdClass;
				
				if (isset($profile->gamertag))		$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `name` = "'.$profile->gamertag.'" WHERE `id` = "'.$playerid.'"');
				if (isset($profile->tier))			$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `Tier` = "'.$profile->tier.'" WHERE `id` = "'.$playerid.'"');
				if (isset($profile->presence))		$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `OnlineStatus` = "'.$profile->presence.'" WHERE `id` = "'.$playerid.'"');
				if (isset($profile->avatar->large))	$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `AvatarTile` = "'.$profile->avatar->large.'" WHERE `id` = "'.$playerid.'"');
				if (isset($profile->gamerscore))	$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `GamerScore` = "'.$profile->gamerscore.'" WHERE `id` = "'.$playerid.'"');
				if (isset($profile->reputation))	$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `Reputation` = "'.$profile->reputation.'" WHERE `id` = "'.$playerid.'"');
				if (isset($games->totalgames))		$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `GameCount` = "'.$games->totalgames.'" WHERE `id` = "'.$playerid.'"');
				if (isset($games->gamerscore->current))		$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `TotalEarnedGamerScore` = "'.$games->gamerscore->current.'" WHERE `id` = "'.$playerid.'"');
				if (isset($games->gamerscore->total))		$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `TotalPossibleGamerScore` = "'.$games->gamerscore->total.'" WHERE `id` = "'.$playerid.'"');
				if (isset($games->achievements->current))	$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `TotalEarnedAchievements` = "'.$games->achievements->current.'" WHERE `id` = "'.$playerid.'"');
				if (isset($games->achievements->total))		$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `TotalPossibleAchievements` = "'.$games->achievements->total.'" WHERE `id` = "'.$playerid.'"');
				if (isset($games->progress))		$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `TotalPercentCompleted` = "'.$games->progress.'" WHERE `id` = "'.$playerid.'"');
				if (isset($games->games))			$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `LastGame` = "'.$this->convert($games->games[0]).'" WHERE `id` = "'.$playerid.'"');
				
				return true;
			} else if (isset($profile->status) AND $profile->status == "error") {
				$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `state` = "'.$profile->data->message.'" WHERE `id` = "'.$playerid.'"');
				return false;
			} else {
				$this->sql->query('UPDATE `'.$this->sql->prefix.'player` SET `state` = "Unknown Error" WHERE `id` = "'.$playerid.'"');
				return false;
			}
		}
		
		
		private function convert($obj) {
			// Funktion: Objekt in JSON-String konvertieren und Slashes maskieren
			$string = json_encode($obj);
			if (get_magic_quotes_gpc()) $string = stripslashes($string);
			if (function_exists("mysql_real_escape_string")) $string = mysql_real_escape_string($string); else $string = addslashes($string);
			return $string;
		}
	}
?>
Return current item: ezStats for XBOX Live