diff --git a/lib/Lacuna/DB/Result/Empire.pm b/lib/Lacuna/DB/Result/Empire.pm index 6b4dcdc7..2c7baeb3 100644 --- a/lib/Lacuna/DB/Result/Empire.pm +++ b/lib/Lacuna/DB/Result/Empire.pm @@ -148,6 +148,7 @@ sub spend_essentia { 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, })->insert; return $self; @@ -156,18 +157,13 @@ sub spend_essentia { sub add_essentia { my ($self, $value, $note, $transaction_id) = @_; $self->essentia( $self->essentia + $value ); - my $api_key; - my $session = $self->current_session; - if (defined $session) { - $api_key = $session->api_key; - } Lacuna->db->resultset('Lacuna::DB::Result::Log::Essentia')->new({ empire_id => $self->id, empire_name => $self->name, amount => $value, description => $note, transaction_id => $transaction_id, - api_key => $api_key, + api_key => (defined $self->current_session) ? $self->current_session->api_key : undef, })->insert; return $self; } @@ -382,18 +378,6 @@ sub find_home_planet { # (SQRT( POW(5-x,2) + POW(8-y,2) )) as distance # then order by distance } - #else { - # my $min_inhabited = sub { - # my $axis = shift; - # return $planets->search({empire_id => { '>' => 0 } })->get_column($axis)->min; - # }; - # my $max_inhabited = sub { - # my $axis = shift; - # return $planets->search({empire_id => { '>' => 0 } })->get_column($axis)->max; - # }; - # $search{x} = { between => [($min_inhabited->('x') - 20), ($max_inhabited->('x') + 20)] }; - # $search{y} = { between => [($min_inhabited->('y') - 20), ($max_inhabited->('y') + 20)] }, - #} # search my $possible_planets = $planets->search(\%search, {order_by => { -desc => ['usable_as_starter'] }}); diff --git a/lib/Lacuna/DB/Result/Log/RPC.pm b/lib/Lacuna/DB/Result/Log/RPC.pm new file mode 100644 index 00000000..31f110f0 --- /dev/null +++ b/lib/Lacuna/DB/Result/Log/RPC.pm @@ -0,0 +1,22 @@ +package Lacuna::DB::Result::Log::RPC; + +use Moose; +use utf8; +no warnings qw(uninitialized); +extends 'Lacuna::DB::Result::Log::WithEmpire'; +use Lacuna::Util; + +__PACKAGE__->table('rpc_log'); +__PACKAGE__->add_columns( + api_key => { data_type => 'varchar', size => 40, is_nullable => 1 }, + module => { data_type => 'varchar', size => 255, is_nullable => 0 }, +); + +after 'sqlt_deploy_hook' => sub { + my ($self, $sqlt_table) = @_; + $sqlt_table->add_index(name => 'idx_api_key', fields => ['api_key']); + $sqlt_table->add_index(name => 'idx_module', fields => ['module']); +}; + +no Moose; +__PACKAGE__->meta->make_immutable(inline_constructor => 0); diff --git a/lib/Lacuna/RPC.pm b/lib/Lacuna/RPC.pm index 47f60b0c..dfa77de9 100644 --- a/lib/Lacuna/RPC.pm +++ b/lib/Lacuna/RPC.pm @@ -41,6 +41,12 @@ sub get_empire_by_session { confess [1010, 'You have already made the maximum number of requests ('.$max.') you can make for one day.']; } $cache->set($cache_key, $empire->id, $rpc_count, 60 * 60 * 24); + Lacuna->db->resultset('Lacuna::DB::Result::Log::RPC')->new({ + empire_id => $empire->id, + empire_name => $empire->name, + module => ref $self, + api_key => $empire->current_session->api_key, + })->insert; return $empire; } else { diff --git a/lib/Lacuna/RPC/Empire.pm b/lib/Lacuna/RPC/Empire.pm index 4f281889..ae1bc133 100644 --- a/lib/Lacuna/RPC/Empire.pm +++ b/lib/Lacuna/RPC/Empire.pm @@ -74,6 +74,12 @@ sub login { confess [1002, 'Empire does not exist.', $name]; } Lacuna->cache->increment('rpc_count_'.format_date(undef,'%d'), $empire->id, 1, 60 * 60 * 26); + Lacuna->db->resultset('Lacuna::DB::Result::Log::RPC')->new({ + empire_id => $empire->id, + empire_name => $empire->name, + module => ref $self, + api_key => $api_key, + })->insert; if ($empire->is_password_valid($password)) { if ($empire->stage eq 'new') { confess [1100, "Your empire has not been completely created. You must complete it in order to play the game.", { empire_id => $empire->id } ]; diff --git a/lib/Lacuna/Web/ApiKey.pm b/lib/Lacuna/Web/ApiKey.pm index 56d9749c..b33f9b44 100644 --- a/lib/Lacuna/Web/ApiKey.pm +++ b/lib/Lacuna/Web/ApiKey.pm @@ -29,15 +29,21 @@ sub www_view_stats { $login->search({date_stamp => { '>=' => $thirty_days_ago }})->count, $login->count ); - my $essentia = Lacuna->db->resultset('Lacuna::DB::Result::Log::Essentia')->search({api_key => $pair->public_key}); - $out .= $row->('Essentia Purchases', - $essentia->search({date_stamp => { '>=' =>$thirty_days_ago }})->count, - $essentia->count - ); - $out .= $row->('Essentia Purchased', + my $essentia = Lacuna->db->resultset('Lacuna::DB::Result::Log::Essentia')->search({api_key => $pair->public_key, amount => { '<' => 0 } }); + $out .= $row->('Essentia Spent', $essentia->search({date_stamp => { '>=' =>$thirty_days_ago }})->get_column('amount')->sum + 0, $essentia->get_column('amount')->sum + 0 ); + my $lottery = Lacuna->db->resultset('Lacuna::DB::Result::Log::Lottery')->search({api_key => $pair->public_key }); + $out .= $row->('Lottery Votes', + $lottery->search({date_stamp => { '>=' =>$thirty_days_ago }})->count, + $lottery->count + ); + my $rpc = Lacuna->db->resultset('Lacuna::DB::Result::Log::RPC')->search({api_key => $pair->public_key }); + $out .= $row->('RPC Calls', + $rpc->search({date_stamp => { '>=' =>$thirty_days_ago }})->count, + $rpc->count + ); $out .= ''; return $self->wrapper($out, { title => 'Your Stats', logo => 1 }); } diff --git a/var/missions/desperate-settlers.mission b/var/missions/desperate-settlers.mission index 81dbc53d..a969cb9e 100755 --- a/var/missions/desperate-settlers.mission +++ b/var/missions/desperate-settlers.mission @@ -1,2 +1,2 @@ # config-file-type: JSON 1 -{"network_19_headline":"A people trapped on an accidental planet. Who will help?","network_19_completion":"A critical error led a doomed colony ship to land on a barren moon of their intended planet. The only need a short range ship for salvation.","description":"Please help us! We never guessed we would find ourselves landed on a rock that could not sustain us. Our ship was damaged and now our food and water is dwindling. If you can get a short-range colony ship to us, we will send you all of the minerals and power left over from our spent and scrapped colony ship. We would offer you more for saving our lives, but we have nothing. Perhaps you will be blessed.","mission_objective":{"ships":[{"stealth":4,"type":"short_range_colony_ship","hold_size":0,"speed":500}]},"max_university_level":"12","name":"Desperate settlers","mission_reward":{"resources":{"bauxite":6000,"chromite":6000,"rutile":6000,"energy":35000,"anthracite":8000,"magnetite":6000,"goethite":6000,"galena":6000,"beryl":8000,"waste":5000}},"mission_type":"trade"} \ No newline at end of file +{"network_19_completion":"A critical error led a doomed colony ship to land on a barren moon of their intended planet. The only need a short range ship for salvation.","mission_type":"trade","mission_objective":{"ships":[{"stealth":0,"type":"short_range_colony_ship","hold_size":0,"speed":0}]},"description":"Please help us! We never guessed we would find ourselves landed on a rock that could not sustain us. Our ship was damaged and now our food and water is dwindling. If you can get a short-range colony ship to us, we will send you all of the minerals and power left over from our spent and scrapped colony ship. We would offer you more for saving our lives, but we have nothing. Perhaps you will be blessed.","mission_reward":{"resources":{"goethite":6000,"rutile":6000,"bauxite":6000,"anthracite":8000,"energy":35000,"chromite":6000,"beryl":8000,"galena":6000,"waste":5000,"magnetite":6000}},"name":"Desperate Settlers","network_19_headline":"A people trapped on an accidental planet. Who will help?","max_university_level":"12"} \ No newline at end of file