File Coverage

lib/BalanceOfPower/Role/Recorder.pm
Criterion Covered Total %
statement 14 177 7.9
branch 0 56 0.0
condition 0 11 0.0
subroutine 5 12 41.6
pod 0 7 0.0
total 19 263 7.2


line stmt bran cond sub pod time code
1             package BalanceOfPower::Role::Recorder;
2             $BalanceOfPower::Role::Recorder::VERSION = '0.400110';
3 13     13   4524 use strict;
  13         16  
  13         277  
4 13     13   94 use v5.10;
  13         26  
5              
6 13     13   38 use Moo::Role;
  13         16  
  13         62  
7 13     13   2513 use BalanceOfPower::Nation;
  13         18  
  13         212  
8 13     13   40 use BalanceOfPower::Executive;
  13         13  
  13         15098  
9              
10             requires 'dump_events';
11             requires 'load_events';
12              
13             my $dump_version = 3;
14              
15              
16             sub dump
17             {
18 0     0 0   my $self = shift;
19 0           my $io = shift;
20 0   0       my $indent = shift || "";
21 0           print {$io} $indent . join(";", $self->name, $self->first_year, $self->current_year, $self->admin_password) . "\n";
  0            
22 0           $self->dump_events($io, " " . $indent);
23             }
24             sub load
25             {
26 0     0 0   my $self = shift;
27 0           my $data = shift;
28 0           my $world_line = ( split /\n/, $data )[0];
29 0           $world_line =~ s/^\s+//;
30 0           chomp $world_line;
31 0           my ($name, $first_year, $current_year, $admin_password) =
32             split ";", $world_line;
33 0           $data =~ s/^.*?\n//;
34 0           my $events = $self->load_events($data);
35 0           return BalanceOfPower::World->new(name => $name,
36             first_year => $first_year, current_year => $current_year, admin_password => $admin_password,
37             events => $events);
38            
39             }
40              
41             sub load_nations
42             {
43 0     0 0   my $self = shift;
44 0           my $data = shift;
45 0           my $version = shift;
46 0           $data .= "EOF\n";
47 0           my $nation_data = "";
48 0           foreach my $l (split "\n", $data)
49             {
50              
51 0 0         if($l !~ /^\s/)
52             {
53 0 0         if($nation_data)
54             {
55 0           my $nation = BalanceOfPower::Nation->load($nation_data, $version);
56 0           my $executive = BalanceOfPower::Executive->new( actor => $nation->name );
57 0           $executive->init($self);
58 0           $nation->executive($executive);
59 0           push @{$self->nations}, $nation;
  0            
60 0           push @{$self->nation_names}, $nation->name;
  0            
61 0           $self->nation_codes->{$nation->code} = $nation->name;
62             }
63 0           $nation_data = $l . "\n";
64             }
65             else
66             {
67 0           $nation_data .= $l . "\n";
68             }
69             }
70             }
71             sub load_players
72             {
73 0     0 0   my $self = shift;
74 0           my $data = shift;
75 0           my $version = shift;
76 0           $data .= "EOF\n";
77 0           my $player_data = "";
78 0           foreach my $l (split "\n", $data)
79             {
80              
81 0 0 0       if($l !~ /^\s/ && $l !~ /^$/)
82             {
83 0 0         if($player_data)
84             {
85 0           my $player = BalanceOfPower::Player->load($player_data, $version, $self);
86 0           push @{$self->players}, $player;
  0            
87             }
88 0           $player_data = $l . "\n";
89             }
90             else
91             {
92 0           $player_data .= $l . "\n";
93             }
94             }
95             }
96              
97             sub load_civil_wars
98             {
99 0     0 0   my $self = shift;
100 0           my $data = shift;
101 0           $data .= "EOF\n";
102 0           my $cw_data = "";
103 0           foreach my $l (split "\n", $data)
104             {
105              
106 0 0 0       if($l !~ /^\s/ && $l !~ /^$/)
107             {
108 0 0         if($cw_data)
109             {
110 0           my $cw = BalanceOfPower::CivilWar->load($cw_data);
111 0           $cw->load_nation($self);
112 0           push @{$self->civil_wars}, $cw;
  0            
113             }
114 0           $cw_data = $l . "\n";
115             }
116             else
117             {
118 0           $cw_data .= $l . "\n";
119             }
120             }
121             }
122              
123              
124              
125             sub dump_all
126             {
127 0     0 0   my $self = shift;
128 0   0       my $file = shift || $self->savefile;
129 0 0         return "No file provided" if ! $file;
130 0           open(my $io, "> $file");
131 0           print {$io} "####### V$dump_version\n";
  0            
132 0           $self->dump($io);
133 0           print {$io} "### NATIONS\n";
  0            
134 0           for(@{$self->nations})
  0            
135             {
136 0           $_->dump($io);
137             }
138 0           print {$io} "### PLAYERS\n";
  0            
139 0           for(@{$self->players})
  0            
140             {
141 0           $_->dump($io);
142             }
143 0           print {$io} "### DIPLOMATIC RELATIONS\n";
  0            
144 0           $self->diplomatic_relations->dump($io);
145 0           print {$io} "### TREATIES\n";
  0            
146 0           $self->treaties->dump($io);
147 0           print {$io} "### BORDERS\n";
  0            
148 0           $self->borders->dump($io);
149 0           print {$io} "### TRADEROUTES\n";
  0            
150 0           $self->trade_routes->dump($io);
151 0           print {$io} "### INFLUENCES\n";
  0            
152 0           $self->influences->dump($io);
153 0           print {$io} "### SUPPORTS\n";
  0            
154 0           $self->military_supports->dump($io);
155 0           print {$io} "### REBEL SUPPORTS\n";
  0            
156 0           $self->rebel_military_supports->dump($io);
157 0           print {$io} "### WARS\n";
  0            
158 0           $self->wars->dump($io);
159 0           print {$io} "### CIVIL WARS\n";
  0            
160 0           for(@{$self->civil_wars})
  0            
161             {
162 0           $_->dump($io);
163             }
164 0           print {$io} "### MEMORIAL\n";
  0            
165 0           $self->dump_memorial($io);
166 0           print {$io} "### CIVIL MEMORIAL\n";
  0            
167 0           $self->dump_civil_memorial($io);
168 0           print {$io} "### STATISTICS\n";
  0            
169 0           $self->dump_statistics($io);
170 0           print {$io} "### EOF\n";
  0            
171 0           close($io);
172 0           return "World saved to $file";
173             }
174              
175             sub load_world
176             {
177 0     0 0   my $self = shift;
178 0           my $file = shift;
179 0 0         open(my $dump, "<", $file) or die "Problems opening $file: $!";
180 0           my $world;
181 0           my $target = "WORLD";
182 0           my $data = "";
183 0           my $version = undef;
184 0           for(<$dump>)
185             {
186 0           my $line = $_;
187 0 0         if(! $version)
188             {
189 0 0         if($line =~ /^####### V(.*)$/)
190             {
191 0           $version = $1;
192 0 0         if($version != $dump_version)
193             {
194 0           say "WARNING: Dump of version $version";
195             }
196 0           next;
197             }
198             else
199             {
200 0           $version = 1;
201 0 0         if($version != $dump_version)
202             {
203 0           say "WARNING: Dump of version $version";
204             }
205             }
206             }
207 0 0         if($line =~ /^### (.*)$/)
208             {
209 0           my $next = $1;
210 0 0         if($target eq 'WORLD')
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
211             {
212 0           $world = $self->load($data);
213 0           $world->savefile($file);
214             }
215             elsif($target eq 'NATIONS')
216             {
217 0           $world->load_nations($data, $version);
218             }
219             elsif($target eq 'PLAYERS')
220             {
221 0           $world->load_players($data, $version);
222             }
223             elsif($target eq 'DIPLOMATIC RELATIONS')
224             {
225 0           $world->diplomatic_relations->load_pack("BalanceOfPower::Relations::Friendship", $data);
226             }
227             elsif($target eq 'TREATIES')
228             {
229 0           $world->treaties->load_pack("BalanceOfPower::Relations::Treaty", $data);
230             }
231             elsif($target eq 'BORDERS')
232             {
233 0           $world->borders->load_pack("BalanceOfPower::Relations::Border", $data);
234             }
235             elsif($target eq 'TRADEROUTES')
236             {
237 0           $world->trade_routes->load_pack("BalanceOfPower::Relations::TradeRoute", $data);
238             }
239             elsif($target eq 'INFLUENCES')
240             {
241 0           $world->influences->load_pack("BalanceOfPower::Relations::Influence", $data);
242             }
243             elsif($target eq 'SUPPORTS')
244             {
245 0           $world->military_supports->load_pack("BalanceOfPower::Relations::MilitarySupport", $data);
246             }
247             elsif($target eq 'REBEL SUPPORTS')
248             {
249 0           $world->rebel_military_supports->load_pack("BalanceOfPower::Relations::MilitarySupport", $data);
250             }
251             elsif($target eq 'WARS')
252             {
253 0           $world->wars->load_pack("BalanceOfPower::Relations::War", $data);
254 0           for($world->wars->all())
255             {
256 0           $_->log_active(0);
257             }
258             }
259             elsif($target eq 'CIVIL WARS')
260             {
261 0           $world->load_civil_wars($data);
262             }
263             elsif($target eq 'MEMORIAL')
264             {
265 0           $world->memorial($world->load_memorial($data));
266             }
267             elsif($target eq 'CIVIL MEMORIAL')
268             {
269 0           $world->civil_memorial($world->load_civil_memorial($data));
270             }
271             elsif($target eq 'STATISTICS')
272             {
273 0           $world->load_statistics($data);
274             }
275 0           $data = "";
276 0           $target = $next;
277             }
278             else
279             {
280 0           $data .= $line;
281             }
282             }
283 0           close($dump);
284 0           return $world;
285             }
286              
287             1;