blib/lib/BalanceOfPower/Role/Mapmaker.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 19 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 112 CC 5 SCAR 16.2
CC
5
Cov
0%
load_borders
12.0
near_nations
6.0
get_group_borders
6.0
CRAP 5.1
1 package BalanceOfPower::Role::Mapmaker;
2 $BalanceOfPower::Role::Mapmaker::VERSION = '0.200000';
3 use v5.10;
4 use strict;
5 use Moo::Role;
6  
7 use BalanceOfPower::Relations::Border;
8 use BalanceOfPower::Relations::RelPack;
9  
10 has borders => (
11 is => 'ro',
12 default => sub { BalanceOfPower::Relations::RelPack->new() },
13 handles => { add_border => 'add_link',
14 border_exists => 'exists_link',
15 print_borders => 'print_links',
16 get_borders => 'links_for_node',
17 near_on_the_map => 'near',
18 distance_on_the_map => 'distance'
19 }
20 );
21  
22  
23 sub load_borders
24 {
25 my $self = shift;
26 my $bordersfile = shift;
27 my $file = shift || $self->data_directory . "/" . $bordersfile;
28 open(my $borders, "<", $file) || die $!;;
29 for(<$borders>)
30 {
31 chomp;
32 my $border = $_;
33 my @nodes = split(/,/, $border);
34 if($self->check_nation_name($nodes[0]) && $self->check_nation_name($nodes[1]))
35 {
36 if($nodes[0] && $nodes[1] && ! $self->border_exists($nodes[0], $nodes[1]))
37 {
38 my $b = BalanceOfPower::Relations::Border->new(node1 => $nodes[0], node2 => $nodes[1]);
39 $self->add_border($b);
40 }
41 }
42 else
43 {
44 say "WRONG BORDER: $border";
45 }
46 }
47 }
48  
49 sub near_nations
50 {
51 my $self = shift;
52 my $nation = shift;
53 my $geographical = shift || 0;
54 if($geographical)
55 {
56 return $self->near_on_the_map($nation, $self->nation_names);
57 }
58 else
59 {
60 return grep { $self->in_military_range($nation, $_) && $nation ne $_ } @{$self->nation_names};
61 }
62 }
63 sub print_near_nations
64 {
65 my $self = shift;
66 my $nation = shift;
67 my $out = "";
68 for($self->near_nations($nation))
69 {
70 $out .= $_ . "\n";
71 }
72 return $out;
73 }
74 sub distance
75 {
76 my $self = shift;
77 my $nation1 = shift;
78 my $nation2 = shift;
79 return $self->distance_on_the_map($nation1, $nation2, $self->nation_names);
80 }
81  
82  
83 sub get_group_borders
84 {
85 my $self = shift;
86 my $group1 = shift;
87 my $group2 = shift;
88 my @from = @{ $group1 };
89 my @to = @{ $group2 };
90 my @out = ();
91 foreach my $to_n (@to)
92 {
93 foreach my $from_n (@from)
94 {
95 if($self->in_military_range($from_n, $to_n))
96 {
97 push @out, $to_n;
98 last;
99 }
100 }
101 }
102 return @out;
103 }
104  
105 #cache management
106  
107  
108 sub print_distance
109 {
110 my $self = shift;
111 my $n1 = shift;
112 my $n2 = shift;
113 return "Distance between $n1 and $n2: " . $self->distance($n1, $n2);
114 }
115  
116  
117  
118 1;