-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(''));
-}
-