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.400115';
3 13     13   6436 use strict;
  13         23  
  13         389  
4 13     13   147 use v5.10;
  13         34  
5              
6 13     13   47 use Moo::Role;
  13         15  
  13         106  
7 13     13   3637 use List::Util qw(shuffle);
  13         18  
  13         971  
8              
9 13     13   66 use BalanceOfPower::Player;
  13         22  
  13         291  
10 13     13   4126 use BalanceOfPower::Targets::Fall;
  13         36  
  13         425  
11              
12 13     13   78 use BalanceOfPower::Constants ':all';
  13         18  
  13         15372  
13              
14             has players => (
15             is => 'rw',
16             default => sub { [] }
17             );
18              
19             sub get_player
20             {
21 29     29 0 983 my $self = shift;
22 29         39 my $name = shift;
23 29         65 my @good = grep { $_->name eq $name} @{$self->players};
  23         123  
  29         91  
24 29 100       64 if(@good)
25             {
26 23         80 return $good[0];
27             }
28             else
29             {
30 6         21 return undef;
31             }
32             }
33             sub create_player
34             {
35 3     3 0 8 my $self = shift;
36 3         8 my $username = shift;
37 3         7 my $position = shift;
38 3   50     30 my $money = shift || START_PLAYER_MONEY;
39 3         17 my $already = $self->get_player($username);
40 3 50       14 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       14 if(! $position)
47             {
48 3         8 my @nations = @{$self->nation_names};
  3         27  
49 3         21 @nations = shuffle @nations;
50 3         12 $position = $nations[0];
51             }
52 3         11 my $log_name = $username . ".log";
53 3         10 $log_name =~ s/ /_/g;
54 3         55 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         35 $pl->delete_log();
56 3         448 $pl->register_event("ENTERING THE GAME");
57 3         29 $self->register_event("$username IS ENTERING THE GAME");
58 3         26 $self->add_player($pl);
59 3         10 return 1;
60             }
61              
62              
63             sub add_player
64             {
65 3     3 0 9 my $self = shift;
66 3         7 my $player = shift;
67 3 50       16 if($self->get_player($player->name))
68             {
69 0         0 return 0;
70             }
71             else
72             {
73 3         8 push @{$self->players}, $player;
  3         12  
74 3         7 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 79 my $self = shift;
87 46         64 for(@{$self->players})
  46         201  
88             {
89 10         54 $_->current_year($self->current_year);
90 10         54 $_->refill_movements();
91             }
92             }
93              
94             sub player_targets
95             {
96 45     45 0 77 my $self = shift;
97 45         71 for(@{$self->players})
  45         197  
98             {
99 10         22 my $p = $_;
100 10 100       55 if($p->no_targets)
101             {
102 2         28 my $obj = BalanceOfPower::Targets::Fall->select_random_target($self);
103 2 50       14 if($obj)
104             {
105 2         32 my $target = BalanceOfPower::Targets::Fall->new(target_obj => $obj,
106             government_id => $obj->government_id,
107             countdown => TIME_FOR_TARGET);
108 2         3429 $p->add_target($target);
109             }
110             }
111             else
112             {
113 8         31 $p->check_targets($self);
114 8         24 $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;