X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=lib%2FNDWeb%2FController%2FMembers.pm;h=2f89142da1e7082ec467fb2fcf63661e03850454;hb=d6b15c14f164e7150ffcaed86e8aa79795b2d89a;hp=d5b634c5b80d57c682cd30c7ec8df9c1ed658f4a;hpb=8518049dc07ba0554aea4224d6c195bbacd9b8a6;p=ndwebbie.git diff --git a/lib/NDWeb/Controller/Members.pm b/lib/NDWeb/Controller/Members.pm index d5b634c..2f89142 100644 --- a/lib/NDWeb/Controller/Members.pm +++ b/lib/NDWeb/Controller/Members.pm @@ -83,12 +83,6 @@ ORDER BY landing_tick DESC }); $announcements->execute($c->user->id); $c->stash(announcements => $announcements->fetchall_arrayref({}) ); - - 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); - } sub posthostupdate : Local { @@ -101,23 +95,6 @@ sub posthostupdate : Local { $c->res->redirect($c->uri_for('')); } -sub postattackgroups : Local { - my ( $self, $c ) = @_; - my $dbh = $c->model; - - 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('')); -} - sub postsmsupdate : Local { my ( $self, $c ) = @_; my $dbh = $c->model; @@ -166,52 +143,6 @@ sub postowncoords : Local { $c->res->redirect($c->uri_for('')); } -sub postfleetupdate : Local { - my ( $self, $c ) = @_; - my $dbh = $c->model; - - my $fleet = $c->req->param('fleet'); - $fleet =~ s/,//g; - my $amount = 0; - my @ships; - while ($fleet =~ m/((?:[A-Z][a-z]+ )*[A-Z][a-z]+)\s+(\d+)/g){ - $amount += $2; - push @ships, [$1,$2]; - } - if ($amount){ - $dbh->begin_work; - eval{ - my $insert = $dbh->prepare(q{INSERT INTO fleets - (pid,name,mission,tick,amount) - VALUES (?,'Main','Full fleet',tick(),?) RETURNING fid}); - my ($id) = $dbh->selectrow_array($insert,undef - ,$c->user->planet,$amount); - $insert = $dbh->prepare(q{INSERT INTO fleet_ships - (fid,ship,amount) VALUES (?,?,?)}); - for my $s (@ships){ - unshift @{$s},$id; - $insert->execute(@{$s}); - } - $insert = $dbh->prepare(q{INSERT INTO full_fleets - (fid,uid) VALUES (?,?)}); - $insert->execute($id,$c->user->id); - $dbh->commit; - }; - if ($@){ - if ($@ =~ m/insert or update on table "fleet_ships" violates foreign key constraint "fleet_ships_ship_fkey"\s+DETAIL:\s+Key \(ship\)=\(([^)]+)\)/){ - $c->flash( error => "'$1' is NOT a valid ship"); - }else{ - $c->flash( error => $@); - } - $dbh->rollback; - } - }else{ - $c->flash( error => 'Fleet does not contain any ships'); - } - - $c->res->redirect($c->uri_for('')); -} - sub postfleetsupdates : Local { my ( $self, $c ) = @_; my $dbh = $c->model; @@ -445,6 +376,75 @@ 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 =~ /expand\s+(\d+):(\d+):(\d+)\*?\s+(\d+):(\d+):(\d+)\s+([^:]*\S+)\s+(?:Ter|Cat|Xan|Zik|Etd)\s+([\d,]+)\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 'No user with these coords' 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 'Duplicate' if $pid; + + } + + $irc->execute($c->user->id, $inc->{message}); + $inc->{status} = 'Added'; + + } catch { + when (m(^(.*) 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 ) = @_; @@ -457,7 +457,7 @@ sub postconfirmation : Local { my $dbh = $c->model; try { - my $findplanet = $dbh->prepare("SELECT planetid(?,?,?,?)"); + my $findplanet = $dbh->prepare(q{SELECT planetid(?,?,?,tick())}); my $addfleet = $dbh->prepare(q{INSERT INTO fleets (name,mission,pid,tick,amount) VALUES ($2,$3,(SELECT pid FROM users WHERE uid = $1),tick(),$4) @@ -467,7 +467,7 @@ sub postconfirmation : Local { UPDATE launch_confirmations SET back = $2 WHERE fid = $1 }); my $addconfirmation = $dbh->prepare(q{INSERT INTO launch_confirmations - (fid,uid,pid,landing_tick,eta,back) VALUES ($1,$2,$3,$4,$5,$6) + (fid,uid,pid,landing_tick,eta,back,num) VALUES ($1,$2,$3,$4,$5,$6,$7) }); my $addships = $dbh->prepare(q{INSERT INTO fleet_ships (fid,ship,amount) VALUES (?,?,?) @@ -475,44 +475,69 @@ UPDATE launch_confirmations SET back = $2 WHERE fid = $1 my $log = $dbh->prepare(q{INSERT INTO forum_posts (ftid,uid,message) VALUES( (SELECT ftid FROM users WHERE uid = $1),$1,$2) }); + my $return = $dbh->prepare(q{ +UPDATE launch_confirmations SET back = tick() +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}); for my $m (@missions){ if ($m->{mission} eq 'Return'){ $c->forward("addReturnFleet", [$m]); - $updatefleet->execute($m->{fid},$m->{back}) if $m->{fid}; - next; + if($m->{fid}){ + $updatefleet->execute($m->{fid},$m->{back}); + next; + }else{ + $m->{pid} = $c->user->planet; + } + }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."; + next; + } } - $m->{pid} = $dbh->selectrow_array($findplanet,undef,@{$m->{target}},$c->stash->{TICK}); - unless ($m->{pid}){ - $m->{warning} = "No planet at @{$m->{target}}, try again next tick."; + + #Recall fleets with same slot number + $return->execute($c->user->id,$m->{num}); + + unless ($m->{mission}){ + $m->{warning} = "Not on a mission, but matching fleets recalled"; next; } $c->forward("findDuplicateFleet", [$m]); if ($m->{match}){ - $m->{warning} = "Already confirmed this fleet, changing back to to match this paste"; - $updatefleet->execute($m->{fid},$m->{back}); + $m->{warning} = "Already confirmed this fleet, updating changed information"; + $updatefleet->execute($m->{fid},$m->{back}) if $m->{pid}; next; } + $m->{fleet} = $dbh->selectrow_array($addfleet,undef,$c->user->id,$m->{name} ,$m->{mission},$m->{amount}); + + if ($m->{mission} eq 'Full fleet'){ + $fullfleet->execute($m->{fleet},$c->user->id); + }else{ + $addconfirmation->execute($m->{fleet},$c->user->id,$m->{pid},$m->{tick},$m->{eta},$m->{back},$m->{num}); + } + if ($m->{mission} eq 'Attack'){ $c->forward("addAttackFleet", [$m]); }elsif ($m->{mission} eq 'Defend'){ $c->forward("addDefendFleet", [$m]); } - $addconfirmation->execute($m->{fleet},$c->user->id,$m->{pid},$m->{tick},$m->{eta},$m->{back}); - for my $ship (@{$m->{ships}}){ $addships->execute($m->{fleet},$ship->{ship},$ship->{amount}); } - $log->execute($c->user->id,"Pasted confirmation for $m->{mission} mission to @{$m->{target}}, landing tick $m->{tick}"); + $log->execute($c->user->id,"Pasted confirmation for $m->{mission} mission to $m->{target}, landing tick $m->{tick}"); } - $dbh->commit; $c->flash(missions => \@missions); + $dbh->commit; $c->signal_bots; } catch { $dbh->rollback; @@ -529,46 +554,97 @@ UPDATE launch_confirmations SET back = $2 WHERE fid = $1 sub parseconfirmations { my ( $missions, $tick ) = @_; return unless $missions; - my @missions; - while ($missions =~ m/\s*([^\n]+?)\s+(\d+):(\d+):(\d+)\s+(\d+):(\d+):(\d+) - \s+\((?:(\d+)\+)?(\d+)\).*?(?:\d+hrs\s+)?\d+mins?\s+ - (Attack|Defend|Return|Fake\ Attack|Fake\ Defend) - (.*?) - (?:Launching\ in\ tick\ (\d+),\ arrival\ in\ tick\ (\d+) - |ETA:\ \d+,\ Return\ ETA:\ (\d+) - |Return\ ETA:\ (\d+) - )/sgx){ - my $tick = $tick; - $tick += $9; - $tick += $8 if defined $8; - $tick = $13 if defined $13; - my $eta = $9; - $eta += $14 if defined $14; - my %mission = ( - name => $1, - mission => $10, - tick => $tick, - eta => $eta, - back => $10 eq 'Return' ? $tick : $tick + $eta - 1, - target => [$5,$6,$7], - from => [$2,$3,$4], - ); - my $ships = $11; - my @ships; - $mission{amount} = 0; - while ($ships =~ m/((?:\w+ )*\w+)\s+\w+\s+(?:(?:\w+|-)\s+){3}(?:Steal|Normal|Emp|Normal\s+Cloaked|Pod|Structure Killer)\s+(\d+)/g){ - $mission{amount} += $2; - push @ships,{ship => $1, amount => $2}; + 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:(\d+)/sx; + my $missionetare = qr/(\d+) (\s+ \(\+\d+\))? \s+ + Arrival:(\d+) \s+ + \QReturn ETA:\E\s*(?:(?Instant) \s+ Cancel \s+ Order + | (?\d+) \s+ Ticks \s+ Recall \s+ Fleet)/sx; + my $etare = qr/(Galaxy:\d+Universe:\d+(?:Alliance:\d+)? + |$missionetare + |$returnetare)\s*/x; + my $missre = qr/((?:Fake\ )?\w+)\s*/x; + if ($missions =~ m/ + Ships \s+ Cla \s+ T\s?1 \s+ T\s?2 \s+ T\s?3 \s+ Base \s+ \(i\) \s (?.+?) \s+ \(i\) \s+ (?.+?) \s+ \(i\) \s+ (?.+?) \s+ \(i\) \s+ TOTAL \s+ + (?.+?) + \QTotal Ships in Fleet\E \s+ (\d+) \s+ (?\d+) \s+ (?\d+) \s+ (?\d+) \s+ + Mission: \s* (?(?:$missre)*) \s* + Target: \s* (?((\d+:\d+:\d+)?\s)*) \s* + \QLaunch Tick:\E \s* (?(\d+\s+)*) \s* + ETA: \s* (?(?:$etare)*) + /sx){ + my %match = %-; + my @targets = split /\s+/, $+{targets}; + my @lts = split /\s+/, $+{lts}; + my @etas; + my $_ = $+{etas}; + while(/$etare/sxg){ + push @etas, $1; + } + my @missions ; + $_ = $+{missions}; + while(/$missre/sxg){ + push @missions, $1; } - $mission{ships} = \@ships; + for my $i (0..2){ + my %mission = ( + name => $match{name}->[$i], + mission => '' , + amount => $match{amount}->[$i], + num => $i, + ships => [] + ); + if ($mission{amount} == 0){ + push @slots,\%mission; + next; + } - if ($mission{amount} == 0){ - warn "No ships in: $ships"; - next; + given(shift @etas){ + when(/$missionetare/sx){ + $mission{tick} = $3; + $mission{eta} = $1 + $+{eta}; + $mission{back} = $3 + $mission{eta} - 1; + $mission{target} = shift @targets; + $mission{lt} = shift @lts; + $mission{mission} = shift @missions; + } + 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?' if $mission{mission} ne 'Return'; + } + } + push @slots,\%mission; + } + push @slots,{ + name => 'Main', + num => 3, + mission => 'Full fleet', + tick => $tick, + amount => 0, + ships => [] + }; + while ($match{ships}->[0] =~ m/(\w+)\s+(FI|CO|FR|DE|CR|BS)[^\d]+((?:\d+\s*){5})/g){ + my $ship = $1; + my @amounts = split /\D+/, $3; + 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 @{$slots[$i]->{ships}},{ship => $ship, amount => $amounts[$i]} if $amounts[$i] > 0; + } + $slots[3]->{amount} += $amounts[3]; } - push @missions,\%mission; } - return @missions; + return @slots; } sub findDuplicateFleet : Private { @@ -577,11 +653,12 @@ sub findDuplicateFleet : Private { my $findfleet = $dbh->prepare(q{ SELECT fid FROM fleets f - JOIN launch_confirmations lc USING (fid) -WHERE uid = $1 AND name = $2 AND mission = $3 AND amount = $4 - AND lc.pid = $5 AND landing_tick = $6 + LEFT JOIN launch_confirmations lc USING (fid) +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->{name} + my $fid = $dbh->selectrow_array($findfleet,undef,$c->user->id,$m->{num} ,$m->{mission},$m->{amount}, $m->{pid}, $m->{tick}); $c->forward("matchShips", [$m,$fid]); $m->{fid} = $fid if $m->{match}; @@ -630,7 +707,7 @@ INSERT INTO defense_missions (fleet,call) VALUES (?,?) if ($call->{call}){ $informDefChannel->execute($m->{fleet},$call->{call}); }else{ - $m->{warning} = "No call for @{$m->{target}} landing tick $m->{tick}"; + $m->{warning} = "No call for $m->{target} landing tick $m->{tick}"; } } @@ -641,17 +718,17 @@ sub addReturnFleet : Private { my $findfleet = $dbh->prepare(q{ SELECT fid FROM fleets f JOIN launch_confirmations lc USING (fid) -WHERE uid = $1 AND name = $2 AND amount = $3 +WHERE uid = $1 AND num = $2 AND amount = $3 AND back >= $4 }); - my $fid = $dbh->selectrow_array($findfleet,undef,$c->user->id,$m->{name} + my $fid = $dbh->selectrow_array($findfleet,undef,$c->user->id,$m->{num} ,$m->{amount}, $m->{tick}); $c->forward("matchShips", [$m,$fid]); if ($m->{match}){ $m->{fid} = $fid; $m->{warning} = "Return fleet, changed back tick to match the return eta."; } else { - $m->{warning} = "Couldn't find a fleet matching this returning fleet. Recall manually."; + $m->{warning} = "Couldn't find a fleet matching this returning fleet, so adding a new fleet that is returning"; } }