From dd92f7fa7f14de0180006e0827b3bc1791f41a94 Mon Sep 17 00:00:00 2001 From: Iain C Docherty Date: Sat, 19 Jan 2013 10:19:05 -0500 Subject: [PATCH] Got essentia calculations correct --- lib/Lacuna/DB/Result/Building/University.pm | 1 + lib/Lacuna/DB/Result/Empire.pm | 54 +++++++++++++++++++-- lib/Lacuna/DB/Result/Market.pm | 3 ++ lib/Lacuna/DB/Result/MercenaryMarket.pm | 1 + lib/Lacuna/RPC/Building/Transporter.pm | 3 +- lib/Lacuna/Tutorial.pm | 1 + lib/Lacuna/Web/Admin.pm | 1 + lib/Lacuna/Web/Pay.pm | 4 ++ var/upgrades/essentia.sql | 6 +++ 9 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 var/upgrades/essentia.sql diff --git a/lib/Lacuna/DB/Result/Building/University.pm b/lib/Lacuna/DB/Result/Building/University.pm index 71a549b9..7e754fef 100644 --- a/lib/Lacuna/DB/Result/Building/University.pm +++ b/lib/Lacuna/DB/Result/Building/University.pm @@ -57,6 +57,7 @@ after finish_upgrade => sub { if (defined $inviter) { $inviter->add_essentia({ amount => 5, + type => 'free', reason => 'invited friend university upgrade', other_empire => $empire, }); diff --git a/lib/Lacuna/DB/Result/Empire.pm b/lib/Lacuna/DB/Result/Empire.pm index ac786925..025f3ed0 100644 --- a/lib/Lacuna/DB/Result/Empire.pm +++ b/lib/Lacuna/DB/Result/Empire.pm @@ -35,7 +35,9 @@ __PACKAGE__->add_columns( player_name => { data_type => 'varchar', size => 100, is_nullable => 1 }, password_recovery_key => { data_type => 'varchar', size => 36, is_nullable => 1 }, last_login => { data_type => 'datetime', is_nullable => 0, set_on_create => 1 }, - essentia => { data_type => 'float', size => [11,1], default_value => 0 }, + essentia_free => { data_type => 'float', size => [11,1], default_value => 0 }, + essentia_game => { data_type => 'float', size => [11,1], default_value => 0 }, + essentia_paid => { data_type => 'float', size => [11,1], default_value => 0 }, university_level => { data_type => 'tinyint', default_value => 0 }, tutorial_stage => { data_type => 'varchar', size => 30, is_nullable => 0, default_value => 'explore_the_ui' }, tutorial_scratch => { data_type => 'text', is_nullable => 1 }, @@ -120,6 +122,14 @@ has current_session => ( predicate => 'has_current_session', ); +sub essentia { + my ($self, $amount) = @_; + + if (defined $amount) { + die "Cannot set essentia"; + } + return $self->essentia_free + $self->essentia_game + $self->essentia_paid; +} sub update_species { my ($self, $me) = @_; @@ -232,19 +242,21 @@ sub spend_essentia { my ($self, $args) = @_; $args->{amount} *= -1; + $args->{type} = 'free' unless $args->{type}; $self->_adjust_essentia($args); } sub add_essentia { my ($self, $args) = @_; + $args->{type} = 'game' unless $args->{type}; $self->_adjust_essentia($args); } sub transfer_essentia { my ($self, $args) = @_; - $self->spend_essentia({ + my $type = $self->spend_essentia({ amount => $args->{amount}, reason => $args->{from_reason}, other_empire => $args->{to_empire}, @@ -254,6 +266,7 @@ sub transfer_essentia { $to_empire->add_essentia({ amount => $args->{amount}, reason => $args->{to_reason}, + type => $type, other_empire => $self, }); $to_empire->update; @@ -263,14 +276,44 @@ sub _adjust_essentia { my ($self, $args) = @_; my $value = $args->{amount}; + my $type = $args->{type} || 'game'; my $note = $args->{reason}; my $transaction_id = $args->{transaction_id}; my $other_empire = $args->{other_empire}; my $other_id = $other_empire ? $other_empire->id : 0; my $other_name = $other_empire ? $other_empire->name : ''; + my $return_type; + $type = "essentia_$type"; $self->discard_changes; - $self->essentia( $self->essentia + $value ); + if ($value >= 0) { + # Adding does not matter what type + $self->$type($self->$type + $value); + $return_type = $type; + } + else { + # Negative, deduct what we can from the $type + $return_type = $return_type || $type if $self->$type > 0; + my $residual = $self->$type + $value; + if ($residual < 0) { + $self->$type(0); + TYPE: + foreach my $type (qw(essentia_free essentia_game essentia_paid)) { + # Now deduct from any other types + $residual = $self->$type + $residual; + if ($residual >= 0) { + $self->$type($residual); + last TYPE; + } + else { + $self->$type(0); + } + } + } + else { + $self->$type($residual); + } + } Lacuna->db->resultset('Lacuna::DB::Result::Log::Essentia')->new({ empire_id => $self->id, empire_name => $self->name, @@ -281,7 +324,10 @@ sub _adjust_essentia { from_id => $other_id, from_name => $other_name, })->insert; - return $self; + + # Return the type of essentia spent or paid + $return_type =~ s/essentia_//; + return $return_type; } sub recalc_messages { diff --git a/lib/Lacuna/DB/Result/Market.pm b/lib/Lacuna/DB/Result/Market.pm index 09df3224..29d9d40b 100644 --- a/lib/Lacuna/DB/Result/Market.pm +++ b/lib/Lacuna/DB/Result/Market.pm @@ -55,9 +55,12 @@ sub withdraw { $ship->land->update if defined $ship; } elsif ($self->transfer_type eq 'transporter') { + # Note, we refund 'free' to stop people essentia-laundering 'free' into 'game' + # by multiple adding/removing trades $body->empire->add_essentia({ amount => 1, reason => 'Withdrew Transporter Trade', + type => 'free', }); $body->empire->update; } diff --git a/lib/Lacuna/DB/Result/MercenaryMarket.pm b/lib/Lacuna/DB/Result/MercenaryMarket.pm index 72307a74..81eea5f1 100644 --- a/lib/Lacuna/DB/Result/MercenaryMarket.pm +++ b/lib/Lacuna/DB/Result/MercenaryMarket.pm @@ -51,6 +51,7 @@ sub withdraw { $body->empire->add_essentia({ amount => $self->cost, reason => 'Withdrew Mercenary Trade', + type => 'free', }); $body->empire->update; $self->delete; diff --git a/lib/Lacuna/RPC/Building/Transporter.pm b/lib/Lacuna/RPC/Building/Transporter.pm index fdf57357..19790abb 100644 --- a/lib/Lacuna/RPC/Building/Transporter.pm +++ b/lib/Lacuna/RPC/Building/Transporter.pm @@ -84,7 +84,8 @@ sub add_to_market { $empire->spend_essentia({ amount => 1, reason => 'Offered Transporter Trade', - })->update; + }); + $empire->update; return { trade_id => $trade->id, status => $self->format_status($empire, $building->body), diff --git a/lib/Lacuna/Tutorial.pm b/lib/Lacuna/Tutorial.pm index c46816ab..63848fc1 100644 --- a/lib/Lacuna/Tutorial.pm +++ b/lib/Lacuna/Tutorial.pm @@ -459,6 +459,7 @@ sub fool { $home->add_plan('Lacuna::DB::Result::Building::Food::Reserve', 2); $empire->add_essentia({ amount => 35, + type => 'free', reason => 'tutorial', }); $empire->update; diff --git a/lib/Lacuna/Web/Admin.pm b/lib/Lacuna/Web/Admin.pm index fa7dc246..7767fcf3 100644 --- a/lib/Lacuna/Web/Admin.pm +++ b/lib/Lacuna/Web/Admin.pm @@ -739,6 +739,7 @@ sub www_add_essentia { $empire->add_essentia({ amount => $request->param('amount'), reason => $request->param('description'), + type => 'free', }); $empire->update; return $self->www_view_empire($request, $id); diff --git a/lib/Lacuna/Web/Pay.pm b/lib/Lacuna/Web/Pay.pm index 8c4d2930..05877274 100644 --- a/lib/Lacuna/Web/Pay.pm +++ b/lib/Lacuna/Web/Pay.pm @@ -94,6 +94,7 @@ sub www_jambool_postback { $empire->add_essentia({ amount => $amount, reason => 'Purchased via Social Gold', + type => 'paid', transaction_id => $transaction_id, }); $empire->update; @@ -153,6 +154,7 @@ sub www_jambool_reversal { $empire->add_essentia({ amount => $amount, reason => 'Reversed via Social Gold', + type => 'paid', transaction_id => $transaction_id, }); $empire->update; @@ -714,6 +716,7 @@ sub www_buy_currency_cc { $empire->add_essentia({ amount => $amount, reason => 'Purchased via iTransact', + type => 'paid', transaction_id => $transaction_id, }); $empire->update; @@ -880,6 +883,7 @@ sub www_paypal_ec_checkout { $empire->add_essentia({ amount => $amount, reason => 'Purchased via PayPal', + type => 'paid', transaction_id => $transaction_id, }); $empire->update; diff --git a/var/upgrades/essentia.sql b/var/upgrades/essentia.sql new file mode 100644 index 00000000..c2638564 --- /dev/null +++ b/var/upgrades/essentia.sql @@ -0,0 +1,6 @@ +alter table empire add column essentia_free float(11,1) not null default 0 after essentia; +alter table empire add column essentia_game float(11,1) not null default 0 after essentia; +alter table empire add column essentia_paid float(11,1) not null default 0 after essentia; + +update empire set essentia_game=essentia; + -- 2.51.2