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,63 @@
<?php
class beta5_tech_acceptOffer {
function beta5_tech_acceptOffer($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->players = $this->lib->game->getLib('beta5/player');
}
// Accepts a research offer
function run($pid, $oid) {
$q = $this->db->query("SELECT * FROM research_assistance WHERE offer_to=$pid AND id=$oid AND (UNIX_TIMESTAMP(NOW())-moment<=86400)");
if (!($q && dbCount($q)==1)) {
return 4;
}
$r = dbFetchHash($q);
if (!is_null($r['accepted'])) {
return $r['accepted'] == 't' ? 2 : 3;
}
if ($r['price'] > 0) {
$pinf = $this->players->call('get', $pid);
if ($r['price'] > $pinf['cash']) {
return 1;
}
}
$this->db->query("UPDATE research_assistance SET accepted=TRUE WHERE id=$oid");
if (is_null($r['technology'])) {
$this->db->query("UPDATE player SET res_assistance=" . $r['player'] . " WHERE id=$pid");
} else {
$tid = $r['technology'];
if (!$this->lib->call('checkDependencies', $pid, $tid)) {
return 5;
}
$q = $this->db->query("SELECT points FROM research WHERE id=$tid");
list($points) = dbFetchArray($q);
$points = ceil(75 * $points / 100);
$q = $this->db->query("SELECT * FROM research_player WHERE player=$pid AND research=$tid");
if (!dbCount($q)) {
$this->db->query("INSERT INTO research_player VALUES($pid,$tid,TRUE,0,$points," . $r['player'] . ")");
} else {
$this->db->query("UPDATE research_player SET possible=TRUE,points=$points,given_by=".$r['player']." WHERE player=$pid AND research=$tid");
}
}
if ($r['price'] > 0) {
$this->db->query("UPDATE player SET cash=cash-" . $r["price"] . " WHERE id=$pid");
$this->db->query("UPDATE player SET cash=cash+" . $r["price"] . " WHERE id=".$r['player']);
}
$tm = time();
$this->db->query("INSERT INTO message(player,sent_on,mtype,ftype,is_new) VALUES(".$r['player'].",$tm,'resdipl','INT',TRUE)");
$q = $this->db->query("SELECT id FROM message WHERE player=".$r['player']." AND sent_on=$tm AND ftype='INT'");
list($mid) = dbFetchArray($q);
$this->db->query("INSERT INTO msg_resdipl VALUES($mid,$oid,1)");
return 0;
}
}
?>

View file

@ -0,0 +1,26 @@
<?php
class beta5_tech_checkDependencies {
function beta5_tech_checkDependencies($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Checks whether a player has already researched a technology's dependencies
function run($pid, $rid) {
$q = $this->db->query("SELECT depends_on FROM research_dep WHERE research=$rid");
if (!dbCount($q)) {
return true;
}
$rv = true;
while ($rv && $r = dbFetchArray($q)) {
$rv = $rv && $this->lib->call('has', $pid, $r[0]);
}
return $rv;
}
}
?>

View file

@ -0,0 +1,47 @@
<?php
class beta5_tech_createTree {
function beta5_tech_createTree($lib) {
$this->lib = $lib;
$this->db = $lib->game->db;
}
function run($player) {
list($res, $rLeaf) = $this->lib->call('getTree');
$pRes = array();
foreach ($res as $r) {
if ($r['optional'] == 0) {
array_push($pRes, $r['id']);
}
}
while (count($pRes) < 2 * count($res) / 5) {
$tried = array();
do {
do {
$rId = $rLeaf[rand(0, count($rLeaf) - 1)];
} while (in_array($rId, $pRes) || in_array($rId, $tried));
$npRes = $pRes;
array_push($tried, $rId);
array_push($npRes, $rId);
$mDeps = $this->lib->mainClass->getAllDependencies($res,$rId);
foreach ($mDeps as $d) {
if (!in_array($d, $npRes)) {
array_push($npRes, $d);
}
}
} while (count($npRes) > 3 * count($res) / 5);
$pRes = $npRes;
}
foreach ($pRes as $rid) {
$this->db->query("INSERT INTO research_player(player,research,possible) VALUES($player,$rid,TRUE)");
}
}
}
?>

View file

@ -0,0 +1,34 @@
<?php
class beta5_tech_declineOffer {
function beta5_tech_declineOffer($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Declines a research offer
function run($pid, $oid) {
$q = $this->db->query("SELECT * FROM research_assistance WHERE offer_to=$pid AND id=$oid AND (UNIX_TIMESTAMP(NOW())-moment<=86400)");
if (!($q && dbCount($q)==1)) {
return 4;
}
$r = dbFetchHash($q);
if (!is_null($r['accepted'])) {
return $r['accepted'] == 't' ? 2 : 3;
}
$this->db->query("UPDATE research_assistance SET accepted=FALSE WHERE id=$oid");
$tm = time();
$this->db->query("INSERT INTO message(player,sent_on,mtype,ftype,is_new) VALUES(".$r['player'].",$tm,'resdipl','INT',TRUE)");
$q = $this->db->query("SELECT id FROM message WHERE player=".$r['player']." AND sent_on=$tm AND ftype='INT'");
list($mid) = dbFetchArray($q);
$this->db->query("INSERT INTO msg_resdipl VALUES($mid,$oid,2)");
return 0;
}
}
?>

View file

@ -0,0 +1,82 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/tech/library/getAvailableTechs.inc
//
// This function checks whether a player's tech trading requests are ok.
// If they are not, it either deletes or re-arranges them.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_tech_getAvailableTechs {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
public function run($player) {
// Get all techs the player has implemented
$q = $this->db->query(
"SELECT r.id FROM research r, research_player p "
. "WHERE r.id = p.research AND p.points = r.points AND p.player = $player "
. "AND (p.in_effect <> 0 OR r.is_law)"
);
$gotTechs = array();
while ($r = dbFetchArray($q)) {
array_push($gotTechs, $r[0]);
}
// If we don't have techs, return
if (empty($gotTechs)) {
return $gotTechs;
}
// Get all techs the player sees
$q = $this->db->query(
"SELECT r.id FROM research r, research_player p "
. "WHERE r.id = p.research AND p.points >= 75 * r.points / 100 AND p.player = $player"
);
$seeTechs = array();
while ($r = dbFetchArray($q)) {
array_push($seeTechs, $r[0]);
}
// Get the amount of dependencies for each tech not in the list
$q = $this->db->query(
"SELECT research,COUNT(*) FROM research_dep "
. "WHERE research NOT IN (" . join(',',$seeTechs) . ") "
. "GROUP BY research"
);
if (!dbCount($q)) {
return array();
}
$depCount = array();
while ($r = dbFetchArray($q)) {
$depCount[$r[0]] = $r[1];
}
// Now get all techs that depend on one of these techs
$q = $this->db->query(
"SELECT research,COUNT(*) FROM research_dep "
. "WHERE research NOT IN (" . join(',',$seeTechs) . ") "
. "AND depends_on IN (" . join(',',$gotTechs) . ")"
. "GROUP BY research"
);
$okTechs = array();
while ($r = dbFetchArray($q)) {
if ($depCount[$r[0]] == $r[1]) {
array_push($okTechs, $r[0]);
}
}
return $okTechs;
}
}
?>

View file

@ -0,0 +1,23 @@
<?php
class beta5_tech_getBudget {
function beta5_tech_getBudget($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Returns an array containing research budget allocations
function run($pid) {
$q = $this->db->query("SELECT research FROM player WHERE id = $pid");
if (!($q && dbCount($q))) {
return array();
}
list($b) = dbFetchArray($q);
return split('!', $b);
}
}
?>

View file

@ -0,0 +1,26 @@
<?php
class beta5_tech_getLaws {
function beta5_tech_getLaws($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Returns the list of laws available to a player, as well as their status
function run($pl) {
$q = $this->db->query(
"SELECT r.id, p.in_effect FROM research r, research_player p "
. "WHERE r.id = p.research AND p.player = $pl AND r.points = p.points AND r.is_law"
);
$a = array();
while ($r = dbFetchArray($q)) {
array_push($a, $r[0]);
array_push($a, $r[1]);
}
return $a;
}
}
?>

View file

@ -0,0 +1,51 @@
<?php
class beta5_tech_getOffers {
function beta5_tech_getOffers($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->players = $this->lib->game->getLib('beta5/player');
}
// Get a list of scientific assistance offers sent and received by a player
function run($pid) {
$pi = $this->players->call('get', $pid);
$interval = $this->lib->game->ticks['day']->interval - 2 * $this->lib->game->ticks['hour']->interval;
$q = $this->db->query("SELECT id FROM research_assistance WHERE offer_to=$pid AND accepted AND (UNIX_TIMESTAMP(NOW()) - moment) < $interval");
$accToday = (dbCount($q) > 0);
$q = $this->db->query("SELECT * FROM research_assistance WHERE player=$pid OR offer_to=$pid ORDER BY moment DESC");
$ol = array();
$t = time();
while ($r = dbFetchHash($q)) {
$o = array();
$o['id'] = $r["id"];
$o['type'] = ($pid == $r['player']) ? 'S' : 'R';
$o['time'] = $r['moment'];
$o['tech'] = $r['technology'];
$o['price'] = $r['price'];
$o['status'] = is_null($r['accepted']) ? ($t - $r['moment'] > $interval ? 'E' : 'P') : ($r['accepted'] == 't' ? 'A' : 'R');
if ($o['status'] == 'P' && $o['type'] == 'R') {
if ($o['price'] > $pi['cash']) {
$o['pending'] = 1;
} elseif ($accToday) {
$o['pending'] = 2;
} elseif ($o['tech'] != "" && $this->lib->call('has', $pid, $o['tech'], true)) {
$o['pending'] = 3;
} elseif ($o['tech'] != "" && !$this->lib->call('checkDependencies', $pid, $o['tech'])) {
$o['pending'] = 4;
} else {
$o['pending'] = 0;
}
}
$pinf = $this->players->call('getName', ($pid == $r['player']) ? $r['offer_to'] : $r['player']);
$o['player'] = $pinf;
array_push($ol, $o);
}
return $ol;
}
}
?>

View file

@ -0,0 +1,25 @@
<?php
class beta5_tech_getPoints {
function beta5_tech_getPoints($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->rules = $this->lib->game->getLib('beta5/rules');
}
// Returns the amount of research points a player gains daily
function run($pid) {
$q = $this->db->query("SELECT SUM(pop) FROM planet WHERE owner = $pid");
if (!($q && dbCount($q))) {
return 0;
}
list($p) = dbFetchArray($q);
$r = $this->rules->call('get', $pid);
return floor($r['research_percent'] * $p / 100);
}
}
?>

View file

@ -0,0 +1,27 @@
<?php
class beta5_tech_getTopicData {
function beta5_tech_getTopicData($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Returns data about a research topic: identifier, title, cost, description
function run($lang, $id) {
$qs = "SELECT r.cost,d.name,d.description,r.category FROM research r,research_txt d ";
$qs .= "WHERE r.id=$id AND d.research=r.id AND d.lang='$lang'";
$q = $this->db->query($qs);
if (!($q && dbCount($q) == 1)) {
return "";
}
$r = dbFetchArray($q);
$str = "$id\n" . utf8entities($r[1]) . "\n" . $r[0] . "#" . $r[3] . "\n";
$str .= preg_replace('/\n/', '<br/>', utf8entities($r[2]));
return $str;
}
}
?>

View file

@ -0,0 +1,36 @@
<?php
class beta5_tech_getTopics {
function beta5_tech_getTopics($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Returns the list of research topics for a player; the 'status argument determines
// the type of list to return: 1 for implemented, 0 for completed, and -1 for "almost
// completed" (more than 75%)
function run($pid, $type) {
$qs = "SELECT r.id, p.points, r.points FROM research_player p, research r ";
$qs .= "WHERE p.player = $pid AND r.id = p.research AND ";
if ($type == 1) {
$qs .= "NOT r.is_law AND p.in_effect <> 0";
} elseif ($type == 0) {
$qs .= "NOT r.is_law AND p.in_effect = 0 AND r.points = p.points";
} elseif ($type == -1) {
$qs .= "p.points < r.points AND p.points >= (75 * r.points / 100)";
}
$a = array();
$q = $this->db->query($qs);
while ($r = dbFetchArray($q)) {
array_push($a, $r[0]);
if ($type == -1)
array_push($a, floor(100 * $r[1] / $r[2]));
}
return $a;
}
}
?>

View file

@ -0,0 +1,43 @@
<?php
class beta5_tech_getTree {
var $data = null;
function beta5_tech_getTree($lib) {
$this->lib = $lib;
$this->db = $lib->game->db;
}
function run() {
if (is_array($this->data)) {
return $this->data;
}
$res = array();
$noReq = array();
$q = $this->db->query("SELECT * FROM research");
while ($r = dbFetchHash($q)) {
$r['depends_on'] = $r['required_by'] = array();
$q2 = $this->db->query("SELECT * FROM research_dep WHERE research=".$r['id']." OR depends_on=".$r['id']);
while ($r2 = dbFetchArray($q2)) {
$dir = ($r2[0] == $r['id']);
array_push($r[$dir ? 'depends_on' : 'required_by'], $r2[$dir ? 1 : 0]);
}
if (($r['optional'] == 1 && !count($r['required_by'])) || $r['optional'] == 2) {
array_push($noReq, $r['id']);
}
$res[$r['id']] = $r;
}
foreach ($noReq as $nrId) {
$this->lib->mainClass->getAllDependencies($res, $nrId);
}
return ($this->data = array($res,$noReq));
}
}
?>

View file

@ -0,0 +1,22 @@
<?php
class beta5_tech_has {
function beta5_tech_has($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Checks whether a player has researched a technology
function run($pid, $rid, $bt = false) {
$cond = $bt ? "p.points >= (75 * r.points / 100)" : "p.points = r.points";
$qs = "SELECT ($cond) FROM research_player p, research r ";
$qs .= "WHERE p.player = $pid AND r.id=p.research AND r.id=$rid";
$q = $this->db->query($qs);
list($r) = dbFetchArray($q);
return ($r == "t");
}
}
?>

View file

@ -0,0 +1,45 @@
<?php
class beta5_tech_implement {
function beta5_tech_implement($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->rules = $this->lib->game->getLib('beta5/rules');
}
// Implements a technology for a player
function run($pl, $id) {
$q = $this->db->query(
"SELECT r.cost FROM research_player p, research r "
. "WHERE p.player = $pl AND p.research = $id AND r.id = $id "
. "AND p.points = r.points AND NOT r.is_law AND p.in_effect = 0"
);
if (!($q && dbCount($q) == 1)) {
return false;
}
list($cost) = dbFetchArray($q);
$q = $this->db->query("SELECT cash FROM player WHERE id = $pl");
list($cash) = dbFetchArray($q);
if ($cash < $cost) {
return false;
}
$this->db->query("UPDATE player SET cash = cash - $cost WHERE id = $pl");
$this->db->query("UPDATE research_player SET in_effect = 1 WHERE player = $pl AND research = $id");
// Change the player's rules
$q = $this->db->query("SELECT rule,modifier FROM research_effect WHERE research=$id");
$rules = array();
while ($r = dbFetchArray($q)) {
$rules[$r[0]] = $r[1];
}
$this->rules->call('change', $pl, $rules);
return true;
}
}
?>

View file

@ -0,0 +1,29 @@
<?php
class beta5_tech_makeOffer {
function beta5_tech_makeOffer($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->msg = $this->lib->game->getLib('beta5/msg');
}
// Makes a research offer
function run($cpid, $tpid, $tid, $pr) {
$tm = time();
$this->db->query(
"INSERT INTO research_assistance(player,price,offer_to,moment" . (is_null($tid) ? "" : ",technology") . ")"
. " VALUES($cpid,$pr,$tpid,$tm" . (is_null($tid) ? "" : ",$tid") . ")"
);
$q = $this->db->query("SELECT id FROM research_assistance WHERE player=$cpid AND moment=$tm");
list($aid) = dbFetchArray($q);
$this->msg->call('send', $tpid, 'resdipl', array(
'offer' => $aid,
'msg_id' => 0
));
}
}
?>

View file

@ -0,0 +1,45 @@
<?php
class beta5_tech_switchLaw {
function beta5_tech_switchLaw($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->rules = $this->lib->game->getLib('beta5/rules');
}
// Enacts or revoke a law
function run($pl, $id) {
$q = $this->db->query(
"SELECT p.in_effect, r.cost FROM research_player p, research r "
. "WHERE p.player = $pl AND p.research = $id AND r.id = $id "
. "AND p.points = r.points AND r.is_law AND p.in_effect IN (0,1)"
);
if (!($q && dbCount($q) == 1)) {
return false;
}
list($ie, $cost) = dbFetchArray($q);
$q = $this->db->query("SELECT cash FROM player WHERE id = $pl");
list($cash) = dbFetchArray($q);
if ($cash < $cost) {
return false;
}
$this->db->query("UPDATE player SET cash = cash - $cost WHERE id = $pl");
$this->db->query("UPDATE research_player SET in_effect = ".($ie==0?"6":"-5")." WHERE player = $pl AND research = $id");
// Change the player's rules
$q = $this->db->query("SELECT rule,modifier FROM research_effect WHERE research = $id");
$rules = array();
while ($r = dbFetchArray($q)) {
$rules[$r[0]] = ($ie ? -1 : 1) * $r[1];
}
$this->rules->call('change', $pl, $rules);
return true;
}
}
?>