« blib/lib/BalanceOfPower/Commands/BuildTroops.pm
↑ Summary
lib/BalanceOfPower/Commands/BuildTroops.pm »
| 1 | package BalanceOfPower::Role::Reporter; | ||
| 2 | $BalanceOfPower::Role::Reporter::VERSION = '0.300010'; | ||
| 3 | use strict; | ||
| 4 | use v5.10; | ||
| 5 | use Moo::Role; | ||
| 6 | use BalanceOfPower::Utils qw( prev_turn get_year_turns as_title ); | ||
| 7 | use BalanceOfPower::Printer; | ||
| 8 | |||
| 9 | with "BalanceOfPower::Role::Logger"; | ||
| 10 | |||
| 11 | has events => ( | ||
| 12 | is => 'rw', | ||
| 13 | default => sub { {} } | ||
| 14 | ); | ||
| 15 | |||
| 16 | sub register_event | ||
| 17 | { | ||
| 18 | my $self = shift; | ||
| 19 | my $event = shift; | ||
| 20 | my $time = $self->current_year ? $self->current_year : "START"; | ||
| 21 | |||
| 22 | $self->events({}) if(! $self->events ); | ||
| 23 | push @{$self->events->{$time}}, $event; | ||
| 24 | $self->log("[" . $self->name . "] $event"); | ||
| 25 | } | ||
| 26 | sub get_events | ||
| 27 | { | ||
| 28 | my $self = shift; | ||
| 29 | my $label = shift; | ||
| 30 | my $year = shift; | ||
| 31 | if($self->events && exists $self->events->{$year}) | ||
| 32 | { | ||
| 33 | my @events = grep { $_ =~ /^$label/ } @{$self->events->{$year}}; | ||
| 34 | return @events; | ||
| 35 | } | ||
| 36 | else | ||
| 37 | { | ||
| 38 | return (); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | sub print_turn_events | ||
| 44 | { | ||
| 45 | my $self = shift; | ||
| 46 | my $y = shift; | ||
| 47 | my $title = shift; | ||
| 48 | my $backlog = shift || 0; | ||
| 49 | my $mode = shift || 'print'; | ||
| 50 | my @to_print; | ||
| 51 | if(! $y) | ||
| 52 | { | ||
| 53 | $y = $self->current_year ? $self->current_year : "START"; | ||
| 54 | } | ||
| 55 | if($y =~ /^\d\d\d\d$/) | ||
| 56 | { | ||
| 57 | @to_print = get_year_turns($y); | ||
| 58 | } | ||
| 59 | elsif($y =~ /^\d\d\d\d\/\d+$/) | ||
| 60 | { | ||
| 61 | @to_print = ($y); | ||
| 62 | for(my $i = 0; $i < $backlog; $i++) | ||
| 63 | { | ||
| 64 | push @to_print, prev_turn($y); | ||
| 65 | $y = prev_turn($y); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | elsif($y eq 'START') | ||
| 69 | { | ||
| 70 | @to_print = ('START'); | ||
| 71 | } | ||
| 72 | else | ||
| 73 | { | ||
| 74 | return ""; | ||
| 75 | } | ||
| 76 | return BalanceOfPower::Printer::print($mode, $self, 'print_turn_events', | ||
| 77 | { title => $title, | ||
| 78 | turns => \@to_print, | ||
| 79 | events => $self->events } ); | ||
| 80 | } | ||
| 81 | sub get_turn_tags | ||
| 82 | { | ||
| 83 | my $self = shift; | ||
| 84 | sub sort_start | ||
| 85 | { | ||
| 86 | return 0 if($a eq $b); | ||
| 87 | return -1 if($a eq 'START'); | ||
| 88 | return 1 if($b eq 'START'); | ||
| 89 | return 1 if($a gt $b); | ||
| 90 | return -1 if($b gt $a); | ||
| 91 | } | ||
| 92 | return sort sort_start keys %{$self->events}; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | sub dump_events | ||
| 98 | { | ||
| 99 | my $self = shift; | ||
| 100 | my $io = shift; | ||
| 101 | my $indent = shift || ""; | ||
| 102 | |||
| 103 | foreach my $y ($self->get_turn_tags()) | ||
| 104 | { | ||
| 105 | print {$io} $indent . "### $y\n"; | ||
| 106 | foreach my $e (@{$self->events->{$y}}) | ||
| 107 | { | ||
| 108 | print {$io} $indent . $e . "\n"; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | sub load_events | ||
| 113 | { | ||
| 114 | my $self = shift; | ||
| 115 | my $data = shift; | ||
| 116 | my @lines = split "\n", $data; | ||
| 117 | my $year = ""; | ||
| 118 | my %events; | ||
| 119 | foreach my $l (@lines) | ||
| 120 | { | ||
| 121 | $l =~ s/^\s//; | ||
| 122 | chomp $l; | ||
| 123 | if($l =~ /### (.*)$/) | ||
| 124 | { | ||
| 125 | $year = $1; | ||
| 126 | $events{$year} = []; | ||
| 127 | } | ||
| 128 | else | ||
| 129 | { | ||
| 130 | push @{$events{$year}}, $l; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | return \%events; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | 1; |