Location: PHPKode > projects > PhpBlock > A9.8/script/server.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";


class Server {
    private static $instance=false;
    private static $infos;
    function getInstance() {
        if(!Server::$instance) {
            Server::$instance = new Server();
        }
        return Server::$instance;
    }


    public static $SERVER_TYPE1_CREATE_PLAYER = 0;
    public static $SERVER_INFO_TYPE1_TICKER = 1;
    public static $SERVER_INFO_TYPE1_TICK_TIME_ELAPSED = 2;
    
    private static $SERVER_TYPE2_X = 0;
    private static $SERVER_TYPE2_Y = 1;
    private static $SERVER_TYPE2_RANGE = 2;
    private static $SERVER_TYPE2_SQUARE_REMAINING_BEFORE_TURN = 3;
    private static $SERVER_TYPE2_TURN_COUNT = 4;
    private static $SERVER_TYPE2_JUMP_SIZE = 5;
    
    /**Add value must be less than 1000*/
    public static $SERVER_TYPE2_HAPPY_HOME_PLAYER_TYPE_BASE = 1000;
    
    public function __construct() {
    	$this->refreshInternalServerInfo();
    }

    public static function reset($parJumpSize=1) {
    	//Delete all those information ?
        executer("REPLACE INTO serverInfo (type1, type2, value) VALUES
        (".Server::$SERVER_TYPE1_CREATE_PLAYER.",".Server::$SERVER_TYPE2_X.",".Constante::getWorldCenterX()."),
        (".Server::$SERVER_TYPE1_CREATE_PLAYER.",".Server::$SERVER_TYPE2_Y.",".Constante::getWorldCenterY()."),
        (".Server::$SERVER_TYPE1_CREATE_PLAYER.",".Server::$SERVER_TYPE2_JUMP_SIZE.",$parJumpSize),
        (".Server::$SERVER_TYPE1_CREATE_PLAYER.",".Server::$SERVER_TYPE2_RANGE.",0),
        (".Server::$SERVER_TYPE1_CREATE_PLAYER.",".Server::$SERVER_TYPE2_SQUARE_REMAINING_BEFORE_TURN.",100),
        (".Server::$SERVER_TYPE1_CREATE_PLAYER.",".Server::$SERVER_TYPE2_TURN_COUNT.",100)
        ");
        Server::getInstance()->refreshInternalServerInfo();
    }

    public function getNextCreatePlayerXY() {
        $locNextXY = array();
        $locNextXY[0] = $this->getCreatePlayerX();
        $locNextXY[1] = $this->getCreatePlayerY();
        return $locNextXY;
    }

    public function nextCreatePlayerXY() {
        list($locX, $locY) = $this->getNextCreatePlayerXY();
        if($this->getCreatePlayerSquareRemainingBeforeTurn() <= 0) {
            //We turn
            $this->setCreatePlayerTurnCount($this->getCreatePlayerTurnCount()+1);
            $this->setCreatePlayerSquareRemainingBeforeTurn($this->getCreatePlayerRange());
        } else {
            //One square less
            $this->setCreatePlayerSquareRemainingBeforeTurn($this->getCreatePlayerSquareRemainingBeforeTurn()-1);
        }
        //We go ahead
        switch($this->getCreatePlayerTurnCount()) {
            case 0:
            //Right
            $locX=$locX+$this->getJumpSize();
            break;
            case 1:
            //Down
            $locY=$locY-$this->getJumpSize();
            break;
            case 2:
            //Left
            $locX=$locX-$this->getJumpSize();
            break;
            case 3:
            //Up
            $locY=$locY+$this->getJumpSize();
            break;
            default:
            //Upper
            $locY=$locY+$this->getJumpSize();
            $this->setCreatePlayerRange($this->getCreatePlayerRange()+1);
            $this->setCreatePlayerSquareRemainingBeforeTurn($this->getCreatePlayerRange());
            $this->setCreatePlayerTurnCount(0);
            break;
        }
        $this->setCreatePlayerX($locX);
        $this->setCreatePlayerY($locY);
    }

    public function refreshServerInfo() {
        disconnect();
        connectToDB();
        ObjectManager::getInstance()->reset();
        $this->refreshInternalServerInfo();
    }

    public function getJumpSize() {
        return $this->getValue(Server::$SERVER_TYPE1_CREATE_PLAYER, Server::$SERVER_TYPE2_JUMP_SIZE);
    }
    public function getCreatePlayerX() {
        return $this->getValue(Server::$SERVER_TYPE1_CREATE_PLAYER, Server::$SERVER_TYPE2_X);
    }
    public function getCreatePlayerY() {
        return $this->getValue(Server::$SERVER_TYPE1_CREATE_PLAYER, Server::$SERVER_TYPE2_Y);
    }
    public function getCreatePlayerRange() {
        return $this->getValue(Server::$SERVER_TYPE1_CREATE_PLAYER, Server::$SERVER_TYPE2_RANGE);
    }
    public function getCreatePlayerSquareRemainingBeforeTurn() {
        return $this->getValue(Server::$SERVER_TYPE1_CREATE_PLAYER, Server::$SERVER_TYPE2_SQUARE_REMAINING_BEFORE_TURN);
    }
    public function getCreatePlayerTurnCount() {
        return $this->getValue(Server::$SERVER_TYPE1_CREATE_PLAYER, Server::$SERVER_TYPE2_TURN_COUNT);
    }

    public function setCreatePlayerX($parValue) {
        $this->updateValue(Server::$SERVER_TYPE1_CREATE_PLAYER, Server::$SERVER_TYPE2_X, $parValue);
    }
    public function setCreatePlayerY($parValue) {
        $this->updateValue(Server::$SERVER_TYPE1_CREATE_PLAYER, Server::$SERVER_TYPE2_Y, $parValue);
    }
    public function setCreatePlayerRange($parValue) {
        $this->updateValue(Server::$SERVER_TYPE1_CREATE_PLAYER, Server::$SERVER_TYPE2_RANGE, $parValue);
    }
    public function setCreatePlayerSquareRemainingBeforeTurn($parValue) {
        $this->updateValue(Server::$SERVER_TYPE1_CREATE_PLAYER, Server::$SERVER_TYPE2_SQUARE_REMAINING_BEFORE_TURN, $parValue);
    }
    public function setCreatePlayerTurnCount($parValue) {
        $this->updateValue(Server::$SERVER_TYPE1_CREATE_PLAYER, Server::$SERVER_TYPE2_TURN_COUNT, $parValue);
    }
    private function updateValue($parType1, $parType2, $parValue) {
        Log::debug("server.php updateValue $parType1, $parType2, $parValue");
        executer("UPDATE serverInfo SET value=$parValue WHERE type1=$parType1 AND type2=$parType2");
        $this->infos[$parType1][$parType2] = $parValue;
    }
    public function upsertValue($parType1, $parType2, $parValue) {
        Log::debug("server.php upsertServer $parType1, $parType2, $parValue");
        executer("REPLACE INTO serverInfo (type1, type2, value) VALUES ($parType1, $parType2, $parValue)");
        $this->infos[$parType1][$parType2] = $parValue;
    }

    public function getValue($parType1, $parType2) {
    	if(isset($this->infos[$parType1][$parType2])) {
    		return $this->infos[$parType1][$parType2];
    	} else {
    		return false;
    	}
    }
    public function refreshInternalServerInfo() {
    	$this->infos=array();
    	$res = executer("SELECT type1, type2, value FROM serverInfo");
    	while($row = mysql_fetch_array($res)) {
    		$locType1 = $row[0];
    		$locType2 = $row[1];
    		$locValue = $row[2];
    		$this->infos[$locType1][$locType2]=$locValue;
    	}
    }
}

function junitServerClass() {
    $locServer = Server::getInstance();
    $locServer->reset(5);
    for($i=0; $i < 100; $i++) {
        list($x, $y) = $locServer->getNextCreatePlayerXY();
        echo "x: ".($x-$xprev).", y: ".($y-$yprev)."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x: $x, y: $y<br>";
        $yprev=$y;
        $xprev=$x;
        $locServer->nextCreatePlayerXY();
    }
}
?>
Return current item: PhpBlock