File Coverage

lib/BalanceOfPower/Relations/Friendship.pm
Criterion Covered Total %
statement 48 127 37.8
branch 17 62 27.4
condition 0 5 0.0
subroutine 14 26 53.8
pod 0 19 0.0
total 79 239 33.0


line stmt bran cond sub pod time code
1             package BalanceOfPower::Relations::Friendship;
2             $BalanceOfPower::Relations::Friendship::VERSION = '0.400105';
3 13     13   47 use strict;
  13         17  
  13         275  
4 13     13   95 use v5.10;
  13         30  
5              
6 13     13   44 use Moo;
  13         18  
  13         58  
7 13     13   2456 use BalanceOfPower::Constants ':all';
  13         20  
  13         5390  
8 13     13   64 use BalanceOfPower::Utils qw( as_html_dangerous );
  13         17  
  13         522  
9              
10 13     13   51 use Term::ANSIColor;
  13         17  
  13         531  
11 13     13   51 use HTML::Entities;
  13         15  
  13         12232  
12              
13             has factor => (
14             is => 'rw'
15             );
16             has crisis_level => (
17             is => 'rw',
18             default => 0
19             );
20             with 'BalanceOfPower::Relations::Role::Relation';
21              
22             sub get_crisis_level
23             {
24 957     957 0 592 my $self = shift;
25 957 100       1272 if($self->factor > PERMANENT_CRISIS_HATE_LIMIT)
26             {
27 900         2000 return $self->crisis_level;
28             }
29             else
30             {
31 57 50       95 if($self->crisis_level < CRISIS_MAX_FACTOR)
32             {
33 57         121 return $self->crisis_level + 1;
34             }
35             else
36             {
37 0         0 return $self->crisis_level;
38             }
39             }
40             }
41             sub status
42             {
43 825     825 0 537 my $self = shift;
44 825 100       1600 if($self->factor == ALLIANCE_FRIENDSHIP_FACTOR)
    100          
45             {
46 23         67 return 'ALLIANCE';
47             }
48             elsif($self->factor == DOMINION_DIPLOMACY)
49             {
50 3         10 return 'INFLUENCE PRESENT';
51             }
52 799 100       1198 if($self->factor <= HATE_LIMIT)
    100          
53             {
54 209         424 return 'HATE';
55             }
56             elsif($self->factor >= LOVE_LIMIT)
57             {
58 310         715 return 'FRIENDSHIP';
59             }
60             else
61             {
62 280         649 return 'NEUTRAL';
63             }
64             }
65             sub status_html_class
66             {
67 0     0 0 0 my $self = shift;
68 0 0       0 if($self->factor == ALLIANCE_FRIENDSHIP_FACTOR)
    0          
69             {
70 0         0 return 'rel-alliance';
71             }
72             elsif($self->factor == DOMINION_DIPLOMACY)
73             {
74 0         0 return 'rel-influence';
75             }
76 0 0       0 if($self->factor <= HATE_LIMIT)
    0          
77             {
78 0         0 return 'rel-hate';
79             }
80             elsif($self->factor >= LOVE_LIMIT)
81             {
82 0         0 return 'rel-friendship';
83             }
84             else
85             {
86 0         0 return 'rel-neutral';
87             }
88             }
89              
90              
91             sub colored_status
92             {
93 0     0 0 0 my $self = shift;
94 0         0 return $self->status_color . $self->status . color("reset");
95             }
96              
97             sub status_color
98             {
99 0     0 0 0 my $self = shift;
100 0 0 0     0 if($self->status eq 'ALLIANCE' || $self->status eq 'INFLUENCE PRESENT')
101             {
102 0         0 return color("cyan bold");
103             }
104 0 0       0 if($self->status eq 'HATE')
    0          
105             {
106 0         0 return color("red bold");
107             }
108             elsif($self->status eq 'FRIENDSHIP')
109             {
110 0         0 return color("green bold");
111             }
112             else
113             {
114 0         0 return "";
115             }
116             }
117              
118              
119             sub html
120             {
121 0     0 0 0 my $self = shift;
122 0         0 my $from = shift;
123 0         0 my $link = encode_entities($self->print($from, 0));
124 0 0       0 if($self->status eq 'HATE')
125             {
126 0         0 $link = as_html_dangerous($link);
127             }
128 0         0 return $link;
129             }
130              
131             sub print
132             {
133 0     0 0 0 my $self = shift;
134 0         0 my $from = shift;
135 0         0 my $color = shift;
136 0 0       0 $color = 1 if(! defined $color);
137 0         0 my $second_node;
138             my $out;
139 0 0       0 if($from)
140             {
141 0 0       0 if($from eq $self->node1)
    0          
142             {
143 0         0 $second_node = $self->node2;
144             }
145             elsif($from eq $self->node2)
146             {
147 0         0 $second_node = $self->node1;
148             }
149             }
150             else
151             {
152 0         0 $from = $self->node1;
153 0         0 $second_node = $self->node2;
154             }
155 0 0       0 $out = $self->status_color if $color;
156 0         0 $out .= $from . " <--> " . $second_node . " [" . $self->factor . " " . $self->status . "]";
157 0 0       0 if($self->get_crisis_level > 0)
158             {
159 0 0       0 if($color)
160             {
161 0         0 $out .= " " . $self->print_crisis_bar();
162             }
163             else
164             {
165 0         0 $out .= " " . $self->print_grey_crisis_bar();
166             }
167             }
168 0 0       0 $out .= color("reset") if $color;
169 0         0 return $out;
170             }
171             sub print_status
172             {
173 0     0 0 0 my $self = shift;
174 0         0 return $self->status_color . $self->status . color("reset");
175             }
176             sub print_crisis
177             {
178 0     0 0 0 my $self = shift;
179 0         0 my $color = shift;
180 0 0       0 $color = 1 if(! defined $color);
181 0 0       0 if($self->get_crisis_level > 0)
182             {
183 0         0 my $out = $self->node1 . " <-> " . $self->node2;
184 0 0       0 if($color)
185             {
186 0         0 return $out . " " . $self->print_crisis_bar();
187             }
188             else
189             {
190 0         0 return $out . " " . $self->print_grey_crisis_bar();
191             }
192             }
193             else
194             {
195 0         0 return "";
196             }
197             }
198             sub html_crisis
199             {
200 0     0 0 0 my $self = shift;
201 0         0 return encode_entities($self->print_crisis(0));
202             }
203             sub print_grey_crisis_bar
204             {
205 0     0 0 0 my $self = shift;
206 0         0 my $out = "";
207 0 0       0 if($self->get_crisis_level > 0)
208             {
209 0         0 $out .= "[";
210 0         0 for(my $i = 0; $i < CRISIS_MAX_FACTOR; $i++)
211             {
212 0 0       0 if($i < $self->get_crisis_level)
213             {
214 0         0 $out .= "*";
215             }
216             else
217             {
218 0         0 $out .= " ";
219             }
220             }
221 0         0 $out .= "]";
222             }
223 0         0 return $out;
224             }
225             sub print_crisis_bar
226             {
227 0     0 0 0 my $self = shift;
228 0         0 return $self->status_color . $self->print_grey_crisis_bar . color("reset");
229             }
230              
231             sub change_factor
232             {
233 56     56 0 53 my $self = shift;
234 56         52 my $delta = shift;
235 56         87 my $new_factor = $self->factor + $delta;
236 56 100       132 $new_factor = $new_factor < 0 ? 0 : $new_factor > 100 ? 100 : $new_factor;
    100          
237 56         96 $self->factor($new_factor);
238             }
239              
240             sub escalate_crisis
241             {
242 19     19 0 29 my $self = shift;
243 19 50       67 if($self->crisis_level < CRISIS_MAX_FACTOR)
244             {
245 19         81 $self->crisis_level($self->crisis_level() + 1);
246             }
247             }
248             sub cooldown_crisis
249             {
250 4     4 0 4 my $self = shift;
251 4 50       17 if($self->crisis_level > 0)
252             {
253 4         18 $self->crisis_level($self->crisis_level() - 1);
254             }
255             }
256             sub is_crisis
257             {
258 683     683 0 454 my $self = shift;
259 683         687 return $self->get_crisis_level() > 0;
260             }
261             sub is_max_crisis
262             {
263 2     2 0 2 my $self = shift;
264 2         5 return $self->get_crisis_level() == CRISIS_MAX_FACTOR;
265             }
266             sub dump
267             {
268 0     0 0   my $self = shift;
269 0           my $io = shift;
270 0   0       my $indent = shift || "";
271 0           print {$io} $indent . join(";", $self->node1, $self->node2, $self->factor, $self->crisis_level) . "\n";
  0            
272             }
273             sub load
274             {
275 0     0 0   my $self = shift;
276 0           my $data = shift;
277 0           $data =~ s/^\s+//;
278 0           chomp $data;
279 0           my ($node1, $node2, $factor, $crisis_level) = split ";", $data;
280 0           return $self->new(node1 => $node1, node2 => $node2, factor => $factor, crisis_level => $crisis_level);
281             }
282              
283              
284             1;