Location: PHPKode > projects > PhpBlock > A9.8/script/alliance/alliance.class.php
<?php
class Alliance {
	public function __construct($row) {
		$this->allianceId = $row[0];
		$this->name = $row[1];		
		$this->leaderId = $row[2];
	}
	
	//TODO : move this in global object manager	
	public static function getAlliance($parAllianceId) {
		$row = mysql_fetch_array(executer("SELECT allianceId, name, leaderId FROM alliance WHERE allianceId=$parAllianceId"));
		if($row) {
			return new Alliance($row);
		}
		return false;
	}
	
	public function getName() {
		return $this->name;
	}
	public function getLeader() {
		global $gloObjectManager;
		return $gloObjectManager->getPlayer($this->leaderId);
	}
	/**
	 * @retrun an arrays of players which ask to join
	 */
	public function getAllPlayersAskingJoin() {
		$locAllPlayers = array();
		global $gloObjectManager;
		$res = executer("SELECT playerId FROM alliancejoin WHERE allianceId=$this->allianceId");
		while($row = mysql_fetch_array($res)) {
			array_push($locAllPlayers, $gloObjectManager->getPlayer($row[0]));
		}
		return $locAllPlayers;
	}
	
	/**
	 * @retrun an arrays of alliance where player ask
	 */
	public static function getAllAlliancesWithAsk($parPlayerId) {
		$locAllAlliances = array();
		global $gloObjectManager;
		$res = executer("SELECT allianceId FROM alliancejoin WHERE playerId=$parPlayerId");
		while($row = mysql_fetch_array($res)) {
			array_push($locAllAlliances, Alliance::getAlliance($row[0]));
		}
		return $locAllAlliances;
	}
	
	public function playerRemoveAskToJoin($parPlayerId) {
		executer("DELETE FROM alliancejoin WHERE allianceId=$this->allianceId AND playerId=$parPlayerId");
	}
	public function playerAskToJoin($parPlayerId) {
		executer("REPLACE INTO alliancejoin (allianceId, playerId) VALUES($this->allianceId, $parPlayerId)");
	}
	public function isAskingToJoin($parPlayerId) {
		$res = executer("SELECT playerId FROM alliancejoin WHERE allianceId=$this->allianceId");
		return mysql_fetch_array($res);
	}
	public function removeLeader() {
		$locAllPlayersFromHisAlliance = $this->getAllPlayers();
		foreach ($locAllPlayersFromHisAlliance as $locPlayerFromHisAlliance) {
			if($locPlayerFromHisAlliance->playerId != $this->leaderId) {
				$locPlayerFromHisAlliance->setAllianceId($locPlayerFromHisAlliance->playerId);
				$locAllianceOfThisPlayer = Alliance::getAlliance($locPlayerFromHisAlliance->playerId);
				$locAllianceOfThisPlayer->setLeaderId($locPlayerFromHisAlliance->playerId);
			}
		}
		$this->setLeaderId(0);
	}
	public function acceptPlayer($parPlayerId) {
		$locAllianceOfPlayer = Alliance::getAlliance($parPlayerId);
		if($locAllianceOfPlayer->leaderId == $parPlayerId) {
			//Remove all players from its alliance if its a leader
			$locAllianceOfPlayer->removeLeader();			
		}
		executer("DELETE FROM alliancejoin WHERE allianceId=$this->allianceId AND playerId=$parPlayerId");
		global $gloObjectManager;
		$gloObjectManager->getPlayer($parPlayerId)->setAllianceId($this->allianceId);
	}
	
	public function rejectPlayer($parPlayerId) {
		$locTargetAlliance = Alliance::getAlliance($parPlayerId);
		$locTargetAlliance->acceptPlayer($parPlayerId);
		$locTargetAlliance->setLeaderId($parPlayerId);
	}
	public function setLeaderId($parPlayerId) {
		executer("UPDATE alliance SET leaderId=$parPlayerId WHERE allianceId=$this->allianceId");
		$this->leaderId = $parPlayerId;
	}
	
	/**
	 * @retrun an arrays of players which are in alliance
	 */
	public function getAllPlayers()  {
		$locAllPlayers = array();
		global $gloObjectManager;
		$res = executer("SELECT playerId FROM player WHERE allianceId=$this->allianceId");
		while($row = mysql_fetch_array($res)) {
			array_push($locAllPlayers, $gloObjectManager->getPlayer($row[0]));
		}
		return $locAllPlayers;
	}
}
?>
Return current item: PhpBlock