]> ruin.nu Git - ndwebbie.git/commitdiff
Converted alliance-resources
authorMichael Andreen <harv@ruin.nu>
Tue, 24 Jun 2008 23:16:45 +0000 (01:16 +0200)
committerMichael Andreen <harv@ruin.nu>
Tue, 24 Jun 2008 23:16:45 +0000 (01:16 +0200)
NDWeb/Pages/Resources.pm [deleted file]
database/group_roles.sql
lib/NDWeb.pm
lib/NDWeb/Controller/Alliances.pm [new file with mode: 0644]
root/lib/site/leftbar.tt2
root/src/alliances/resources.tt2 [new file with mode: 0644]
t/controller_Alliances.t [new file with mode: 0644]
templates/resources.tmpl [deleted file]

diff --git a/NDWeb/Pages/Resources.pm b/NDWeb/Pages/Resources.pm
deleted file mode 100644 (file)
index f06620d..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-#**************************************************************************
-#   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.         *
-#**************************************************************************/
-
-package NDWeb::Pages::Resources;
-use strict;
-use warnings FATAL => 'all';
-use CGI qw/:standard/;
-use NDWeb::Include;
-
-use base qw/NDWeb::XMLPage/;
-
-$NDWeb::Page::PAGES{resources} = __PACKAGE__;
-
-sub render_body {
-       my $self = shift;
-       my ($BODY) = @_;
-       $self->{TITLE} = 'Alliance Resources';
-       my $DBH = $self->{DBH};
-       my $error;
-
-       return $self->noAccess unless $self->isHC;
-
-       my $order = "respplanet DESC";
-       if (defined param('order') && param('order') =~ /^(size|score|resources|respplanet|nscore|nscore2|nscore3)$/){
-               $order = "$1 DESC";
-       }
-
-
-       my $query = $DBH->prepare(qq{
-               SELECT a.id,a.name,a.relationship,s.members,s.score,s.size,r.resources,r.planets, resources/planets AS respplanet, 
-               resources / 300 AS scoregain, score + (resources / 300) AS nscore, 
-               (resources/planets*LEAST(members,60))/300 AS scoregain2, score + (resources/planets*LEAST(members,60))/300 AS nscore2,
-               (s.size::int8*(1464-tick())*250)/100 + score + (resources/planets*LEAST(members,60))/300 AS nscore3,
-               (s.size::int8*(1464-tick())*250)/100 AS scoregain3
-               FROM (SELECT alliance_id AS id,sum(metal+crystal+eonium) AS resources, count(*) AS planets 
-               FROM planets p join planet_scans c ON p.id = c.planet GROUP by alliance_id) r 
-               NATURAL JOIN alliances a 
-               LEFT OUTER JOIN (SELECT * FROM alliance_stats WHERE tick = (SELECT max(tick) FROM alliance_stats)) s ON a.id = s.id
-               ORDER BY $order
-               });
-       $query->execute;
-       my @alliances;
-       while (my $alliance = $query->fetchrow_hashref){
-               push @alliances,$alliance;
-       }
-       $BODY->param(Alliances => \@alliances);
-
-       $BODY->param(Error => $error);
-       return $BODY;
-}
-1;
index 20efb1fe7c3f987b1a6f6cfbcd54d83d6597a2c3..fc50bef26848e1d72b0f5c25568acb7774f404c1 100644 (file)
@@ -4,7 +4,8 @@ CREATE TABLE roles (
 
 CREATE TABLE group_roles (
        gid INTEGER REFERENCES groups(gid),
-       role VARCHAR(32) REFERENCES roles(role)
+       role VARCHAR(32) REFERENCES roles(role),
+       PRIMARY KEY(gid,role)
 );
 
 INSERT INTO roles VALUES('member_menu');
@@ -16,6 +17,7 @@ INSERT INTO roles VALUES('attack_menu');
 INSERT INTO roles VALUES('no_fleet_update');
 INSERT INTO roles VALUES('admin_users');
 INSERT INTO roles VALUES('rankings_planet_intel');
+INSERT INTO roles VALUES('alliances_resources');
 
 INSERT INTO group_roles (gid,role) VALUES(2,'member_menu');
 INSERT INTO group_roles (gid,role) VALUES(2,'attack_menu');
@@ -30,6 +32,7 @@ INSERT INTO group_roles (gid,role) VALUES(1,'hc_menu');
 INSERT INTO group_roles (gid,role) VALUES(1,'intel_menu');
 INSERT INTO group_roles (gid,role) VALUES(1,'admin_users');
 INSERT INTO group_roles (gid,role) VALUES(1,'rankings_planet_intel');
+INSERT INTO group_roles (gid,role) VALUES(1,'alliances_resources');
 
 INSERT INTO group_roles (gid,role) VALUES(3,'dc_menu');
 INSERT INTO group_roles (gid,role) VALUES(3,'bc_menu');
@@ -37,3 +40,4 @@ INSERT INTO group_roles (gid,role) VALUES(3,'hc_menu');
 INSERT INTO group_roles (gid,role) VALUES(3,'intel_menu');
 INSERT INTO group_roles (gid,role) VALUES(3,'admin_users');
 INSERT INTO group_roles (gid,role) VALUES(3,'rankings_planet_intel');
+INSERT INTO group_roles (gid,role) VALUES(3,'alliances_resources');
index 8cc8388798f1d2dec3b48f1d2fd483be1b41c890..85cbae140bb2afb9bfa31bf382c75c1058f0747b 100644 (file)
@@ -54,6 +54,7 @@ __PACKAGE__->setup(qw/
        /);
 
 __PACKAGE__->deny_access_unless('/users',[qw/admin_users/]);
+__PACKAGE__->deny_access_unless('/alliances/resources',[qw/alliances_resources/]);
 
 =head1 NAME
 
diff --git a/lib/NDWeb/Controller/Alliances.pm b/lib/NDWeb/Controller/Alliances.pm
new file mode 100644 (file)
index 0000000..e82f542
--- /dev/null
@@ -0,0 +1,80 @@
+package NDWeb::Controller::Alliances;
+
+use strict;
+use warnings;
+use parent 'Catalyst::Controller';
+
+=head1 NAME
+
+NDWeb::Controller::Alliances - Catalyst Controller
+
+=head1 DESCRIPTION
+
+Catalyst Controller.
+
+=head1 METHODS
+
+=cut
+
+
+=head2 index 
+
+=cut
+
+sub index :Path :Args(0) {
+    my ( $self, $c ) = @_;
+
+    $c->response->body('Matched NDWeb::Controller::Alliances in Alliances.');
+}
+
+sub resources : Local {
+    my ( $self, $c, $order ) = @_;
+       my $dbh = $c->model;
+
+       if (defined $order && $order =~ /^(size|score|resources|hidden|resplanet|hidplanet|nscore|nscore2|nscore3)$/){
+               $order = "$1 DESC";
+       }else{
+               $order = "resplanet DESC";
+       }
+
+       my $query = $dbh->prepare(qq{
+               SELECT a.id,a.name,a.relationship,s.members,s.score,s.size
+               ,r.resources,r.hidden,r.planets
+               ,(resources/planets)::bigint AS resplanet
+               ,(hidden/planets)::bigint AS hidplanet
+               ,((resources / 300) + (hidden / 100))::bigint AS scoregain
+               ,(score + (resources / 300) + (hidden / 100))::bigint AS nscore
+               ,((resources/planets*scoremem)/300 + (hidden/planets*scoremem)/100)::bigint AS scoregain2
+               ,(score + (resources/planets*scoremem)/300
+                       + (hidden/planets*scoremem)/100)::bigint AS nscore2
+               ,((s.size::int8*(1400-tick())*250)/100 + score + (resources/planets*scoremem)/300
+                       + (hidden/planets*scoremem)/100)::bigint AS nscore3
+               ,(s.size::int8*(1400-tick())*250)/100 AS scoregain3
+               FROM (SELECT alliance_id AS id,sum(metal+crystal+eonium) AS resources, sum(hidden) AS hidden, count(*) AS planets 
+               FROM planets p join planet_scans c ON p.id = c.planet GROUP by alliance_id) r 
+                       NATURAL JOIN alliances a 
+                       LEFT OUTER JOIN (SELECT *,LEAST(members,60) AS scoremem FROM alliance_stats
+                               WHERE tick = (SELECT max(tick) FROM alliance_stats)) s ON a.id = s.id
+               ORDER BY $order
+               });
+       $query->execute;
+       my @alliances;
+       while (my $alliance = $query->fetchrow_hashref){
+               push @alliances,$alliance;
+       }
+       $c->stash(alliances => \@alliances);
+}
+
+
+=head1 AUTHOR
+
+A clever guy
+
+=head1 LICENSE
+
+This library is free software, you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+1;
index a456fb9268934fe1f40e3ca5142dc50875c0aca8..be71cf35b7159dd43699802e9115f894323f12df 100644 (file)
@@ -91,7 +91,7 @@
        <li><a href="/alliances">Alliances</a></li>
        <li><a href="/hostileAlliances">Hostile Alliances</a></li>
        <li><a href="/memberIntel">Member Intel</a></li>
-       <li><a href="/resources">Alliance resources</a></li>
+       <li><a href="/alliances/resources">Alliance resources</a></li>
        <li><a href="/planetNaps">Planet Naps</a></li>
        <li><a href="/mail">Mail</a></li>
 </ul>
diff --git a/root/src/alliances/resources.tt2 b/root/src/alliances/resources.tt2
new file mode 100644 (file)
index 0000000..dec56a9
--- /dev/null
@@ -0,0 +1,34 @@
+[% META title = 'Alliance resources' %]
+<table>
+       <tr>
+               <th>Name</th><th>Relationship</th><th>Members (in res calc)</th>
+               <th><a href="[% c.uri_for('resources','score') %]">Score</a></th>
+               <th><a href="[% c.uri_for('resources','size') %]">Size</a></th>
+               <th><a href="[% c.uri_for('resources','resources') %]">Resources</a></th>
+               <th><a href="[% c.uri_for('resources','hidden') %]">Hidden resources</a></th>
+               <th><a href="[% c.uri_for('resources','resplanet') %]">Resources / Planet</a></th>
+               <th><a href="[% c.uri_for('resources','hidplanet') %]">Hidden Resources / Planet</a></th>
+               <th><a href="[% c.uri_for('resources','nscore') %]">Res score (gain)</a>[1]</th>
+               <th><a href="[% c.uri_for('resources','nscore2') %]">Avg res score (gain)</a>[2]</th>
+               <th><a href="[% c.uri_for('resources','nscore3') %]">Estimated score</a>[3]</th>
+       </tr>
+[% FOR a IN alliances %]
+       <tr class="[% loop.count % 2 == 0 ? 'even' : 'odd' %]">
+               <td><a href="[% c.uri_for("edit",a.id) %]">[% a.name %]</a></td>
+               <td class="[% a.relationship %]">[% a.relationship %]</td>
+               <td>[% a.members %] ([% a.planets %])</td>
+               <td>[% a.score %]</td>
+               <td>[% a.size %]</td>
+               <td>[% a.resources %]</td>
+               <td>[% a.hidden %]</td>
+               <td>[% a.resplanet %]</td>
+               <td>[% a.hidplanet %]</td>
+               <td>[% a.nscore %] ([% a.scoregain %])</td>
+               <td>[% a.nscore2 %] ([% a.nscore2 - a.score %])</td>
+               <td>[% a.nscore2 %] ([% a.nscore3 - a.score %])</td>
+       </tr>
+[% END %]
+</table>
+<p>[1] = New score if resources are spent (gain is just the extra value compared to unspent resources) + hidden production getting released</p>
+<p>[2] = Same as [1] but calculated with avg resources and max 60 members</p>
+<p>[3] = Same as [2] but also includes resources if roids are kept to end of round (gain is just from the roids)</p>
diff --git a/t/controller_Alliances.t b/t/controller_Alliances.t
new file mode 100644 (file)
index 0000000..9c46641
--- /dev/null
@@ -0,0 +1,10 @@
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+BEGIN { use_ok 'Catalyst::Test', 'NDWeb' }
+BEGIN { use_ok 'NDWeb::Controller::Alliances' }
+
+ok( request('/alliances')->is_success, 'Request should succeed' );
+
+
diff --git a/templates/resources.tmpl b/templates/resources.tmpl
deleted file mode 100644 (file)
index 69132f3..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-<TMPL_VAR NAME=Error>
-<table>
-       <tr>
-               <th>Name</th><th>Relationship</th><th>Members (in res calc)</th>
-               <th><a href="/<TMPL_VAR NAME=PAGE>?order=score">Score</a></th>
-               <th><a href="/<TMPL_VAR NAME=PAGE>?order=size">Size</a></th>
-               <th><a href="/<TMPL_VAR NAME=PAGE>?order=resources">Resources</a></th>
-               <th><a href="/<TMPL_VAR NAME=PAGE>?order=respplanet">Resources / Planet</a></th>
-               <th><a href="/<TMPL_VAR NAME=PAGE>?order=nscore">Res score (gain)</a>[1]</th>
-               <th><a href="/<TMPL_VAR NAME=PAGE>?order=nscore2">Avg res score (gain)</a>[2]</th>
-               <th><a href="/<TMPL_VAR NAME=PAGE>?order=nscore3">Estimated score</a>[3]</th>
-       </tr>
-<TMPL_LOOP Alliances>
-       <tr class="<TMPL_IF __odd__>odd<TMPL_ELSE>even</TMPL_IF>">
-               <td><a href="alliances?alliance=<TMPL_VAR NAME=Id>"><TMPL_VAR NAME=Name></a></td>
-               <td class="<TMPL_VAR NAME=relationship>"><TMPL_VAR NAME=relationship></td>
-               <td><TMPL_VAR NAME=members> (<TMPL_VAR NAME=planets>)</td>
-               <td><TMPL_VAR NAME=score></td>
-               <td><TMPL_VAR NAME=size></td>
-               <td><TMPL_VAR NAME=resources></td>
-               <td><TMPL_VAR NAME=respplanet></td>
-               <td><TMPL_VAR NAME=nscore> (<TMPL_VAR NAME=scoregain>)</td>
-               <td> <TMPL_VAR NAME=nscore2> (<TMPL_VAR NAME=scoregain2>)</td>
-               <td> <TMPL_VAR NAME=nscore3> (<TMPL_VAR NAME=scoregain3>)</td>
-       </tr>
-</TMPL_LOOP>
-</table>
-<p>[1] = New score if resources are spent (gain is just the extra value compared to unspent resources)</p>
-<p>[2] = Same as [1] but calculated with avg resources and max 60 members</p>
-<p>[3] = Same as [2] but also includes resources if roids are kept to end of round (gain is just from the roids)</p>