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,51 @@
<?php
class beta5_alliance_acceptRequest {
function beta5_alliance_acceptRequest($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->msgs = $this->lib->game->getLib('beta5/msg');
}
// Accepts a request to join an alliance
function run($aid, $pid, $kid) {
$q = $this->db->query("SELECT id FROM player WHERE alliance=$aid AND a_status='REQ' AND id=$pid");
if (!($q && dbCount($q) == 1)) {
return;
}
$a = $this->lib->call('get', $aid);
if (is_null($a)) {
return;
}
$tag = addslashes($a['tag']);
$tm = time();
$this->msgs->call('send', $pid, 'alint', array(
"msg_type" => 10,
"alliance" => $aid,
"tag" => $a['tag'],
"player" => $pid,
));
$l = $this->lib->call('getKeepers', $aid);
foreach ($l as $id) {
if ($id == $kid) {
continue;
}
$this->msgs->call('send', $id, 'alint', array(
"msg_type" => 11,
"alliance" => $aid,
"tag" => $a['tag'],
"player" => $pid,
));
}
$this->db->query("UPDATE player SET a_status='IN',a_vote=NULL,a_grade=NULL WHERE id=$pid");
}
}
?>

View file

@ -0,0 +1,35 @@
<?php
class beta5_alliance_addCandidate {
function beta5_alliance_addCandidate($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Adds a new candidate for an alliance's presidency
function run($aid,$pid) {
$this->db->query("INSERT INTO alliance_candidate(alliance,candidate) VALUES($aid,$pid)");
$q = $this->db->query("SELECT id FROM alliance_candidate WHERE candidate=$pid");
list($id) = dbFetchArray($q);
$this->db->query("UPDATE player SET a_vote=$id WHERE id=$pid");
$a = $this->lib->call('get', $aid);
$tag = addslashes($a['tag']);
$tm = time();
$vl = $this->lib->call('getVoters', $aid);
foreach ($vl as $id) {
if ($id == $pid) {
continue;
}
$this->db->query("INSERT INTO message(player,sent_on,mtype,ftype,is_new) VALUES($id,$tm,'alint','INT',TRUE)");
$q = $this->db->query("SELECT id FROM message WHERE player=$id AND sent_on=$tm AND ftype='INT' ORDER BY id DESC LIMIT 1");
list($mid) = dbFetchArray($q);
$this->db->query("INSERT INTO msg_alint VALUES($mid,$aid,'$tag',$pid,16)");
}
}
}
?>

View file

@ -0,0 +1,29 @@
<?php
class beta5_alliance_cancelRequest {
function beta5_alliance_cancelRequest($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Cancels a request to join an alliance
function run($p, $a) {
$this->db->query("UPDATE player SET alliance=NULL,a_status=NULL,a_vote=NULL WHERE id=$p");
$ainf = $this->lib->call('get', $a);
$tag = addslashes($ainf['tag']);
$l = $this->lib->call('getKeepers', $a);
$tm = time();
foreach ($l as $id) {
$this->db->query("INSERT INTO message(player,sent_on,mtype,ftype,is_new) VALUES($id,$tm,'alint','INT',TRUE)");
$q = $this->db->query("SELECT id FROM message WHERE player=$id AND sent_on=$tm AND ftype='INT'");
list($mid) = dbFetchArray($q);
$this->db->query("INSERT INTO msg_alint VALUES($mid,$a,'$tag',$p,1)");
}
}
}
?>

View file

@ -0,0 +1,35 @@
<?php
class beta5_alliance_changeRank {
function beta5_alliance_changeRank($lib) {
$this->lib = $lib;
$this->game = $this->lib->game;
$this->db = $this->game->db;
}
// Change an alliance member's rank
function run($pid, $rid) {
$this->db->query("UPDATE player SET a_grade=$rid WHERE alliance IS NOT NULL AND a_status='IN' AND id=$pid");
$q = $this->db->query(
"SELECT r.can_vote,r.can_be_cand,a.leader FROM player p,alliance a,alliance_grade r "
. "WHERE p.id=$pid AND r.alliance=p.alliance AND a.id=p.alliance AND "
. "((p.a_grade IS NOT NULL AND p.a_grade=r.id) OR (p.a_grade IS NULL AND r.id=a.default_grade))"
);
list($cv,$cc,$lid) = dbFetchArray($q);
$lockedAlliances = (int) $this->game->params['lockalliances'];
$lockedAlliances = ($lockedAlliances > 1) ? 1 : 0;
if ($pid != $lid && ! $lockedAlliances) {
if ($cv == 0) {
$this->db->query("UPDATE player SET a_vote=NULL WHERE id=$pid");
}
if ($cc == 0) {
$this->db->query("DELETE FROM alliance_candidate WHERE candidate=$pid");
}
}
// FIXME: message
}
}
?>

View file

@ -0,0 +1,39 @@
<?php
class beta5_alliance_create {
function beta5_alliance_create($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Create an alliance
function run($tag, $name, $founder) {
$t = addslashes($tag);
$this->db->query("INSERT INTO alliance_grade(name) VALUES ('DEFGRADE-$t')");
$q = $this->db->query("SELECT id FROM alliance_grade WHERE name='DEFGRADE-$t' AND alliance IS NULL");
list($gid) = dbFetchArray($q);
$this->db->query(
"INSERT INTO alliance(tag,name,leader,successor,default_grade) VALUES('$t','"
. addslashes($name) . "',$founder,NULL,$gid)"
);
$i = $this->lib->call('getId', $tag);
if (is_null($i)) {
return null;
}
$this->db->query("UPDATE alliance_grade SET name=NULL,alliance=$i WHERE name='DEFGRADE-$t' AND alliance IS NULL");
$this->db->query("UPDATE player SET alliance=$i,a_status='IN',a_vote=NULL WHERE id=$founder");
$rkLib =& $this->lib->game->getLib('main/rankings');
$rt = $rkLib->call('getType', 'a_general');
$rkLib->call('append', $rt, $tag);
return $i;
}
}
?>

View file

@ -0,0 +1,29 @@
<?php
class beta5_alliance_createForum {
function beta5_alliance_createForum($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Creates an alliance forum
function run($aid, $name, $userPost, $after, $description) {
if ($after == -1) {
$ao = -1;
} else {
$q = $this->db->query("SELECT forder FROM af_forum WHERE id=$after");
list($ao) = dbFetchArray($q);
}
$this->db->query("UPDATE af_forum SET forder=forder+1 WHERE forder>$ao AND alliance=$aid");
$ao ++;
$this->db->query(
"INSERT INTO af_forum(alliance,forder,title,description,user_post) VALUES("
. "$aid,$ao,'" . addslashes($name) . "','". addslashes($description) . "',"
. ($userPost?'TRUE':'FALSE') . ")"
);
}
}
?>

View file

@ -0,0 +1,47 @@
<?php
class beta5_alliance_createRank {
function beta5_alliance_createRank($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Creates a new rank within the alliance
function run($aid, $name, $privs, $kick, $change, $fread, $fmod) {
$n = addslashes($name);
$ll = array('NO', 'PY', 'PL', 'PLD');
$lt = array('NO', 'SL', 'SR', 'VL', 'MR');
$this->db->query(
"INSERT INTO alliance_grade(alliance,name,list_access,attacks,can_set_grades,"
. "can_kick,can_accept,forum_admin,dipl_contact,can_vote,can_be_cand,tech_trade)"
. " VALUES($aid,'$n','".$ll[$privs['list_access']]."',".dbBool($privs['attacks'])
. ",".dbBool($privs['can_set_grades']).",".dbBool($privs['can_kick']).","
. dbBool($privs['can_accept']).','.dbBool($privs['forum_admin']).","
. dbBool($privs['dipl_contact']).",".dbBool($privs['can_vote']).","
. dbBool($privs['can_be_cand']).",'".$lt[$privs['tech_trade']] . "')"
);
$q = $this->db->query("SELECT id FROM alliance_grade WHERE alliance=$aid AND name='$n'");
if (!($q && dbCount($q))) {
return;
}
list($rid) = dbFetchArray($q);
foreach ($kick as $id) {
$this->db->query("INSERT INTO algr_kick VALUES($rid,$id)");
}
foreach ($change as $id) {
$this->db->query("INSERT INTO algr_chgrade VALUES($rid,$id)");
}
foreach ($fread as $id) {
$this->db->query("INSERT INTO algr_forums VALUES($rid,$id,FALSE)");
}
foreach ($fmod as $id) {
$this->db->query("INSERT INTO algr_forums VALUES($rid,$id,TRUE)");
}
}
}
?>

View file

@ -0,0 +1,20 @@
<?php
class beta5_alliance_deleteForum {
function beta5_alliance_deleteForum($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Destroys an alliance forum
function run($id) {
$q = $this->db->query("SELECT alliance,forder FROM af_forum WHERE id=$id");
list($aid,$o) = dbFetchArray($q);
$this->db->query("DELETE FROM af_forum WHERE id=$id");
$this->db->query("UPDATE af_forum SET forder=forder-1 WHERE alliance=$aid AND forder>$o");
}
}
?>

View file

@ -0,0 +1,18 @@
<?php
class beta5_alliance_deleteRank {
function beta5_alliance_deleteRank($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Deletes a rank from an alliance's ACL
function run($aid, $rId, $nrId) {
$this->db->query("UPDATE player SET a_grade=$nrId WHERE alliance=$aid AND a_status='IN' AND a_grade=$rId");
$this->db->query("DELETE FROM alliance_grade WHERE alliance=$aid AND id=$rId");
}
}
?>

View file

@ -0,0 +1,30 @@
<?php
class beta5_alliance_get {
function beta5_alliance_get($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Get alliance data
function run($id) {
$q = $this->db->query("SELECT * FROM alliance WHERE id = $id");
if (!($q && dbCount($q) == 1)) {
return null;
}
$r = dbFetchHash($q);
$q = $this->db->query(
"SELECT COUNT(*),ROUND(AVG(s.x)),ROUND(AVG(s.y)) FROM player y,planet p,system s "
. "WHERE y.alliance=$id AND y.a_status='IN' AND p.owner=y.id AND s.id=p.system"
);
list($r['nplanets'],$r['avgx'],$r['avgy']) = dbFetchArray($q);
if (is_null($r['avgx'])) {
$r['avgx'] = $r['avgy'] = 0;
}
return $r;
}
}
?>

View file

@ -0,0 +1,29 @@
<?php
class beta5_alliance_getCandidates {
function beta5_alliance_getCandidates($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Get the list of alliance candidates as well as the number of votes they have
function run($aid) {
$l = array();
$q = $this->db->query("SELECT c.id,c.candidate,COUNT(*) FROM alliance_candidate c,"
. "player p WHERE c.alliance=$aid AND p.a_vote=c.id GROUP BY c.id, c.candidate");
while ($r = dbFetchArray($q)) {
$q2 = $this->db->query("SELECT p.name,a.name FROM player p,account a WHERE p.id={$r[1]} AND p.userid=a.id");
$r2 = dbFetchArray($q2);
$l[$r[0]] = array(
"votes" => $r[2],
"pid" => $r[1],
"name" => is_null($r2[0]) ? $r2[1] : $r2[0]
);
}
return $l;
}
}
?>

View file

@ -0,0 +1,27 @@
<?php
class beta5_alliance_getForums {
function beta5_alliance_getForums($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Reads the list of an alliance's forums
function run($aid) {
$q = $this->db->query("SELECT id,forder,title,description,user_post FROM af_forum WHERE alliance=$aid ORDER BY forder");
$rs = array();
while ($r = dbFetchArray($q)) {
$rs[$r[0]] = array(
"order" => $r[1],
"title" => $r[2],
"description" => $r[3],
"user_post" => ($r[4] == 't')
);
}
return $rs;
}
}
?>

View file

@ -0,0 +1,34 @@
<?php
class beta5_alliance_getForumsComplete {
function beta5_alliance_getForumsComplete($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->players = $this->lib->game->getLib('beta5/player');
}
// Reads the list of an alliance's forums, with details
function run($aid) {
$q = $this->db->query("SELECT * FROM af_forum WHERE alliance=$aid ORDER BY forder");
$a = array();
while ($rs = dbFetchHash($q)) {
if ($rs['last_post'] != "") {
$q2 = $this->db->query("SELECT author,moment FROM af_post WHERE id=".$rs['last_post']);
list($au,$mo) = dbFetchArray($q2);
$au = $this->players->call('getName', $au);
$rs['last'] = array(
"moment" => $mo,
"author" => $au
);
} else {
$rs['last'] = null;
}
array_push($a, $rs);
}
return $a;
}
}
?>

View file

@ -0,0 +1,21 @@
<?php
class beta5_alliance_getId {
function beta5_alliance_getId($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Get alliance identifier by tag
function run($tag) {
$q = $this->db->query("SELECT id FROM alliance WHERE LOWER(tag)='" . addslashes(strtolower($tag)) . "'");
if (!($q && dbCount($q) == 1)) {
return null;
}
list($id) = dbFetchArray($q);
return $id;
}
}
?>

View file

@ -0,0 +1,46 @@
<?php
class beta5_alliance_getKeepers {
function beta5_alliance_getKeepers($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Get a list of IDs for the players that can accept new members into an alliance
function run($a) {
$q = $this->db->query("SELECT leader FROM alliance WHERE id=$a");
list($id) = dbFetchArray($q);
if (!is_null($id)) {
$l = array($id);
} else {
$l = array();
}
$q = $this->db->query("SELECT id FROM alliance_grade WHERE alliance=$a AND can_accept");
if (!$q || dbCount($q) == 0) {
return $l;
}
$gl = array();
while ($r = dbFetchArray($q)) {
list($g) = $r;
array_push($gl, $g);
}
$q = $this->db->query(
"SELECT p.id FROM player p, alliance a WHERE a.id=$a AND p.alliance=a.id AND p.a_status='IN' "
. "AND (p.a_grade IN (" . join(',',$gl) . ") OR (p.a_grade IS NULL AND a.default_grade IN (".join(',',$gl).")))"
);
while ($r = dbFetchArray($q)) {
list($p) = $r;
if (is_null($id) || $p != $id) {
array_push($l, $p);
}
}
return $l;
}
}
?>

View file

@ -0,0 +1,27 @@
<?php
class beta5_alliance_getMembers {
function beta5_alliance_getMembers($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Reads the list of alliance members and their ranks
function run($aid) {
$q = $this->db->query(
"SELECT p.id,p.name,a.name,p.a_grade FROM player p,account a "
. "WHERE p.alliance=$aid AND p.a_status='IN' AND a.id=p.userid");
$rs = array();
while ($r = dbFetchArray($q)) {
$rs[$r[0]] = array(
"name" => is_null($r[1]) ? $r[2] : $r[1],
"rank" => $r[3]
);
}
return $rs;
}
}
?>

View file

@ -0,0 +1,34 @@
<?php
class beta5_alliance_getMilitary {
function beta5_alliance_getMilitary($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Get the list of planets under attack in an alliance
function run($aid) {
$q = $this->db->query(
"SELECT p.id AS id,p.name AS name,l.id AS owner,a.v_players AS pllist,"
. "a.v_friendly AS friendly,a.v_enemy AS enemy,s.x AS x,s.y AS y,p.orbit AS orbit "
. " FROM planet p,attacks a,player l,system s "
. "WHERE l.alliance=$aid AND l.a_status='IN' AND p.owner=l.id AND a.planet=p.id AND s.id=p.system"
);
$rs = array();
while ($r = dbFetchHash($q)) {
$r['f_list'] = $r['e_list'] = array();
if ($r['pllist'] == 't') {
$q2 = $this->db->query("SELECT DISTINCT owner,attacking FROM fleet WHERE location=".$r['id']);
while ($r2=dbFetchArray($q2)) {
array_push($r[($r2[1] == 't' ? "e" : "f").'_list'], $r2[0]);
}
}
$rs[$r['id']] = $r;
}
return $rs;
}
}
?>

View file

@ -0,0 +1,42 @@
<?php
class beta5_alliance_getPlanets {
function beta5_alliance_getPlanets($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->planets = $this->lib->game->getLib('beta5/planet');
$this->players = $this->lib->game->getLib('beta5/player');
}
// Returns the list of planets belonging to players of the alliance
function run($aid) {
$rs = array();
$q = $this->db->query("SELECT p.id,p.name,a.name FROM player p,account a WHERE a.id=p.userid AND p.alliance=$aid AND p.a_status='IN'");
while ($r = dbFetchArray($q)) {
$oid = $r[0]; $oname = ($r[1] == "") ? $r[2] : $r[1];
$ppl = $this->players->call('getPlanets', $oid);
foreach ($ppl as $pid => $pname) {
$pinf = $this->planets->call('byId', $pid);
$rs[$pid] = array(
"name" => $pname,
"ownerId" => $oid,
"owner" => $oname,
"x" => $pinf['x'],
"y" => $pinf['y'],
"orbit" => $pinf['orbit'] + 1,
"fact" => $pinf['ifact'] + $pinf['mfact'],
"turrets" => $pinf['turrets'],
);
$q2 = $this->db->query("SELECT COUNT(*) FROM fleet WHERE location=$pid AND attacking");
list($ua) = dbFetchArray($q2);
$rs[$pid]['attack'] = ($ua != 0);
}
}
return $rs;
}
}
?>

View file

@ -0,0 +1,79 @@
<?php
class beta5_alliance_getPrivileges {
function beta5_alliance_getPrivileges($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->players = $this->lib->game->getLib('beta5/player');
}
// Returns the list of privileges a player has inside an alliance
function run($p) {
$nr = array(
'list_access' => 0,
'tech_trade' => 0,
'attacks' => 0,
'can_set_grades' => 0,
'can_kick' => 0,
'can_accept' => 0,
'forum_admin' => 0,
'dipl_contact' => 0,
'can_vote' => 0,
'can_be_cand' => 0,
'is_leader' => 0
);
$pi = $this->players->call('get', $p);
if (is_null($pi['aid'])) {
return $nr;
}
$a = $this->lib->call('get', $pi['aid']);
if (is_null($a)) {
return $nr;
}
if ($a['leader'] == $p) {
$pr = array(
'list_access' => 3,
'tech_trade' => ($a['enable_tt'] == 'N' ? 0 : 4),
'attacks' => 1,
'can_set_grades' => 1,
'can_kick' => 1,
'can_accept' => 1,
'forum_admin' => 1,
'dipl_contact' => 1,
'can_vote' => 0,
'can_be_cand' => 1,
'is_leader' => 1
);
// Get all ranks (-> kick, change)
$q = $this->db->query("SELECT id FROM alliance_grade WHERE alliance=".$a['id']);
$ar = array();
while ($r = dbFetchArray($q)) {
array_push($ar, $r[0]);
}
$pr['kick_ranks'] = $pr['change_ranks'] = $ar;
// Forums
$pr['f_read'] = $pr['f_mod'] = array();
$q = $this->db->query("SELECT id FROM af_forum WHERE alliance=".$a['id']);
while ($r = dbFetchArray($q)) {
array_push($pr['f_mod'], $r[0]);
}
} elseif (is_null($pi['a_grade'])) {
$pr = $this->lib->call('getRankPrivileges', $a['default_grade']);
} else {
$pr = $this->lib->call('getRankPrivileges', $pi['a_grade']);
}
if ($a['democracy'] == "f") {
$pr['can_vote'] = $pr['can_be_cand'] = 'f';
}
return $pr;
}
}
?>

View file

@ -0,0 +1,96 @@
<?php
class beta5_alliance_getRankPrivileges {
function beta5_alliance_getRankPrivileges($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Returns the privileges associated with a rank
function run($id) {
$q = $this->db->query("SELECT * FROM alliance_grade WHERE id=$id");
$pr = dbFetchHash($q);
$pr['is_leader'] = 0;
// Transform list access
switch ($pr['list_access']) :
case 'NO ': $pr['list_access'] = 0; break;
case 'PY ': $pr['list_access'] = 1; break;
case 'PL ': $pr['list_access'] = 2; break;
case 'PLD': $pr['list_access'] = 3; break;
endswitch;
// Transform tech trade tool access
$q = $this->db->query("SELECT enable_tt FROM alliance WHERE id = {$pr['alliance']}");
list($enableTT) = dbFetchArray($q);
switch ($pr['tech_trade']) :
case 'NO': $pr['tech_trade'] = 0; break;
case 'SL': $pr['tech_trade'] = ($enableTT != 'N' ? 1 : 0); break;
case 'SR': $pr['tech_trade'] = ($enableTT == 'N' ? 0 : ($enableTT == 'S' ? 1 : 2)); break;
case 'VL': $pr['tech_trade'] = ($enableTT != 'N' ? 3 : 0); break;
case 'MR': $pr['tech_trade'] = ($enableTT != 'N' ? 4 : 0); break;
endswitch;
// Transform PostgreSQL booleans into something usable by the current scripts
$fields = array("attacks", "can_set_grades", "can_kick", "can_accept",
"can_be_kicked", "forum_admin", "dipl_contact", "can_vote", "can_be_cand");
foreach ($fields as $f) {
$pr[$f] = ($pr[$f] == 't') ? 1 : 0;
}
// Get kickable ranks
$a = array();
if ($pr['can_kick']) {
$q = $this->db->query("SELECT kick FROM algr_kick WHERE grade = $id AND kick <> $id");
if (!dbCount($q)) {
$q = $this->db->query(
"SELECT id FROM alliance_grade "
. "WHERE alliance = {$pr['alliance']} AND id <> $id"
);
}
while ($r = dbFetchArray($q)) {
array_push($a, $r[0]);
}
}
$pr['kick_ranks'] = $a;
// Get changeable ranks
$a = array();
if ($pr['can_set_grades']) {
$q = $this->db->query("SELECT can_change FROM algr_chgrade WHERE grade=$id AND can_change!=$id");
if (!dbCount($q)) {
$q = $this->db->query(
"SELECT id FROM alliance_grade "
. "WHERE alliance={$pr['alliance']} AND id <> $id"
);
}
while ($r = dbFetchArray($q)) {
array_push($a, $r[0]);
}
}
$pr['change_ranks'] = $a;
// Forum privileges
$pr['f_read'] = $pr['f_mod'] = array();
if ($pr['forum_admin'] == 0) {
$q = $this->db->query("SELECT forum,is_mod FROM algr_forums WHERE grade=$id");
while ($r = dbFetchArray($q)) {
array_push($pr[($r[1] == 't') ? "f_mod" : "f_read"], $r[0]);
}
} else {
$q = $this->db->query(
"SELECT f.id FROM af_forum f,alliance_grade r "
. "WHERE r.id = $id AND f.alliance = r.alliance"
);
while ($r = dbFetchArray($q)) {
array_push($pr['f_mod'], $r[0]);
}
}
return $pr;
}
}
?>

View file

@ -0,0 +1,32 @@
<?php
class beta5_alliance_getRankSize {
function beta5_alliance_getRankSize($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Get the number of alliance members at a certain rank
function run($aid, $rId) {
$a = $this->lib->call('get', $aid);
$qs = "SELECT COUNT(*) FROM player WHERE alliance=$aid AND a_status='IN' AND ";
if ($rId == $a['default_grade']) {
$qs .= "(a_grade=$rId OR a_grade IS NULL)";
} else {
$qs .= "a_grade=$rId";
}
if (!is_null($a['leader'])) {
$qs .= " AND id<>" . $a['leader'];
}
$q = $this->db->query($qs);
if (!($q && dbCount($q))) {
return 0;
}
list($r) = dbFetchArray($q);
return $r;
}
}
?>

View file

@ -0,0 +1,22 @@
<?php
class beta5_alliance_getRanks {
function beta5_alliance_getRanks($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Reads the list of ranks associated with an alliance
function run($aid) {
$q = $this->db->query("SELECT id,name FROM alliance_grade WHERE alliance=$aid");
$rs = array();
while ($r = dbFetchArray($q)) {
$rs[$r[0]] = is_null($r[1]) ? "-" : $r[1];
}
return $rs;
}
}
?>

View file

@ -0,0 +1,22 @@
<?php
class beta5_alliance_getRequests {
function beta5_alliance_getRequests($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Get the list of pending requests for an alliance
function run($aid) {
$q = $this->db->query("SELECT p.id,p.name,a.name FROM player p, account a WHERE p.alliance=$aid AND p.a_status='REQ' AND a.id=p.userid");
$rs = array();
while ($r = dbFetchArray($q)) {
$rs[$r[0]] = ($r[1] == "") ? $r[2] : $r[1];
}
return $rs;
}
}
?>

View file

@ -0,0 +1,93 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/alliance/library/getTechList.inc
//
// This function retrieves the technology list for an alliance.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_alliance_getTechList {
public function __construct($lib) {
$this->lib = $lib;
$this->game = $this->lib->game;
$this->db = $this->game->db;
$this->players = $this->game->getLib('beta5/player');
}
public function run($alliance, $hasRequests) {
// Get tech trading privileges for all players in the alliance
$players = $this->getPlayers($alliance, $hasRequests);
// Get the technology list for each player that is authorized to submit it,
// and get requests for players who can submit requests
$techList = array();
foreach ($players as $player => $priv) {
// Ignore unprivileged players
if ($priv == 0) {
continue;
}
// Get list and player status
list($lastSub, $list) = $this->getPlayerList($player);
$techList[$player] = array(
"submitted" => $lastSub,
"list" => $list,
"vacation" => $this->players->call('isOnVacation', $player) ? 1 : 0,
"restrict" => $this->players->call('isRestrained', $player) ? 1 : 0
);
// Ignore players who can't submit requess
if ($priv == 1) {
continue;
}
$techList[$player]['requests'] = $this->lib->call('getTechRequests', $player);
}
return $techList;
}
private function getPlayers($alliance, $hasRequests) {
$q = $this->db->query(
"SELECT p.id, (CASE p.id = a.leader WHEN TRUE THEN 'MR' ELSE r.tech_trade END) "
. "FROM player p, alliance a, alliance_grade r "
. "WHERE p.alliance = $alliance AND p.a_status = 'IN' AND r.alliance = p.alliance "
. "AND (p.a_grade = r.id OR p.a_grade IS NULL AND r.name IS NULL) "
. "AND a.id = p.alliance"
);
$players = array();
while ($r = dbFetchArray($q)) {
switch ($r[1]) {
case 'NO': $priv = 0; break;
case 'SL': $priv = 1; break;
default: $priv = $hasRequests ? 2 : 1; break;
}
$players[$r[0]] = $priv;
}
return $players;
}
private function getPlayerList($player) {
$list = array();
$subAt = 0;
$q = $this->db->query("SELECT tech, status, submitted FROM tech_trade_list WHERE member = $player");
while ($r = dbFetchArray($q)) {
$list[$r[0]] = $r[1];
$subAt = $r[2];
}
return array($subAt, $list);
}
}
?>

View file

@ -0,0 +1,36 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/alliance/library/getTechOrder.inc
//
// This function retrieves technology trading orders from the database.
// Depending on the $send parameter, it can fetch either orders to send
// technologies or orders to receive technologies.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_alliance_getTechOrder {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
public function run($player, $send) {
$q = $this->db->query(
"SELECT tech, " . ($send ? "send_to" : "player") . ", submitted, obeyed FROM tech_trade_order "
. "WHERE " . ($send ? "player" : "send_to") . " = $player "
. "FOR UPDATE"
);
if (!dbCount($q)) {
return array(null, null, null, null);
}
return dbFetchArray($q);
}
}
?>

View file

@ -0,0 +1,58 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/alliance/library/getTechOrders.inc
//
// This function retrieves all technology trading orders for an alliance
// from the database.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_alliance_getTechOrders {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
public function run($alliance) {
// Get the orders for all players
$q = $this->db->query(
"SELECT player, send_to, tech, submitted, obeyed FROM tech_trade_order "
. "WHERE alliance = $alliance"
);
// Create the list of orders
$orders = array();
while ($r = dbFetchArray($q)) {
if (is_null($r[4])) {
$r[4] = 0;
}
if (!is_array($orders[$r[0]])) {
$orders[$r[0]] = array();
}
$orders[$r[0]]['sendTo'] = $r[1];
$orders[$r[0]]['sendTech'] = $r[2];
$orders[$r[0]]['sendSub'] = $r[3];
$orders[$r[0]]['sendDone'] = $r[4];
if (!is_array($orders[$r[1]])) {
$orders[$r[1]] = array();
}
$orders[$r[1]]['recvFrom'] = $r[0];
$orders[$r[1]]['recvTech'] = $r[2];
$orders[$r[1]]['recvSub'] = $r[3];
$orders[$r[1]]['recvDone'] = $r[4];
}
return $orders;
}
}
?>

View file

@ -0,0 +1,38 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/alliance/library/getTechRequests.inc
//
// This function returns the list of technology requests for a player.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_alliance_getTechRequests {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
public function run($player) {
// Get the player's current requests, if any
$q = $this->db->query(
"SELECT tech FROM tech_trade_request "
. "WHERE player = $player ORDER BY priority"
);
$requests = array();
while ($r = dbFetchArray($q)) {
array_push($requests, $r[0]);
}
return $requests;
}
}
?>

View file

@ -0,0 +1,38 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/alliance/library/getTechSubmission.inc
//
// This function retrieves technology list submission data from the
// tech trading database and determines whether a player can submit his
// list again.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_alliance_getTechSubmission {
public function __construct($lib) {
$this->lib = $lib;
$this->game = $this->lib->game;
$this->db = $this->game->db;
}
public function run($player) {
$q = $this->db->query(
"SELECT submitted FROM tech_trade_list WHERE member = $player LIMIT 1"
);
if (!dbCount($q)) {
return array(true, null, null);
}
list($when) = dbFetchArray($q);
$nextSubmission = $when + $this->game->ticks['battle']->interval;
return array($nextSubmission <= time(), $when, $nextSubmission);
}
}
?>

View file

@ -0,0 +1,46 @@
<?php
class beta5_alliance_getVoters {
function beta5_alliance_getVoters($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Get the list of all people in an alliance that can vote, as well as the current leader
function run($a) {
$q = $this->db->query("SELECT leader FROM alliance WHERE id=$a");
list($id) = dbFetchArray($q);
if (!is_null($id)) {
$l = array($id);
} else {
$l = array();
}
$q = $this->db->query("SELECT id FROM alliance_grade WHERE alliance=$a AND can_vote");
if (!$q || dbCount($q) == 0) {
return $l;
}
$gl = array();
while ($r = dbFetchArray($q)) {
list($g) = $r;
array_push($gl, $g);
}
$q = $this->db->query(
"SELECT p.id FROM player p, alliance a WHERE a.id=$a AND p.alliance=a.id AND p.a_status='IN' "
. "AND (p.a_grade IN (" . join(',',$gl) . ") OR (p.a_grade IS NULL AND a.default_grade IN (".join(',',$gl).")))"
);
while ($r = dbFetchArray($q)) {
list($p) = $r;
if (is_null($id) || $p != $id) {
array_push($l, $p);
}
}
return $l;
}
}
?>

View file

@ -0,0 +1,18 @@
<?php
class beta5_alliance_kick {
function beta5_alliance_kick($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Kick an alliance member
function run($pid) {
$this->lib->call('leave', $pid, true);
// FIXME: message
}
}
?>

View file

@ -0,0 +1,84 @@
<?php
class beta5_alliance_leave {
function beta5_alliance_leave($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->players = $this->lib->game->getLib('beta5/player');
$this->rankings = $this->lib->game->getLib('main/rankings');
}
// Leave an alliance
function run($pid, $isKick = false) {
$p = $this->players->call('get', $pid);
if (is_null($p) || is_null($p['aid'])) {
return;
}
$aid = $p['aid'];
$a = $this->lib->call('get', $aid);
if (is_null($a)) {
return;
}
// Delete tech trading data
$this->db->query("DELETE FROM tech_trade_list WHERE member = $pid");
$this->db->query("DELETE FROM tech_trade_request WHERE player = $pid");
$this->db->query("DELETE FROM tech_trade_order WHERE player = $pid OR send_to = $pid");
$msgId = 9;
$mpid = $pid;
if ($pid == $a['leader']) {
if (!is_null($a['successor'])) {
$this->lib->call('stepDown', $aid, true);
$msgId = 14;
$mpid = $a['successor'];
} else {
$this->db->query("UPDATE alliance SET leader=NULL,democracy=TRUE WHERE id=$aid");
$this->db->query("UPDATE alliance_grade SET can_vote=TRUE,can_be_cand=TRUE WHERE alliance=$aid");
$msgId = 15;
}
} elseif ($pid == $a['successor']) {
$this->db->query("UPDATE alliance SET successor=NULL WHERE id=$aid");
}
if ($a['democracy'] == 't') {
$this->db->query("DELETE FROM alliance_candidate WHERE alliance=$aid AND candidate=".$pid);
}
$this->db->query("UPDATE player SET alliance=NULL,a_vote=NULL,a_status=NULL,a_grade=NULL WHERE id=$pid");
$q = $this->db->query("SELECT COUNT(*) FROM player WHERE alliance=$aid AND a_status='IN'");
list($pc) = dbFetchArray($q);
if ($pc == 0) {
$this->db->query("UPDATE player SET alliance=NULL,a_vote=NULL,a_status=NULL,a_grade=NULL WHERE alliance=$aid");
$rt = $this->rankings->call('getType', 'a_general');
$this->rankings->call('delete', $rt, $a['tag']);
$this->db->query("DELETE FROM alliance WHERE id=$aid");
return;
}
if ($isKick) {
return;
}
if ($msgId == 9) {
$l = $this->lib->call('getKeepers', $aid);
} else {
$l = array_keys($this->lib->call('getMembers', $aid));
}
$tag = addslashes($a['tag']);
$tm = time();
l::FIXME("Use message API"); // FIXME
foreach ($l as $id) {
$this->db->query("INSERT INTO message(player,sent_on,mtype,ftype,is_new) VALUES($id,$tm,'alint','INT',TRUE)");
$q = $this->db->query("SELECT id FROM message WHERE player=$id AND sent_on=$tm AND ftype='INT' ORDER BY id DESC LIMIT 1");
list($mid) = dbFetchArray($q);
$this->db->query("INSERT INTO msg_alint VALUES($mid,$aid,'$tag',$mpid,$msgId)");
}
}
}
?>

View file

@ -0,0 +1,20 @@
<?php
class beta5_alliance_modifyForum {
function beta5_alliance_modifyForum($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Modifies an alliance forum
function run($id, $name, $userPost, $description) {
$this->db->query(
"UPDATE af_forum SET title='".addslashes($name)."',user_post=".($userPost?'TRUE':'FALSE').",description='".addslashes($description)
."' WHERE id=$id"
);
}
}
?>

View file

@ -0,0 +1,77 @@
<?php
class beta5_alliance_modifyRank {
function beta5_alliance_modifyRank($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Modifies a new rank within the alliance
function run($rid, $name, $privs, $kick, $change, $fread, $fmod) {
$ll = array('NO ','PY ','PL ','PLD');
$lt = array('NO', 'SL', 'SR', 'VL', 'MR');
if (is_null($name)) {
$n = "NULL";
} else {
$n = "'".addslashes($name)."'";
}
$qs = "UPDATE alliance_grade SET name=$n";
foreach ($privs as $p => $v) {
$qs .= ",$p=";
if ($p == "list_access") {
$qs .= "'" . $ll[$v] . "'";
} elseif ($p == "tech_trade") {
$qs .= "'" . $lt[$v] . "'";
} else {
$qs .= dbBool($v);
}
}
$qs .= " WHERE id=$rid";
$this->db->query($qs);
$q = $this->db->query("SELECT alliance FROM alliance_grade WHERE id=$rid");
list($aid) = dbFetchArray($q);
$q = $this->db->query("SELECT default_grade,leader FROM alliance WHERE id=$aid");
list($did, $lid) = dbFetchArray($q);
if ($rid == $did) {
$n = " IS NULL";
} else {
$n = "=$rid";
}
if ($privs['can_vote'] == 0) {
$this->db->query("UPDATE player SET a_vote=NULL WHERE a_grade$n AND alliance=$aid AND a_status='IN'" . (is_null($lid) ? "": " AND id<>$lid"));
}
if ($privs['can_be_cand'] == 0) {
$q = $this->db->query("SELECT id FROM player WHERE a_grade$n AND alliance=$aid AND a_status='IN'" . (is_null($lid) ? "": " AND id<>$lid"));
$a = array();
while ($r = dbFetchArray($q)) {
array_push($a, $r[0]);
}
if (count($a)) {
$this->db->query("DELETE FROM alliance_candidate WHERE candidate IN (".join(',',$a).")");
}
}
$this->db->query("DELETE FROM algr_kick WHERE grade=$rid");
foreach ($kick as $id) {
$this->db->query("INSERT INTO algr_kick VALUES($rid,$id)");
}
$this->db->query("DELETE FROM algr_chgrade WHERE grade=$rid");
foreach ($change as $id) {
$this->db->query("INSERT INTO algr_chgrade VALUES($rid,$id)");
}
$this->db->query("DELETE FROM algr_forums WHERE grade=$rid");
foreach ($fread as $id) {
$this->db->query("INSERT INTO algr_forums VALUES($rid,$id,".dbBool(0).")");
}
foreach ($fmod as $id) {
$this->db->query("INSERT INTO algr_forums VALUES($rid,$id,".dbBool(1).")");
}
}
}
?>

View file

@ -0,0 +1,36 @@
<?php
class beta5_alliance_moveForum {
function beta5_alliance_moveForum($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Moves an alliance forum up or down in the list
function run($id, $up) {
$q = $this->db->query("SELECT alliance,forder FROM af_forum WHERE id=$id");
list($aid,$o) = dbFetchArray($q);
if ($o == 0 && $up) {
return;
}
$q = $this->db->query("SELECT MAX(forder) FROM af_forum WHERE alliance=$aid");
list($mo) = dbFetchArray($q);
if ($o == $mo && !$up) {
return;
}
$this->db->query("UPDATE af_forum SET forder=" . ($mo+1) . " WHERE id=$id");
if ($up) {
$this->db->query("UPDATE af_forum SET forder=forder+1 WHERE alliance=$aid AND forder=".($o-1));
$this->db->query("UPDATE af_forum SET forder=".($o-1)." WHERE id=$id");
} else {
$this->db->query("UPDATE af_forum SET forder=forder-1 WHERE alliance=$aid AND forder=".($o+1));
$this->db->query("UPDATE af_forum SET forder=".($o+1)." WHERE id=$id");
}
}
}
?>

View file

@ -0,0 +1,51 @@
<?php
class beta5_alliance_rejectRequest {
function beta5_alliance_rejectRequest($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->msgs = $this->lib->game->getLib('beta5/msg');
}
// Rejects a request to join an alliance
function run($aid, $pid, $kid) {
$q = $this->db->query("SELECT id FROM player WHERE alliance=$aid AND a_status='REQ' AND id=$pid");
if (!($q && dbCount($q) == 1)) {
return;
}
$a = $this->lib->call('get', $aid);
if (is_null($a)) {
return;
}
$tag = addslashes($a['tag']);
$tm = time();
$this->msgs->call('send', $pid, 'alint', array(
"msg_type" => 12,
"alliance" => $aid,
"tag" => $tag,
"player" => $pid,
));
$l = $this->lib->call('getKeepers', $aid);
foreach ($l as $id) {
if ($id == $kid) {
continue;
}
$this->msgs->call('send', $id, 'alint', array(
"msg_type" => 13,
"alliance" => $aid,
"tag" => $tag,
"player" => $pid,
));
}
$this->db->query("UPDATE player SET a_status=NULL,alliance=NULL,a_vote=NULL,a_grade=NULL WHERE id=$pid");
}
}
?>

View file

@ -0,0 +1,32 @@
<?php
class beta5_alliance_removeCandidate {
function beta5_alliance_removeCandidate($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Removes a candidate for an alliance's presidency
function run($aid,$pid) {
$this->db->query("DELETE FROM alliance_candidate WHERE candidate=$pid");
$a = $this->lib->call('get', $aid);
$tag = addslashes($a['tag']);
$tm = time();
$vl = $this->lib->call('getVoters', $aid);
foreach ($vl as $id) {
if ($id == $pid) {
continue;
}
$this->db->query("INSERT INTO message(player,sent_on,mtype,ftype,is_new) VALUES($id,$tm,'alint','INT',TRUE)");
$q = $this->db->query("SELECT id FROM message WHERE player=$id AND sent_on=$tm AND ftype='INT' ORDER BY id DESC LIMIT 1");
list($mid) = dbFetchArray($q);
$this->db->query("INSERT INTO msg_alint VALUES($mid,$aid,'$tag',$pid,17)");
}
}
}
?>

View file

@ -0,0 +1,29 @@
<?php
class beta5_alliance_sendRequest {
function beta5_alliance_sendRequest($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Sends a request to join an alliance
function run($p, $a) {
$this->db->query("UPDATE player SET alliance=$a,a_status='REQ',a_vote=NULL WHERE id=$p");
$ainf = $this->lib->call('get', $a);
$tag = addslashes($ainf['tag']);
$l = $this->lib->call('getKeepers', $a);
$tm = time();
foreach ($l as $id) {
$this->db->query("INSERT INTO message(player,sent_on,mtype,ftype,is_new) VALUES($id,$tm,'alint','INT',TRUE)");
$q = $this->db->query("SELECT id FROM message WHERE player=$id AND sent_on=$tm AND ftype='INT'");
list($mid) = dbFetchArray($q);
$this->db->query("INSERT INTO msg_alint VALUES($mid,$a,'$tag',$p,0)");
}
}
}
?>

View file

@ -0,0 +1,47 @@
<?php
class beta5_alliance_setDemocratic {
function beta5_alliance_setDemocratic($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Switches an alliance's government mode
function run($aid, $demo) {
$a = $this->lib->call('get', $aid);
if (is_null($a)) {
return null;
}
$ad = ($a['democracy'] != "f");
if ($ad == $demo) {
return;
}
$this->db->query("UPDATE alliance SET democracy=".dbBool($demo)." WHERE id=$aid");
if ($demo) {
$this->db->query("INSERT INTO alliance_candidate(alliance,candidate) VALUES ($aid,".$a['leader'].")");
$q = $this->db->query("SELECT id FROM alliance_candidate WHERE candidate=".$a['leader']." AND alliance=$aid");
list($cid) = dbFetchArray($q);
$this->db->query("UPDATE player SET a_vote=$cid WHERE id=".$a['leader']);
$msId = 4;
} else {
$this->db->query("DELETE FROM alliance_candidate WHERE alliance=$aid");
$msId = 5;
}
$q = $this->db->query("SELECT id FROM player WHERE alliance=$aid AND a_status='IN' AND id<>".$a['leader']);
$tm = time();
$tag = addslashes($a['tag']);
while ($r = dbFetchArray($q)) {
$id = $r[0];
$this->db->query("INSERT INTO message(player,sent_on,mtype,ftype,is_new) VALUES($id,$tm,'alint','INT',TRUE)");
$q2 = $this->db->query("SELECT id FROM message WHERE player=$id AND sent_on=$tm AND ftype='INT' AND mtype='alint' ORDER BY id DESC LIMIT 1");
list($mid) = dbFetchArray($q2);
$this->db->query("INSERT INTO msg_alint VALUES($mid,$aid,'$tag',".$a['leader'].",$msId)");
}
}
}
?>

View file

@ -0,0 +1,23 @@
<?php
class beta5_alliance_setForumAccess {
function beta5_alliance_setForumAccess($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Modifies the access list for an alliance forum
function run($id, $readers, $mods) {
$this->db->query("DELETE FROM algr_forums WHERE forum=$id");
foreach ($readers as $rid) {
$this->db->query("INSERT INTO algr_forums VALUES($rid,$id,false)");
}
foreach ($mods as $rid) {
$this->db->query("INSERT INTO algr_forums VALUES($rid,$id,true)");
}
}
}
?>

View file

@ -0,0 +1,38 @@
<?php
class beta5_alliance_setSuccessor {
function beta5_alliance_setSuccessor($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Changes the successor for the current leader
function run($aid, $sid) {
$a = $this->lib->call('get', $aid);
if (is_null($a) || $a['successor'] == $sid) {
return;
}
$tag = addslashes($a['tag']);
$tm = time();
if (!is_null($a['successor'])) {
$id = $a['successor'];
$this->db->query("INSERT INTO message(player,sent_on,mtype,ftype,is_new) VALUES($id,$tm,'alint','INT',TRUE)");
$q = $this->db->query("SELECT id FROM message WHERE player=$id AND sent_on=$tm AND ftype='INT' AND mtype='alint' ORDER BY id DESC LIMIT 1");
list($mid) = dbFetchArray($q);
$this->db->query("INSERT INTO msg_alint VALUES($mid,$aid,'$tag',".$a['leader'].",2)");
}
$this->db->query("UPDATE alliance SET successor=".(is_null($sid)?"NULL":$sid) . " WHERE id=$aid");
if (!is_null($sid)) {
$this->db->query("INSERT INTO message(player,sent_on,mtype,ftype,is_new) VALUES($sid,$tm,'alint','INT',TRUE)");
$q = $this->db->query("SELECT id FROM message WHERE player=$sid AND sent_on=$tm AND ftype='INT' AND mtype='alint' ORDER BY id DESC LIMIT 1");
list($mid) = dbFetchArray($q);
$this->db->query("INSERT INTO msg_alint VALUES($mid,$aid,'$tag',".$a['leader'].",3)");
}
}
}
?>

View file

@ -0,0 +1,46 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/alliance/library/setTechRequests.inc
//
// This function updates the list of requests made by a player. It
// should only be called after it has been made sure that the player is
// a member of an alliance, that he can submit requests and that his
// list of requested technologies is valid.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_alliance_setTechRequests {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
public function run($player, $requests) {
// Delete the player's requests
$this->db->query("DELETE FROM tech_trade_request WHERE player = $player");
// Get the player's alliance; if we're here, then the player *is* in an alliance
$q = $this->db->query("SELECT alliance FROM player WHERE id = $player");
list($alliance) = dbFetchArray($q);
// Delete the player's requests and reinserts them while removing any tech that is now "seen"
$this->db->query("DELETE FROM tech_trade_request WHERE player = $player");
$prio = 0;
foreach ($requests as $req) {
$this->db->query(
"INSERT INTO tech_trade_request (alliance, player, priority, tech) "
. "VALUES ($alliance, $player, $prio, $req)"
);
$prio ++;
}
}
}
?>

View file

@ -0,0 +1,34 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/alliance/library/setTechTradeMode.inc
//
// This function allows the alliance president to change the tech trade
// mode for the alliance.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_alliance_setTechTradeMode {
static private $okModes = array('N', 'S', 'R');
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
public function run($alliance, $mode) {
if (!in_array($mode, self::$okModes)) {
return false;
}
$x = $this->db->query("UPDATE alliance SET enable_tt = '$mode' WHERE id = $alliance");
return !!$x;
}
}
?>

View file

@ -0,0 +1,58 @@
<?php
class beta5_alliance_stepDown {
function beta5_alliance_stepDown($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Makes the alliance leader step down from power
function run($aid, $isLeave = false) {
$a = $this->lib->call('get', $aid);
if (is_null($a) || is_null($a['successor'])) {
return;
}
$this->db->query("UPDATE alliance SET leader=".$a['successor'].",successor=NULL WHERE id=$aid");
if ($a['democracy'] == 't') {
$q = $this->db->query("SELECT id FROM alliance_candidate WHERE candidate=".$a['successor']);
if (dbCount($q) == 1) {
$q2 = $this->db->query("SELECT id FROM alliance_candidate WHERE candidate=".$a['leader']);
list($r1) = dbFetchArray($q);
list($r2) = dbFetchArray($q2);
$this->db->query("UPDATE player SET a_vote=$r2 WHERE a_vote=$r1");
$this->db->query("DELETE FROM alliance_candidate WHERE candidate=".$a['successor']);
} else {
$q = $this->db->query("SELECT id FROM alliance_candidate WHERE candidate={$a['leader']}");
list($id) = dbFetchArray($q);
$this->db->query("UPDATE player SET a_vote=$id WHERE id={$a['successor']}");
}
$this->db->query("UPDATE alliance_candidate SET candidate=".$a['successor']." WHERE candidate=".$a['leader']);
}
if ($isLeave) {
return;
}
$tm = time();
$tag = addslashes($a['tag']);
$qm = $this->db->query("SELECT id FROM player WHERE alliance=$aid AND a_status='IN'");
while ($r = dbFetchArray($qm)) {
$id = $r[0];
$this->db->query("INSERT INTO message(player,sent_on,mtype,ftype,is_new) VALUES($id,$tm,'alint','INT',TRUE)");
$q = $this->db->query("SELECT id FROM message WHERE player=$id AND sent_on=$tm AND ftype='INT' AND mtype='alint' ORDER BY id DESC LIMIT 1");
list($mid) = dbFetchArray($q);
if ($id == $a['leader']) {
$st = $a['successor'].",6";
} elseif ($id == $a['successor']) {
$st = $a['leader'].",7";
} else {
$st = $a['successor'].",8";
}
$this->db->query("INSERT INTO msg_alint VALUES($mid,$aid,'$tag',$st)");
}
}
}
?>

View file

@ -0,0 +1,69 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/alliance/library/submitTechList.inc
//
// This function stores a player's technology list in his alliance's
// tech trading database.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_alliance_submitTechList {
public function __construct($lib) {
$this->lib = $lib;
$this->game = $this->lib->game;
$this->db = $this->game->db;
}
public function run($player) {
// Get alliance
$pInfo = $this->game->getLib('beta5/player')->call('get', $player);
$alliance = $pInfo['aid'];
// Get the player's technologies
$techLib = $this->game->getLib('beta5/tech');
$implemented = $techLib->call('getTopics', $player, 1);
$completed = $techLib->call('getTopics', $player, 0);
$foreseen = $techLib->call('getTopics', $player, -1);
$laws = $techLib->call('getLaws', $player);
// Delete previous tech submission
$this->db->query("DELETE FROM tech_trade_list WHERE member = $player");
// Insert technologies
for ($i = 0; $i < count($laws) / 2; $i ++) {
$this->db->query(
"INSERT INTO tech_trade_list (alliance, member, tech, status) "
. "VALUES ($alliance, $player, {$laws[$i * 2]}, 'L')"
);
}
for ($i = 0; $i < count($completed); $i ++) {
$this->db->query(
"INSERT INTO tech_trade_list (alliance, member, tech, status) "
. "VALUES ($alliance, $player, {$completed[$i]}, 'N')"
);
}
for ($i = 0; $i < count($implemented); $i ++) {
$this->db->query(
"INSERT INTO tech_trade_list (alliance, member, tech, status) "
. "VALUES ($alliance, $player, {$implemented[$i]}, 'I')"
);
}
for ($i = 0; $i < count($foreseen) / 2; $i ++) {
$this->db->query(
"INSERT INTO tech_trade_list (alliance, member, tech, status) "
. "VALUES ($alliance, $player, {$foreseen[$i * 2]}, 'F')"
);
}
// Update request list
$this->lib->call('updateRequests', $player);
}
}
?>

View file

@ -0,0 +1,119 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/alliance/library/submitTechOrders.inc
//
// This function updates the alliance's orders for the next 24 hours.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_alliance_submitTechOrders {
public function __construct($lib) {
$this->lib = $lib;
$this->game = $this->lib->game;
$this->db = $this->game->db;
$this->msgs = $this->game->getLib('beta5/msg');
}
public function run($alliance, $orders) {
// List all players included in the orders
$players = array_keys($orders);
foreach ($orders as $oPid => $order) {
if (!in_array($order[1], $players)) {
array_push($players, $order[1]);
}
}
// Check if the players are in the alliance
$q = $this->db->query(
"SELECT id FROM player WHERE id IN (" . join(',', $players) . ") "
. "AND (alliance <> $alliance OR a_status <> 'IN ')"
);
if (dbCount($q)) {
return false;
}
// Get the tech lists for all players
$techList = $this->lib->call('getTechList', $alliance, false);
// Get all dependencies
$dependencies = $this->getDependencies();
// For each order, check if the sender had submitted the technology as
// either New, Implemented or Law, and whether he can actually send
// technologies. Also check if the receiver had submitted his list, if
// he can receive techs and if the list doesn't contain the technology
// to be sent. Oh, and if he has the dependencies.
foreach ($orders as $sender => $order) {
list($tech, $receiver) = $order;
if (!is_array($techList[$sender]) || !is_array($techList[$receiver])
|| $sender == $receiver || !$techList[$sender]['submitted']
|| $techList[$sender]['vacation'] || $techList[$sender]['restrict']
|| !$techList[$receiver]['submitted'] || $techList[$receiver]['vacation']
|| $techList[$receiver]['restrict']
|| !array_key_exists($tech, $techList[$sender]['list'])
|| $techList[$sender]['list'][$tech] == 'F'
|| array_key_exists($tech, $techList[$receiver]['list']) ) {
return false;
}
if (is_array($dependencies[$tech])) {
foreach ($dependencies[$tech] as $dep) {
if (!array_key_exists($dep, $techList[$receiver]['list'])) {
l::trace("dep $dep not found");
return false;
}
$dStatus = $techList[$receiver]['list'][$dep];
if ($dStatus != 'I' && $dStatus != 'L') {
return false;
}
}
}
}
// Delete current orders
$this->db->query("DELETE FROM tech_trade_order WHERE alliance = $alliance");
// Insert new orders
foreach ($orders as $sender => $order) {
list($tech, $receiver) = $order;
$this->db->query(
"INSERT INTO tech_trade_order (alliance, player, send_to, tech) "
. "VALUES($alliance, $sender, $receiver, $tech)"
);
}
// Send private message to all players included in the exchange
$q = $this->db->query("SELECT tag FROM alliance WHERE id = $alliance");
list($tag) = dbFetchArray($q);
foreach ($players as $player) {
$this->msgs->call('send', $player, 'alint', array(
"msg_type" => 20,
"tag" => $tag,
"alliance" => $alliance
));
}
return true;
}
private function getDependencies() {
$q = $this->db->query("SELECT research, depends_on FROM research_dep");
$deps = array();
while ($r = dbFetchArray($q)) {
if (!is_array($deps[$r[0]])) {
$deps[$r[0]] = array();
}
array_push($deps[$r[0]], $r[1]);
}
return $deps;
}
}
?>

View file

@ -0,0 +1,34 @@
<?php
class beta5_alliance_takePresidency {
function beta5_alliance_takePresidency($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Makes a player the new president for an alliance
function run($aid, $pid) {
$this->db->query("UPDATE alliance SET leader=$pid,successor=NULL WHERE id=$aid");
$a = $this->lib->call('get', $aid);
$tag = addslashes($a['tag']);
$tm = time();
$l = array_keys($this->lib->call('getMembers', $aid));
foreach ($l as $id) {
$this->db->query("INSERT INTO message(player,sent_on,mtype,ftype,is_new) VALUES($id,$tm,'alint','INT',TRUE)");
$q = $this->db->query("SELECT id FROM message WHERE player=$id AND sent_on=$tm AND ftype='INT' ORDER BY id DESC LIMIT 1");
list($mid) = dbFetchArray($q);
if ($id == $pid) {
$mt = 18;
} else {
$mt = 19;
}
$this->db->query("INSERT INTO msg_alint VALUES($mid,$aid,'$tag',$pid,$mt)");
}
}
}
?>

View file

@ -0,0 +1,75 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/alliance/library/updateRequests.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_alliance_updateRequests {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
public function run($player) {
// Get the player's alliance
$q = $this->db->query("SELECT alliance,a_status FROM player WHERE id = $player");
list($alliance, $aStat) = dbFetchArray($q);
// If the player is not a member of any alliance, remove all requests and leave
if (is_null($alliance) || $aStat != 'IN ') {
$this->db->query("DELETE FROM tech_trade_request WHERE player = $player");
return;
}
// Get the player's current requests, if any
$q = $this->db->query(
"SELECT alliance, tech FROM tech_trade_request "
. "WHERE player = $player ORDER BY priority"
);
$requests = array();
while ($r = dbFetchArray($q)) {
if ($r[0] != $alliance) {
// Requests are from another alliance; delete them and leave
$this->db->query("DELETE FROM tech_trade_request WHERE player = $player");
return;
}
array_push($requests, $r[1]);
}
// Get all technologies or laws that are at least foreseen
$q = $this->db->query(
"SELECT r.id FROM research_player p, research r "
. "WHERE r.id = p.research AND p.points >= 75 * r.points / 100 AND p.player = $player"
);
$seenTechs = array();
while ($r = dbFetchArray($q)) {
array_push($seenTechs, $r[0]);
}
// Delete the player's requests and reinserts them while removing any tech that is now "seen"
$this->db->query("DELETE FROM tech_trade_request WHERE player = $player");
$prio = 0;
foreach ($requests as $req) {
if (in_array($req, $seenTechs)) {
continue;
}
$this->db->query(
"INSERT INTO tech_trade_request (alliance, player, priority, tech) "
. "VALUES ($alliance, $player, $prio, $req)"
);
$prio ++;
}
}
}
?>

View file

@ -0,0 +1,54 @@
<?php
class beta5_alliance_updateVictory {
function beta5_alliance_updateVictory($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Compute victory conditions for an alliance
function run($aid) {
// Get the total amount of planets
$q = $this->db->query("SELECT COUNT(*) FROM planet WHERE status = 0");
list($pCount) = dbFetchArray($q);
// Get the amount of planets the alliance controls
$q = $this->db->query("SELECT COUNT(*) FROM planet WHERE owner IN ("
. "SELECT id FROM player WHERE alliance = $aid AND a_status='IN')");
list($aCount) = dbFetchArray($q);
// Compute the ratio
$pRatio = $aCount / $pCount;
if ($pRatio > 0.75) {
$pRatio = 0.75;
// Check if the alliance was already scheduled for victory
$q = $this->db->query("SELECT time_of_victory FROM alliance_victory WHERE alliance=$aid");
if (dbCount($q)) {
list($tov) = dbFetchArray($q);
if (time() > $tov) {
$tValue = 0.25;
} else {
$diff = $tov - time();
if ($diff > 604800) {
$diff = 604800;
}
$tValue = (604800 - $diff) / 2419200; // 7*24*3600*4
}
logText("tov = $tov, diff = $diff, tval = $tValue");
} else {
$this->db->query("INSERT INTO alliance_victory(alliance, time_of_victory)"
. " VALUES ($aid, UNIX_TIMESTAMP(NOW()) + 604800)");
$tValue = 0;
}
} else {
$tValue = 0;
$this->db->query("DELETE FROM alliance_victory WHERE alliance=$aid");
}
return round(($pRatio + $tValue) * 100);
}
}
?>