]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Alliances.pm
e82f54270f32cf99a9ef6daa8b734f9842bdc5cc
[ndwebbie.git] / lib / NDWeb / Controller / Alliances.pm
1 package NDWeb::Controller::Alliances;
2
3 use strict;
4 use warnings;
5 use parent 'Catalyst::Controller';
6
7 =head1 NAME
8
9 NDWeb::Controller::Alliances - Catalyst Controller
10
11 =head1 DESCRIPTION
12
13 Catalyst Controller.
14
15 =head1 METHODS
16
17 =cut
18
19
20 =head2 index 
21
22 =cut
23
24 sub index :Path :Args(0) {
25     my ( $self, $c ) = @_;
26
27     $c->response->body('Matched NDWeb::Controller::Alliances in Alliances.');
28 }
29
30 sub resources : Local {
31     my ( $self, $c, $order ) = @_;
32         my $dbh = $c->model;
33
34         if (defined $order && $order =~ /^(size|score|resources|hidden|resplanet|hidplanet|nscore|nscore2|nscore3)$/){
35                 $order = "$1 DESC";
36         }else{
37                 $order = "resplanet DESC";
38         }
39
40         my $query = $dbh->prepare(qq{
41                 SELECT a.id,a.name,a.relationship,s.members,s.score,s.size
42                 ,r.resources,r.hidden,r.planets
43                 ,(resources/planets)::bigint AS resplanet
44                 ,(hidden/planets)::bigint AS hidplanet
45                 ,((resources / 300) + (hidden / 100))::bigint AS scoregain
46                 ,(score + (resources / 300) + (hidden / 100))::bigint AS nscore
47                 ,((resources/planets*scoremem)/300 + (hidden/planets*scoremem)/100)::bigint AS scoregain2
48                 ,(score + (resources/planets*scoremem)/300
49                         + (hidden/planets*scoremem)/100)::bigint AS nscore2
50                 ,((s.size::int8*(1400-tick())*250)/100 + score + (resources/planets*scoremem)/300
51                         + (hidden/planets*scoremem)/100)::bigint AS nscore3
52                 ,(s.size::int8*(1400-tick())*250)/100 AS scoregain3
53                 FROM (SELECT alliance_id AS id,sum(metal+crystal+eonium) AS resources, sum(hidden) AS hidden, count(*) AS planets 
54                 FROM planets p join planet_scans c ON p.id = c.planet GROUP by alliance_id) r 
55                         NATURAL JOIN alliances a 
56                         LEFT OUTER JOIN (SELECT *,LEAST(members,60) AS scoremem FROM alliance_stats
57                                 WHERE tick = (SELECT max(tick) FROM alliance_stats)) s ON a.id = s.id
58                 ORDER BY $order
59                 });
60         $query->execute;
61         my @alliances;
62         while (my $alliance = $query->fetchrow_hashref){
63                 push @alliances,$alliance;
64         }
65         $c->stash(alliances => \@alliances);
66 }
67
68
69 =head1 AUTHOR
70
71 A clever guy
72
73 =head1 LICENSE
74
75 This library is free software, you can redistribute it and/or modify
76 it under the same terms as Perl itself.
77
78 =cut
79
80 1;