diff --git a/bin/clean_up_empires.pl b/bin/clean_up_empires.pl index 0fc1eee8..d1bd8c0e 100644 --- a/bin/clean_up_empires.pl +++ b/bin/clean_up_empires.pl @@ -63,7 +63,11 @@ while (my $empire = $inactives->next) { if ($empire->essentia >= 1) { unless (Lacuna->cache->get('empire_inactive',$empire->id)) { out('Preventing self-destruct by spending essentia.'); - $empire->spend_essentia(1, 'prevent self-destruct')->update; + $empire->spend_essentia({ + amount => 1, + reason => 'prevent self-destruct', + }); + $empire->update; Lacuna->cache->set('empire_inactive',$empire->id,1,60*60*24); } } diff --git a/bin/determine_lottery_winner.pl b/bin/determine_lottery_winner.pl index 7d9487b5..314c992f 100644 --- a/bin/determine_lottery_winner.pl +++ b/bin/determine_lottery_winner.pl @@ -32,7 +32,11 @@ X: foreach my $x (int($config->get('map_size/x')->[0]/250) .. int($config->get(' my $empire = $empires->find($empire_id); if ($empire) { out('Winner: '.$empire->name); - $empire->add_essentia(10, 'Entertainment District Lottery')->update; + $empire->add_essentia({ + amount => 10, + reason => 'Entertainment District Lottery', + }); + $empire->update; $empire->send_predefined_message( tags => ['Alert'], filename => 'we_won_the_lottery.txt', diff --git a/bin/trickle_essentia_veins.pl b/bin/trickle_essentia_veins.pl index 0802368f..8b2af640 100644 --- a/bin/trickle_essentia_veins.pl +++ b/bin/trickle_essentia_veins.pl @@ -39,8 +39,12 @@ sub trickle { if ($body->empire_id) { my $empire = $body->empire; if (defined $empire) { - out($empire->name); - $empire->add_essentia(4, 'Essentia Vein')->update; + out($empire->name); + $empire->add_essentia({ + amount => 4, + reason => 'Essentia Vein', + }); + $empire->update; } } } diff --git a/lib/Lacuna/AI/DeLambert.pm b/lib/Lacuna/AI/DeLambert.pm index 08c53cd1..07ee6bba 100644 --- a/lib/Lacuna/AI/DeLambert.pm +++ b/lib/Lacuna/AI/DeLambert.pm @@ -411,9 +411,13 @@ sub buy_trade { my $offer_ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($trade->ship_id); return if not defined $offer_ship; - $self->empire->spend_essentia($trade->ask, 'Trade Price', 0, $trade->body->empire->id, $trade->body->empire->name )->update; - $trade->body->empire->add_essentia($trade->ask, 'Trade Income', 0, $self->empire->id, $self->empire->name)->update; - + $empire->transfer_essentia({ + amount => $trade->ask, + from_reason => 'Trade Price', + to_empire => $trade->body->empire, + to_reason => 'Trade Income', + })->update; + $offer_ship->send( target => $colony, payload => $trade->payload, diff --git a/lib/Lacuna/DB/Result/Building/GeneticsLab.pm b/lib/Lacuna/DB/Result/Building/GeneticsLab.pm index b710c903..1b41ab7c 100644 --- a/lib/Lacuna/DB/Result/Building/GeneticsLab.pm +++ b/lib/Lacuna/DB/Result/Building/GeneticsLab.pm @@ -136,7 +136,10 @@ sub experiment { if ($empire->essentia < 2) { confess [1011, 'You need 2 essentia to perform a graft experiment.']; } - $empire->spend_essentia(2, 'genetics lab graft experiment'); + $empire->spend_essentia({ + amount => 2, + reason => 'genetics lab graft experiment', + }); my $graft = 0; my $survival = 0; my $message; diff --git a/lib/Lacuna/DB/Result/Building/Trade.pm b/lib/Lacuna/DB/Result/Building/Trade.pm index cbf440c9..b3fae214 100644 --- a/lib/Lacuna/DB/Result/Building/Trade.pm +++ b/lib/Lacuna/DB/Result/Building/Trade.pm @@ -278,25 +278,25 @@ sub recalc_waste_production { } before delete => sub { - my ($self) = @_; + my ($self) = @_; - my $market = Lacuna->db->resultset('Lacuna::DB::Result::Market'); - my @to_be_deleted = $market->search( { body_id => $self->body_id, + my $market = Lacuna->db->resultset('Lacuna::DB::Result::Market'); + my @to_be_deleted = $market->search( { body_id => $self->body_id, transfer_type => 'trade'} )->get_column('id')->all; - foreach my $id (@to_be_deleted) { - my $trade = $market->find($id); - next unless defined $trade; - $trade->body->empire->send_predefined_message( + foreach my $id (@to_be_deleted) { + my $trade = $market->find($id); + next unless defined $trade; + $trade->body->empire->send_predefined_message( filename => 'trade_withdrawn.txt', params => [join("\n",@{$trade->format_description_of_payload}), $trade->ask.' essentia'], tags => ['Trade','Alert'], - ); - $trade->withdraw; - } - $self->waste_ships->update({task=>'Docked'}); - $self->waste_chains->delete_all; - $self->body->needs_recalc(1); - $self->body->update; + ); + $trade->withdraw; + } + $self->waste_ships->update({task=>'Docked'}); + $self->waste_chains->delete_all; + $self->body->needs_recalc(1); + $self->body->update; }; before 'can_downgrade' => sub { diff --git a/lib/Lacuna/DB/Result/Building/Transporter.pm b/lib/Lacuna/DB/Result/Building/Transporter.pm index 29523b71..f5302374 100644 --- a/lib/Lacuna/DB/Result/Building/Transporter.pm +++ b/lib/Lacuna/DB/Result/Building/Transporter.pm @@ -112,15 +112,11 @@ sub trade_one_for_one { confess [1011, 'You need 3 essentia to conduct this trade.']; } $body->can_add_type($want, $quantity); - $empire->spend_essentia(3, 'Lacunans Trade')->update; - #my $cargo_log = Lacuna->db->resultset('Lacuna::DB::Result::Log::Cargo'); - #$cargo_log->new({ - # message => 'transporter one for one', - # body_id => $self->body_id, - # data => { have => $have, want => $want, quantity => $quantity }, - # object_type => ref($self), - # object_id => $self->id, - #})->insert; + $empire->spend_essentia({ + amount => 3, + reason => 'Lacunans Trade', + }); + $empire->update; $body->spend_type($have, $quantity); $body->add_type($want, $quantity); $body->update; diff --git a/lib/Lacuna/DB/Result/Building/University.pm b/lib/Lacuna/DB/Result/Building/University.pm index 3c9f57a3..71a549b9 100644 --- a/lib/Lacuna/DB/Result/Building/University.pm +++ b/lib/Lacuna/DB/Result/Building/University.pm @@ -55,7 +55,12 @@ after finish_upgrade => sub { if (defined $invite) { my $inviter = $invite->inviter; if (defined $inviter) { - $inviter->add_essentia(5, 'invited friend university upgrade', 0, $empire->id, $empire->name)->update; + $inviter->add_essentia({ + amount => 5, + reason => 'invited friend university upgrade', + other_empire => $empire, + }); + $inviter->update; $inviter->send_predefined_message( filename => 'friend_essentia.txt', params => [$empire->id, $empire->name], diff --git a/lib/Lacuna/DB/Result/Building/Waste/Exchanger.pm b/lib/Lacuna/DB/Result/Building/Waste/Exchanger.pm index 27065627..86697bf2 100644 --- a/lib/Lacuna/DB/Result/Building/Waste/Exchanger.pm +++ b/lib/Lacuna/DB/Result/Building/Waste/Exchanger.pm @@ -102,7 +102,10 @@ sub recycle { # spend $body->spend_waste($total); if ($use_essentia) { - $body->empire->spend_essentia(2, 'recycling subsidy')->update; + $body->empire->spend_essentia({ + amount => 2, + reason => 'recycling subsidy', + })->update; $self->finish_work; } else { diff --git a/lib/Lacuna/DB/Result/Building/Waste/Recycling.pm b/lib/Lacuna/DB/Result/Building/Waste/Recycling.pm index 2baf9860..1304e78a 100644 --- a/lib/Lacuna/DB/Result/Building/Waste/Recycling.pm +++ b/lib/Lacuna/DB/Result/Building/Waste/Recycling.pm @@ -73,7 +73,11 @@ sub recycle { # spend $body->spend_waste($total); if ($use_essentia) { - $body->empire->spend_essentia(2, 'recycling subsidy')->update; + $body->empire->spend_essentia({ + amount => 2, + reason => 'recycling subsidy', + }); + $body->empire->update; $self->finish_work; } else { diff --git a/lib/Lacuna/DB/Result/Empire.pm b/lib/Lacuna/DB/Result/Empire.pm index 0d122ade..f6359b11 100644 --- a/lib/Lacuna/DB/Result/Empire.pm +++ b/lib/Lacuna/DB/Result/Empire.pm @@ -229,27 +229,43 @@ sub add_medal { } sub spend_essentia { - my ($self, $value, $note, $transaction_id, $from_id, $from_name) = @_; - $from_id = 0 unless defined($from_id); - $from_name = "" unless defined($from_name); - $self->essentia( $self->essentia - $value ); - Lacuna->db->resultset('Lacuna::DB::Result::Log::Essentia')->new({ - empire_id => $self->id, - empire_name => $self->name, - amount => $value * -1, - description => $note, - api_key => (defined $self->current_session) ? $self->current_session->api_key : undef, - transaction_id => $transaction_id, - from_id => $from_id, - from_name => $from_name, - })->insert; - return $self; + my ($self, $args) = @_; + + $args->{amount} *= -1; + $self->_adjust_essentia($args); } sub add_essentia { - my ($self, $value, $note, $transaction_id, $from_id, $from_name) = @_; - $from_id = 0 unless defined($from_id); - $from_name = "" unless defined($from_name); + my ($self, $args) = @_; + + $self->_adjust_essentia($args); +} + +sub transfer_essentia { + my ($self, $args) = @_; + + $self->spend_essentia({ + amount => $args->{amount}, + reason => $args->{from_reason}, + other_empire => $args->{to_empire}, + }); + $to_empire->add_essentia({ + amount => $args->{amount}, + reason => $args->{to_reason}, + other_empire => $self, + }); +} + +sub _adjust_essentia { + my ($self, $args) = @_; + + my $value = $args->{amount}; + my $note = $args->{reason}; + my $transaction_id = $args->{transaction_id}; + my $other_empire = $args->{other_empire}; + my $other_id = $from_empire ? $from_empire->id : 0; + my $other_name = $from_empire ? $from_empire->name : ''; + $self->discard_changes; $self->essentia( $self->essentia + $value ); Lacuna->db->resultset('Lacuna::DB::Result::Log::Essentia')->new({ @@ -259,8 +275,8 @@ sub add_essentia { description => $note, transaction_id => $transaction_id, api_key => (defined $self->current_session) ? $self->current_session->api_key : undef, - from_id => $from_id, - from_name => $from_name, + from_id => $other_id, + from_name => $other_name, })->insert; return $self; } @@ -904,7 +920,11 @@ sub redeem_essentia_code { $code_string, ], ); - $self->add_essentia($amount, 'Essentia Code Redemption', $code_string); + $self->add_essentia({ + amount => $amount, + reason => 'Essentia Code Redemption', + transaction_id => $code_string, + }); $self->update; return $self; } diff --git a/lib/Lacuna/DB/Result/Market.pm b/lib/Lacuna/DB/Result/Market.pm index fe9ef0d8..09df3224 100644 --- a/lib/Lacuna/DB/Result/Market.pm +++ b/lib/Lacuna/DB/Result/Market.pm @@ -55,7 +55,11 @@ sub withdraw { $ship->land->update if defined $ship; } elsif ($self->transfer_type eq 'transporter') { - $body->empire->add_essentia(1, 'Withdrew Transporter Trade')->update; + $body->empire->add_essentia({ + amount => 1, + reason => 'Withdrew Transporter Trade', + }); + $body->empire->update; } $self->delete; } diff --git a/lib/Lacuna/DB/Result/MercenaryMarket.pm b/lib/Lacuna/DB/Result/MercenaryMarket.pm index e7bc6e17..72307a74 100644 --- a/lib/Lacuna/DB/Result/MercenaryMarket.pm +++ b/lib/Lacuna/DB/Result/MercenaryMarket.pm @@ -48,7 +48,11 @@ sub withdraw { $self->unload($body,'withdraw'); # set the withdraw flag because mercs get special treatment my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($self->ship_id); $ship->land->update if defined $ship; - $body->empire->add_essentia($self->cost, 'Withdrew Mercenary Trade')->update; + $body->empire->add_essentia({ + amount => $self->cost, + reason => 'Withdrew Mercenary Trade', + }); + $body->empire->update; $self->delete; } diff --git a/lib/Lacuna/DB/Result/Mission.pm b/lib/Lacuna/DB/Result/Mission.pm index 6245476d..d9c0bd78 100644 --- a/lib/Lacuna/DB/Result/Mission.pm +++ b/lib/Lacuna/DB/Result/Mission.pm @@ -94,7 +94,11 @@ sub add_rewards { my $rewards = $self->params->get('mission_reward'); # essentia if (exists $rewards->{essentia}) { - $body->empire->add_essentia($rewards->{essentia}, 'mission reward')->update; + $body->empire->add_essentia({ + amount => $rewards->{essentia}, + reason => 'mission reward', + }); + $body->empire->update; } # happiness @@ -149,7 +153,11 @@ sub spend_objectives { my $objectives = $self->params->get('mission_objective'); # essentia if (exists $objectives->{essentia}) { - $body->empire->spend_essentia($objectives->{essentia},'mission objective')->update; + $body->empire->spend_essentia({ + amount => $objectives->{essentia}, + reason => 'mission objective', + }); + $body->empire->update; } # happiness diff --git a/lib/Lacuna/RPC/Building/Archaeology.pm b/lib/Lacuna/RPC/Building/Archaeology.pm index 8abfebb3..9042d9f4 100644 --- a/lib/Lacuna/RPC/Building/Archaeology.pm +++ b/lib/Lacuna/RPC/Building/Archaeology.pm @@ -120,7 +120,10 @@ sub subsidize_search { } $building->finish_work->update; - $empire->spend_essentia(2, 'glyph search subsidy after the fact'); + $empire->spend_essentia({ + amount => 2, + reason => 'glyph search subsidy after the fact', + }); $empire->update; return $self->view($empire, $building); diff --git a/lib/Lacuna/RPC/Building/BlackHoleGenerator.pm b/lib/Lacuna/RPC/Building/BlackHoleGenerator.pm index 1e2c7014..ca90c748 100644 --- a/lib/Lacuna/RPC/Building/BlackHoleGenerator.pm +++ b/lib/Lacuna/RPC/Building/BlackHoleGenerator.pm @@ -734,7 +734,10 @@ sub generate_singularity { } } if ($subsidize) { - $empire->spend_essentia($chance->{essentia_cost},'BHG perfection subsidy after the fact'); + $empire->spend_essentia({ + amount => $chance->{essentia_cost}, + reason => 'BHG perfection subsidy after the fact', + }); $empire->update; } @@ -1824,7 +1827,10 @@ sub subsidize_cooldown { } $building->finish_work->update; - $empire->spend_essentia(2, 'BHG cooldown subsidy after the fact'); + $empire->spend_essentia({ + amount => 2, + reason => 'BHG cooldown subsidy after the fact', + }); $empire->update; return $self->view($empire, $building); diff --git a/lib/Lacuna/RPC/Building/Capitol.pm b/lib/Lacuna/RPC/Building/Capitol.pm index 4564f4e8..3d923e81 100644 --- a/lib/Lacuna/RPC/Building/Capitol.pm +++ b/lib/Lacuna/RPC/Building/Capitol.pm @@ -33,7 +33,10 @@ sub rename_empire { Lacuna::RPC::Empire->new->is_name_available($name); $building->body->add_news(100, '%s has officially changed its name to %s.', $empire->name, $name); - $empire->spend_essentia($building->rename_empire_cost, 'rename empire'); + $empire->spend_essentia({ + amount => $building->rename_empire_cost, + reason => 'rename empire', + }); $empire->name($name); $empire->update; diff --git a/lib/Lacuna/RPC/Building/Development.pm b/lib/Lacuna/RPC/Building/Development.pm index 6b271030..b8ddc1fc 100644 --- a/lib/Lacuna/RPC/Building/Development.pm +++ b/lib/Lacuna/RPC/Building/Development.pm @@ -33,7 +33,10 @@ sub subsidize_build_queue { if ($empire->essentia < $subsidy) { confess [1011, "You don't have enough essentia."]; } - $empire->spend_essentia($subsidy, 'construction subsidy'); + $empire->spend_essentia({ + amount => $subsidy, + reason => 'construction subsidy', + }); $empire->update; $building->subsidize_build_queue; return { @@ -62,9 +65,11 @@ sub subsidize_one_build { if ($empire->essentia < $subsidy) { confess [1011, "You don't have enough essentia."]; } - $empire->spend_essentia($subsidy, 'construction subsidy'); + $empire->spend_essentia({ + amount => $subsidy, + reason => 'construction subsidy', + }); $empire->update; -# $scheduled_building->reschedule_queue; $building->subsidize_build_queue($scheduled_building); return { diff --git a/lib/Lacuna/RPC/Building/Intelligence.pm b/lib/Lacuna/RPC/Building/Intelligence.pm index 92c05b96..488d3044 100644 --- a/lib/Lacuna/RPC/Building/Intelligence.pm +++ b/lib/Lacuna/RPC/Building/Intelligence.pm @@ -119,7 +119,10 @@ sub subsidize_training { confess [1011, "Not enough essentia."]; } - $empire->spend_essentia($cost, 'spy training subsidy after the fact'); + $empire->spend_essentia({ + amount => $cost, + reason => 'spy training subsidy after the fact', + }); $empire->update; my $now = DateTime->now; diff --git a/lib/Lacuna/RPC/Building/MercenariesGuild.pm b/lib/Lacuna/RPC/Building/MercenariesGuild.pm index bb1120ed..f87c1e42 100644 --- a/lib/Lacuna/RPC/Building/MercenariesGuild.pm +++ b/lib/Lacuna/RPC/Building/MercenariesGuild.pm @@ -95,22 +95,15 @@ sub accept_from_market { $guard->cancel; - if ($trade->body->empire->id == $empire->id) { - # Selling to oneself - $empire->spend_essentia(0, 'Mercenary Price', 0, $trade->body->empire->id, $trade->body->empire->name )->update; - } - else { - $empire->spend_essentia($trade->ask, 'Mercenary Price', 0, $trade->body->empire->id, $trade->body->empire->name )->update; - $trade->body->empire->add_essentia($trade->ask, 'Mercenary Income', 0, $empire->id, $empire->name)->update; + if ($trade->body->empire->id != $empire->id) { + $empire->transfer_essentia({ + amount => $trade->ask, + from_reason => 'Mercenary Price', + to_empire => $trade->body->empire, + to_reason => 'Mercenary Income', + }); } - #my $cargo_log = Lacuna->db->resultset('Lacuna::DB::Result::Log::Cargo'); - #$cargo_log->new({ - # message => 'mercenaries guild offer accepted', - # body_id => $trade->body_id, - # data => $trade->payload, - # object_type => ref($trade), - # object_id => $trade->id, - #})->insert; + $offer_ship->send( target => $body, payload => $trade->payload, @@ -118,29 +111,19 @@ sub accept_from_market { my $id = $trade->payload->{mercenary}; my $spy = Lacuna->db->resultset('Lacuna::DB::Result::Spies')->find($id); if (defined($spy)) { - unless ($spy->empire_id == $body->empire_id) { - $spy->empire_id($body->empire_id); - } + $spy->empire_id($body->empire_id); $spy->from_body_id($body->id); $spy->on_body_id($body->id); $spy->update; } - #$cargo_log->new({ - # message => 'send offer', - # body_id => $offer_ship->foreign_body_id, - # data => $offer_ship->payload, - # object_type => ref($offer_ship), - # object_id => $offer_ship->id, - #})->insert; - if ($trade->body->empire->id != $empire->id) { - # Don't notify yourself - $trade->body->empire->send_predefined_message( - tags => ['Trade','Alert'], - filename => 'trade_accepted.txt', - params => [join("\n",@{$trade->format_description_of_payload}), $trade->ask.' essentia', $empire->id, $empire->name], - ); + # Don't notify yourself + $trade->body->empire->send_predefined_message( + tags => ['Trade','Alert'], + filename => 'trade_accepted.txt', + params => [join("\n",@{$trade->format_description_of_payload}), $trade->ask.' essentia', $empire->id, $empire->name], + ); } $trade->delete; @@ -159,7 +142,10 @@ sub add_to_market { confess [1011, "You need $cost essentia to make a trade using the Mercenaries Guild."]; } my $trade = $building->add_to_market($cost, $spy_id, $ask, $ship_id); - $empire->spend_essentia($cost, 'Offered Mercenary Trade')->update; + $empire->spend_essentia({ + amount => $cost, + reason => 'Offered Mercenary Trade', + )->update; return { trade_id => $trade->id, status => $self->format_status($empire, $building->body), diff --git a/lib/Lacuna/RPC/Building/Park.pm b/lib/Lacuna/RPC/Building/Park.pm index b53fd988..366077c7 100644 --- a/lib/Lacuna/RPC/Building/Park.pm +++ b/lib/Lacuna/RPC/Building/Park.pm @@ -52,7 +52,10 @@ sub subsidize_party { } $building->finish_work->update; - $empire->spend_essentia(2, 'party subsidy after the fact'); + $empire->spend_essentia({ + amount => 2, + reason => 'party subsidy after the fact', + }); $empire->update; return $self->view($empire, $building); diff --git a/lib/Lacuna/RPC/Building/SSLa.pm b/lib/Lacuna/RPC/Building/SSLa.pm index 854f5732..3744fe2b 100644 --- a/lib/Lacuna/RPC/Building/SSLa.pm +++ b/lib/Lacuna/RPC/Building/SSLa.pm @@ -53,7 +53,10 @@ sub subsidize_plan { } $building->finish_work->update; - $empire->spend_essentia(2, 'ssl plan subsidy after the fact'); + $empire->spend_essentia({ + amount => 2, + reason => 'ssl plan subsidy after the fact', + }); $empire->update; return $self->view($empire, $building); diff --git a/lib/Lacuna/RPC/Building/Shipyard.pm b/lib/Lacuna/RPC/Building/Shipyard.pm index 50d2133e..0a6ebd65 100644 --- a/lib/Lacuna/RPC/Building/Shipyard.pm +++ b/lib/Lacuna/RPC/Building/Shipyard.pm @@ -63,7 +63,10 @@ sub subsidize_build_queue { confess [1011, "Not enough essentia."]; } - $empire->spend_essentia($cost, 'ship build subsidy after the fact'); + $empire->spend_essentia({ + amount => $cost, + reason => 'ship build subsidy after the fact', + }); $empire->update; while (my $ship = $ships->next) { diff --git a/lib/Lacuna/RPC/Building/TheDillonForge.pm b/lib/Lacuna/RPC/Building/TheDillonForge.pm index 841c2fc0..61b890d1 100644 --- a/lib/Lacuna/RPC/Building/TheDillonForge.pm +++ b/lib/Lacuna/RPC/Building/TheDillonForge.pm @@ -144,7 +144,10 @@ sub subsidize { } $building->finish_work->update; - $empire->spend_essentia($subsidy_cost, 'Dillon Forge subsidy after the fact'); + $empire->spend_essentia({ + amount => $subsidy_cost, + reason => 'Dillon Forge subsidy after the fact', + }); $empire->update; return $self->view($empire, $building); diff --git a/lib/Lacuna/RPC/Building/Trade.pm b/lib/Lacuna/RPC/Building/Trade.pm index a53cf7f2..4bfed7d5 100644 --- a/lib/Lacuna/RPC/Building/Trade.pm +++ b/lib/Lacuna/RPC/Building/Trade.pm @@ -494,9 +494,13 @@ sub accept_from_market { $guard->cancel; - $empire->spend_essentia($trade->ask, 'Trade Price', 0, $trade->body->empire->id, $trade->body->empire->name )->update; - $trade->body->empire->add_essentia($trade->ask, 'Trade Income', 0, $empire->id, $empire->name)->update; - + $empire->transfer_essentia({ + amount => $trade->ask, + from_reason => 'Trade Price', + to_empire => $trade->body->empire, + to_reason => 'Trade Income', + })->update; + $offer_ship->send( target => $body, payload => $trade->payload, diff --git a/lib/Lacuna/RPC/Building/Transporter.pm b/lib/Lacuna/RPC/Building/Transporter.pm index 850731a4..fdf57357 100644 --- a/lib/Lacuna/RPC/Building/Transporter.pm +++ b/lib/Lacuna/RPC/Building/Transporter.pm @@ -55,7 +55,11 @@ sub push_items { confess [1010, 'You cannot push items to a planet that does not have a transporter.']; } $building->push_items($target, $transporter, $items); - $empire->spend_essentia(2, 'Transporter Push')->update; # has to go after due to validation in push_goods + $empire->spend_essentia({ + amount => 2, + reason => 'Transporter Push', + }); + $empire->update; # has to go after due to validation in push_goods return { status => $self->format_status($empire, $building->body), }; @@ -77,7 +81,10 @@ sub add_to_market { confess [1011, "You need 1 essentia to make a trade using the Subspace Transporter."]; } my $trade = $building->add_to_market($offer, $ask); - $empire->spend_essentia(1, 'Offered Transporter Trade')->update; + $empire->spend_essentia({ + amount => 1, + reason => 'Offered Transporter Trade', + })->update; return { trade_id => $trade->id, status => $self->format_status($empire, $building->body), @@ -142,16 +149,18 @@ sub accept_from_market { $guard->cancel; - $empire->spend_essentia($trade->ask + 1, 'Trade Price and Transporter Cost', 0, $trade->body->empire->id, $trade->body->empire->name)->update; - $trade->body->empire->add_essentia($trade->ask, 'Trade Income', 0, $empire->id, $empire->name)->update; - #my $cargo_log = Lacuna->db->resultset('Lacuna::DB::Result::Log::Cargo'); - #$cargo_log->new({ - # message => 'transporter offer accepted', - # body_id => $trade->body_id, - # data => $trade->payload, - # object_type => ref($trade), - # object_id => $trade->id, - #})->insert; + $empire->transfer_essentia({ + amount => $trade->ask, + from_reason => 'Trade Price', + to_empire => $trade->body->empire, + to_reason => 'Trade Income', + }); + $empire->spend_essentia({ + amount => 1, + reason => 'Transporter Cost', + }); + $empire->update; + $trade->body->empire->send_predefined_message( tags => ['Trade','Alert'], filename => 'trade_accepted.txt', diff --git a/lib/Lacuna/RPC/Building/WasteExchanger.pm b/lib/Lacuna/RPC/Building/WasteExchanger.pm index 7b042dee..e3980f64 100644 --- a/lib/Lacuna/RPC/Building/WasteExchanger.pm +++ b/lib/Lacuna/RPC/Building/WasteExchanger.pm @@ -57,7 +57,10 @@ sub subsidize_recycling { } $building->finish_work->update; - $empire->spend_essentia(2, 'recycling subsidy after the fact'); + $empire->spend_essentia({ + amount => 2, + reason => 'recycling subsidy after the fact', + }); $empire->update; return $self->view($empire, $building); diff --git a/lib/Lacuna/RPC/Building/WasteRecycling.pm b/lib/Lacuna/RPC/Building/WasteRecycling.pm index e1e5c72d..95ec3618 100644 --- a/lib/Lacuna/RPC/Building/WasteRecycling.pm +++ b/lib/Lacuna/RPC/Building/WasteRecycling.pm @@ -57,7 +57,10 @@ sub subsidize_recycling { } $building->finish_work->update; - $empire->spend_essentia(2, 'recycling subsidy after the fact'); + $empire->spend_essentia({ + amount => 2, + reason => 'recycling subsidy after the fact', + }); $empire->update; return $self->view($empire, $building); diff --git a/lib/Lacuna/RPC/Empire.pm b/lib/Lacuna/RPC/Empire.pm index 2e117c88..974dcf0f 100644 --- a/lib/Lacuna/RPC/Empire.pm +++ b/lib/Lacuna/RPC/Empire.pm @@ -725,7 +725,10 @@ sub boost { unless ($empire->essentia >= 5) { confess [1011, 'Not enough essentia.']; } - $empire->spend_essentia(5, $type.' boost'); + $empire->spend_essentia({ + amount => 5, + reason => $type.' boost', + }); my $start = DateTime->now; $start = $empire->$type if ($empire->$type > $start); $start->add(days=>7); @@ -898,7 +901,10 @@ sub redefine_species { confess [1009, 'Your minimum growth affinity is '.$limits->{min_growth}.'.']; } - $empire->spend_essentia(100, 'redefine species'); + $empire->spend_essentia({ + amount => 100, + reason => 'redefine species', + }); $empire->update_species($me); $empire->update; $empire->planets->update({needs_recalc=>1}); diff --git a/lib/Lacuna/Role/Container.pm b/lib/Lacuna/Role/Container.pm index c111717d..7391fefe 100644 --- a/lib/Lacuna/Role/Container.pm +++ b/lib/Lacuna/Role/Container.pm @@ -60,7 +60,10 @@ sub unload { delete $payload->{ships}; } if (exists $payload->{essentia}) { - $body->empire->add_essentia($payload->{essentia}, 'Trade Unloaded'); + $body->empire->add_essentia({ + amount => $payload->{essentia}, + reason => 'Trade Unloaded', + }); $body->empire->update; delete $payload->{essentia}; } diff --git a/lib/Lacuna/Tutorial.pm b/lib/Lacuna/Tutorial.pm index 1f21450e..c46816ab 100644 --- a/lib/Lacuna/Tutorial.pm +++ b/lib/Lacuna/Tutorial.pm @@ -457,7 +457,11 @@ sub fool { if ($finish) { if ($home->food_hour >= $empire->tutorial_scratch) { $home->add_plan('Lacuna::DB::Result::Building::Food::Reserve', 2); - $empire->add_essentia(35, 'tutorial')->update; + $empire->add_essentia({ + amount => 35, + reason => 'tutorial', + }); + $empire->update; $self->start('essentia'); return undef; } diff --git a/lib/Lacuna/Web/Admin.pm b/lib/Lacuna/Web/Admin.pm index ae46441c..fa7dc246 100644 --- a/lib/Lacuna/Web/Admin.pm +++ b/lib/Lacuna/Web/Admin.pm @@ -736,7 +736,11 @@ sub www_add_essentia { unless (defined $empire) { confess [404, 'Empire not found.']; } - $empire->add_essentia($request->param('amount'), $request->param('description'))->update; + $empire->add_essentia({ + amount => $request->param('amount'), + reason => $request->param('description'), + }); + $empire->update; return $self->www_view_empire($request, $id); } diff --git a/lib/Lacuna/Web/MissionCurator.pm b/lib/Lacuna/Web/MissionCurator.pm index ef21b8f1..7411d177 100644 --- a/lib/Lacuna/Web/MissionCurator.pm +++ b/lib/Lacuna/Web/MissionCurator.pm @@ -21,7 +21,11 @@ sub www_add_essentia { } my $curator = $empires->search({name=>$request->user},{rows=>1})->single; my $jt = $empires->find(2); - $empire->add_essentia(100, 'Mission Pack Approved By '.$curator->name)->update; + $empire->add_essentia({ + amount => 100, + reason => 'Mission Pack Approved By '.$curator->name, + }); + $empire->update; $empire->send_message( from => $curator, subject => 'Mission Bounty', @@ -36,8 +40,18 @@ sub www_add_essentia { ); my $dt_parser = Lacuna->db->storage->datetime_parser; my $seven_days_ago = $dt_parser->format_datetime( DateTime->now->subtract(days => 7) ); - my $recent = Lacuna->db->resultset('Lacuna::DB::Result::Log::Essentia')->search({ empire_id => $curator->id, description => 'Mission Curator', date_stamp => { '>' => $seven_days_ago}})->count; - $curator->add_essentia(100, 'Mission Curator')->update unless $recent; + my $recent = Lacuna->db->resultset('Lacuna::DB::Result::Log::Essentia')->search({ + empire_id => $curator->id, + description => 'Mission Curator', + date_stamp => { '>' => $seven_days_ago}, + })->count; + if (not $recent) { + $curator->add_essentia({ + amount => 100, + reason => 'Mission Curator', + }); + $curator->update; + } return $self->www_default($request, 'Essentia Added'); } diff --git a/lib/Lacuna/Web/Pay.pm b/lib/Lacuna/Web/Pay.pm index 69168280..8c4d2930 100644 --- a/lib/Lacuna/Web/Pay.pm +++ b/lib/Lacuna/Web/Pay.pm @@ -91,11 +91,12 @@ sub www_jambool_postback { # add essentia and alert user my $amount = $request->param('premium_currency_amount') / 100; - $empire->add_essentia( - $amount, - 'Purchased via Social Gold', - $transaction_id, - )->update; + $empire->add_essentia({ + amount => $amount, + reason => 'Purchased via Social Gold', + transaction_id => $transaction_id, + }); + $empire->update; $empire->send_predefined_message( tags => ['Alert'], filename => 'purchase_essentia.txt', @@ -147,12 +148,14 @@ sub www_jambool_reversal { # reverse the transaction my $amount = $transaction->amount; - $empire->add_essentia( - $amount, - 'Reversed via Social Gold', - $transaction_id, - ); - + # NOTE Should this be 'spend_essentia'? + # + $empire->add_essentia({ + amount => $amount, + reason => 'Reversed via Social Gold', + transaction_id => $transaction_id, + }); + $empire->update; return ['OK']; } @@ -708,11 +711,12 @@ sub www_buy_currency_cc { # add essentia and alert user my $amount = $request->param('premium_currency_amount'); - $empire->add_essentia( - $amount, - 'Purchased via iTransact', - $transaction_id, - )->update; + $empire->add_essentia({ + amount => $amount, + reason => 'Purchased via iTransact', + transaction_id => $transaction_id, + }); + $empire->update; $empire->send_predefined_message( tags => ['Alert'], filename => 'purchase_essentia.txt', @@ -873,11 +877,12 @@ sub www_paypal_ec_checkout { # add essentia and alert user my $amount = $order->{currency}; - $empire->add_essentia( - $amount, - 'Purchased via PayPal', - $transaction_id, - )->update; + $empire->add_essentia({ + amount => $amount, + reason => 'Purchased via PayPal', + transaction_id => $transaction_id, + }); + $empire->update; $empire->send_predefined_message( tags => ['Alert'], filename => 'purchase_essentia.txt', diff --git a/t/010_Empire.t b/t/010_Empire.t index 447ae24c..66fb78ca 100644 --- a/t/010_Empire.t +++ b/t/010_Empire.t @@ -218,7 +218,8 @@ $result = $tester->post('empire', 'create', \%empire2); $empire2{id} = $result->{result}; $result = $tester->post('empire', 'found', [$empire2{id},'Anonymous']); $e2 = Lacuna->db->resultset('Lacuna::DB::Result::Empire')->find($empire2{id}); -$e2->add_essentia(200, 'test')->update; +$e2->add_essentia({ amount => 200, reason => 'test'}); +$e2->update; my $session2 = $result->{result}{session_id}; $result = $tester->post('empire', 'get_status', [$session2]); is($result->{result}{empire}{essentia}, '200.0', 'added essentia works'); diff --git a/t/080_Boosts.t b/t/080_Boosts.t index b292f733..df18c485 100644 --- a/t/080_Boosts.t +++ b/t/080_Boosts.t @@ -11,7 +11,8 @@ my $tester = TestHelper->new->generate_test_empire; my $session_id = $tester->session->id; my $empire = $tester->empire; my $home = $empire->home_planet; -$empire->add_essentia(10,'testing boosts')->update; +$empire->add_essentia({ amount => 10, reason => 'testing boosts'}); +$empire->update; my $base = $tester->post('body','get_status', [$session_id, $home->id]); $tester->post('empire','boost_ore', [$session_id]); diff --git a/t/100_Capitol.t b/t/100_Capitol.t index 146f0d3c..c0ab0cca 100644 --- a/t/100_Capitol.t +++ b/t/100_Capitol.t @@ -13,7 +13,8 @@ my $session_id = $tester->session->id; my $result; -$tester->empire->add_essentia(30, 'test rename')->update; +$tester->empire->add_essentia({ amount => 30, reason => 'test rename' }); +$tester->empire->update; my $capitol = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ class => 'Lacuna::DB::Result::Building::Capitol', x => 1, diff --git a/t/120_Development.t b/t/120_Development.t index a294b461..5379bf7f 100644 --- a/t/120_Development.t +++ b/t/120_Development.t @@ -12,7 +12,8 @@ my $session_id = $tester->session->id; my $empire = $tester->empire; my $home = $empire->home_planet; my $result; -$empire->add_essentia(10,'testing development')->update; +$empire->add_essentia({ amount => 10, reason => 'testing development'}); +$empire->update; my $uni = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ x => 0, diff --git a/t/180_MercenariesGuild.t b/t/180_MercenariesGuild.t index d3325ce9..99c05e03 100644 --- a/t/180_MercenariesGuild.t +++ b/t/180_MercenariesGuild.t @@ -74,7 +74,7 @@ $result = $tester->post('mercenariesguild', 'add_to_market', [$session_id, $merc is($result->{error}{code}, 1011, 'can call add_to_market'); ok($result->{error}{message} =~ / 2\.9 essentia /, 'requires 2.9 essentia to add_to_market'); -$empire->add_essentia(100, 'Topping up'); +$empire->add_essentia({amount => 100, reason => 'Topping up'}); $empire->update(); $result = $tester->post('mercenariesguild', 'add_to_market', [$session_id, $merc->id, undef, 1]); diff --git a/t/260_SpaceStationLab.t b/t/260_SpaceStationLab.t index 30813876..95162cc2 100644 --- a/t/260_SpaceStationLab.t +++ b/t/260_SpaceStationLab.t @@ -66,7 +66,8 @@ $home->update; $result = $tester->post('ssla', 'make_plan', [$session_id, $ssla->id, 'ibs', 1]); is($result->{result}{make_plan}{making}, 'Interstellar Broadcast System (1+0)', 'making plan'); -$empire->add_essentia(10,'testing')->update; +$empire->add_essentia({ amount => 10, reason => 'testing'}); +$empire->update; $result = $tester->post('ssla', 'subsidize_plan', [$session_id, $ssla->id]); ok(!exists $result->{result}{make_plan}{making}, 'subsidize making plan'); diff --git a/t/280_Transporter.t b/t/280_Transporter.t index ee275e51..27319073 100644 --- a/t/280_Transporter.t +++ b/t/280_Transporter.t @@ -11,7 +11,8 @@ TestHelper->clear_all_test_empires; my $tester = TestHelper->new->generate_test_empire->build_infrastructure; my $session_id = $tester->session->id; my $empire = $tester->empire; -$empire->add_essentia(100,'testing transporter')->update; +$empire->add_essentia({ amount => 100, reason => 'testing transporter'}); +$empire->update; my $home = $empire->home_planet; my $result; diff --git a/t/400_TheDillonForge.t b/t/400_TheDillonForge.t index c6778218..f3bc742e 100644 --- a/t/400_TheDillonForge.t +++ b/t/400_TheDillonForge.t @@ -99,7 +99,8 @@ is_deeply($result->{error}, { code => 1011, }); -$empire->add_essentia(100,'testing The Dillon Forge')->update; +$empire->add_essentia({ amount => 100, reason => 'testing The Dillon Forge'}); +$empire->update; $result = $tester->post('thedillonforge', 'subsidize', [$session_id, $forge->id]); diff --git a/t/bugs/0003_PurchaseTooManyShips.t b/t/bugs/0003_PurchaseTooManyShips.t index a28872e3..edd9c4e4 100644 --- a/t/bugs/0003_PurchaseTooManyShips.t +++ b/t/bugs/0003_PurchaseTooManyShips.t @@ -15,11 +15,13 @@ my $tester = TestHelper->new({ big_producer => 1 })->generate_test_empire->build my $tester_session_id = $tester->session->id; my $empire = $tester->empire; my $home = $empire->home_planet; -$empire->add_essentia(100, 'testing transporter')->update; +$empire->add_essentia({ amount => 100, reason => 'testing transporter'}); +$empire->update; my $trader = TestHelper->new({empire_name => 'TLE Test Trader', big_producer => 1})->generate_test_empire->build_infrastructure; my $trader_session_id = $trader->session->id; -$trader->empire->add_essentia(100, 'testing transporter')->update; +$trader->empire->add_essentia({ amount => 100, reason => 'testing transporter'}); +$trader->empire->update; # Build an SST and a space port on both the tester and the trader empires diff --git a/t/bugs/0041_SelectManyPlansOrGlyphs.t b/t/bugs/0041_SelectManyPlansOrGlyphs.t index cea9fc65..7a87defd 100644 --- a/t/bugs/0041_SelectManyPlansOrGlyphs.t +++ b/t/bugs/0041_SelectManyPlansOrGlyphs.t @@ -18,7 +18,8 @@ my $tester = TestHelper->new({ big_producer => 1 })->generate_test_empire->build my $tester_session_id = $tester->session->id; my $empire = $tester->empire; my $home = $empire->home_planet; -$empire->add_essentia(100, 'testing transporter')->update; +$empire->add_essentia({ amount => 100, reason => 'testing transporter'}); +$empire->update; # Build an SST and a space port