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

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