Skip to content

Commit

Permalink
Most functions now return by value
Browse files Browse the repository at this point in the history
Now that most variables are assigned by value instead of by reference,
we can modify our function interfaces to only return results by value.

See smrealms#317.
  • Loading branch information
hemberger committed Mar 10, 2019
1 parent 63ce1a2 commit 8bde156
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 129 deletions.
6 changes: 3 additions & 3 deletions lib/Default/AbstractSmrAccount.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ abstract class AbstractSmrAccount {
return self::DEFAULT_HOTKEYS;
}

public static function &getAccount($accountID,$forceUpdate = false) {
public static function getAccount($accountID,$forceUpdate = false) {
if($forceUpdate || !isset(self::$CACHE_ACCOUNTS[$accountID])) {
self::$CACHE_ACCOUNTS[$accountID] = new SmrAccount($accountID);
}
Expand Down Expand Up @@ -416,7 +416,7 @@ abstract class AbstractSmrAccount {
return $this->score;
}

public function &getIndividualScores(SmrPlayer $player = null) {
public function getIndividualScores(SmrPlayer $player = null) {
$gameID=0;
if($player!=null)
$gameID = $player->getGameID();
Expand Down Expand Up @@ -462,7 +462,7 @@ abstract class AbstractSmrAccount {
return $this->referrerID>0;
}

public function &getReferrer() {
public function getReferrer() {
return SmrAccount::getAccount($this->getReferrerID());
}

Expand Down
34 changes: 17 additions & 17 deletions lib/Default/AbstractSmrCombatWeapon.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,22 @@ abstract class AbstractSmrCombatWeapon {
return array('MaxDamage' => $this->getMaxDamage(), 'Shield' => $this->getShieldDamage(), 'Armour' => $this->getArmourDamage(), 'Rollover' => $this->isDamageRollover());
}

abstract public function &getModifiedDamageAgainstForces(AbstractSmrPlayer $weaponPlayer, SmrForce $forces);
abstract public function &getModifiedDamageAgainstPort(AbstractSmrPlayer $weaponPlayer, SmrPort $port);
abstract public function &getModifiedDamageAgainstPlanet(AbstractSmrPlayer $weaponPlayer, SmrPlanet $planet);
abstract public function &getModifiedPortDamageAgainstPlayer(SmrPort $port, AbstractSmrPlayer $targetPlayer);
abstract public function &getModifiedDamageAgainstPlayer(AbstractSmrPlayer $weaponPlayer, AbstractSmrPlayer $targetPlayer);
abstract public function &getModifiedForceDamageAgainstPlayer(SmrForce $forces, AbstractSmrPlayer $targetPlayer);

protected function &doPlayerDamageToForce(array &$return, AbstractSmrPlayer $weaponPlayer, SmrForce $forces) {
abstract public function getModifiedDamageAgainstForces(AbstractSmrPlayer $weaponPlayer, SmrForce $forces);
abstract public function getModifiedDamageAgainstPort(AbstractSmrPlayer $weaponPlayer, SmrPort $port);
abstract public function getModifiedDamageAgainstPlanet(AbstractSmrPlayer $weaponPlayer, SmrPlanet $planet);
abstract public function getModifiedPortDamageAgainstPlayer(SmrPort $port, AbstractSmrPlayer $targetPlayer);
abstract public function getModifiedDamageAgainstPlayer(AbstractSmrPlayer $weaponPlayer, AbstractSmrPlayer $targetPlayer);
abstract public function getModifiedForceDamageAgainstPlayer(SmrForce $forces, AbstractSmrPlayer $targetPlayer);

protected function doPlayerDamageToForce(array &$return, AbstractSmrPlayer $weaponPlayer, SmrForce $forces) {
$return['WeaponDamage'] = $this->getModifiedDamageAgainstForces($weaponPlayer,$forces);
$return['ActualDamage'] = $forces->doWeaponDamage($return['WeaponDamage']);
if($return['ActualDamage']['KillingShot'])
$return['KillResults'] = $forces->killForcesByPlayer($weaponPlayer);
return $return;
}

protected function &doPlayerDamageToPlayer(array &$return, AbstractSmrPlayer $weaponPlayer, AbstractSmrPlayer $targetPlayer) {
protected function doPlayerDamageToPlayer(array &$return, AbstractSmrPlayer $weaponPlayer, AbstractSmrPlayer $targetPlayer) {
$return['WeaponDamage'] = $this->getModifiedDamageAgainstPlayer($weaponPlayer,$targetPlayer);
$return['ActualDamage'] = $targetPlayer->getShip()->doWeaponDamage($return['WeaponDamage']);

Expand All @@ -101,23 +101,23 @@ abstract class AbstractSmrCombatWeapon {
return $return;
}

protected function &doPlayerDamageToPort(array &$return, AbstractSmrPlayer $weaponPlayer, SmrPort $port) {
protected function doPlayerDamageToPort(array &$return, AbstractSmrPlayer $weaponPlayer, SmrPort $port) {
$return['WeaponDamage'] = $this->getModifiedDamageAgainstPort($weaponPlayer,$port);
$return['ActualDamage'] = $port->doWeaponDamage($return['WeaponDamage']);
if($return['ActualDamage']['KillingShot'])
$return['KillResults'] = $port->killPortByPlayer($weaponPlayer);
return $return;
}

protected function &doPlayerDamageToPlanet(array &$return, AbstractSmrPlayer $weaponPlayer, SmrPlanet $planet, $delayed) {
protected function doPlayerDamageToPlanet(array &$return, AbstractSmrPlayer $weaponPlayer, SmrPlanet $planet, $delayed) {
$return['WeaponDamage'] = $this->getModifiedDamageAgainstPlanet($weaponPlayer,$planet);
$return['ActualDamage'] = $planet->doWeaponDamage($return['WeaponDamage'],$delayed);
if($return['ActualDamage']['KillingShot'])
$return['KillResults'] = $planet->killPlanetByPlayer($weaponPlayer);
return $return;
}

protected function &doPortDamageToPlayer(array &$return, SmrPort $port, AbstractSmrPlayer $targetPlayer) {
protected function doPortDamageToPlayer(array &$return, SmrPort $port, AbstractSmrPlayer $targetPlayer) {
$return['WeaponDamage'] = $this->getModifiedPortDamageAgainstPlayer($port,$targetPlayer);
$return['ActualDamage'] = $targetPlayer->getShip()->doWeaponDamage($return['WeaponDamage']);

Expand All @@ -126,7 +126,7 @@ abstract class AbstractSmrCombatWeapon {
return $return;
}

protected function &doPlanetDamageToPlayer(array &$return, SmrPlanet $planet, AbstractSmrPlayer $targetPlayer) {
protected function doPlanetDamageToPlayer(array &$return, SmrPlanet $planet, AbstractSmrPlayer $targetPlayer) {
$return['WeaponDamage'] = $this->getModifiedPlanetDamageAgainstPlayer($planet,$targetPlayer);
$return['ActualDamage'] = $targetPlayer->getShip()->doWeaponDamage($return['WeaponDamage']);

Expand All @@ -135,7 +135,7 @@ abstract class AbstractSmrCombatWeapon {
return $return;
}

protected function &doForceDamageToPlayer(array &$return, SmrForce $forces, AbstractSmrPlayer $targetPlayer) {
protected function doForceDamageToPlayer(array &$return, SmrForce $forces, AbstractSmrPlayer $targetPlayer) {
$return['WeaponDamage'] = $this->getModifiedForceDamageAgainstPlayer($forces,$targetPlayer);
$return['ActualDamage'] = $targetPlayer->getShip()->doWeaponDamage($return['WeaponDamage']);

Expand All @@ -144,7 +144,7 @@ abstract class AbstractSmrCombatWeapon {
return $return;
}

abstract public function &shootForces(AbstractSmrPlayer $weaponPlayer, SmrForce $forces);
abstract public function &shootPlayer(AbstractSmrPlayer $weaponPlayer, AbstractSmrPlayer $targetPlayer);
abstract public function &shootPlayerAsForce(SmrForce $forces, AbstractSmrPlayer $targetPlayer);
abstract public function shootForces(AbstractSmrPlayer $weaponPlayer, SmrForce $forces);
abstract public function shootPlayer(AbstractSmrPlayer $weaponPlayer, AbstractSmrPlayer $targetPlayer);
abstract public function shootPlayerAsForce(SmrForce $forces, AbstractSmrPlayer $targetPlayer);
}
14 changes: 7 additions & 7 deletions lib/Default/AbstractSmrLocation.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AbstractSmrLocation {
protected $shipsSold;
protected $weaponsSold;

public static function &getAllLocations($forceUpdate = false) {
public static function getAllLocations($forceUpdate = false) {
if($forceUpdate || !isset(self::$CACHE_ALL_LOCATIONS)) {
$db = new SmrMySqlDatabase();
$db->query('SELECT * FROM location_type ORDER BY location_type_id');
Expand All @@ -36,7 +36,7 @@ class AbstractSmrLocation {
return self::$CACHE_ALL_LOCATIONS;
}

public static function &getSectorLocations($gameID,$sectorID,$forceUpdate = false) {
public static function getSectorLocations($gameID,$sectorID,$forceUpdate = false) {
if($forceUpdate || !isset(self::$CACHE_SECTOR_LOCATIONS[$gameID][$sectorID])) {
$db = new SmrMySqlDatabase();
$db->query('SELECT * FROM location JOIN location_type USING (location_type_id) WHERE sector_id = ' . $db->escapeNumber($sectorID) . ' AND game_id=' . $db->escapeNumber($gameID));
Expand All @@ -50,7 +50,7 @@ class AbstractSmrLocation {
return self::$CACHE_SECTOR_LOCATIONS[$gameID][$sectorID];
}

public static function &getLocation($locationTypeID, $forceUpdate=false, $db=null) {
public static function getLocation($locationTypeID, $forceUpdate=false, $db=null) {
if($forceUpdate || !isset(self::$CACHE_LOCATIONS[$locationTypeID])) {
self::$CACHE_LOCATIONS[$locationTypeID] = new SmrLocation($locationTypeID, $db);
}
Expand Down Expand Up @@ -225,7 +225,7 @@ class AbstractSmrLocation {
}
}

public function &getHardwareSold() {
public function getHardwareSold() {
if(!isset($this->hardwareSold)) {
$this->hardwareSold = array();
$this->db->query('SELECT hardware_type_id FROM location_sells_hardware WHERE ' . $this->SQL);
Expand Down Expand Up @@ -261,7 +261,7 @@ class AbstractSmrLocation {
unset($this->hardwareSold[$hardwareTypeID]);
}

public function &getShipsSold() {
public function getShipsSold() {
if(!isset($this->shipsSold)) {
$this->shipsSold = array();
$this->db->query('SELECT * FROM location_sells_ships WHERE ' . $this->SQL);
Expand Down Expand Up @@ -296,7 +296,7 @@ class AbstractSmrLocation {
unset($this->shipsSold[$shipTypeID]);
}

public function &getWeaponsSold() {
public function getWeaponsSold() {
if(!isset($this->weaponsSold)) {
$this->weaponsSold = array();
$this->db->query('SELECT * FROM location_sells_weapons JOIN weapon_type USING (weapon_type_id) WHERE ' . $this->SQL);
Expand Down Expand Up @@ -330,7 +330,7 @@ class AbstractSmrLocation {
unset($this->weaponsSold[$weaponTypeID]);
}

public function &getLinkedLocations() {
public function getLinkedLocations() {
$linkedLocations = array();
if($this->isHQ()) {
if($this->getTypeID()==LOCATION_TYPE_FEDERAL_HQ) {
Expand Down
30 changes: 15 additions & 15 deletions lib/Default/AbstractSmrPlayer.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ abstract class AbstractSmrPlayer {
return $this->gameID;
}

public function &getGame() {
public function getGame() {
return SmrGame::getGame($this->gameID);
}

Expand Down Expand Up @@ -108,19 +108,19 @@ abstract class AbstractSmrPlayer {
}
}

public function &getSectorPlanet() {
public function getSectorPlanet() {
return SmrPlanet::getPlanet($this->getGameID(),$this->getSectorID());
}

public function &getSectorPort() {
public function getSectorPort() {
return SmrPort::getPort($this->getGameID(),$this->getSectorID());
}

public function getSectorID() {
return $this->sectorID;
}

public function &getSector() {
public function getSector() {
return SmrSector::getSector($this->getGameID(),$this->getSectorID());
}

Expand Down Expand Up @@ -526,7 +526,7 @@ abstract class AbstractSmrPlayer {
return $this->getAccountID() == $this->getAlliance($forceUpdate)->getLeaderID();
}

public function &getAlliance($forceUpdate = false) {
public function getAlliance($forceUpdate = false) {
return SmrAlliance::getAlliance($this->getAllianceID(), $this->getGameID(), $forceUpdate);
}

Expand Down Expand Up @@ -653,25 +653,25 @@ abstract class AbstractSmrPlayer {
return $rels[$raceID];
}

abstract public function &getShip();
abstract public function getShip();

public function &shootPlayer(AbstractSmrPlayer $targetPlayer) {
public function shootPlayer(AbstractSmrPlayer $targetPlayer) {
return $this->getShip()->shootPlayer($targetPlayer);
}

public function &shootForces(SmrForce $forces) {
public function shootForces(SmrForce $forces) {
return $this->getShip()->shootForces($forces);
}

public function &shootPort(SmrPort $port) {
public function shootPort(SmrPort $port) {
return $this->getShip()->shootPort($port);
}

public function &shootPlanet(SmrPlanet $planet, $delayed) {
public function shootPlanet(SmrPlanet $planet, $delayed) {
return $this->getShip()->shootPlanet($planet, $delayed);
}

public function &shootPlayers(array $targetPlayers) {
public function shootPlayers(array $targetPlayers) {
return $this->getShip()->shootPlayers($targetPlayers);
}

Expand Down Expand Up @@ -925,10 +925,10 @@ abstract class AbstractSmrPlayer {
}

abstract public function killPlayer($sectorID);
abstract public function &killPlayerByPlayer(AbstractSmrPlayer $killer);
abstract public function &killPlayerByForces(SmrForce $forces);
abstract public function &killPlayerByPort(SmrPort $port);
abstract public function &killPlayerByPlanet(SmrPlanet $planet);
abstract public function killPlayerByPlayer(AbstractSmrPlayer $killer);
abstract public function killPlayerByForces(SmrForce $forces);
abstract public function killPlayerByPort(SmrPort $port);
abstract public function killPlayerByPlanet(SmrPlanet $planet);


public function getTurns() {
Expand Down
28 changes: 14 additions & 14 deletions lib/Default/AbstractSmrPort.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class AbstractSmrPort {
self::$CACHE_CACHED_PORTS = array();
}

public static function &getPort($gameID, $sectorID, $forceUpdate=false, $db=null) {
public static function getPort($gameID, $sectorID, $forceUpdate=false, $db=null) {
if($forceUpdate || !isset(self::$CACHE_PORTS[$gameID][$sectorID])) {
self::$CACHE_PORTS[$gameID][$sectorID] = new SmrPort($gameID, $sectorID, $db);
}
Expand All @@ -87,7 +87,7 @@ class AbstractSmrPort {
unset(self::$CACHE_PORTS[$gameID][$sectorID]);
}

public static function &createPort($gameID,$sectorID) {
public static function createPort($gameID,$sectorID) {
if(!isset(self::$CACHE_PORTS[$gameID][$sectorID])) {
$p = new SmrPort($gameID,$sectorID);
self::$CACHE_PORTS[$gameID][$sectorID] = $p;
Expand Down Expand Up @@ -247,25 +247,25 @@ class AbstractSmrPort {
return $this->getVisibleGoods('Buy', $player);
}

public function &getAllGoodIDs() {
public function getAllGoodIDs() {
return $this->goodIDs['All'];
}

/**
* Get IDs of goods that can be sold to the port
*/
public function &getSoldGoodIDs() {
public function getSoldGoodIDs() {
return $this->goodIDs['Sell'];
}

/**
* Get IDs of goods that can be bought from the port
*/
public function &getBoughtGoodIDs() {
public function getBoughtGoodIDs() {
return $this->goodIDs['Buy'];
}

public function &getGood($goodID) {
public function getGood($goodID) {
if ($this->hasGood($goodID)) {
return Globals::getGood($goodID);
} else {
Expand Down Expand Up @@ -743,7 +743,7 @@ class AbstractSmrPort {
return $this->sectorID;
}

public function &getSector() {
public function getSector() {
return SmrSector::getSector($this->getGameID(),$this->getSectorID());
}

Expand Down Expand Up @@ -814,7 +814,7 @@ class AbstractSmrPort {
return $this->getLevel() + 3;
}

public function &getWeapons() {
public function getWeapons() {
$weapons = array();
for ($i=0; $i<$this->getNumWeapons(); ++$i) {
$weapons[$i] = SmrWeapon::getWeapon(WEAPON_PORT_TURRET);
Expand Down Expand Up @@ -1036,7 +1036,7 @@ class AbstractSmrPort {
}
return false;
}
public static function &getCachedPort($gameID,$sectorID,$accountID,$forceUpdate=false) {
public static function getCachedPort($gameID,$sectorID,$accountID,$forceUpdate=false) {
if($forceUpdate || !isset(self::$CACHE_CACHED_PORTS[$gameID][$sectorID][$accountID])) {
$db = new SmrMySqlDatabase();
$db->query('SELECT visited, port_info
Expand Down Expand Up @@ -1128,11 +1128,11 @@ class AbstractSmrPort {
}


public function &shootPlayer(AbstractSmrPlayer $targetPlayer) {
public function shootPlayer(AbstractSmrPlayer $targetPlayer) {
return $this->shootPlayers(array($targetPlayer));
}

public function &shootPlayers(array $targetPlayers) {
public function shootPlayers(array $targetPlayers) {
$results = array('Port' => $this, 'TotalDamage' => 0, 'TotalDamagePerTargetPlayer' => array());
foreach($targetPlayers as $targetPlayer) {
$results['TotalDamagePerTargetPlayer'][$targetPlayer->getAccountID()] = 0;
Expand Down Expand Up @@ -1164,7 +1164,7 @@ class AbstractSmrPort {
return $results;
}

public function &doWeaponDamage(array $damage) {
public function doWeaponDamage(array $damage) {
$alreadyDead = $this->isDestroyed();
$shieldDamage = 0;
$cdDamage = 0;
Expand Down Expand Up @@ -1218,7 +1218,7 @@ class AbstractSmrPort {
return $actualDamage;
}

protected function &getAttackersToCredit() {
protected function getAttackersToCredit() {
//get all players involved for HoF
$attackers = array();
$this->db->query('SELECT account_id,level FROM player_attacks_port WHERE ' . $this->SQL . ' AND time > ' . $this->db->escapeNumber(TIME - self::TIME_TO_CREDIT_RAID));
Expand Down Expand Up @@ -1265,7 +1265,7 @@ class AbstractSmrPort {
return $credits;
}

public function &killPortByPlayer(AbstractSmrPlayer $killer) {
public function killPortByPlayer(AbstractSmrPlayer $killer) {
$return = array();

// Port is destroyed, so empty the port of all trade goods
Expand Down
Loading

0 comments on commit 8bde156

Please sign in to comment.