]> ruin.nu Git - ndwebbie.git/blobdiff - lib/NDWeb/Controller/Members.pm
Use new race infrastructure for members stats
[ndwebbie.git] / lib / NDWeb / Controller / Members.pm
index f3fdedb0fea609cfe2217d1cf3a2996207f34f03..52bcb4c0f2a251304b6e7da4cebfb7e16e9b092e 100644 (file)
@@ -34,7 +34,7 @@ sub index : Path : Args(0) {
        $c->stash(u => $dbh->selectrow_hashref(q{SELECT pid AS planet,defense_points
                        ,attack_points,scan_points,humor_points
                        , (attack_points+defense_points+scan_points/20)::NUMERIC(5,1) as total_points
-                       , sms,rank,hostmask,call_if_needed,sms_note,defprio
+                       , rank,defprio
                FROM users_defprio WHERE uid = ?
                        },undef,$c->user->id)
        );
@@ -59,14 +59,6 @@ sub index : Path : Args(0) {
                        },undef,$c->user->planet)
        );
 
-       my $calls = $dbh->prepare(q{
-SELECT * FROM defcalls
-WHERE uid = $1 AND landing_tick >= tick()
-ORDER BY landing_tick DESC
-               });
-       $calls->execute($c->user->id);
-       $c->stash(calls => $calls->fetchall_arrayref({}) );
-
        $c->stash(fleets => member_fleets($dbh, $c->user->id,$c->user->planet));
 
        my $announcements = $dbh->prepare(q{SELECT ft.ftid, u.username,ft.subject,
@@ -78,32 +70,32 @@ ORDER BY landing_tick DESC
                        LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $1) ftv ON ftv.ftid = ft.ftid
                WHERE ft.fbid = 1
                GROUP BY ft.ftid, ft.subject,ft.sticky,u.username
-               HAVING count(NULLIF(COALESCE(ft.sticky OR fp.time > ftv.time,TRUE),FALSE)) >= 1
+               HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= 1
                ORDER BY sticky DESC,last_post DESC
                });
        $announcements->execute($c->user->id);
        $c->stash(announcements => $announcements->fetchall_arrayref({}) );
-}
-
-sub posthostupdate : Local {
-       my ( $self, $c ) = @_;
-       my $dbh = $c->model;
 
-       $dbh->do(q{UPDATE users SET hostmask = ? WHERE uid = ?
-               },undef, html_escape $c->req->param('hostname'), $c->user->id);
+       my ($attackgroups) = $dbh->selectrow_array(q{
+SELECT array_agg(gid) FROM groupmembers WHERE gid IN ('x','y','z') AND uid = $1
+               }, undef, $c->user->id);
+       $c->stash(attackgroups => $attackgroups);
 
-       $c->res->redirect($c->uri_for(''));
 }
 
-sub postsmsupdate : Local {
+sub postattackgroups : Local {
        my ( $self, $c ) = @_;
        my $dbh = $c->model;
 
-       my $callme = $c->req->param('callme') || 0;
-       $dbh->do(q{
-UPDATE users SET sms = $1, call_if_needed =  $2, sms_note = $3 WHERE uid = $4
-               },undef, html_escape $c->req->param('sms'),$callme
-               ,$c->req->param('smsnote'), $c->user->id);
+       my @groups = $c->req->param('class');
+       $dbh->do(q{DELETE FROM groupmembers WHERE gid IN ('x','y','z') AND gid <> ALL($1) AND uid = $2
+               },undef, \@groups, $c->user->id);
+
+       $dbh->do(q{INSERT INTO groupmembers (uid,gid) (
+               SELECT $2, gid FROM unnest($1::text[]) AS gid WHERE gid IN ('x','y','z')
+       EXCEPT
+               SELECT uid,gid FROM groupmembers WHERE uid = $2
+               )},undef, \@groups, $c->user->id);
 
        $c->res->redirect($c->uri_for(''));
 }
@@ -137,10 +129,11 @@ sub postowncoords : Local {
                        $c->flash(error => "No planet at coords: $x:$y:$z");
                }
        }else{
-               $c->flash(error => $c->req->param('planet') . " are not valid coords.");
+               my $error = $c->req->param('planet') . " are not valid coords.";
+               $c->flash(error => $error);
        }
 
-       $c->res->redirect($c->uri_for(''));
+       $c->res->redirect($c->uri_for('/'.$c->session->{referrer}));
 }
 
 sub postfleetsupdates : Local {
@@ -192,17 +185,19 @@ sub postircrequest : Local {
        my ( $self, $c ) = @_;
        my $dbh = $c->model;
 
+       my $message = $c->req->param('message');
        if ($c->req->param('channel')){
                my $query = $dbh->prepare(q{
 INSERT INTO irc_requests (uid,channel,message) VALUES($1,$2,$3)
                });
-               $query->execute($c->user->id,$c->req->param('channel'),$c->req->param('message'));
+               my $channel = $c->user->id,$c->req->param('channel');
+               $query->execute($channel, $message);
                $c->signal_bots;
 
-               $c->flash(reply => "Msg sent to: ".$c->req->param('channel'));
+               $c->flash(reply => "Msg sent to: ".$channel);
                $c->res->redirect($c->uri_for('ircrequest'));
        }else{
-               $c->stash(ircmessage => $c->req->param('message'));
+               $c->stash(ircmessage => $message);
                $c->go('ircrequest');
        }
 }
@@ -251,7 +246,7 @@ sub stats : Local {
        my $limit = 'LIMIT 10';
        $limit = '' if $c->check_user_roles(qw/members_points_nolimit/);
 
-       my ($races) = $dbh->selectrow_array(q{SELECT enum_range(null::race)::text[]});
+       my ($races) = $dbh->selectrow_array(q{SELECT array_agg(race ORDER BY race_id) FROM races WHERE race_id >= 0});
        $c->stash(races => $races);
        my $query = $dbh->prepare(q{
 SELECT nick
@@ -304,7 +299,8 @@ sub postintelmessage : Local {
 SELECT coords(x,y,z), tick() FROM current_planet_stats WHERE pid = $1
                }, undef, $c->user->planet);
 
-       $c->req->param(message => "[i]Posted by $coords at tick $tick [/i]\n\n" . $c->req->param('message'));
+       my $message = "[i]Posted by $coords at tick $tick [/i]\n\n" . $c->req->param('message');
+       $c->req->param(message => $message);
        $c->forward('/forum/insertThread',[12]);
        $c->forward('/forum/insertPost',[$c->stash->{thread}]);
        $c->flash(intelmessage => 1);
@@ -330,7 +326,7 @@ sub insertintel : Private {
                });
        my @scans;
        my $intel = $c->req->param('message');
-       while ($intel =~ m{http://[\w.]+/.+?scan(_id|_grp)?=(\w+)}g){
+       while ($intel =~ m{https?://[\w.]+/.+?scan(_id|_grp)?=(\w+)}g){
                my $groupscan = (defined $1 && $1 eq '_grp') || 0;
                my %scan;
                $scan{id} = $2;
@@ -357,13 +353,13 @@ sub insertintel : Private {
        });
        my @intel;
        while ($intel =~ m/(\d+):(\d+):(\d+)\*?\s+(\d+):(\d+):(\d+)
-               \*?\s+(.+)(?:Ter|Cat|Xan|Zik|Etd)?
-               \s+(\d+)\s+(Attack|Defend)\s+(\d+)/gx){
+               \*?\s+(A|D)\s+(.+?)\s+(?:(?:\w+)\s+)?(\d+)\s+(\d+)/gx){
                my $ingal = ($1 == $4 && $2 == $5) || 0;
                my $lt = $tick + $10;
                my $back = ($ingal ? $lt + 4 : undef);
+               my $mission = $7 eq 'A' ? 'Attack' : 'Defend';
                eval {
-                       $addintel->execute($7,$9,$lt,$1,$2,$3,$4,$5,$6,$tick,$10,$8
+                       $addintel->execute($8,$mission,$lt,$1,$2,$3,$4,$5,$6,$tick,$10,$9
                                ,$ingal,$back, $c->user->id);
                        push @intel,"Added $&";
                };
@@ -376,75 +372,6 @@ sub insertintel : Private {
        $c->flash(scans => \@scans);
 }
 
-sub addincs : Local {
-       my ( $self, $c ) = @_;
-       $c->stash(incs => $c->flash->{incs});
-
-}
-
-sub postincs : Local {
-       my ( $self, $c ) = @_;
-       my $dbh = $c->model;
-
-       my @incs;
-
-       my $user = $dbh->prepare(q{
-SELECT uid FROM users u
-WHERE pid = planetid($1,$2,$3,tick())
-       AND uid IN (SELECT uid FROM groupmembers WHERE gid = 'M')
-               });
-       my $call = $dbh->prepare(q{
-SELECT call
-FROM calls WHERE uid = $1 AND landing_tick = tick() + $2
-               });
-       my $fleet = $dbh->prepare(q{
-SELECT pid
-FROM incomings i
-WHERE pid = planetid($1,$2,$3,tick()) AND amount = $4 and fleet = $5 AND call = $6
-               });
-       my $irc = $dbh->prepare(q{
-INSERT INTO irc_requests (uid,channel,message) VALUES($1,'def',$2)
-               });
-
-       my $msg = $c->req->param('message');
-       while ($msg =~ /(\d+):(\d+):(\d+)\*?\s+(\d+):(\d+):(\d+)\s+([^:]*\S+)\s+(?:Ter|Cat|Xan|Zik|Etd)\s+([\d,]+)\s+Attack\s+(\d+)/gc
-                       || $msg =~ /(\d+):(\d+):(\d+)\s+(\d+):(\d+):(\d+)\s+\((?:Ter|Cat|Xan|Zik|Etd)\)\s+([^,]*\S+)\s+([\d,]+)\s+(\d+)\s+\(\d+\)/gc){
-
-               my $inc = {message => $&};
-               my $amount = $8;
-               {
-                       $amount =~ s/,//g;
-               }
-               try {
-                       my $uid = $dbh->selectrow_array($user,undef,$1,$2,$3);
-                       die '<i>No user with these coords</i>' unless $uid;
-
-                       my $call = $dbh->selectrow_array($call,undef,$uid,$9);
-                       if ($call){
-                               my $pid = $dbh->selectrow_hashref($fleet,undef,$4,$5,$6,$amount,$7,$call);
-                               die '<i>Duplicate</i>' if $pid;
-
-                       }
-
-                       $irc->execute($c->user->id, $inc->{message});
-                       $inc->{status} = '<b>Added</b>';
-
-               } catch {
-                       when (m(^(<i>.*</i>) at )){
-                               $inc->{status} = $1;
-                       }
-                       default {
-                               $inc->{status} = $_;
-                       }
-               };
-               push @incs, $inc;
-       }
-
-       $c->signal_bots if @incs;
-       $c->flash(incs => \@incs);
-       $c->res->redirect($c->uri_for('addincs'));
-}
-
 sub launchConfirmation : Local {
        my ( $self, $c ) = @_;
 
@@ -482,7 +409,8 @@ WHERE uid = $1 AND num = $2 AND back > tick()
                my $fullfleet = $dbh->prepare(q{INSERT INTO full_fleets
                                        (fid,uid) VALUES (?,?)});
                $dbh->begin_work;
-               my @missions = parseconfirmations($c->req->param('mission'), $c->stash->{TICK});
+               my $mission = $c->req->param('mission');
+               my @missions = parseconfirmations($mission, $c->stash->{TICK});
                for my $m (@missions){
                        if ($m->{mission} eq 'Return'){
                                $c->forward("addReturnFleet", [$m]);
@@ -492,7 +420,7 @@ WHERE uid = $1 AND num = $2 AND back > tick()
                                }else{
                                        $m->{pid} = $c->user->planet;
                                }
-                       }elsif ($m->{target} =~ /^(\d+):(\d+):(\d+)$/) {
+                       }elsif ($m->{target} ~~ /^(\d+):(\d+):(\d+)$/) {
                                $m->{pid} = $dbh->selectrow_array($findplanet,undef,$1,$2,$3);
                                unless ($m->{pid}){
                                        $m->{warning} = "No planet at $m->{target}, try again next tick.";
@@ -554,54 +482,84 @@ WHERE uid = $1 AND num = $2 AND back > tick()
 sub parseconfirmations {
        my ( $missions, $tick ) = @_;
        return unless $missions;
-       my @missions;
-       $missions =~ s/,//g;
+       my @slots;
+       $missions =~ s/\s?,\s?//g;
+       $missions =~ s/\s*([:+])\s*/$1/g;
+       $missions =~ s/\(\s/(/g;
+       $missions =~ s/\s\)/)/g;
+       my $returnetare = qr/(\d+) \s+
+               Arrival:\s*(\d+)/sx;
+       my $missionetare = qr/\s* (\d+ \+ \s*)? (\d+) \s+
+               Arrival:\s*(\d+) \s+
+               \QReturn ETA:\E\s*(?:(?<eta>Instant) \s+ Cancel \s+ Order
+                       | (?<eta>\d+) \s+ Ticks \s+ Recall \s+ Fleet)/sx;
+       my $etare = qr/(Galaxy:\d+Universe:\d+(?:Alliance:\d+)?
+               |$missionetare
+               |$returnetare)\s*/x;
+       my $missre = qr/((?:Alliance\ Standby)|(?:(?:Fake\ )?\w+))\s*/x;
        if ($missions =~ m/
-               Ships \s?\t Cla \s?\t T1 \s?\t T2 \s?\t T3 \s?\t Base\ \(i\) \s (?<name>.+?)\ \(i\) \s?\t (?<name>.+?)\ \(i\) \s?\t (?<name>.+?)\ \(i\) \s?\t TOTAL \W+
+               Ships \s+ Cla \s+ T\s?1 \s+ T\s?2 \s+ T\s?3 \s+ ETA \s+ Base \s+ \(i\) \s (?<name>.+?) \s+ \(i\) \s+ (?<name>.+?) \s+ \(i\) \s+ (?<name>.+?) \s+ \(i\) \s+ TOTAL \s+
                (?<ships>.+?)
-               \QTotal Ships in Fleet\E \s?\t (\d+) \s?\t (?<amount>\d+) \s?\t (?<amount>\d+) \s?\t (?<amount>\d+) \W+
-               Mission: \t (?<mission>\w*) \t (?<mission>\w*) \t (?<mission>\w*) \W+
-               Target: \t (?<targets>((\d+:\d+:\d+)?\t)*) \W+
-               \QLaunch Tick:\E \t (?<lts>(\d*\t)*) \W+
-               ETA: \t? (?<etas>([^\t]+\t?)*)
+               \QTotal Ships in Fleet\E \s+ (\d+) \s+ (?<amount>\d+) \s+ (?<amount>\d+) \s+ (?<amount>\d+) \s+
+               Mission: \s* (?<missions>(?:$missre)*)  \s*
+               Target: \s* (?<targets>((\d+:\d+:\d+)?\s)*) \s*
+               \QLaunch Tick:\E \s* (?<lts>(\d+\s+)*) \s*
+               ETA: \s* (?<etas>(?:$etare)*)
                /sx){
                my %match = %-;
-               my @etas = split /\t/, $+{etas};
-               my @targets = split /\t/, $+{targets};
-               my @lts = split /\t/, $+{lts};
+               my @targets = split /\s+/, $+{targets};
+               my @lts = split /\s+/, $+{lts};
+               my @etas;
+               local $_ = $+{etas};
+               while(/$etare/sxg){
+                       push @etas, $1;
+               }
+               my @missions ;
+               $_ = $+{missions};
+               while(/$missre/sxg){
+                       push @missions, $1;
+               }
                for my $i (0..2){
                        my %mission = (
                                name => $match{name}->[$i],
-                               mission => $match{mission}->[$i],
+                               mission => '' ,
                                amount => $match{amount}->[$i],
                                num => $i,
                                ships => []
                        );
-                       if      ($mission{amount} == 0){
-                               push @missions,\%mission;
+                       if ($mission{amount} == 0){
+                               push @slots,\%mission;
+                               next;
+                       }
+
+                       if ($missions[0] eq 'Alliance Standby'){
+                               shift @missions;
+                               push @slots,\%mission;
                                next;
                        }
 
-                       $mission{target} = shift @targets;
-                       $mission{lt} = shift @lts;
                        given(shift @etas){
-                               when(/^(\d+) (\s+ \(\+\d+\))? \W+
-                                               Arrival:\ (\d+) \W+
-                                               \QReturn ETA: \E(Instant|\d+)/sx){
+                               when(/$missionetare/sx){
                                        $mission{tick} = $3;
-                                       $mission{eta} = $1 + $4;
+                                       $mission{eta} = $2 + $+{eta};
                                        $mission{back} = $3 + $mission{eta} - 1;
+                                       $mission{target} = shift @targets;
+                                       $mission{lt} = shift @lts;
+                                       $mission{mission} = shift @missions;
                                }
-                               when(/^(\d+) \W+
-                                       Arrival:\ (\d+)/sx){
+                               when(/$returnetare/sx){
                                        $mission{tick} = $2;
                                        $mission{eta} = $1;
                                        $mission{back} = $2;
+                                       $mission{target} = shift @targets;
+                                       $mission{lt} = shift @lts;
+                                       $mission{mission} = shift @missions;
+                                       die "Did you forget some at the end? '$mission{mission}'" if $mission{mission} ne 'Return';
                                }
                        }
-                       push @missions,\%mission;
+                       push @slots,\%mission;
                }
-               push @missions,{
+               push @slots,{
                        name => 'Main',
                        num => 3,
                        mission => 'Full fleet',
@@ -609,18 +567,18 @@ sub parseconfirmations {
                        amount => 0,
                        ships => []
                };
-               while ($match{ships}->[0] =~ m/((?:\w+ )*\w+)\s+(FI|CO|FR|DE|CR|BS)[^\d]+([\d\s]+)/g){
+               while ($match{ships}->[0] =~ m/(\w[ \w]+?)\s+(FI|CO|FR|DE|CR|BS|--)[^\d]+\d+\s+((?:\d+\s*){5})/g){
                        my $ship = $1;
                        my @amounts = split /\D+/, $3;
-                       my $amount = shift @amounts;
-                       die "Ships don't sum up properly" if $amounts[3] != $amount + $amounts[0] + $amounts[1] + $amounts[2];
+                       my $base = shift @amounts;
+                       die "Ships don't sum up properly" if $amounts[3] != $base + $amounts[0] + $amounts[1] + $amounts[2];
                        for my $i (0..3){
-                               push @{$missions[$i]->{ships}},{ship => $ship, amount => $amounts[$i]} if $amounts[$i] > 0;
+                               push @{$slots[$i]->{ships}},{ship => $ship, amount => $amounts[$i]} if $amounts[$i] > 0;
                        }
-                       $missions[3]->{amount} += $amounts[3];
+                       $slots[3]->{amount} += $amounts[3];
                }
        }
-       return @missions;
+       return @slots;
 }
 
 sub findDuplicateFleet : Private {
@@ -630,8 +588,9 @@ sub findDuplicateFleet : Private {
        my $findfleet = $dbh->prepare(q{
 SELECT fid FROM fleets f
        LEFT JOIN launch_confirmations lc USING (fid)
-WHERE f.pid = (SELECT pid FROM users WHERE uid = $1) AND mission = $3 AND amount = $4 AND
-       COALESCE(uid = $1 AND num = $2 AND lc.pid = $5 AND landing_tick = $6, TRUE)
+WHERE f.pid = (SELECT pid FROM users WHERE uid = $1)
+       AND mission = $3 AND amount = $4 AND (mission <> 'Full fleet' OR tick > $6 - 6)
+       AND COALESCE(uid = $1 AND num = $2 AND lc.pid = $5 AND landing_tick = $6, TRUE)
                });
        my $fid = $dbh->selectrow_array($findfleet,undef,$c->user->id,$m->{num}
                ,$m->{mission},$m->{amount}, $m->{pid}, $m->{tick});
@@ -766,7 +725,7 @@ sub member_fleets {
                ,NULL AS recalled, mission
        FROM fleets f
        WHERE pid = $2 AND tick <= tick() AND tick >= tick() -  24
-               AND name IN ('Main','Advanced Unit') AND mission = 'Full fleet'
+               AND name IN ('Main', 'Advanced Unit', 'Military') AND mission = 'Full fleet'
        ORDER BY mission,name,tick DESC, fid DESC
 ) UNION (
        SELECT fid,name,landing_tick AS tick, eta, amount