File Coverage

lib/BalanceOfPower/Role/GameMaster.pm
Criterion Covered Total %
statement 69 84 82.1
branch 8 16 50.0
condition 1 4 25.0
subroutine 12 14 85.7
pod 0 7 0.0
total 90 125 72.0


line stmt bran cond sub pod time code
1             package BalanceOfPower::Role::GameMaster;
2             $BalanceOfPower::Role::GameMaster::VERSION = '0.400105';
3 13     13   5453 use strict;
  13         18  
  13         309  
4 13     13   121 use v5.10;
  13         30  
5              
6 13     13   43 use Moo::Role;
  13         19  
  13         96  
7 13     13   3051 use List::Util qw(shuffle);
  13         15  
  13         855  
8              
9 13     13   58 use BalanceOfPower::Player;
  13         19  
  13         230  
10 13     13   3484 use BalanceOfPower::Targets::Fall;
  13         24  
  13         389  
11              
12 13     13   66 use BalanceOfPower::Constants ':all';
  13         16  
  13         12949  
13              
14             has players => (
15             is => 'rw',
16             default => sub { [] }
17             );
18              
19             sub get_player
20             {
21 29     29 0 1069 my $self = shift;
22 29         29 my $name = shift;
23 29         21 my @good = grep { $_->name eq $name} @{$self->players};
  23         73  
  29         59  
24 29 100       48 if(@good)
25             {
26 23         58 return $good[0];
27             }
28             else
29             {
30 6         16 return undef;
31             }
32             }
33             sub create_player
34             {
35 3     3 0 4 my $self = shift;
36 3         6 my $username = shift;
37 3         3 my $position = shift;
38 3   50     17 my $money = shift || START_PLAYER_MONEY;
39 3         13 my $already = $self->get_player($username);
40 3 50       10 if($already)
41             {
42 0 0       0 $already->position($position) if $position;
43 0 0       0 $already->money($money) if $money;
44 0         0 return 0;
45             }
46 3 50       19 if(! $position)
47             {
48 3         10 my @nations = @{$self->nation_names};
  3         15  
49 3         16 @nations = shuffle @nations;
50 3         6 $position = $nations[0];
51             }
52 3         6 my $log_name = $username . ".log";
53 3         9 $log_name =~ s/ /_/g;
54 3         38 my $pl = BalanceOfPower::Player->new(name => $username, money => $money, log_name => $log_name, log_dir => $self->log_dir, current_year => $self->current_year, position => $position);
55 3         22 $pl->delete_log();
56 3         14 $pl->register_event("ENTERING THE GAME");
57 3         18 $self->register_event("$username IS ENTERING THE GAME");
58 3         14 $self->add_player($pl);
59 3         6 return 1;
60             }
61              
62              
63             sub add_player
64             {
65 3     3 0 4 my $self = shift;
66 3         6 my $player = shift;
67 3 50       13 if($self->get_player($player->name))
68             {
69 0         0 return 0;
70             }
71             else
72             {
73 3         5 push @{$self->players}, $player;
  3         9  
74 3         6 return 1;
75             }
76             }
77             sub delete_player
78             {
79 0     0 0 0 my $self = shift;
80 0         0 my $player = shift;
81 0         0 my @players = grep { $_->name ne $player} @{$self->players};
  0         0  
  0         0  
82 0         0 $self->players(\@players);
83             }
84             sub player_start_turn
85             {
86 46     46 0 54 my $self = shift;
87 46         49 for(@{$self->players})
  46         144  
88             {
89 10         32 $_->current_year($self->current_year);
90 10         30 $_->refill_movements();
91             }
92             }
93              
94             sub player_targets
95             {
96 45     45 0 53 my $self = shift;
97 45         55 for(@{$self->players})
  45         146  
98             {
99 10         10 my $p = $_;
100 10 100       34 if($p->no_targets)
101             {
102 2         18 my $obj = BalanceOfPower::Targets::Fall->select_random_target($self);
103 2 50       26 if($obj)
104             {
105 2         21 my $target = BalanceOfPower::Targets::Fall->new(target_obj => $obj,
106             government_id => $obj->government_id,
107             countdown => TIME_FOR_TARGET);
108 2         1730 $p->add_target($target);
109             }
110             }
111             else
112             {
113 8         17 $p->check_targets($self);
114 8         18 $p->click_targets();
115             }
116             }
117             }
118             sub print_targets
119             {
120 0     0 0   my $self = shift;
121 0           my $player = shift;
122 0   0       my $mode = shift || 'print';
123 0           my $player_obj = $self->get_player($player);
124 0           return BalanceOfPower::Printer::print($mode, $self, 'print_targets',
125             { player => $player,
126             points => $player_obj->mission_points,
127             targets => $player_obj->targets,
128             } );
129             }
130              
131             1;