<?php
include_once $PATH_TO_CODE."/script/player.class.php";
class DungeonPlayer extends Player {
public function __construct($parPlayerId) {
parent::__construct($parPlayerId);
}
protected function initPlayer($row) {
parent::initPlayer($row);
$this->cash = $row['cash'];
$this->cash2 = $row['cash2'];
$this->shame = $row['shame'];
$this->playerType = $row['playerType'];
}
public function setShame($parShame) {
if($this->shame <> $parShame) {
$this->shame = $parShame;
executer("UPDATE player SET shame = $this->shame WHERE playerId = $this->playerId");
}
}
public function addShame($parFlux) {
$this->setShame($this->shame + $parFlux);
}
public function setCash($parCash) {
if($this->cash <> $parCash) {
$this->cash = $parCash;
executer("UPDATE player SET cash = $this->cash WHERE playerId = $this->playerId");
}
}
public function addCash($parFlux) {
$this->setCash($this->cash + $parFlux);
}
public function setCash2($parCash2) {
if($this->cash2 <> $parCash2) {
$this->cash2 = min(DungeonConstante::$MAX_EVIL_POINT, $parCash2);
executer("UPDATE player SET cash2 = $this->cash2 WHERE playerId = $this->playerId");
}
}
public function addCash2($parFlux) {
$this->setCash2($this->cash2 + $parFlux);
}
/*
public function earnGold($parFlux) {
Log::debug("Player($this->playerId)::earnGold parFlux: $parFlux");
$locRemainingGold=$parFlux;
if($this->playerType == DungeonConstante::$PLAYER_TYPE_HUMAN_KNIGHT) {
$locZoneToFind = DungeonConstante::$ZONE_TYPE_CITY;
$locRatioGoldPower = DungeonConstante::$HUMAN_RATIO_GOLD_POWER;
$locMaxGoldPerZone = DungeonConstante::$CITY_MAX_GOLD;
} else if($this->playerType == DungeonConstante::$PLAYER_TYPE_DUNGEON_MASTER) {
$locZoneToFind = DungeonConstante::$ZONE_TYPE_TREASURE;
$locRatioGoldPower = DungeonConstante::$DUNGEON_MASTER_RATIO_GOLD_POWER;
$locMaxGoldPerZone = DungeonConstante::$TREASURE_MAX_GOLD;
} else {
$locZoneToFind = DungeonConstante::$ZONE_TYPE_NECROPOLIS;
$locRatioGoldPower = DungeonConstante::$UNDEAD_RATIO_GOLD_POWER;
$locMaxGoldPerZone = DungeonConstante::$NECROPOLIS_MAX_GOLD;
}
$res = executer("SELECT zone.zoneId FROM zone, block
WHERE zone.gold < $locMaxGoldPerZone
AND zone.blockId=block.blockId
AND block.playerId=$this->playerId
AND zone.zoneType=$locZoneToFind");
$this->addGoldWithRes($locRemainingGold, $res, $locMaxGoldPerZone);
if($locRemainingGold > 0) {
Log::debug("Player($this->playerId)::earnGold locRemainingGold: $locRemainingGold");
$res = executer("SELECT zone.zoneId FROM zone, block
WHERE zone.blockId=block.blockId
AND block.playerId=$this->playerId
AND zone.occuped=0
AND zone.zoneType=".Constante::$ZONE_TYPE_PLAIN." ORDER BY RAND()");
$this->addGoldWithRes($locRemainingGold, $res, $locMaxGoldPerZone, true, $locZoneToFind);
}
$this->addCash(($parFlux-$locRemainingGold)*$locRatioGoldPower);
}
*/
private function addGoldWithRes(&$parRemainingGold, $parRes, $parMaxGoldPerZone, $parSetZoneType=false, $parNewZoneType=0) {
Log::debug("Player($this->playerId)::addGoldToRes parRemainingGold: $parRemainingGold");
global $gloObjectManager;
$locContinue = true;
while($locContinue) {
$locContinue = ($parRemainingGold > 0) && ($row = mysql_fetch_array($parRes));
if($locContinue) {
$locZone = $gloObjectManager->getZone($row[0]);
$locIsSurface = $locZone->isSurface();
if($this->playerType == DungeonConstante::$PLAYER_TYPE_HUMAN_KNIGHT) {
$locGoodZone = $locIsSurface;
} else {
$locGoodZone = !$locIsSurface;
}
if($locGoodZone) {
if($parSetZoneType) {
$locZone->setZoneType($parNewZoneType);
}
$locZoneGold = $locZone->gold;
$locGoldToGive = min($parRemainingGold, $parMaxGoldPerZone-$locZoneGold);
$locZone->addGold($locGoldToGive);
$parRemainingGold -= $locGoldToGive;
Log::debug("Player($this->playerId)::addGoldToRes locGoldToGive: $locGoldToGive");
}
}
}
}
/**
* @return average population for this player per zone
*/
public function getAveragePopulationPerZone() {
$res = executer("SELECT AVG(zone.population) FROM zone, block WHERE zone.population > 0 and block.playerId=$this->playerId AND block.blockId=zone.blockId");
if($row=mysql_fetch_array($res)) {
return $row[0];
} else {
return 0;
}
}
public function setPlayerType($parPlayerType) {
$this->playerType=$parPlayerType;
executer("UPDATE player SET playerType=$parPlayerType WHERE playerId=$this->playerId");
}
public function getBarrackCount() {
return count($this->getBarracksZoneId());
}
public function getBarracksZoneId() {
$res = executer("SELECT zone.zoneId
FROM zone, block
WHERE block.playerId = $this->playerId
AND zone.blockId=block.blockId
AND zone.zoneType=".DungeonConstante::$ZONE_TYPE_BARRACK);
$locBarracksZoneId = array();
while($row = mysql_fetch_array($res)) {
array_push($locBarracksZoneId, $row[0]);
}
return $locBarracksZoneId;
}
public function getIncomeFormulaForBarrack() {
$locCountOfBarrack = $this->getBarrackCount();
if($locCountOfBarrack > 1) {
return "-".($locCountOfBarrack-1)."*".DungeonConstante::$UPKEEP_BY_BARRACK_BEGIN_TWO;
} else {
return "only 1 barrack so no upkeep";
}
}
public static function selectIncomeForBarrack($parPlayerId=false) {
return executer("SELECT block.playerId, -(COUNT(block.playerId) - 1)*".DungeonConstante::$UPKEEP_BY_BARRACK_BEGIN_TWO."
FROM block, zone WHERE block.playerId > 0
".($parPlayerId?" AND block.playerId=$parPlayerId":"")."
AND zone.blockId=block.blockId
AND zone.zoneType=".DungeonConstante::$ZONE_TYPE_BARRACK."
GROUP BY block.playerId
HAVING COUNT(block.playerId) > 0");
}
public static function selectIncomeForBlock($parPlayerId=false) {
return executer("SELECT playerId, -IFNULL(COUNT(playerId), 0)*".DungeonConstante::$UPKEEP_BY_BLOCK_OWNING." FROM block WHERE playerId > 0
".($parPlayerId?" AND playerId=$parPlayerId":"")." GROUP BY playerId");
}
public function getIncomeFormulaForBlock() {
return "-".count(Block::getAllBlocksId($this->playerId))."*".DungeonConstante::$UPKEEP_BY_BLOCK_OWNING;
}
public static function selectAverageIncomeForGoldMine() {
$locAvg=0;
$locCount=0;
$res = DungeonPlayer::selectIncomeForGoldMine();
while($row = mysql_fetch_array($res)) {
$locAvg+=$row[0];
$locCount++;
}
if(0 == $locCount) {
return 0;
} else {
return $locAvg/$locCount;
}
}
public static function selectIncomeForGoldMine($parPlayerId=false) {
return executer("SELECT playerId, IFNULL(COUNT(zone.zoneId), 0)*".DungeonConstante::$EARN_GOLD_PER_ZONE_EXPLOITED."
FROM block, zone
WHERE block.playerId > 0
AND block.isInLostOfControl = 0
AND zone.blockId = block.blockId
AND zone.zoneType=".DungeonConstante::$ZONE_TYPE_GOLD_EXPLOITED.($parPlayerId?"
AND playerId=$parPlayerId":"")."
GROUP BY playerId");
}
public function getIncomeFormulaForGoldMine() {
$locIncomePerGoldMine=(
DungeonConstante::$EARN_GOLD_PER_ZONE_EXPLOITED*(
$this->playerType==DungeonConstante::$PLAYER_TYPE_HUMAN_KNIGHT?
DungeonConstante::$HUMAN_RATIO_GOLD_POWER:
DungeonConstante::$DUNGEON_MASTER_RATIO_GOLD_POWER));
$locIncome=DungeonPlayer::selectIncomeForGoldMine($this->playerId);
if($locIncome=mysql_fetch_array($locIncome)) {
$locIncome = $locIncome[1];
} else {
$locIncome = 0;
}
return ($locIncome/DungeonConstante::$EARN_GOLD_PER_ZONE_EXPLOITED)."*".$locIncomePerGoldMine;
}
public static function selectIncomeForPopulation($parPlayerId=false) {
return executer("SELECT playerId, ROUND(SQRT(IFNULL(SUM(zone.population/100), 0))*".DungeonConstante::$EARN_PER_POPULATION.")
FROM block, zone
WHERE block.playerId > 0
AND block.isInLostOfControl = 0
AND zone.blockId = block.blockId
AND zone.population > 0 ".($parPlayerId?"
AND playerId=$parPlayerId":"")."
GROUP BY playerId");
}
public function getIncomeFormulaForPopulation() {
$locIncome=DungeonPlayer::selectIncomeForPopulation($this->playerId);
if($locIncome=mysql_fetch_array($locIncome)) {
$locIncome = $locIncome[1];
} else {
$locIncome = 0;
}
$locPopulation = ($locIncome/DungeonConstante::$EARN_PER_POPULATION)*($locIncome/DungeonConstante::$EARN_PER_POPULATION);
return "sqrt($locPopulation)*".DungeonConstante::$EARN_PER_POPULATION."=$locIncome";
}
public static function selectIncomeForUpToNullCash($parPlayerId=false) {
return executer("SELECT playerId, -cash*".DungeonConstante::$FACTOR_TO_SET_CASH_TO_ZERO." FROM player WHERE cash < 0 ".($parPlayerId?" AND playerId=$parPlayerId":""));
}
public function getIncomeFormulaForUpToNullCash() {
if($this->cash >= 0) {
return 0;
} else {
return (-$this->cash)."*".DungeonConstante::$FACTOR_TO_SET_CASH_TO_ZERO;
}
}
public static function selectIncomeForShame($parPlayerId=false) {
return executer("SELECT playerId, -shame FROM player WHERE shame > 0 ".($parPlayerId?" AND playerId=$parPlayerId":""));
}
public function getIncomeFormulaForShame() {
if($this->shame <= 0) {
return 0;
} else {
return -$this->shame;
}
}
public static function updateShame() {
executer("UPDATE player SET shame=shame-cash*".DungeonConstante::$FACTOR_FOR_SHAME." WHERE cash < 0");
executer("UPDATE player SET shame=shame*".DungeonConstante::$FACTOR_TO_SET_SHAME_TO_ZERO);
executer("UPDATE player SET shame=0 WHERE shame < 100");
}
public static function selectIncomeTroopUpkeep($parPlayerId=false) {
return executer("SELECT object.playerId, -SUM(objectInfo.value)
FROM objectInfo, object
WHERE objectInfo.objectId=object.objectId
".($parPlayerId?"AND object.playerId=$parPlayerId":"")."
AND objectInfo.type1=".RealObject::$REAL_OBJECT_INFO_TYPE1_COST."
AND objectInfo.type2=".RealObject::$REAL_OBJECT_INFO_TYPE2_CURRENT_UPKEEP_COST."
AND object.isLobotomize<>".RealObject::$UNIT_IS_DEAD."
AND objectInfo.value > 0
GROUP BY object.playerId");
}
public function getIncomeFormulaTroopUpkeep() {
$res = DungeonPlayer::selectIncomeTroopUpkeep($this->playerId);
if($row = mysql_fetch_array($res)) {
return "upkeep sum is ".(-$row[1])." for ".count(Player::staticGetAllRealObjectId($this->playerId, true))." units";
} else {
return "no upkeep for your troop";
}
}
public function getStartZoneId() {
$locZoneTargetId=false;
$locBarracksZoneId = $this->getBarracksZoneId();
if(count($locBarracksZoneId) > 0) {
$locZoneTargetId = $locBarracksZoneId[0];
}
if($locZoneTargetId) {
return $locZoneTargetId;
} else {
return parent::getStartZoneId();
}
}
public function isNewbie() {
return $this->getLevel() < DungeonConstante::$PLAYER_LEVEL_NEWBIE;
}
public function getLevel() {
$res = executer(
"SELECT SUM(SQRT(objectInfo.value))
FROM object, objectInfo
WHERE object.playerId=".$this->playerId."
AND objectInfo.objectId=object.objectId
AND objectInfo.type1=".DungeonRealObject::$REAL_OBJECT_INFO_TYPE1_LEVEL."
AND objectInfo.type2=".DungeonRealObject::$REAL_OBJECT_INFO_TYPE2_CURRENT_POINT."
AND object.isLobotomize<>".RealObject::$UNIT_IS_DEAD);
if($row = mysql_fetch_array($res)) {
$locSumLevel = $row[0];
} else {
$locSumLevel = 0;
}
$locLevel = count(Block::getAllBlocksId($this->playerId)) + $locSumLevel;
if($this->cash2 > 0 && $this->shame == 0) {
$locLevel += sqrt($this->cash2/1000.0);
}
return $locLevel;
}
}
?>