File Coverage

lib/BalanceOfPower/Player/Role/Cargo.pm
Criterion Covered Total %
statement 14 50 28.0
branch 0 10 0.0
condition 0 4 0.0
subroutine 5 9 55.5
pod 0 4 0.0
total 19 77 24.6


line stmt bran cond sub pod time code
1             package BalanceOfPower::Player::Role::Cargo;
2             $BalanceOfPower::Player::Role::Cargo::VERSION = '0.400105';
3 13     13   4283 use strict;
  13         23  
  13         279  
4 13     13   93 use v5.10;
  13         31  
5 13     13   36 use Moo::Role;
  13         14  
  13         62  
6              
7 13     13   2644 use BalanceOfPower::Constants ':all';
  13         24  
  13         5320  
8 13     13   68 use BalanceOfPower::Utils;
  13         19  
  13         4278  
9              
10             has hold => (
11             is => 'ro',
12             default => sub { {} }
13             );
14              
15             sub add_cargo
16             {
17 0     0 0   my $self = shift;
18 0           my $type = shift;
19 0           my $q = shift;
20 0           my $cost = shift;
21 0           my $stat = shift;
22              
23 0           my $present_q = $self->get_cargo($type);
24 0           my $present_cost = $self->get_cargo($type, 'cost');
25 0           my $present_stat = $self->get_cargo($type, 'stat');
26            
27 0           my $new_q = $present_q + $q;
28 0 0         my $new_cost = $q > 0 ? (($present_q * $cost) + ($q * $cost))/($new_q) : $present_cost;
29 0 0         my $new_stat = $q > 0 ? (($present_q * $stat) + ($q * $stat))/($new_q) : $present_stat;
30            
31 0 0         return -1 if($new_q < 0);
32 0           $self->hold->{$type} = { q => $new_q,
33             cost => $new_cost,
34             stat => $new_stat };
35 0           return 1;
36             }
37              
38             sub get_cargo
39             {
40 0     0 0   my $self = shift;
41 0           my $type = shift;
42 0   0       my $what = shift || 'q';
43 0 0         if(exists $self->hold->{$type})
44             {
45 0           return $self->hold->{$type}->{$what};
46             }
47             else
48             {
49 0           return 0;
50             }
51             }
52              
53             sub cargo_free_space
54             {
55 0     0 0   my $self = shift;
56 0           my $occupied = 0;
57 0           foreach my $t (%{$self->hold})
  0            
58             {
59 0           $occupied += $self->hold->{$t}->{'q'};
60             }
61 0 0         if($occupied > CARGO_TOTAL_SPACE)
62             {
63 0           say "Load exceed available space";
64 0           return 0;
65             }
66             else
67             {
68 0           return CARGO_TOTAL_SPACE - $occupied;
69             }
70             }
71              
72             sub print_cargo
73             {
74 0     0 0   my $self = shift;
75 0   0       my $mode = shift || 'print';
76 0           my $data = {};
77 0           foreach my $p ( ( 'goods', 'luxury', 'arms', 'tech', 'culture' ) )
78             {
79 0           $data->{$p} = $self->get_cargo($p);
80             }
81 0           $data->{'free'} = $self->cargo_free_space;
82 0           return BalanceOfPower::Printer::print($mode, $self, 'print_cargo', $data);
83             }
84              
85              
86             1;