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

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 211 CC 20 SCAR 48.1
CC
20
Cov
0%
load_world
240.0
load_nations
12.0
load_players
12.0
CRAP 123.3
1 package BalanceOfPower::Role::Recorder;
2 $BalanceOfPower::Role::Recorder::VERSION = '0.300010';
3 use strict;
4 use v5.10;
5  
6 use Moo::Role;
7 use BalanceOfPower::Nation;
8 use BalanceOfPower::Executive;
9  
10 requires 'dump_events';
11 requires 'load_events';
12  
13  
14 sub dump
15 {
16 my $self = shift;
17 my $io = shift;
18 my $indent = shift || "";
19 print {$io} $indent . join(";", $self->name, $self->first_year, $self->current_year, $self->admin_password) . "\n";
20 $self->dump_events($io, " " . $indent);
21 }
22 sub load
23 {
24 my $self = shift;
25 my $data = shift;
26 my $world_line = ( split /\n/, $data )[0];
27 $world_line =~ s/^\s+//;
28 chomp $world_line;
29 my ($name, $first_year, $current_year, $admin_password) =
30 split ";", $world_line;
31 $data =~ s/^.*?\n//;
32 my $events = $self->load_events($data);
33 return BalanceOfPower::World->new(name => $name,
34 first_year => $first_year, current_year => $current_year, admin_password => $admin_password,
35 events => $events);
36
37 }
38  
39 sub load_nations
40 {
41 my $self = shift;
42 my $data = shift;
43 $data .= "EOF\n";
44 my $nation_data = "";
45 foreach my $l (split "\n", $data)
46 {
47  
48 if($l !~ /^\s/)
49 {
50 if($nation_data)
51 {
52 my $nation = BalanceOfPower::Nation->load($nation_data);
53 my $executive = BalanceOfPower::Executive->new( actor => $nation->name );
54 $executive->init($self);
55 $nation->executive($executive);
56 push @{$self->nations}, $nation;
57 push @{$self->nation_names}, $nation->name;
58 $self->nation_codes->{$nation->code} = $nation->name;
59 }
60 $nation_data = $l . "\n";
61 }
62 else
63 {
64 $nation_data .= $l . "\n";
65 }
66 }
67 }
68 sub load_players
69 {
70 my $self = shift;
71 my $data = shift;
72 $data .= "EOF\n";
73 my $player_data = "";
74 foreach my $l (split "\n", $data)
75 {
76  
77 if($l !~ /^\s/ && $l !~ /^$/)
78 {
79 if($player_data)
80 {
81 my $player = BalanceOfPower::Player->load($player_data);
82 push @{$self->players}, $player;
83 }
84 $player_data = $l . "\n";
85 }
86 else
87 {
88 $player_data .= $l . "\n";
89 }
90 }
91 }
92  
93  
94 sub dump_all
95 {
96 my $self = shift;
97 my $file = shift || $self->savefile;
98 return "No file provided" if ! $file;
99 open(my $io, "> $file");
100 $self->dump($io);
101 print {$io} "### NATIONS\n";
102 for(@{$self->nations})
103 {
104 $_->dump($io);
105 }
106 print {$io} "### PLAYERS\n";
107 for(@{$self->players})
108 {
109 $_->dump($io);
110 }
111 print {$io} "### DIPLOMATIC RELATIONS\n";
112 $self->diplomatic_relations->dump($io);
113 print {$io} "### TREATIES\n";
114 $self->treaties->dump($io);
115 print {$io} "### BORDERS\n";
116 $self->borders->dump($io);
117 print {$io} "### TRADEROUTES\n";
118 $self->trade_routes->dump($io);
119 print {$io} "### INFLUENCES\n";
120 $self->influences->dump($io);
121 print {$io} "### SUPPORTS\n";
122 $self->military_supports->dump($io);
123 print {$io} "### REBEL SUPPORTS\n";
124 $self->rebel_military_supports->dump($io);
125 print {$io} "### WARS\n";
126 $self->wars->dump($io);
127 print {$io} "### MEMORIAL\n";
128 $self->dump_memorial($io);
129 print {$io} "### STATISTICS\n";
130 $self->dump_statistics($io);
131 print {$io} "### EOF\n";
132 close($io);
133 return "World saved to $file";
134 }
135  
136 sub load_world
137 {
138 my $self = shift;
139 my $file = shift;
140 open(my $dump, "<", $file) or die "Problems opening $file: $!";
141 my $world;
142 my $target = "WORLD";
143 my $data = "";
144 for(<$dump>)
145 {
146 my $line = $_;
147 if($line =~ /^### (.*)$/)
148 {
149 my $next = $1;
150 if($target eq 'WORLD')
151 {
152 $world = $self->load($data);
153 $world->savefile($file);
154 }
155 elsif($target eq 'NATIONS')
156 {
157 $world->load_nations($data);
158 }
159 elsif($target eq 'PLAYERS')
160 {
161 $world->load_players($data);
162 }
163 elsif($target eq 'DIPLOMATIC RELATIONS')
164 {
165 $world->diplomatic_relations->load_pack("BalanceOfPower::Relations::Friendship", $data);
166 }
167 elsif($target eq 'TREATIES')
168 {
169 $world->treaties->load_pack("BalanceOfPower::Relations::Treaty", $data);
170 }
171 elsif($target eq 'BORDERS')
172 {
173 $world->borders->load_pack("BalanceOfPower::Relations::Border", $data);
174 }
175 elsif($target eq 'TRADEROUTES')
176 {
177 $world->trade_routes->load_pack("BalanceOfPower::Relations::TradeRoute", $data);
178 }
179 elsif($target eq 'INFLUENCES')
180 {
181 $world->influences->load_pack("BalanceOfPower::Relations::Influence", $data);
182 }
183 elsif($target eq 'SUPPORTS')
184 {
185 $world->military_supports->load_pack("BalanceOfPower::Relations::MilitarySupport", $data);
186 }
187 elsif($target eq 'REBEL SUPPORTS')
188 {
189 $world->rebel_military_supports->load_pack("BalanceOfPower::Relations::MilitarySupport", $data);
190 }
191 elsif($target eq 'WARS')
192 {
193 $world->wars->load_pack("BalanceOfPower::Relations::War", $data);
194 for($world->wars->all())
195 {
196 $_->log_active(0);
197 }
198 }
199 elsif($target eq 'MEMORIAL')
200 {
201 $world->memorial($world->load_memorial($data));
202 }
203 elsif($target eq 'STATISTICS')
204 {
205 $world->load_statistics($data);
206 }
207 $data = "";
208 $target = $next;
209 }
210 else
211 {
212 $data .= $line;
213 }
214 }
215 close($dump);
216 return $world;
217 }
218  
219 1;