Added full source code

This commit is contained in:
Emmanuel BENOîT 2016-01-10 11:01:49 +01:00
commit 33f8586698
1377 changed files with 123808 additions and 0 deletions

View file

@ -0,0 +1,46 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/library/checkPlanetName.inc
//
// This function checks whether a planet name is valid or not
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_checkPlanetName {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->planets = $this->lib->game->getLib('beta5/planet');
}
public function run($name) {
if (trim($name) != $name) {
$rv = 7;
} elseif (strlen($name) > 15) {
$rv = 1;
} elseif (preg_match('/[^A-Za-z0-9_\.\-\+@\/'."'".' ]/', $name)) {
$rv = 2;
} elseif (preg_match('/\s\s+/', $name)) {
$rv = 3;
} elseif (strlen($name) < 2) {
$rv = 4;
} elseif (!preg_match('/[A-Za-z]/', $name)) {
$rv = 5;
} elseif ($this->planets->call('nameExists', $name)) {
$rv = 6;
} else {
$rv = 0;
}
return $rv;
}
}
?>

View file

@ -0,0 +1,23 @@
<?php
class beta5_getPlayerCount {
function beta5_getPlayerCount($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
/** Get player count. */
function run() {
$r = $this->db->query("SELECT COUNT(*) FROM player "
. "WHERE (quit IS NULL OR UNIX_TIMESTAMP(NOW())-quit<86400) AND NOT hidden");
if (!$r) {
return 0;
}
list($c) = dbFetchArray($r);
return $c;
}
}
?>

View file

@ -0,0 +1,30 @@
<?php
class beta5_getPlayerStatus {
function beta5_getPlayerStatus($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($pid) {
$q = $this->db->query("SELECT cash FROM player WHERE id=$pid");
if ($q && dbCount($q)) {
list($cash) = dbFetchArray($q);
} else {
$cash = 0;
}
$q = $this->db->query("SELECT COUNT(*) FROM planet WHERE owner=$pid");
if ($q && dbCount($q)) {
list($planets) = dbFetchArray($q);
} else {
$planets = 0;
}
return array($planets, $cash, 0);
}
}
?>

View file

@ -0,0 +1,119 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/library/investigate.inc
//
// This function investigates two accounts in a game, checking for
// suspicious events such as donations, sales, tech exchange, or planets
// retaken by one of the players after having been abandonned by the
// other.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_investigate {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
public function run($player1, $player2, $now, $delay) {
// Create the events record, generate timing part of the queries
$events = array(
"LSE" => 0,
"SE" => 0,
"HSE" => 0,
"VHSE" => 0
);
$tQuery = "BETWEEN " . (1 + $now - $delay) . " AND $now";
// Get the amount of donations in the interval
$q = $this->db->query(
"SELECT amount FROM donation_log "
. "WHERE \"time\" $tQuery "
. "AND (source = $player1 AND target = $player2 "
. "OR source = $player2 AND target = $player1)"
);
while ($r = dbFetchArray($q)) {
list($amount) = $r;
if ($amount > 10000000) {
$et = "VHSE";
} elseif ($amount > 1000000) {
$et = "HSE";
} elseif ($amount > 100000) {
$et = "SE";
} else {
$et = "LSE";
}
$events[$et] ++;
}
// Get the amount of sales in the interval
$q = $this->db->query(
"SELECT sell_price FROM sale_history "
. "WHERE ended $tQuery "
. "AND (from_player = $player1 AND to_player = $player2 "
. "OR from_player = $player2 AND to_player = $player1)"
);
while ($r = dbFetchArray($q)) {
list($price) = $r;
if (is_null($price)) {
continue;
}
if ($price == 0) {
$et = "HSE";
} else {
$et = "SE";
}
$events[$et] ++;
}
// Get planets that were abandonned by one and retaken by the other
$q = $this->db->query(
"SELECT planet FROM abandon_log "
. "WHERE retake_time $tQuery AND retake_time - abandon_time < 5 * 86400 "
. "AND (former_owner = $player1 AND retake_owner = $player2 "
. "OR former_owner = $player2 AND retake_owner = $player1)"
);
while ($r = dbFetchArray($q)) {
$events["SE"] ++;
}
// Get tech exchanges
$q = $this->db->query(
"SELECT accepted, price FROM research_assistance "
. "WHERE moment $tQuery "
. "AND (player = $player1 AND offer_to = $player2 "
. "OR player = $player2 AND offer_to = $player1)"
);
while ($r = dbFetchArray($q)) {
list($accepted, $price) = $r;
if ($accepted == 'f') {
$et = "LSE";
} elseif ($price > 1000 || is_null($accepted)) {
$et = "SE";
} elseif ($price < 1000) {
$et = "HSE";
}
$events[$et] ++;
}
// Generate the actual array of events
$actualEvents = array("CHECK");
foreach ($events as $eType => $eCount) {
if ($eCount > 0) {
array_push($actualEvents, "$eType-$eCount");
}
}
return $actualEvents;
}
}
?>

View file

@ -0,0 +1,31 @@
<?php
class beta5_isFinished {
function __construct($lib) {
$this->lib = $lib;
$this->game = $this->lib->game;
$this->db = $this->game->getDBAccess();
}
function run() {
if ($this->game->params['victory'] == 0) {
return false;
}
// Check for victory on normal matches
if ($this->game->params['victory'] == 1) {
$q = $this->db->query("SELECT COUNT(*) FROM alliance_victory "
. "WHERE UNIX_TIMESTAMP(NOW()) >= time_of_victory");
list($c) = dbFetchArray($q);
return ($c > 0);
}
// Check for victory on CTF matches
$q = $this->db->query("SELECT * FROM ctf_points WHERE points = 100");
return (dbCount($q) == 1);
}
}
?>

View file

@ -0,0 +1,142 @@
<?php
class beta5_leaveGame {
function beta5_leaveGame($lib) {
$this->lib = $lib;
$this->game = $lib->game;
$this->db = $this->lib->game->db;
$this->rankings = $this->lib->game->getLib('main/rankings');
$this->alliance = $this->lib->game->getLib('beta5/alliance');
$this->planets = $this->lib->game->getLib('beta5/planet');
$this->players = $this->lib->game->getLib('beta5/player');
$this->sales = $this->lib->game->getLib('beta5/sale');
}
function run($id, $reason) {
if ($this->game->params['victory'] != 0) {
return;
}
logText("Player #$id is LEAVING game ($reason)!");
if ($reason == 'KICKED') {
$reason = 'KICK';
}
$pinf = $this->players->call('get', $id);
$name = $pinf['name'];
$msgs = array();
// Leave alliance
if (!is_null($pinf['aid'])) {
$this->alliance->call('leave', $id, true);
$a = "'" . addslashes($pinf['alliance']) . "'";
$q = $this->db->query("SELECT id FROM player WHERE alliance={$pinf['aid']} AND a_status='IN'");
while ($r = dbFetchArray($q)) {
$msgs[$r[0]] = array($a, dbBool(0), 0, 0);
}
}
// Marketplace
$q = $this->db->query("SELECT id,player,finalized,sold_to FROM sale WHERE player=$id OR sold_to=$id");
while ($r = dbFetchArray($q)) {
list($sid,$seller,$fin,$buyer) = $r;
if (is_null($fin)) {
$ga = 'cancel';
} else {
$ga = 'cancelTransfer';
if ($seller == $id) {
$tInc = 0;
$fInc = 1;
$t = $buyer;
} else {
$tInc = 1;
$fInc = 0;
$t = $seller;
}
if (is_array($msgs[$t])) {
$msgs[$t][2] += $tInc;
$msgs[$t][3] += $fInc;
} else {
$msgs[$t] = array('NULL', dbBool(0), $tInc, $fInc);
}
}
$this->sales->call($ga, $seller, $sid);
}
// FIXME: probes
$this->db->query("DELETE FROM fleet WHERE owner=$id");
// Remove planets and build queues
$q = $this->db->query(
"SELECT p.id, s.id FROM planet p, system s "
. "WHERE p.owner = $id AND s.id = p.system "
. "FOR UPDATE OF p, s"
);
$systems = array();
while ($r = dbFetchArray($q)) {
if (!in_array($r[1], $systems)) {
$systems[] = $r[1];
}
$this->db->query("DELETE FROM buildqueue WHERE planet=".$r[0]);
$this->db->query("DELETE FROM planet_abandon_time WHERE id = {$r[0]}");
// $this->db->query("DELETE FROM built_probes WHERE planet={$r[0]}");
$this->planets->call('updateMaxPopulation', $r[0], $pid, null);
}
foreach ($systems as $systemID) {
$this->db->query("UPDATE system SET prot = 0 WHERE id = $systemID");
}
$this->db->query("UPDATE planet SET owner=NULL,sale=NULL,bh_prep=NULL,abandon=NULL,beacon=0,"
. "vacation='NO',built_probe=FALSE,probe_policy=NULL WHERE owner=$id");
// Remove research data
$this->db->query("UPDATE player SET res_assistance=NULL WHERE id=$id OR res_assistance=$id");
$this->db->query("DELETE FROM research_player WHERE player=$id");
// FIXME: doesn't work for some reason - investigate
//$this->db->query("DELETE FROM rule WHERE player=$id");
// Remove player from TA lists and enemy lists
$this->db->query("DELETE FROM trusted WHERE player=$id");
$q = $this->db->query("SELECT player,level FROM trusted WHERE friend=$id");
while ($r = dbFetchArray($q)) {
list($pl,$l) = $r;
if (!is_array($msgs[$pl])) {
$msgs[$pl] = array('NULL', dbBool(1), 0, 0);
} else {
$msgs[$pl][1] = dbBool(1);
}
$this->db->query("DELETE FROM trusted WHERE friend=$id AND player=$pl");
$q2 = $this->db->query("SELECT level FROM trusted WHERE level > $l AND player=$pl "
. "ORDER BY level FOR UPDATE");
while ($r2 = dbFetchArray($q2)) {
$this->db->query("UPDATE trusted SET level=level-1 WHERE player=$pl AND level={$r2[0]}");
}
}
$this->db->query("DELETE FROM enemy_alliance WHERE player=$id");
$this->db->query("DELETE FROM enemy_player WHERE player=$id");
// Remove player from the rankings
$types = array('general','financial','military','civ','round','idr');
foreach ($types as $t) {
$rt = $this->rankings->call('getType', "p_$t");
$this->rankings->call('delete', $rt, $name);
}
// Inform other players
$tm = time();
foreach ($msgs as $pid => $data) {
$this->db->query("INSERT INTO message(player,sent_on,mtype,ftype,is_new) VALUES($pid,$tm,'leave','INT',TRUE)");
$q = $this->db->query("SELECT id FROM message WHERE player=$pid AND sent_on=$tm AND mtype='leave' ORDER BY id DESC LIMIT 1");
list($mid) = dbFetchArray($q);
$this->db->query("INSERT INTO msg_leave VALUES($mid,$id,'$reason'," . join(',', $data) . ")");
}
$this->db->query("UPDATE player SET quit=UNIX_TIMESTAMP(NOW()) - 86401 WHERE id=$id");
}
}
?>

View file

@ -0,0 +1,23 @@
<?php
class beta5_leaveVacation {
function beta5_leaveVacation($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Called when a player leaves vacation mode
function run($player) {
if ($this->lib->game->params['novacation'] == 1) {
return;
}
$this->db->query("UPDATE planet SET vacation='NO' WHERE owner=$player");
// FIXME: Send messages
}
}
?>

View file

@ -0,0 +1,118 @@
<?php
class beta5_listing {
function beta5_listing($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Generate a listing for the generic Listing JS component
function run($data, $conf, $param, $md5) {
// Extract parameters
$pArray = explode('#', $param);
$page = (int)array_shift($pArray);
$perPage = (int)array_shift($pArray);
$sField = array_shift($pArray);
$sDir = (array_shift($pArray) == 1);
$search = join('#', $pArray);
$pArray = explode('!', $search);
$srMode = (int)array_shift($pArray);
$srText = join('!', $pArray);
// Apply search parameters
if (is_array($conf['searchModes']) && count($conf['searchModes'])
&& $srText != "" && count($data)) {
if (is_null($conf['searchModes'][$srMode])) {
$srMode = 0;
}
$field = $conf['searchModes'][$srMode];
$nRes = array();
foreach ($data as $e) {
if (stristr($e[$field], $srText) === false) {
continue;
}
array_push($nRes, $e);
}
$data = $nRes;
}
// Sort it
if (is_array($conf['sortable']) && count($conf['sortable']) && count($data)) {
$sFields = array_keys($conf['sortable']);
if (!in_array($sField, $sFields)) {
$sField = $sFields[0];
}
$sType = $conf['sortable'][$sField];
$sArr = array();
foreach ($data as $i => $e) {
$v = $e[$sField];
if (is_null($sArr[$v])) {
$sArr[$v] = array();
}
array_push($sArr[$v], $i);
}
$sVals = array_keys($sArr);
sort($sVals, $sType);
if ($sDir)
$sVals = array_reverse($sVals);
$nRes = array();
foreach ($sVals as $v) {
foreach ($sArr[$v] as $i) {
array_push($nRes, $data[$i]);
}
}
$data = $nRes;
}
// Apply paging
if (is_array($conf['perPage']) && count($conf['perPage'])) {
if (!in_array($perPage, $conf['perPage'])) {
$perPage = $conf['perPage'][0];
}
$c = count($data);
$m = $c % $perPage;
$nPages = ($c - $m) / $perPage + ($m ? 1 : 0);
if (count($data)) {
if ($page < 0) {
$page = 0;
} else if ($page >= $nPages) {
$page = $nPages - 1;
}
$nRes = array();
$fi = $page * $perPage;
for ($i=$fi;$i<min($c, $fi + $perPage);$i++) {
array_push($nRes, $data[$i]);
}
$data = $nRes;
}
}
// Check MD5 value
$nMD5 = md5(serialize($data));
if ($md5 == $nMD5) {
return "KEEP#$nPages";
}
// Generate output
$rStr = array("$nMD5#$nPages");
foreach ($data as $e) {
$a = array();
foreach ($conf['output'] as $f) {
array_push($a, utf8entities($e[$f]));
}
array_push($rStr, join($a, "#"));
}
return join("\n", $rStr);
}
}
?>

View file

@ -0,0 +1,45 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/library/preJoin.inc
//
// This function lets a player who had registered into the game
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_preJoin {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
public function run($account) {
// Get the planet name from the queue
$q = $this->db->query("SELECT p_name FROM planet_reg_queue WHERE account = $account");
if (!($q && dbCount($q))) {
$this->db->end(false);
return false;
}
list($pName) = dbFetchArray($q);
// Delete the registration entry
$this->db->query("DELETE FROM planet_reg_queue WHERE account = $account");
// Register
if ($this->lib->call('register', $account, $pName, null)) {
$this->db->end(false);
return false;
}
return true;
}
}
?>

View file

@ -0,0 +1,31 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/library/preRegister.inc
//
// This function adds an account to the registration queue.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_preRegister {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
public function run($account, $planet) {
$this->db->query(
"INSERT INTO planet_reg_queue (account, p_name) "
. "VALUES ($account, '" . addslashes($planet) . "')"
);
}
}
?>

View file

@ -0,0 +1,167 @@
<?php
class beta5_register {
function beta5_register($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($uid, $planet, $nick) {
// Player mustn't be playing already
if ($this->lib->call('doesUserPlay', $uid)) {
return 1;
}
// Extract the player's name
$q = $this->db->query("SELECT name FROM account WHERE id=$uid");
list($pn) = dbFetchArray($q);
if (is_null($nick)) {
$nick = $pn;
}
$n = addslashes($nick);
// Nickname must be unique
$q = $this->db->query("SELECT name FROM account WHERE (LOWER(name)=LOWER('$n') AND id <> $uid)");
if (!$q || dbCount($q)) {
return 2;
}
$q = $this->db->query("SELECT name FROM player WHERE LOWER(name)=LOWER('$n')");
if (!$q || dbCount($q) || ($nick == $pn && $this->lib->call('hasPlayed', $uid))) {
return 2;
}
// Planet name must be unique
$planetLib = $this->lib->game->getLib('beta5/planet');
if ($planetLib->call('nameExists', $planet)) {
return 3;
}
// Create player record
$ni = ($nick == $pn) ? "null" : "'$n'";
$initCash = $this->lib->game->params['initialcash'];
$cf = $initCash ? ",cash" : "";
$cv = $initCash ? ",$initCash" : "";
$pid = $this->db->query("INSERT INTO player(userid,name$cf) VALUES($uid,$ni$cv)");
if (!$pid) {
return 4;
}
// Initialize rules
$this->db->query("INSERT INTO rule (name,player,value) SELECT name,$pid,value FROM rule_def");
// Initialize rankings
$rLib = $this->lib->game->getLib('main/rankings');
$types = array('general','civ','military','financial','idr');
foreach ($types as $t) {
$rt = $rLib->call('getType', "p_$t");
$rLib->call('append', $rt, $nick);
}
// Handle locked alliances if required
if ((int) $this->lib->game->params['lockalliances'] > 1 && ! ($team = $this->lockedAlliances($pid))) {
return 4;
}
if ((int) $this->lib->game->params['usemap'] == 0) {
// Assign planet random planets for matches and rounds
$pLib = $this->lib->game->getLib('beta5/player');
$plid = $pLib->call('assign', $pid, $planet);
} else {
// Assign planet according to map specifications for CTF games
$ctfLib = $this->lib->game->getLib('beta5/ctf');
$plid = $ctfLib->call('assign', $pid, $planet, $team);
// Send welcoming message to the player
$ctfLib->call('message', $pid, 0, $team);
$ctfLib->call('joinMessage', $team, $pid);
}
if (!$plid) {
return 4;
}
$this->db->query("UPDATE player SET first_planet=$plid WHERE id=$pid");
// Initialize session
$_SESSION[game::sessName()] = array("player" => $pid);
return 0;
}
private function lockedAlliances($player) {
// Lock the table
$this->db->query("LOCK TABLE alliance IN ACCESS EXCLUSIVE MODE");
// Get the amount of alliances in the database
$q = $this->db->query("SELECT COUNT(*) FROM alliance");
if (!($q && dbCount($q))) {
return 0;
}
list($nAlliances) = dbFetchArray($q);
// Check if there are enough
$nWanted = (int) $this->lib->game->params['lockalliances'];
if ($nWanted > $nAlliances) {
// No, we need to create one
$rv = $this->createDefaultAlliance($player);
} else {
// Yes, have the player join the smallest alliance
$rv = $this->joinDefaultAlliance($player);
}
return $rv;
}
private function createDefaultAlliance($player) {
// Fetch default alliance records that haven't been used yet
$q = $this->db->query("SELECT tag,name FROM default_alliance WHERE tag NOT IN ("
. "SELECT tag FROM alliance)");
if (!($q && dbCount($q))) {
return false;
}
$defaults = array();
while ($r = dbFetchHash($q)) {
array_push($defaults, $r);
}
// Choose a random alliance tag from the list
$toCreate = $defaults[rand(0, count($defaults) - 1)];
// Create the alliance
$aLib = $this->lib->game->getLib('beta5/alliance');
$aID = $aLib->call('create', $toCreate['tag'], $toCreate['name'], $player);
if (is_null($aID)) {
return false;
}
// Make the alliance a democracy
$aLib->call('setDemocratic', $aID, true);
return $aID;
}
private function joinDefaultAlliance($player) {
// Get the ID of the smallest alliance
$q = $this->db->query("SELECT alliance,COUNT(*) AS members FROM player "
. "WHERE alliance IS NOT NULL GROUP BY alliance "
. "ORDER BY members,alliance LIMIT 1");
if (!($q && dbCount($q))) {
return false;
}
list($aID, $junk) = dbFetchArray($q);
// Have the player join the alliance
$this->db->query("UPDATE player SET alliance = $aID, a_status = 'IN', a_vote = NULL, a_grade = NULL "
. "WHERE id = $player");
return $aID;
}
}
?>

View file

@ -0,0 +1,33 @@
<?php
class beta5_startVacation {
function beta5_startVacation($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Called when a player enters vacation mode
function run($player) {
if ($this->lib->game->params['novacation'] == 1) {
return;
}
$q = $this->db->query("SELECT id FROM planet WHERE owner=$player");
while ($r = dbFetchArray($q)) {
$qa = $this->db->query("SELECT COUNT(*) FROM fleet WHERE location={$r[0]} AND attacking");
if (!$qa) {
$mode = 'YES';
} else {
list($c) = dbFetchArray($qa);
$mode = $c ? 'PEND' : 'YES';
}
$this->db->query("UPDATE planet SET vacation='$mode' WHERE id={$r[0]}");
}
// FIXME: Send messages
}
}
?>

View file

@ -0,0 +1,243 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/library/updateRankings.inc
//
// This function computes the rankings for a Beta 5 game instance.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_updateRankings {
private $lib;
private $db;
private $game;
private $fleets;
private $planets;
private $players;
private $rankings;
public function __construct($lib) {
$this->lib = $lib;
$this->game = $this->lib->game;
$this->db = $this->game->db;
$this->fleets = $lib->game->getLib('beta5/fleet');
$this->planets = $this->game->getLib('beta5/planet');
$this->players = $this->game->getLib('beta5/player');
$this->rankings = $lib->game->getLib('main/rankings');
}
public function run($dryRun = false) {
// Compute and update player rankings
list($players, $playersById, $genRankings) = $this->computePlayerRankings($dryRun);
// Round and alliance rankings
$this->computeRoundRankings($players, $dryRun);
$this->computeAllianceRankings($genRankings, $dryRun);
return array($players, $playersById);
}
private function computePlayerRankings($dryRun) {
// Compute player rankings
$this->at = time();
$q = $this->db->query("SELECT id FROM player "
. "WHERE (quit IS NULL OR UNIX_TIMESTAMP(NOW()) - quit < 86400) AND NOT hidden");
$genR = $civR = $milR = $finR = array();
$genRankings = array();
$players = array();
$playersById = array();
while ($r = dbFetchArray($q)) {
// Get player name
$pn = $this->players->call('getName',$r[0]);
$players[$pn] = array(
"player" => $r[0],
"tick_at" => $this->at,
);
$playersById[$r[0]] = $pn;
// Get planet data
$q2 = $this->db->query(
"SELECT SUM(pop), SUM(ifact), SUM(mfact), SUM(turrets), "
. "AVG(happiness), SUM(beacon), AVG(corruption) "
. "FROM planet WHERE owner={$r[0]}"
);
$pld = dbFetchArray($q2);
// Get fleet data
$q2 = $this->db->query(
"SELECT SUM(gaships), SUM(fighters), SUM(cruisers), SUM(bcruisers) "
. "FROM fleet WHERE owner={$r[0]}");
$fld = dbFetchArray($q2);
$players[$pn]['gaships'] = (int) $fld[0];
$players[$pn]['fighters'] = (int) $fld[1];
$players[$pn]['cruisers'] = (int) $fld[2];
$players[$pn]['bcruisers'] = (int) $fld[3];
// Financial ranking
$q2 = $this->db->query("SELECT cash FROM player WHERE id={$r[0]}");
list($cash) = dbFetchArray($q2);
$players[$pn]['cash'] = $cash;
$q2 = $this->db->query(
"SELECT pop, ifact, mfact, turrets, corruption, happiness "
. "FROM planet WHERE owner={$r[0]}"
);
$income = 0;
while ($r2 = dbFetchArray($q2)) {
$ir = $this->planets->call('getIncome', $r[0], $r2[0], $r2[5],
$r2[1], $r2[2], $r2[3], $r2[4]);
$income += $ir[0];
}
$upkeep = $this->fleets->call('getUpkeep', $r[0], $fld[0], $fld[1], $fld[2], $fld[3]);
$profit = max(0, $income[0] - $upkeep);
$fr = round($cash / 2000);
$fr += round($profit / 2) + round($income / 1.5);
$fr += round($pld[1] * 201);
if (!is_array($finR[$fr])) {
$finR[$fr] = array();
}
array_push($finR[$fr], $pn);
$players[$pn]['f_rank'] = $fr;
// Military ranking
$fpower = $this->fleets->call('getPower', $r[0], $fld[0], $fld[1], $fld[2], $fld[3]);
$players[$pn]['fleet'] = $fpower;
$mr = $pld[3] * 8 + $fpower * 7 + $pld[2] * 40 + $pld[5] * 100;
if (!is_array($milR[$mr])) {
$milR[$mr] = array();
}
array_push($milR[$mr], $pn);
$players[$pn]['m_rank'] = $mr;
// Civilian ranking
$q2 = $this->db->query("SELECT SUM(points) FROM research_player WHERE player={$r[0]}");
list($rpoints) = dbFetchArray($q2);
$players[$pn]['tech_points'] = $rpoints;
$cr = round(pow(1 + $pld[4] / 100, 4) * $pld[0] * 3);
$cr -= round($cr * $pld[6] / 48000);
$cr += round(sqrt($rpoints*1000) * 3);
if (!is_array($civR[$cr])) {
$civR[$cr] = array();
}
array_push($civR[$cr], $pn);
$players[$pn]['c_rank'] = $cr;
// General ranking
$gr = $cr + $mr + $fr;
if (!is_array($genR[$gr])) {
$genR[$gr] = array();
}
array_push($genR[$gr], $pn);
$genRankings[$r[0]] = $gr;
$players[$pn]['g_rank'] = $gr;
}
// Update player rankings if this is not a dry run
if (! $dryRun) {
$rankings = array($finR, $milR, $civR, $genR);
$rTypes = array('p_financial', 'p_military', 'p_civ', 'p_general');
for ($i = 0; $i < count($rTypes); $i ++) {
$rt = $this->rankings->call('getType', $rTypes[$i]);
$this->rankings->call('update', $rt, $rankings[$i]);
}
}
return array($players, $playersById, $genRankings);
}
private function computeRoundRankings(&$players, $dryRun) {
// Get the general top 15
$rt = $this->rankings->call('getType', 'p_general');
$rr = $this->rankings->call('getAll', $rt, 15);
$top15 = array();
foreach ($rr as $r) {
$top15[$r['id']] = array($r['ranking'], $r['points']);
}
// Generate the new values
$rt = $this->rankings->call('getType', 'p_round');
$o = $this->rankings->call('getAll', $rt);
$round = array();
foreach ($o as $r) {
$round[$r['id']] = $r['points'];
}
foreach ($top15 as $n => $r) {
$round[$n] += round((16-$r[0]) * $r[1] / 24000);
}
// Update the rankings
$rndR = array();
foreach ($round as $n => $p) {
if (!is_array($rndR[$p])) {
$rndR[$p] = array();
}
array_push($rndR[$p], $n);
$players[$n]['o_rank'] = $p;
}
if (! $dryRun) {
$this->rankings->call('update', $rt, $rndR);
}
}
private function computeAllianceRankings(&$genRankings, $dryRun) {
// Sum up the values
$q = $this->db->query(
"SELECT p.id, a.tag FROM player p,alliance a "
. "WHERE p.alliance=a.id AND p.a_status='IN' "
. "AND (p.quit IS NULL OR UNIX_TIMESTAMP(NOW()) - p.quit < 86400)"
);
$alliances = array();
while ($r = dbFetchArray($q)) {
if (is_null($alliances[$r[1]])) {
$alliances[$r[1]] = $genRankings[$r[0]];
} else {
$alliances[$r[1]] += $genRankings[$r[0]];
}
}
// If there are victory conditions, add that to the alliances' score
if ($this->lib->game->params['victory'] == 1) {
$q = $this->db->query("SELECT id,tag FROM alliance");
$aLib = $this->lib->game->getLib('beta5/alliance');
while ($r = dbFetchArray($q)) {
$vVal = (100 + $aLib->call('updateVictory', $r[0])) / 100;
$alliances[$r[1]] = round($alliances[$r[1]] * $vVal);
}
} elseif ($this->lib->game->params['victory'] == 2) {
$q = $this->db->query("SELECT id,tag FROM alliance");
$aLib = $this->lib->game->getLib('beta5/ctf');
while ($r = dbFetchArray($q)) {
$vVal = (100 + $aLib->call('getTeamPoints', $r[0])) / 100;
$alliances[$r[1]] = round($alliances[$r[1]] * $vVal);
}
}
// Update the rankings
$allR = array();
foreach ($alliances as $n => $p) {
if (!is_array($allR[$p])) {
$allR[$p] = array();
}
array_push($allR[$p], $n);
}
if (! $dryRun) {
$rt = $this->rankings->call('getType', 'a_general');
$this->rankings->call('update', $rt, $allR);
}
}
}
?>