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,349 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/prot/library/checkSystem.inc
//
// This function checks a protected system's status and updates the
// Peacekeepers status tables.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_prot_checkSystem {
private $checkedSystems = array();
private $peacekeepersID = null;
private $systemID = null;
private $ownerID = null;
private $fleetOwners = null;
private $pkRecords = null;
private $pkOffenders = null;
private $pkEnemies = null;
private $deleteRecords = null;
private $actions = null;
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
public function run($planetID) {
// Get the system's ID
$systemID = $this->getSystem($planetID);
if ($systemID == -1) {
return;
}
$this->systemID = $systemID;
l::trace("Checking protection status in system #$systemID");
$this->now = time();
// Get the protected player's ID
$this->fetchProtectedPlayerID();
if (is_null($this->ownerID)) {
l::error("No owner found in system #$systemID!");
$this->db->query("UPDATE system SET prot = 0 WHERE id = $systemID");
return;
}
// Get the ID of the Peacekeepers
$this->fetchPeacekeepersID();
// Get the list of all players who have fleets in the system
$this->fetchFleetOwners();
// Get the Peacekeeper's records for the system, as well
// as fleet owners status (offenses and enemy status)
$this->fetchPKRecords();
$this->fetchPKOffenders();
$this->fetchPKEnemies();
// Update status records using the current list of fleets
$this->updateRecords();
}
private function getSystem($planetID) {
$q = $this->db->query(
"SELECT s.id, s.prot FROM planet p "
. "LEFT JOIN system s ON s.id = p.system "
. "WHERE p.id = $planetID"
);
list($systemID, $protection) = dbFetchArray($q);
if ($protection == 0 || in_array($systemID, $this->checkedSystems)) {
return -1;
}
array_push($this->checkedSystems, $systemID);
return $systemID;
}
private function fetchProtectedPlayerID() {
$q = $this->db->query(
"SELECT owner FROM planet WHERE system = {$this->systemID} AND owner IS NOT NULL LIMIT 1"
);
list($this->ownerID) = dbFetchArray($q);
$this->ownerAllies = null;
}
private function fetchPeacekeepersID() {
if (is_null($this->peacekeepersID)) {
$this->peacekeepersID = $this->lib->call('getPeacekeepers');
}
}
private function fetchFleetOwners() {
$q = $this->db->query(
"SELECT DISTINCT f.owner, f.attacking FROM fleet f "
. "LEFT JOIN planet p ON p.id = f.location "
. "WHERE p.system = {$this->systemID} "
. "AND f.owner NOT IN ({$this->ownerID}, {$this->peacekeepersID})"
);
$result = array();
while ($r = dbFetchArray($q)) {
$attack = ($r[1] == 't');
if (array_key_exists($r[0], $result)) {
$result[$r[0]] = $result[$r[0]] || $attack;
} else {
$result[$r[0]] = $attack;
}
}
$this->fleetOwners = $result;
}
private function fetchPKRecords() {
$q = $this->db->query(
"SELECT player, status, switch_at FROM pk_sys_status "
. "WHERE system = {$this->systemID} "
. "FOR UPDATE"
);
$result = array();
while ($r = dbFetchArray($q)) {
$result[$r[0]] = array(
"status" => $r[1],
"switch_at" => $r[2]
);
}
$this->pkRecords = $result;
}
private function fetchPKOffenders() {
$this->pkOffenders = array();
if (empty($this->fleetOwners)) {
return;
}
$q = $this->db->query(
"SELECT player, nb_offenses FROM pk_offenses "
. "WHERE player IN (" . join(",", array_keys($this->fleetOwners)) . ") "
. "FOR UPDATE"
);
while ($r = dbFetchArray($q)) {
$this->pkOffenders[$r[0]] = $r[1];
}
}
private function fetchPKEnemies() {
$this->pkEnemies = array();
if (empty($this->fleetOwners)) {
return;
}
$q = $this->db->query(
"SELECT player FROM pk_enemy "
. "WHERE player IN (" . join(",", array_keys($this->fleetOwners)) . ") "
. "AND until > UNIX_TIMESTAMP(NOW()) "
. "FOR UPDATE"
);
while ($r = dbFetchArray($q)) {
array_push($this->pkEnemies, $r[0]);
}
}
private function updateRecords() {
$this->deleteRecords = array();
$this->actions = array(
"enemy" => array(),
"other" => array()
);
// Check all existing records and update them
foreach ($this->pkRecords as $player => $record) {
if (array_key_exists($player, $this->fleetOwners)) {
$this->handleExistingRecord($player, $record);
} else {
array_push($this->deleteRecords, $player);
}
}
// Add records for fleets that weren't here before
foreach ($this->fleetOwners as $player => $attacking) {
if (! array_key_exists($player, $this->pkRecords)) {
$this->addRecord($player, $attacking);
}
}
// Destroy records that are no longer necessary
$this->purgeOldRecords();
// Send the actions we generated to the main library
$this->lib->call('addActions', $this->actions);
}
private function handleExistingRecord($player, $record) {
l::trace(" Updating record for player #$player with status {$record['status']}");
if ($record['status'] == 'E' || $record['status'] == 'O') {
l::trace(" -> already attacking or enemy");
return;
}
if ($record['status'] == 'W' && !$this->fleetOwners[$player]) {
l::trace(" => warned player (switch_at = {$record['switch_at']}, now = {$this->now})");
if ($this->isAlly($player)) {
l::trace(" -> player is now an allied of the owner");
$this->addAction($player, 'A', false);
} elseif ($record['switch_at'] <= $this->now) {
l::trace(" -> player has been around for too long");
$this->addAction($player, 'O', false);
}
return;
}
if ($this->fleetOwners[$player]) {
l::trace(" -> player has switched to attack");
$this->increaseOffense($player, $record['status'] == 'W' ? 1 : 2, 'O', false);
return;
}
l::trace(" -> allied player");
if (! $this->isAlly($player)) {
l::trace(" => player is no longer an ally of the owner");
$this->addAction($player, 'W', false);
}
}
private function addRecord($player, $attacking) {
l::trace(" Adding record for " . ($attacking ? "attacking " : "") . "player #$player");
if (in_array($player, $this->pkEnemies)) {
l::trace(" -> player is an enemy");
$this->addAction($player, 'O', true);
return;
}
if ($attacking) {
l::trace(" -> player is attacking");
$this->increaseOffense($player, 2, 'O', true);
return;
}
if ($this->isAlly($player)) {
l::trace(" -> player is an ally of the owner");
$this->addAction($player, 'A', true);
} else {
l::trace(" -> player must be warned");
$this->increaseOffense($player, 1, 'W', true);
}
}
private function purgeOldRecords() {
if (!empty($this->deleteRecords)) {
$this->db->query(
"DELETE FROM pk_sys_status "
. "WHERE system = {$this->systemID} "
. "AND player IN (" . join(',', $this->deleteRecords) . ")"
);
}
}
private function isAlly($player) {
if (is_null($this->ownerAllies)) {
$this->fetchOwnerAllies();
}
return in_array($player, $this->ownerAllies);
}
private function fetchOwnerAllies() {
$q = $this->db->query(
"SELECT alliance, a_status FROM player WHERE id = {$this->ownerID}"
);
list($alliance, $aStatus) = dbFetchArray($q);
$qString = "SELECT friend AS ally FROM trusted WHERE player = {$this->ownerID}";
if (!is_null($alliance) && $aStatus == 'IN') {
$qString .= " UNION SELECT id AS trusted FROM player "
. "WHERE a_status = 'IN' AND alliance = $alliance";
}
$q = $this->db->query($qString);
$this->ownerAllies = array();
while ($q = dbFetchArray($q)) {
array_push($this->ownerAllies, $r[0]);
}
}
private function increaseOffense($player, $points, $aType, $mustInsert) {
if (is_null($this->pkOffenders[$player])) {
$tot = $this->pkOffenders[$player] = $points;
$this->db->query("INSERT INTO pk_offenses (player, nb_offenses) VALUES ($player, $points)");
} else {
$tot = ($this->pkOffenders[$player] + $points);
$this->db->query(
"UPDATE pk_offenses SET nb_offenses = nb_offenses + $points WHERE player = $player"
);
}
if ($tot > 6) {
l::trace(" => PLAYER #$player HAS BEEN DECLARED AN ENEMY");
$this->declareEnemy($player);
} else {
$this->addAction($player, $aType, $mustInsert);
}
return ($tot > 6);
}
private function addAction($player, $aType, $mustInsert) {
array_push($this->actions['other'], array(
'player' => $player,
'system' => $this->systemID,
'type' => $aType,
'mustInsert' => $mustInsert
));
}
private function declareEnemy($player) {
if (!in_array($player, $this->actions['enemy'])) {
array_push($this->actions['enemy'], $player);
}
}
}
?>

View file

@ -0,0 +1,32 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/prot/library/isPlayerMarked.inc
//
// This function checks whether a player is marked for destruction by
// the Peacekeepers on a specific planet.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_prot_isPlayerMarked {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
public function run($playerID, $planetID) {
$q = $this->db->query(
"SELECT p.id FROM planet p, pk_sys_status s "
. "WHERE p.id = $planetID AND s.system = p.system "
. "AND s.status = 'O' AND s.player = $playerID"
);
return dbCount($q) == 1;
}
}
?>

View file

@ -0,0 +1,299 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/prot/library/updateFleets.inc
//
// This function checks all protected planets on which a player is
// marked as attacking to make sure he gets destroyed. In addition,
// it checks planets on which the Peacekeepers have fleets and on which
// they are no longer required.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_prot_updateFleets {
private static $fleetName = "Peacekeeper Commando";
private $peacekeepers = null;
public function __construct($lib) {
$this->lib = $lib;
$this->game = $this->lib->game;
$this->db = $this->game->db;
$this->fleets = $this->game->getLib('beta5/fleet');
$this->msgs = $this->game->getLib('beta5/msg');
}
public function run() {
// Get Peacekeepers player ID
$this->peacekeepers = $this->lib->call('getPeacekeepers');
// Check for systems with enemy fleets
$rPlanets = $this->checkBattleSystems();
// Check for fleets to evacuate
$ePlanets = $this->checkEvacuations($rPlanets);
}
private function checkBattleSystems() {
$reinforce = array();
$enemyFleets = $this->getEnemyFleets();
if (empty($enemyFleets)) {
return $reinforce;
}
$sysOwners = $this->getSystemOwners(array_keys($enemyFleets));
foreach ($enemyFleets as $systemID => $planets) {
$ownerID = $sysOwners[$systemID]['owner'];
$ownPlanets = $sysOwners[$systemID]['planets'];
foreach ($planets as $planetID => $eFleets) {
// Check enemy fleet size
$q = $this->db->query(
"SELECT SUM(fighters + gaships / 2), SUM(cruisers), SUM(bcruisers) "
. "FROM fleet "
. "WHERE id IN (" . join(',', $eFleets) . ")"
);
list($eFighters, $eCruisers, $eBattleCruisers) = dbFetchArray($q);
// Check Peacekeeper fleet size
$q = $this->db->query(
"SELECT SUM(fighters), SUM(cruisers), SUM(bcruisers) FROM fleet "
. "WHERE location = $planetID AND owner = {$this->peacekeepers}"
);
list($pkFighters, $pkCruisers, $pkBattleCruisers) = dbFetchArray($q);
// Compute fleet size requirements for Peacekeepers
$rFighters = ceil($eFighters * 2 - (int) $pkFighters);
$rCruisers = $eCruisers * 2 - (int) $pkCruisers;
$rBattleCruisers = $eBattleCruisers * 2 - (int) $pkBattleCruisers;
// Do we need to reinforce ?
if ($rFighters > 0 || $rCruisers > 0 || $rBattleCruisers > 0) {
// If we need fighters, do we have enough haul space?
$haul = $rCruisers * 20 + $rBattleCruisers * 15;
if ($haul * 0.9 < $rFighters) {
// Add cruisers to match haul size
$rCruisers += ceil(($rFighters - ($haul * 0.9)) / 20);
}
$reinforce[$planetID] = array(true, $rFighters, $rCruisers, $rBattleCruisers);
} else {
$reinforce[$planetID] = array(false);
}
// Make sure all enemy fleets are attacking and battle-ready
$this->db->query(
"UPDATE fleet SET attacking = 't', time_spent = (CASE "
. "WHEN time_spent <= 15 THEN 16 "
. "ELSE time_spent "
. "END) WHERE id IN (" . join(',', $eFleets) . ")"
);
// If we're on a planet not owned by the system's owner,
// make sure his fleets are defending
if (!in_array($planetID, $ownPlanets)) {
$this->db->query(
"UPDATE fleet SET attacking = 'f' "
. "WHERE location = $planetID AND owner = $ownerID"
);
}
}
}
foreach ($reinforce as $planetID => $reinforcements) {
if (!$reinforcements[0]) {
continue;
}
array_shift($reinforcements);
$this->reinforce($planetID, $reinforcements);
}
return array_keys($reinforce);
}
private function getEnemyFleets() {
$q = $this->db->query(
"SELECT f.id, p.id, p.system FROM fleet f, planet p, pk_sys_status s "
. "WHERE s.status = 'O' AND p.system = s.system "
. "AND f.owner = s.player AND f.location = p.id "
. "ORDER BY p.id, f.owner "
. "FOR UPDATE OF f, p"
);
$result = array();
while ($r = dbFetchArray($q)) {
if (!is_array($result[$r[2]])) {
$result[$r[2]] = array();
}
if (!is_array($result[$r[2]][$r[1]])) {
$result[$r[2]][$r[1]] = array();
}
array_push($result[$r[2]][$r[1]], $r[0]);
}
return $result;
}
private function getSystemOwners($systems) {
$q = $this->db->query(
"SELECT system, id, owner FROM planet "
. "WHERE system IN (" . join(',', $systems) . ") "
. "ORDER BY system, (owner IS NOT NULL) DESC"
);
$result = array();
while ($r = dbFetchArray($q)) {
if (!is_array($result[$r[0]])) {
$result[$r[0]] = array(
'owner' => $r[2],
'planets' => array($r[1])
);
} elseif (!is_null($r[2])) {
array_push($result[$r[0]]['planets'], $r[1]);
}
}
return $result;
}
private function reinforce($planetID, $fSize) {
// Generate fleet
$name = addslashes(self::$fleetName);
$this->db->query(
"INSERT INTO fleet (location, owner, fighters, cruisers, bcruisers, time_spent, name) "
. "VALUES ($planetID, {$this->peacekeepers}, {$fSize[0]}, {$fSize[1]}, "
. "{$fSize[2]}, 16, '$name')"
);
$fPower = $this->fleets->call('getPower', $this->peacekeepers, 0, $fSize[0], $fSize[1], $fSize[2]);
// Get planet name
$q = $this->db->query("SELECT name FROM planet WHERE id = $planetID");
list($pName) = dbFetchArray($q);
// Get random origin (nebula or planetary remains)
$q = $this->db->query(
"SELECT id, name FROM planet WHERE status > 0 ORDER BY RANDOM() LIMIT 1"
);
list($originID, $origin) = dbFetchArray($q);
$origin = addslashes($origin);
// Get list of players and status
$q = $this->db->query(
"SELECT owner AS player, FALSE AS attacking FROM planet "
. "WHERE id = $planetID AND owner IS NOT NULL "
. "UNION SELECT DISTINCT owner AS player, attacking FROM fleet "
. "WHERE location = $planetID AND owner <> {$this->peacekeepers}"
);
// Send messages
while ($r = dbFetchArray($q)) {
$mid = $this->msgs->call('send', $r[0], 'flmove', array(
'p_id' => $planetID,
'p_name' => $pName
));
$this->db->query(
"INSERT INTO flmove_data VALUES ($mid, '$name', {$this->peacekeepers}, 0, "
. "{$fSize[0]}, {$fSize[1]}, {$fSize[2]}, $fPower, '{$r[1]}', 't', "
. "$originID, '$origin')"
);
}
}
private function checkEvacuations($noCheck) {
if (empty($noCheck)) {
$ncQuery = "";
} else {
$ncQuery = "AND location NOT IN (" . join(',', $noCheck) . ")";
}
// Get PK fleets on planets where no battle is taking place
$q = $this->db->query(
"SELECT id FROM fleet "
. "WHERE owner = {$this->peacekeepers} AND location IS NOT NULL $ncQuery "
. "FOR UPDATE"
);
if (dbCount($q) == 0) {
return;
}
// Merge PK fleets
$fleets = array();
while ($r = dbFetchArray($q)) {
array_push($fleets, $r[0]);
}
$fleets = $this->fleets->call('merge', $fleets, array($this->peacekeepers), "");
// Remove extra fighters and delete empty fleets if required
$this->db->query(
"UPDATE fleet SET fighters = (CASE "
. "WHEN fighters < cruisers * 20 + bcruisers * 10 THEN fighters "
. "ELSE cruisers * 20 + bcruisers * 10 "
. "END) WHERE id IN (" . join(',', $fleets) . ")"
);
$this->db->query(
"DELETE FROM fleet WHERE cruisers = 0 AND bcruisers = 0 AND id IN (" . join(',', $fleets) . ")"
);
// Fetch all remaining fleets and evacuate them
$q = $this->db->query(
"SELECT p.id, p.name, f.id, f.fighters, f.cruisers, f.bcruisers FROM fleet f, planet p "
. "WHERE p.id = f.location AND f.id IN (" . join(',', $fleets) . ")"
);
$locations = array();
$fleets = array();
while ($r = dbFetchArray($q)) {
$this->evacuate($r);
array_push($locations, $r[0]);
array_push($fleets, $r[2]);
}
// Delete fleets
if (! empty($fleets)) {
$this->db->query("DELETE FROM fleet WHERE id IN (" . join(',', $fleets) . ")");
}
return $locations;
}
private function evacuate($fleetRecord) {
list($planetID, $planet, $fleet, $fighters, $cruisers, $bCruisers) = $fleetRecord;
l::trace("Peacekeeper fleet #$fleet leaving planet $planet (#$planetID)");
$planet = addslashes($planet);
$fName = addslashes(self::$fleetName);
$fPower = $this->fleets->call('getPower', $this->peacekeepers, 0, $fighters, $cruisers, $bCruisers);
// Get list of players and status
$q = $this->db->query(
"SELECT owner AS player, FALSE AS attacking FROM planet "
. "WHERE id = $planetID AND owner IS NOT NULL "
. "UNION SELECT DISTINCT owner AS player, attacking FROM fleet "
. "WHERE location = $planetID AND owner <> {$this->peacekeepers}"
);
// Send messages
while ($r = dbFetchArray($q)) {
$mid = $this->msgs->call('send', $r[0], 'flmove', array(
'p_id' => $planetID,
'p_name' => $planet
));
$this->db->query(
"INSERT INTO flmove_data VALUES ($mid, '$fName', {$this->peacekeepers}, 0, "
. "$fighters, $cruisers, $bCruisers, $fPower, '{$r[1]}', 'f', NULL, NULL)"
);
}
}
}
?>