diff --git a/bin/rotate_taxes_paid.pl b/bin/rotate_taxes_paid.pl new file mode 100644 index 00000000..67bceabf --- /dev/null +++ b/bin/rotate_taxes_paid.pl @@ -0,0 +1,48 @@ +use 5.010; +use strict; +use lib '/data/Lacuna-Server/lib'; +use Lacuna::DB; +use Lacuna; +use Lacuna::Util qw(format_date); +use Getopt::Long; +$|=1; + +our $quiet; + +GetOptions( + 'quiet' => \$quiet, +); + +out('Started'); +my $start = time; + +out('Loading DB'); +our $db = Lacuna->db; + +advance(); + + +my $finish = time; +out('Finished'); +out((($finish - $start)/60)." minutes have elapsed"); + + +############### +## SUBROUTINES +############### + +sub advance { + $db->do("update taxes set paid_6 = paid_5, paid_5 = paid_4, paid_4 = paid_3, paid_3 = paid_2, paid_2 = paid_1, paid_1 = paid_0, paid_0 = 0"); +} + + +# UTILITIES + +sub out { + my $message = shift; + unless ($quiet) { + say format_date(DateTime->now), " ", $message; + } +} + + diff --git a/bin/run_daily.sh b/bin/run_daily.sh index 2b2be0b3..91638268 100755 --- a/bin/run_daily.sh +++ b/bin/run_daily.sh @@ -7,3 +7,5 @@ perl alert_inactive_users.pl perl diablotin/send_attack.pl --randomize& #perl saben/send_attack.pl --randomize& #perl trelvestian/check_for_total_victory.pl +perl rotate_taxes_paid.pl + diff --git a/docs/Parliament.pod b/docs/Parliament.pod index 7dc14b28..bc6900e3 100644 --- a/docs/Parliament.pod +++ b/docs/Parliament.pod @@ -73,6 +73,34 @@ The unique id of the parliament. +=head2 view_taxes_collected (session_id, building_id ) + +Returns a list of the empires that have paid taxes and how much has been paid for the past seven days. + + { + "status" : { ... }, + "taxes_collected" : [ + { + "id" : "id-goes-here", + "name" : "Klingons", + "paid" : [ 0, 1000, 0, 0, 1500, 500, 500 ], + "total" : 3500, + }, + ... + ] + } + +=head3 session_id + +A session id. + +=head3 building_id + +The unique id of the parliament. + + + + =head2 get_stars_in_jurisdiction (session_id, building_id ) Returns a list of the stars in the jurisdiction of this space station. @@ -719,6 +747,43 @@ The unique id of a mining platform. See Cdb->resultset('Lacuna::DB::Result::Taxes')->search({empire_id=>$self->id,station_id=>$station_id})->single; + if (defined $taxes) { + $taxes->{paid_0} += $amount; + $taxes->update; + } + else { + Lacuna->db->resultset('Lacuna::DB::Result::Taxes')->new({ + empire_id => $self->id, + station_id => $station_id, + paid_0 => $amount, + })->insert; + } +} + no Moose; __PACKAGE__->meta->make_immutable(inline_constructor => 0); diff --git a/lib/Lacuna/DB/Result/Taxes.pm b/lib/Lacuna/DB/Result/Taxes.pm new file mode 100644 index 00000000..a9088cd5 --- /dev/null +++ b/lib/Lacuna/DB/Result/Taxes.pm @@ -0,0 +1,37 @@ +package Lacuna::DB::Result::Taxes; + +use Moose; +use utf8; +no warnings qw(uninitialized); +extends 'Lacuna::DB::Result'; +use Lacuna::Util qw(format_date); +use DateTime; + +__PACKAGE__->table('taxes'); +__PACKAGE__->add_columns( + empire_id => { data_type => 'int', size => 11, is_nullable => 0 }, + station_id => { data_type => 'int', size => 11, is_nullable => 0 }, + paid_6 => { data_type => 'int', size => 11, is_nullable => 0 }, + paid_5 => { data_type => 'int', size => 11, is_nullable => 0 }, + paid_4 => { data_type => 'int', size => 11, is_nullable => 0 }, + paid_3 => { data_type => 'int', size => 11, is_nullable => 0 }, + paid_2 => { data_type => 'int', size => 11, is_nullable => 0 }, + paid_1 => { data_type => 'int', size => 11, is_nullable => 0 }, + paid_0 => { data_type => 'int', size => 11, is_nullable => 0 }, +); + +sub get_status { + my $self = shift; + return { + id => $self->empire_id, + name => $self->empire->name, + paid => [ $self->paid_6, $self->paid_5, $self->paid_4, $self->paid_3, $self->paid_2, $self->paid_1, $self->paid_0 ], + total => $self->paid_6 + $self->paid_5 + $self->paid_4 + $self->paid_3 + $self->paid_2 + $self->paid_1 + $self->paid_0, + }; +} + +__PACKAGE__->belongs_to('empire', 'Lacuna::DB::Result::Empire', 'empire_id'); +__PACKAGE__->belongs_to('station', 'Lacuna::DB::Result::Map::Body', 'station_id'); + +no Moose; +__PACKAGE__->meta->make_immutable(inline_constructor => 0); diff --git a/lib/Lacuna/RPC/Building/Parliament.pm b/lib/Lacuna/RPC/Building/Parliament.pm index a2662614..ee0e3153 100644 --- a/lib/Lacuna/RPC/Building/Parliament.pm +++ b/lib/Lacuna/RPC/Building/Parliament.pm @@ -111,22 +111,6 @@ sub view_laws { }; } -sub view_taxes_collected { - my ($self, $session_id, $building_id) = @_; - my $empire = $self->get_empire_by_session($session_id); - my $building = $self->get_building($empire, $building_id); - my @out; -# BUG get the taxes collected here - my $taxes = $building->taxes->search({}); - while (my $tax = $taxes->next) { - push @out, $tax->get_status($empire); - } - return { - status => $self->format_status($empire, $building->body), - taxes_collected => \@out, - }; -} - sub cast_vote { my ($self, $session_id, $building_id, $proposition_id, $vote) = @_; my $empire = $self->get_empire_by_session($session_id); @@ -451,36 +435,6 @@ sub propose_rename_asteroid { }; } -sub propose_taxation { - my ($self, $session_id, $building_id, $taxes) = @_; - my $empire = $self->get_empire_by_session($session_id); - if ($empire->current_session->is_sitter) { - confess [1015, 'Sitters cannot create propositions.']; - } - my $building = $self->get_building($empire, $building_id); - unless ($building->level >= 15) { - confess [1013, 'Parliament must be level 15 to propose taxation.',15]; - } - unless ($taxes =~ /^\d+$/ && $taxes > 0 ) - { - confess [1009, 'Taxes must be an integer greater than 0.']; - } - my $proposition = Lacuna->db->resultset('Lacuna::DB::Result::Propositions')->new({ - type => 'Taxation', - name => 'Tax of '.$taxes.' resources per day', - description => 'Implement a tax of '.$taxes. ' resources per day for all empires in the jurisdiction of this station', - scratch => { taxes => $taxes }, - proposed_by_id => $empire->id, - }); - $proposition->station($building->body); - $proposition->proposed_by($empire); - $proposition->insert; - return { - status => $self->format_status($empire, $building->body), - proposition => $proposition->get_status($empire), - }; -} - sub propose_rename_uninhabited { my ($self, $session_id, $building_id, $planet_id, $name) = @_; my $empire = $self->get_empire_by_session($session_id); @@ -730,9 +684,54 @@ sub propose_expel_member { }; } +sub propose_taxation { + my ($self, $session_id, $building_id, $taxes) = @_; + my $empire = $self->get_empire_by_session($session_id); + if ($empire->current_session->is_sitter) { + confess [1015, 'Sitters cannot create propositions.']; + } + my $building = $self->get_building($empire, $building_id); + unless ($building->level >= 15) { + confess [1013, 'Parliament must be level 15 to propose taxation.',15]; + } + unless ($taxes =~ /^\d+$/ && $taxes > 0 ) + { + confess [1009, 'Taxes must be an integer greater than 0.']; + } + my $proposition = Lacuna->db->resultset('Lacuna::DB::Result::Propositions')->new({ + type => 'Taxation', + name => 'Tax of '.$taxes.' resources per day', + description => 'Implement a tax of '.$taxes. ' resources per day for all empires in the jurisdiction of this station', + scratch => { taxes => $taxes }, + proposed_by_id => $empire->id, + }); + $proposition->station($building->body); + $proposition->proposed_by($empire); + $proposition->insert; + return { + status => $self->format_status($empire, $building->body), + proposition => $proposition->get_status($empire), + }; +} + +sub view_taxes_collected { + my ($self, $session_id, $building_id) = @_; + my $empire = $self->get_empire_by_session($session_id); + my $building = $self->get_building($empire, $building_id); + my @out; + my $taxes = Lacuna->db->resultset('Lacuna::DB::Result::Taxes')->search({station_id => $building->body_id}); + while (my $tax = $taxes->next) { + push @out, $tax->get_status(); + } + return { + status => $self->format_status($empire, $building->body), + taxes_collected => \@out, + }; +} + -__PACKAGE__->register_rpc_method_names(qw(get_bodies_for_star_in_jurisdiction get_mining_platforms_for_asteroid_in_jurisdiction propose_evict_mining_platform propose_members_only_mining_rights propose_members_only_colonization propose_rename_asteroid propose_rename_uninhabited propose_broadcast_on_network19 get_stars_in_jurisdiction propose_rename_star propose_repeal_law propose_seize_star propose_transfer_station_ownership view_propositions view_laws cast_vote propose_fire_bfg propose_writ propose_elect_new_leader propose_induct_member propose_expel_member)); +__PACKAGE__->register_rpc_method_names(qw(get_bodies_for_star_in_jurisdiction get_mining_platforms_for_asteroid_in_jurisdiction propose_evict_mining_platform propose_members_only_mining_rights propose_members_only_colonization propose_rename_asteroid propose_rename_uninhabited propose_broadcast_on_network19 get_stars_in_jurisdiction propose_rename_star propose_repeal_law propose_seize_star propose_transfer_station_ownership view_propositions view_laws cast_vote propose_fire_bfg propose_writ propose_elect_new_leader propose_induct_member propose_expel_member propose_taxation view_taxes_collected)); no Moose; __PACKAGE__->meta->make_immutable; diff --git a/lib/Lacuna/Role/Ship/Arrive/CargoExchange.pm b/lib/Lacuna/Role/Ship/Arrive/CargoExchange.pm index e6644a6b..7f147f0b 100644 --- a/lib/Lacuna/Role/Ship/Arrive/CargoExchange.pm +++ b/lib/Lacuna/Role/Ship/Arrive/CargoExchange.pm @@ -9,6 +9,19 @@ after handle_arrival_procedures => sub { # do nothing, because it is uninhabited } elsif ($self->direction eq 'out') { + if ($self->foreign_body->isa('Lacuna::DB::Result::Map::Body::Planet::Station')) { + my $amount = 0; + my $payload = $self->payload; + if (exists $payload->{resources}) { + my %resources = %{$payload->{resources}}; + foreach my $type (keys %resources) { + $amount += $resources{$type}; + } + } + if ($amount > 0) { + $self->empire->pay_taxes($self->foreign_body->id, $amount); + } + } $self->unload($self->foreign_body); } else { diff --git a/t/Parliament.t b/t/Parliament.t index a452b714..5a3685b3 100644 --- a/t/Parliament.t +++ b/t/Parliament.t @@ -1,5 +1,5 @@ use lib '../lib'; -use Test::More tests => 24; +use Test::More tests => 27; use Test::Deep; use Data::Dumper; use 5.010; @@ -53,6 +53,11 @@ is($result->{result}{propositions}[0]{name}, 'Rename Station', 'got a list of pr $result = $tester->post('parliament', 'cast_vote', [$session_id, $par->id, $result->{result}{propositions}[0]{id}, 1]); is($result->{result}{proposition}{my_vote}, 1, 'got my vote'); +$empire->pay_taxes($station->id,500); +$result = $tester->post('parliament', 'view_taxes_collected', [$session_id, $par->id]); +is($result->{result}{taxes_collected}[0]{name}, $empire->name, 'found my payment'); +is($result->{result}{taxes_collected}[0]{total}, 500, 'my payment is correct'); + $result = $tester->post('parliament', 'propose_writ', [$session_id, $par->id, 'Do the big thing.', 'Make it go.']); is($result->{result}{proposition}{name}, 'Do the big thing.', 'writ proposed'); $result = $tester->post('parliament', 'cast_vote', [$session_id, $par->id, $result->{result}{proposition}{id}, 1]); @@ -92,6 +97,9 @@ is($result->{error}{data}, 13, 'members mining rights requires level 13 parliame $result = $tester->post('parliament', 'propose_evict_mining_platform', [$session_id, $par->id]); is($result->{error}{data}, 14, 'evict mining platform requires level 14 parliament'); +$result = $tester->post('parliament', 'propose_taxation', [$session_id, $par->id]); +is($result->{error}{data}, 15, 'Setting a tax rate requires level 15 parliament'); + $result = $tester->post('parliament', 'propose_rename_uninhabited', [$session_id, $par->id]); is($result->{error}{data}, 17, 'renaming uninhabited requires level 17 parliament'); diff --git a/var/upgrades/3.0500.sql b/var/upgrades/3.0500.sql new file mode 100644 index 00000000..98ae4675 --- /dev/null +++ b/var/upgrades/3.0500.sql @@ -0,0 +1,19 @@ +CREATE TABLE `taxes` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `empire_id` int(11) NOT NULL, + `station_id` int(11) NOT NULL, + `paid_6` int(11) NOT NULL DEFAULT 0, + `paid_5` int(11) NOT NULL DEFAULT 0, + `paid_4` int(11) NOT NULL DEFAULT 0, + `paid_3` int(11) NOT NULL DEFAULT 0, + `paid_2` int(11) NOT NULL DEFAULT 0, + `paid_1` int(11) NOT NULL DEFAULT 0, + `paid_0` int(11) NOT NULL DEFAULT 0, + PRIMARY KEY (`id`), + KEY `taxes_idx_empire_id` (`empire_id`), + KEY `taxes_idx_station_id` (`station_id`), + CONSTRAINT `taxes_fk_empire_id` FOREIGN KEY (`empire_id`) REFERENCES `empire` (`id`), + CONSTRAINT `taxes_fk_station_id` FOREIGN KEY (`station_id`) REFERENCES `body` (`id`) +); + + diff --git a/var/www/public/changes.txt b/var/www/public/changes.txt index 04a64018..7542972b 100644 --- a/var/www/public/changes.txt +++ b/var/www/public/changes.txt @@ -5,6 +5,7 @@ - API: Added proposing taxation at Parliament level 15. - API: Added proposing renaming uninhabited planets at Parliament level 17. - API: Added proposing members only colonization at Parliament level 18. + - API: Added view_taxes_collected to the Parliament module. - Fix: Police Station docs now show up under Modules instead of Buildings. - Fix: view_ships_orbiting will now return the ships that are orbiting your planet instead of just the count.