]> ruin.nu Git - ndwebbie.git/commitdiff
Replaced intelquery function with a view
authorMichael Andreen <harv@ruin.nu>
Sat, 22 Aug 2009 17:51:40 +0000 (19:51 +0200)
committerMichael Andreen <harv@ruin.nu>
Sat, 22 Aug 2009 17:51:40 +0000 (19:51 +0200)
database/intel.sql [new file with mode: 0644]
lib/NDWeb/Controller/Alliances.pm
lib/NDWeb/Controller/Intel.pm
lib/NDWeb/Include.pm
root/src/alliances/edit.tt2
root/src/intel/index.tt2
root/src/intel/planet.tt2

diff --git a/database/intel.sql b/database/intel.sql
new file mode 100644 (file)
index 0000000..38098fc
--- /dev/null
@@ -0,0 +1,13 @@
+DROP VIEW IF EXISTS full_intel;
+CREATE VIEW full_intel AS
+SELECT s.alliance AS salliance ,coords(s.x,s.y,s.z) AS scoords, i.sender, s.nick AS snick
+       ,t.alliance AS talliance,coords(t.x,t.y,t.z) AS tcoords, i.target, t.nick AS tnick
+       ,i.mission, i.tick, MIN(i.eta) AS eta, i.amount, i.ingal
+       ,uid,u.username
+FROM intel i
+       JOIN users u USING (uid)
+       JOIN current_planet_stats t ON i.target = t.pid
+       JOIN current_planet_stats s ON i.sender = s.pid
+GROUP BY i.tick,i.mission,t.x,t.y,t.z,s.x,s.y,s.z,i.amount,i.ingal,u.username,uid
+       ,t.alliance,s.alliance,t.nick,s.nick,i.sender,i.target
+;
index 348884acaa1893c1b18335c37d42f8d10c79f79b..5f3372655474d454f355131c6ebd0c58d0390372 100644 (file)
@@ -4,8 +4,6 @@ use strict;
 use warnings;
 use parent 'Catalyst::Controller';
 
-use NDWeb::Include;
-
 =head1 NAME
 
 NDWeb::Controller::Alliances - Catalyst Controller
@@ -95,13 +93,15 @@ sub edit : Local {
        my $ticks = $c->req->param('ticks') || 48;
        $c->stash(showticks => $ticks);
 
-       $query = $dbh->prepare(intelquery q{
-                       o.alliance AS oalliance ,coords(o.x,o.y,o.z) AS ocoords, i.sender
-                       ,t.alliance AS talliance,coords(t.x,t.y,t.z) AS tcoords, i.target
-               },q{NOT ingal AND (o.alliance = $1 OR t.alliance = $1)
-                       AND (i.mission = 'Defend' OR i.mission = 'AllyDef')
-                       AND COALESCE( t.alliance != o.alliance, TRUE)
-                       AND i.tick > (tick() - $2)
+       $query = $dbh->prepare(q{
+SELECT salliance, scoords, sender, talliance, tcoords, target
+       ,mission, tick AS landingtick, eta, amount, ingal, username
+FROM full_intel
+WHERE NOT ingal AND (salliance = $1 OR talliance = $1)
+       AND (mission = 'Defend' OR mission = 'AllyDef')
+       AND COALESCE( talliance <> salliance, TRUE)
+       AND tick > (tick() - $2)
+ORDER BY tick DESC, mission
                });
        $query->execute($a->{name}, $ticks);
        $c->stash(intel => $query->fetchall_arrayref({}) );
index 015fe339d6b9ce1ed1672ccca0869dd4fe2bb589..bafea58ea8be45ad64da1f5450990e5b3193ad52 100644 (file)
@@ -31,13 +31,14 @@ sub index :Path : Args(0) {
        my $ticks = $c->req->param('ticks') || 48;
        $c->stash(showticks => $ticks);
 
-       my $query = $dbh->prepare(intelquery q{
-                       o.alliance AS oalliance ,coords(o.x,o.y,o.z) AS ocoords, i.sender
-                       ,t.alliance AS talliance,coords(t.x,t.y,t.z) AS tcoords, i.target
-               },q{not ingal
-                       AND ((COALESCE( t.alliance != o.alliance,TRUE) AND (i.mission = 'Defend' OR i.mission = 'AllyDef' ))
-                               OR ( t.alliance = o.alliance AND i.mission = 'Attack'))
-                       AND i.tick > (tick() - $1)
+       my $query = $dbh->prepare(q{
+SELECT salliance, scoords, sender, talliance, tcoords, target
+       ,mission, tick AS landingtick, eta, amount, ingal, username
+FROM full_intel
+WHERE NOT ingal AND tick > (tick() - $1)
+       AND ((COALESCE( talliance <> salliance,TRUE) AND (mission = 'Defend' OR mission = 'AllyDef' ))
+               OR ( talliance = salliance AND mission = 'Attack'))
+ORDER BY tick DESC, mission
                });
        $query->execute($ticks);
        $c->stash(intel => $query->fetchall_arrayref({}) );
@@ -82,15 +83,23 @@ sub planet : Local {
        $c->stash(govs => ["","Feu", "Dic", "Dem","Uni"]);
        $c->stash(planetstatus => ["","Friendly", "NAP", "Hostile"]);
 
-       $query = $dbh->prepare(intelquery q{i.sender
-                       ,o.alliance AS oalliance,coords(o.x,o.y,o.z) AS ocoords
-               },q{i.target = $1 AND i.tick > (tick() - $2)});
+       $query = $dbh->prepare(q{
+SELECT salliance, scoords, sender
+       ,mission, tick AS landingtick, eta, amount, ingal, username
+FROM full_intel
+WHERE target = $1 AND tick > (tick() - $2)
+ORDER BY tick DESC, mission
+               });
        $query->execute($id,$ticks);
        $c->stash(incoming => $query->fetchall_arrayref({}) );
 
-       $query = $dbh->prepare(intelquery q{i.target
-                       ,t.alliance AS talliance,coords(t.x,t.y,t.z) AS tcoords
-               },q{i.sender = $1 AND i.tick > (tick() - $2)});
+       $query = $dbh->prepare(q{
+SELECT talliance, tcoords, target
+       ,mission, tick AS landingtick, eta, amount, ingal, username
+FROM full_intel
+WHERE sender = $1 AND tick > (tick() - $2)
+ORDER BY tick DESC, mission
+               });
        $query->execute($id,$ticks);
        $c->stash(outgoing => $query->fetchall_arrayref({}) );
 
index 1cbcca9de3e09790280105192f16df6345ab1f2d..e619a228d7506402b232975eebf5ac6155d7f58c 100644 (file)
@@ -27,7 +27,7 @@ use CGI qw/:standard/;
 our @ISA = qw/Exporter/;
 
 our @EXPORT = qw/parseMarkup
-       intelquery html_escape
+       html_escape
        comma_value array_expand/;
 
 sub html_escape($) {
@@ -76,18 +76,6 @@ sub parseMarkup ($) {
        return $text;
 }
 
-sub intelquery {
-       my ($columns,$where) = @_;
-       return qq{
-SELECT $columns, i.mission, i.tick AS landingtick,MIN(i.eta) AS eta, i.amount, i.ingal, u.username
-FROM (intel i NATURAL JOIN users u)
-       JOIN current_planet_stats t ON i.target = t.pid
-       JOIN current_planet_stats o ON i.sender = o.pid
-WHERE $where
-GROUP BY i.tick,i.mission,t.x,t.y,t.z,o.x,o.y,o.z,i.amount,i.ingal,u.username,t.alliance,o.alliance,t.nick,o.nick,i.sender,i.target
-ORDER BY i.tick DESC, i.mission};
-}
-
 sub array_expand ($) {
        my ($array) = @_;
 
index 878fcfc9139862ff4b754b0d57ccceb0f455dc86..e7f6fefdace6295f6a3f256bdbc0f26525860d35 100644 (file)
@@ -71,8 +71,8 @@
        </tr>
 [% FOR i IN intel %]
        <tr class="[% loop.count % 2 == 0 ? 'even' : 'odd' %]">
-       <td>[% i.oalliance %]</td>
-       <td><a href="[% c.uri_for('/intel/planet',i.sender) %]">[% i.ocoords %]</a></td>
+       <td>[% i.salliance %]</td>
+       <td><a href="[% c.uri_for('/intel/planet',i.sender) %]">[% i.scoords %]</a></td>
        <td class="[% i.mission %]">[% i.mission %]</td>
        <td align="center">[% i.landingtick %]</td>
        <td align="center">[% i.eta %]</td>
index f4e067f437bd61ed72bcc1206c5c96a44767737b..73a7eba842517519764dab751ffa02d37516059b 100644 (file)
@@ -27,8 +27,8 @@
        </tr>
 [% FOR i IN intel %]
        <tr class="[% loop.count % 2 == 0 ? 'even' : 'odd' %]">
-       <td>[% i.oalliance %]</td>
-       <td><a href="[% c.uri_for('planet',i.sender) %]">[% i.ocoords %]</a></td>
+       <td>[% i.salliance %]</td>
+       <td><a href="[% c.uri_for('planet',i.sender) %]">[% i.scoords %]</a></td>
        <td class="[% i.mission %]">[% i.mission %]</td>
        <td align="center">[% i.landingtick %]</td>
        <td align="center">[% i.eta %]</td>
index 7d18e536dc6b495e2984f2fd84b404fc6b4cdfc2..4e5eb631185398fbd9ceb3e990edbc69e80d5340 100644 (file)
@@ -69,8 +69,8 @@
        </tr>
 [% FOR i IN incoming %]
        <tr class="[% loop.count % 2 == 0 ? 'even' : 'odd' %]">
-       <td>[% i.oalliance %]</td>
-       <td><a href="[% c.uri_for('planet',i.sender) %]">[% i.ocoords %]</a></td>
+       <td>[% i.salliance %]</td>
+       <td><a href="[% c.uri_for('planet',i.sender) %]">[% i.scoords %]</a></td>
        <td class="[% i.ingal ? 'ingal' : i.mission %]">[% i.mission %]</td>
        <td align="center">[% i.landingtick %]</td>
        <td align="center">[% i.eta %]</td>