]> ruin.nu Git - ndwebbie.git/commitdiff
initial commit of alliances
authorMichael Andreen <harv@ruin.nu>
Fri, 15 Dec 2006 11:47:03 +0000 (11:47 +0000)
committerMichael Andreen <harv@ruin.nu>
Fri, 15 Dec 2006 11:47:03 +0000 (11:47 +0000)
alliances.pl [new file with mode: 0644]
index.pl
templates/alliances.tmpl [new file with mode: 0644]

diff --git a/alliances.pl b/alliances.pl
new file mode 100644 (file)
index 0000000..101f2db
--- /dev/null
@@ -0,0 +1,123 @@
+#**************************************************************************
+#   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
+#                                                                         *
+#   This program is free software; you can redistribute it and/or modify  *
+#   it under the terms of the GNU General Public License as published by  *
+#   the Free Software Foundation; either version 2 of the License, or     *
+#   (at your option) any later version.                                   *
+#                                                                         *
+#   This program is distributed in the hope that it will be useful,       *
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+#   GNU General Public License for more details.                          *
+#                                                                         *
+#   You should have received a copy of the GNU General Public License     *
+#   along with this program; if not, write to the                         *
+#   Free Software Foundation, Inc.,                                       *
+#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
+#**************************************************************************/
+
+use strict;
+use POSIX;
+our $BODY;
+our $DBH;
+our $LOG;
+my $error;
+
+$ND::TEMPLATE->param(TITLE => 'Alliances');
+
+die "You don't have access" unless isHC();
+
+my $alliance;
+if (param('alliance') =~ /^(\d+)$/){
+       my $query = $DBH->prepare(q{SELECT id,name, relationship FROM alliances WHERE id = ?});
+       $alliance = $DBH->selectrow_hashref($query,undef,$1);
+}
+if ($alliance && param ('cmd') eq 'coords'){
+       $DBH->begin_work;
+       $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
+}
+if ($alliance && param ('cmd') eq 'change'){
+       $DBH->begin_work;
+       $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
+}
+
+if ($alliance){
+       $BODY->param(Alliance => $alliance->{name});
+       $BODY->param(Id => $alliance->{id});
+       my @relationships;
+       for my $relationship ("&nbsp;","Friendly", "NAP", "Hostile"){
+               push @relationships,{Rel => $relationship, Selected => $relationship eq $alliance->{relationship}}
+       }
+       $BODY->param(Relationships => \@relationships);
+
+       my $order = "p.x,p.y,p.z";
+       if (param('order') =~ /^(score|size|value|xp|hit_us|race)$/){
+               $order = "$1 DESC";
+       }
+       my $members = $DBH->prepare(qq{
+SELECT coords(x,y,z), nick, ruler, planet, race, size, score, value, xp,
+       planet_status,hit_us, sizerank, scorerank, valuerank, xprank
+       FROM current_planet_stats p
+WHERE p.alliance_id = ?
+ORDER BY $order});
+       my @members;
+       $members->execute($alliance->{id});
+       my $i = 0;
+       while (my $member = $members->fetchrow_hashref){
+               $member->{ODD} = $i % 2;
+               push @members,$member;
+               $i++;
+       }
+       $BODY->param(Members => \@members);
+
+       my $query = $DBH->prepare(intelquery('o.alliance AS oalliance,coords(o.x,o.y,o.z) AS origin, t.alliance AS talliance,coords(t.x,t.y,t.z) AS target',qq{not ingal AND (t.alliance_id = ? OR t.alliance_id = ?)
+       AND (i.mission = 'Defend' OR i.mission = 'AllyDef')
+       AND (t.alliance_id != ? OR t.alliance_id IS NULL OR o.alliance_id != ? OR o.alliance_id IS NULL)
+       AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
+                       }));
+       $query->execute($alliance->{id},$alliance->{id},$alliance->{id},$alliance->{id}) or $error .= $DBH->errstr;
+
+       my @intel;
+       my $i = 0;
+       while (my $intel = $query->fetchrow_hashref){
+               if ($intel->{ingal}){
+                       $intel->{missionclass} = 'ingal';
+               }else{
+                       $intel->{missionclass} = $intel->{mission};
+               }
+               $intel->{ODD} = $i % 2;
+               push @intel,$intel;
+               $i++;
+       }
+       $BODY->param(Intel => \@intel);
+}else{
+
+       my $order = "score DESC";
+       if (param('order') =~ /^(score|kscore|size|ksize|members|kmem|kxp|kxp|scavg|kscavg|siavg|ksiavg|kxpavg|kvalue|kvalavg)$/){
+               $order = "$1 DESC";
+       }
+       my $query = $DBH->prepare(qq{
+SELECT DISTINCT a.id,name,COALESCE(s.score,SUM(p.score)) AS score,COALESCE(s.size,SUM(p.size)) AS size,s.members,count(*) AS kmem,
+       COALESCE(SUM(p.score),-1) AS kscore, COALESCE(SUM(p.size),-1) AS ksize, COALESCE(SUM(p.xp),-1) AS kxp,COALESCE(SUM(p.value),-1) AS kvalue,
+       COALESCE(s.score/LEAST(s.members,60),-1) AS scavg, COALESCE(AVG(p.score)::int,-1) AS kscavg, COALESCE(s.size/s.members,-1) AS siavg,
+       COALESCE(AVG(p.size)::int,-1) AS ksiavg, COALESCE(AVG(p.xp)::int,-1) AS kxpavg, COALESCE(AVG(p.value)::int,-1) AS kvalavg
+FROM alliances a 
+       LEFT OUTER JOIN (SELECT * FROM alliance_stats WHERE tick = (SELECT max(tick) FROM alliance_stats)) s ON s.id = a.id
+       LEFT OUTER JOIN current_planet_stats p ON p.alliance_id = a.id
+GROUP BY a.id,a.name,s.score,s.size,s.members
+ORDER BY $order
+               })or $error .= $DBH->errstr;
+       $query->execute or $error .= $DBH->errstr;
+       my @alliances;
+       my $i = 0;
+       while (my $alliance = $query->fetchrow_hashref){
+               next if ($alliance->{score} < 1 && $alliance->{kscore} < 1);
+               $alliance->{ODD} = $i % 2;
+               push @alliances, $alliance;
+               $i++;
+       }
+       $BODY->param(Alliances => \@alliances);
+}
+$BODY->param(Error => $error);
+1;
index b41a889c5c5979cdd77fadb54977c524ceaf2542..e557be179b6bc51e6c3e03f71008c86e4dacf9ac 100755 (executable)
--- a/index.pl
+++ b/index.pl
@@ -64,7 +64,7 @@ while (my ($name,$attack,$gid) = $query->fetchrow()){
 our $LOG = $DBH->prepare('INSERT INTO log (uid,text) VALUES(?,?)');
 
 my $page = 'main';
-if (param('page') =~ /^(main|check|motd|points|covop|top100|launchConfirmation|addintel|defrequest|raids|editRaid|calls|intel|users)$/){
+if (param('page') =~ /^(main|check|motd|points|covop|top100|launchConfirmation|addintel|defrequest|raids|editRaid|calls|intel|users|alliances)$/){
        $page = $1;
 }
 
diff --git a/templates/alliances.tmpl b/templates/alliances.tmpl
new file mode 100644 (file)
index 0000000..85f84ff
--- /dev/null
@@ -0,0 +1,106 @@
+<TMPL_VAR NAME=Error>
+<TMPL_IF Alliance>
+<form action="/index.pl" method="post"><fieldset> <legend>Alliances: </legend>
+       <input type="hidden" name="page" value="alliances"/>
+       <input type="hidden" name="cmd" value="change"/>
+       <input type="hidden" name="alliance" value="<TMPL_VAR NAME=Id>"/>
+       <p>Alliance: <TMPL_VAR NAME=Alliance></p>
+       <p>Relationship: <select name="relationship">
+               <TMPL_LOOP Relationships>
+                       <option value="<TMPL_VAR NAME=Rel>" <TMPL_IF NAME=Selected>selected="selected"</TMPL_IF>><TMPL_VAR NAME=Rel></option>
+               </TMPL_LOOP>
+               </select>
+               <input type="checkbox" name="crelationship"/></p>
+       <p><input type="submit" value="Submit"/></p>
+</fieldset></form>
+<div class="leftinfo">
+<table>
+       <tr>
+               <th><a href="index.pl?page=alliances&amp;alliance=<TMPL_VAR NAME=Id>&amp;order=coords">Coords</a></th>
+               <th>Nick</th>
+               <th><a href="index.pl?page=alliances&amp;alliance=<TMPL_VAR NAME=Id>&amp;order=hit_us">Hit us</a></th>
+               <th>Planet</th>
+               <th><a href="index.pl?page=alliances&amp;alliance=<TMPL_VAR NAME=Id>&amp;order=race">Race</a></th>
+               <th><a href="index.pl?page=alliances&amp;alliance=<TMPL_VAR NAME=Id>&amp;order=size">Size (rank)</a></th>
+               <th><a href="index.pl?page=alliances&amp;alliance=<TMPL_VAR NAME=Id>&amp;order=score">Score (rank)</a></th>
+               <th><a href="index.pl?page=alliances&amp;alliance=<TMPL_VAR NAME=Id>&amp;order=value">Value (rank)</a></th>
+               <th><a href="index.pl?page=alliances&amp;alliance=<TMPL_VAR NAME=Id>&amp;order=xp">XP (rank)</a></th>
+       </tr>
+       <TMPL_LOOP Members>
+       <tr class="<TMPL_IF ODD>odd<TMPL_ELSE>even</TMPL_IF>">
+               <td><a href="index.pl?page=check&amp;coords=<TMPL_VAR NAME=Coords>"><TMPL_VAR NAME=Coords></a></td>
+               <td class="<TMPL_VAR NAME=Planet_status>"><a href="index.pl?page=intel&amp;coords=<TMPL_VAR NAME=Coords>"><TMPL_VAR NAME=Nick>(<TMPL_VAR NAME=Planet_status>)</a></td>
+               <td><TMPL_VAR NAME=hit_us></td>
+               <td><TMPL_VAR NAME=ruler> OF <TMPL_VAR NAME=planet></td>
+               <td><TMPL_VAR NAME=race></td>
+               <td><TMPL_VAR NAME=size> (<TMPL_VAR NAME=sizerank>)</td>
+               <td><TMPL_VAR NAME=score> (<TMPL_VAR NAME=scorerank>)</td>
+               <td><TMPL_VAR NAME=value> (<TMPL_VAR NAME=valuerank>)</td>
+               <td><TMPL_VAR NAME=xp> (<TMPL_VAR NAME=xprank>)</td>
+       </tr>
+       </TMPL_LOOP>
+</table>
+</div>
+<div class="leftinfo">
+<form action="index.pl" method="post"><fieldset> <legend>Coords</legend>
+       <input type="hidden" name="page" value="alliances"/>
+       <input type="hidden" name="cmd" value="coords"/>
+       <input type="hidden" name="alliance" value="<TMPL_VAR NAME=Id>"/>
+       <p>Paste a list of coords that you want to add to this alliance</p>
+       <textarea rows="25" cols="40" name="coords"></textarea>
+       <p><input type="submit" value="Submit"/></p>
+</fieldset></form>
+</div>
+<div class="leftinfo">
+<table>
+       <tr>
+               <th>Alliance</th><th>Sender</th>
+               <th>Mission</th><th>Landing tick</th><th>ETA</th><th>Amount</th>
+               <th>Target</th><th>Alliance</th>
+               <th>Ingal</th><th>Reported by</th>
+       </tr>
+       <TMPL_LOOP Intel>
+       <tr class="<TMPL_IF ODD>odd<TMPL_ELSE>even</TMPL_IF>">
+       <td><a href="index.pl?page=intel&amp;coords=<TMPL_VAR NAME=Origin>"><TMPL_VAR NAME=Origin></a></td>
+       <td><TMPL_VAR NAME=OAlliance></td>
+       <td class="<TMPL_VAR NAME=MissionClass>"><TMPL_VAR NAME=Mission></td>
+       <td align="center"><TMPL_VAR NAME=landingtick></td>
+       <td align="center"><TMPL_VAR NAME=ETA></td>
+       <td><TMPL_VAR NAME=Amount></td>
+       <td><a href="index.pl?page=intel&amp;coords=<TMPL_VAR NAME=Target>"><TMPL_VAR NAME=Target></a></td>
+       <td><TMPL_VAR NAME=TAlliance></td>
+       <td><TMPL_VAR NAME=Ingal></td>
+       <td><TMPL_VAR NAME=Username></td>
+       </tr>
+       </TMPL_LOOP>
+</table>
+</div>
+
+<TMPL_ELSE>
+<table>
+       <tr>
+               <th>User</th>
+               <th>Known members</th>
+               <th>Total members</th>
+               <th><a href="index.pl?page=alliances&amp;order=score">Score</a> (<a href="index.pl?page=alliances&amp;order=kscore">known planets</a>)</th>
+               <th><a href="index.pl?page=alliances&amp;order=scavg">Avg Score</a> (<a href="index.pl?page=alliances&amp;order=kscavg">known planets</a>)</th>
+               <th><a href="index.pl?page=alliances&amp;order=size">Size</a> (<a href="index.pl?page=alliances&amp;order=ksize">known planets</a>)</th>
+               <th><a href="index.pl?page=alliances&amp;order=siavg">Avg Size</a> (<a href="index.pl?page=alliances&amp;order=ksiavg">known planets</a>)</th>
+               <th><a href="index.pl?page=alliances&amp;order=kxp">XP</a> (<a href="index.pl?page=alliances&amp;order=kxpavg">avg</a>)</th>
+               <th><a href="index.pl?page=alliances&amp;order=kvalue">Value</a> (<a href="index.pl?page=alliances&amp;order=kvalavg">avg</a>)</th>
+       </tr>
+<TMPL_LOOP Alliances>
+       <tr class="<TMPL_IF ODD>odd<TMPL_ELSE>even</TMPL_IF>">
+               <td><a href="index.pl?page=alliances&amp;alliance=<TMPL_VAR NAME=Id>"><TMPL_VAR NAME=Name></a></td>
+               <td><TMPL_VAR NAME=kmem></td>
+               <td><TMPL_VAR NAME=members></td>
+               <td><TMPL_VAR NAME=score> (<TMPL_VAR NAME=kscore>)</td>
+               <td><TMPL_VAR NAME=scavg> (<TMPL_VAR NAME=kscavg>)</td>
+               <td><TMPL_VAR NAME=size> (<TMPL_VAR NAME=ksize>)</td>
+               <td><TMPL_VAR NAME=siavg> (<TMPL_VAR NAME=ksiavg>)</td>
+               <td><TMPL_VAR NAME=kxp> (<TMPL_VAR NAME=kxpavg>)</td>
+               <td><TMPL_VAR NAME=kvalue> (<TMPL_VAR NAME=kvalavg>)</td>
+       </tr>
+</TMPL_LOOP>
+</table>
+</TMPL_IF>