From 00325acb2f3a1ae09e4a73d09a33f056dfb60fda Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Wed, 25 Jun 2008 01:16:45 +0200 Subject: [PATCH] Converted alliance-resources --- NDWeb/Pages/Resources.pm | 67 -------------------------- database/group_roles.sql | 6 ++- lib/NDWeb.pm | 1 + lib/NDWeb/Controller/Alliances.pm | 80 +++++++++++++++++++++++++++++++ root/lib/site/leftbar.tt2 | 2 +- root/src/alliances/resources.tt2 | 34 +++++++++++++ t/controller_Alliances.t | 10 ++++ templates/resources.tmpl | 30 ------------ 8 files changed, 131 insertions(+), 99 deletions(-) delete mode 100644 NDWeb/Pages/Resources.pm create mode 100644 lib/NDWeb/Controller/Alliances.pm create mode 100644 root/src/alliances/resources.tt2 create mode 100644 t/controller_Alliances.t delete mode 100644 templates/resources.tmpl diff --git a/NDWeb/Pages/Resources.pm b/NDWeb/Pages/Resources.pm deleted file mode 100644 index f06620d..0000000 --- a/NDWeb/Pages/Resources.pm +++ /dev/null @@ -1,67 +0,0 @@ -#************************************************************************** -# Copyright (C) 2006 by Michael Andreen * -# * -# 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; diff --git a/database/group_roles.sql b/database/group_roles.sql index 20efb1f..fc50bef 100644 --- a/database/group_roles.sql +++ b/database/group_roles.sql @@ -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'); diff --git a/lib/NDWeb.pm b/lib/NDWeb.pm index 8cc8388..85cbae1 100644 --- a/lib/NDWeb.pm +++ b/lib/NDWeb.pm @@ -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 index 0000000..e82f542 --- /dev/null +++ b/lib/NDWeb/Controller/Alliances.pm @@ -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; diff --git a/root/lib/site/leftbar.tt2 b/root/lib/site/leftbar.tt2 index a456fb9..be71cf3 100644 --- a/root/lib/site/leftbar.tt2 +++ b/root/lib/site/leftbar.tt2 @@ -91,7 +91,7 @@
  • Alliances
  • Hostile Alliances
  • Member Intel
  • -
  • Alliance resources
  • +
  • Alliance resources
  • Planet Naps
  • Mail
  • diff --git a/root/src/alliances/resources.tt2 b/root/src/alliances/resources.tt2 new file mode 100644 index 0000000..dec56a9 --- /dev/null +++ b/root/src/alliances/resources.tt2 @@ -0,0 +1,34 @@ +[% META title = 'Alliance resources' %] + + + + + + + + + + + + + +[% FOR a IN alliances %] + + + + + + + + + + + + + + +[% END %] +
    NameRelationshipMembers (in res calc)ScoreSizeResourcesHidden resourcesResources / PlanetHidden Resources / PlanetRes score (gain)[1]Avg res score (gain)[2]Estimated score[3]
    [% a.name %][% a.relationship %][% a.members %] ([% a.planets %])[% a.score %][% a.size %][% a.resources %][% a.hidden %][% a.resplanet %][% a.hidplanet %][% a.nscore %] ([% a.scoregain %])[% a.nscore2 %] ([% a.nscore2 - a.score %])[% a.nscore2 %] ([% a.nscore3 - a.score %])
    +

    [1] = New score if resources are spent (gain is just the extra value compared to unspent resources) + hidden production getting released

    +

    [2] = Same as [1] but calculated with avg resources and max 60 members

    +

    [3] = Same as [2] but also includes resources if roids are kept to end of round (gain is just from the roids)

    diff --git a/t/controller_Alliances.t b/t/controller_Alliances.t new file mode 100644 index 0000000..9c46641 --- /dev/null +++ b/t/controller_Alliances.t @@ -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 index 69132f3..0000000 --- a/templates/resources.tmpl +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameRelationshipMembers (in res calc)ScoreSizeResourcesResources / PlanetRes score (gain)[1]Avg res score (gain)[2]Estimated score[3]
    () () () ()
    -

    [1] = New score if resources are spent (gain is just the extra value compared to unspent resources)

    -

    [2] = Same as [1] but calculated with avg resources and max 60 members

    -

    [3] = Same as [2] but also includes resources if roids are kept to end of round (gain is just from the roids)

    -- 2.39.2