Location: PHPKode > projects > PhpBlock > A9.8/script/player.class.php
<?php
include_once $PATH_TO_CODE."/script/fonction.php";
include_once $PATH_TO_CODE."/script/zone.class.php";

include_once $PATH_TO_CODE."/script/imagemanager.class.php";

include_once $PATH_TO_CODE."/script/log.class.php";

include_once $PATH_TO_CODE."/script/alliance/alliance.class.php";


class Player {
    public function __construct($parPlayerId) {
        $this->playerId = $parPlayerId;
        Log::debug("Player::__construct this->playerId $this->playerId");
        
        $res = executer("SELECT * FROM player WHERE playerId =$this->playerId");
        $this->initPlayer(mysql_fetch_array($res));
    }

    protected function initPlayer($row) {
        Log::debug("Player $this->playerId initPlayer ");
        $this->color = $row['color'];
        $this->allianceId = $row['allianceId'];
        $this->playerName = $row['playerName'];
        $this->firmName = $row['firmName'];
        $this->language = $row['language'];
        $this->IAMode = $row['IAMode'];
    }
    public function setLanguage($parNewLanguage) {
    	$this->language = $parNewLanguage;
    	executer("UPDATE player set language='$this->language' WHERE playerId=$this->playerId");
    }
    public function getLanguage() {
   		return $this->language;
    }
	public function getAlliance() {
		return Alliance::getAlliance($this->allianceId);
	}
	
    public function setAllianceId($parAllianceId) {
        global $gloObjectManager;
        $this->allianceId = $parAllianceId;
        executer("UPDATE player SET allianceId=$parAllianceId WHERE playerId=$this->playerId");
        //TODO : update block, object and other stuff !!
        $res = executer("SELECT blockId FROM block WHERE playerId = $this->playerId");
        while($row = mysql_fetch_array($res)) {
            $gloObjectManager->getBlock($row[0])->setAllianceId($this->allianceId);
        }
        $res = executer("SELECT objectId FROM object WHERE playerId = $this->playerId");
        while($row = mysql_fetch_array($res)) {
            $gloObjectManager->getRealObject($row[0])->setAllianceId($this->allianceId);
        }
    }
    public static function getPlayerId($parLogin, $parPassword) {
        $res = executer("SELECT playerId FROM player WHERE login LIKE '$parLogin' AND pass LIKE MD5('$parPassword')");
        if($row = mysql_fetch_array($res)) {
            return $row[0];
        } else {
            return false;
        }
    }

    public function getStartZoneId() {
        $locBlocksId = Block::getAllBlocksId($this->playerId);
        if(count($locBlocksId) > 0) {
            return $locZoneTargetId = $locBlocksId[0];
        } else {
            return Zone::getZoneId(Constante::getWorldCenterX(), Constante::getWorldCenterY(), Site::$ZONE_COORD_Z_GROUND_LEVEL);
        }
    }

    public static function create($parPlayerName, $parEntityNameLogin, $parPlayerLogin, $parPlayerPassword, $parColor, $parOverwrite=false) {
        executer(($parOverwrite?"REPLACE":"INSERT")." INTO `player` (`playerName`, `firmName`, `login` , `pass` , `cash`, `color` )
        VALUES ('$parPlayerName', '$parEntityNameLogin', '$parPlayerLogin', MD5('$parPlayerPassword'), 0, '$parColor')");
        $locPlayerId = getLastInsertedId();
        executer("UPDATE player SET allianceId=$locPlayerId WHERE playerId=$locPlayerId");
        executer(($parOverwrite?"REPLACE":"INSERT")." INTO `alliance` (`allianceId`, `leaderId`, `name`)
        VALUES ('$locPlayerId', '$locPlayerId', '$parEntityNameLogin')");
        return $locPlayerId;
    }
    
    public static function testPlayerCreation($parPlayerName, $parEntityNameLogin, $parPlayerLogin, $parPlayerPassword, $parColor, $parEmail, &$parMessageResult) {
        $parMessageResult = false;
        if(mysql_fetch_array(executer("SELECT * FROM player WHERE LOWER(color)=LOWER('$parColor')"))) {
            $parMessageResult = "color '$parColor' is already used";
            return false;
        }
        if(mysql_fetch_array(executer("SELECT * FROM player WHERE LOWER(playerName)=LOWER('$parPlayerName')"))) {
            $parMessageResult = "player name '$parPlayerName' is already used";
            return false;
        }
        if(mysql_fetch_array(executer("SELECT * FROM player WHERE LOWER(firmName)=LOWER('$parEntityNameLogin')"))) {
            $parMessageResult = "entity name '$parEntityNameLogin' is already used";
            return false;
        }
        if(mysql_fetch_array(executer("SELECT * FROM player WHERE LOWER(login)=LOWER('$parPlayerLogin')"))) {
            $parMessageResult = "login '$parPlayerLogin' is already used";
            return false;
        }
        if(!checkEmail($parEmail)) {
            $parMessageResult = "email '$parEmail' is not valid";
            return false;
        }
        if(mysql_fetch_array(executer("SELECT * FROM player WHERE LOWER(email)=LOWER('$parEmail')"))) {
            $parMessageResult = "email '$parEmail' is already used";
            return false;
        }
        return true;
    }

    public static function staticGetAllRealObjectId($parPlayerId, $parMustBeAlive=false) {
    	$locArrayOfObjectId = array();
    	$req = "SELECT objectId from object WHERE playerId=$parPlayerId ";
    	if($parMustBeAlive) {
    		$req .= " AND isLobotomize<>".RealObject::$UNIT_IS_DEAD;
    	}
    	$res = executer($req);
        while($row = mysql_fetch_array($res)) {
        	$locObjectId = $row[0];
            array_push($locArrayOfObjectId, $locObjectId);
        }
        return $locArrayOfObjectId;
    }
    public static function staticGetAllRealObject($parPlayerId, $parMustBeAlive=false) {
    	$locArrayOfObjectId = Player::staticGetAllRealObjectId($parPlayerId, $parMustBeAlive);
    	global $gloObjectManager;
    	return $gloObjectManager->getRealObjects($locArrayOfObjectId);
    }
    public function getAllRealObject($parMustBeAlive=false) {
    	return Player::staticGetAllRealObject($this->playerId, $parMustBeAlive);
    }
    public static function staticGetLogin($parPlayerId) {
    	$res = executer("SELECT login FROM player where playerId=$parPlayerId");
    	$row = mysql_fetch_array($res);
    	return $row[0];
    }
    public function toStringMessage() {
    	return $this->playerName;
    }
}
?>
Return current item: PhpBlock