| 1 | package BalanceOfPower::Utils; | ||
| 2 | $BalanceOfPower::Utils::VERSION = '0.200000'; | ||
| 3 | use BalanceOfPower::Constants ':all'; | ||
| 4 | use Term::ANSIColor; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use base 'Exporter'; | ||
| 10 | |||
| 11 | sub prev_turn | ||
| 12 | { | ||
| 13 | my $year = shift; | ||
| 14 | my ($y, $i) = split '/', $year; | ||
| 15 | $i ||= 1; | ||
| 16 | if($i == 1) | ||
| 17 | { | ||
| 18 | return ($y -1) . '/' . TURNS_FOR_YEAR; | ||
| 19 | } | ||
| 20 | else | ||
| 21 | { | ||
| 22 | return $y . '/' . ($i - 1); | ||
| 23 | } | ||
| 24 | } | ||
| 25 | sub next_turn | ||
| 26 | { | ||
| 27 | my $year = shift; | ||
| 28 | my ($y, $i) = split '/', $year; | ||
| 29 | if($i == TURNS_FOR_YEAR) | ||
| 30 | { | ||
| 31 | return ($y +1) . '/' . '1'; | ||
| 32 | } | ||
| 33 | else | ||
| 34 | { | ||
| 35 | return $y . '/' . ($i + 1); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | sub get_year_turns | ||
| 39 | { | ||
| 40 | my $year = shift; | ||
| 41 | return ($year) if($year =~ /\d+\/\d+/); | ||
| 42 | my @turns = (); | ||
| 43 | for(my $i = 1; $i<= TURNS_FOR_YEAR; $i++) | ||
| 44 | { | ||
| 45 | push @turns, $year . '/' . $i; | ||
| 46 | } | ||
| 47 | return @turns; | ||
| 48 | } | ||
| 49 | sub split_turn | ||
| 50 | { | ||
| 51 | my $turn = shift; | ||
| 52 | if($turn =~ /(\d+)(\/(\d+))?/) | ||
| 53 | { | ||
| 54 | my $turn_y = $1; | ||
| 55 | my $turn_t = $3 ? $3 : 1; | ||
| 56 | return ($turn_y, $turn_t); | ||
| 57 | } | ||
| 58 | else | ||
| 59 | { | ||
| 60 | (undef, undef); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | sub from_to_turns | ||
| 64 | { | ||
| 65 | my $from = shift; | ||
| 66 | my $to = shift; | ||
| 67 | my ($from_y, $from_t) = split_turn($from); | ||
| 68 | return () if(! $from_y); | ||
| 69 | my ($to_y, $to_t) = split_turn($to); | ||
| 70 | return () if(! $from_y); | ||
| 71 | return () | ||
| 72 | if($to_y < $from_y || ($to_y == $from_y && $to_t < $from_t)); | ||
| 73 | my $goon = 1; | ||
| 74 | my $to_add_y = $from_y; | ||
| 75 | my $to_add_t = $from_t; | ||
| 76 | my @turns = (); | ||
| 77 | while(1) | ||
| 78 | { | ||
| 79 | my $to_add = $to_add_y . '/' . $to_add_t; | ||
| 80 | push @turns, $to_add; | ||
| 81 | last if($to_add eq $to); | ||
| 82 | if($to_add_t < TURNS_FOR_YEAR) | ||
| 83 | { | ||
| 84 | $to_add_t++; | ||
| 85 | } | ||
| 86 | else | ||
| 87 | { | ||
| 88 | $to_add_y++; | ||
| 89 | $to_add_t = 1; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | return @turns; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | |||
| 98 | # first < second -> -1 | ||
| 99 | # first = second -> 0 | ||
| 100 | # first > second -> 1 | ||
| 101 | # | ||
| 102 | sub compare_turns | ||
| 103 | { | ||
| 104 | my $first = shift; | ||
| 105 | my $second = shift; | ||
| 106 | return 0 if ($first eq $second); | ||
| 107 | my ($first_y, $first_t) = split_turn($first); | ||
| 108 | return undef if(! $first_y); | ||
| 109 | my ($second_y, $second_t) = split_turn($second); | ||
| 110 | return undef if(! $second_y); | ||
| 111 | return 0 if($first_y == $second_y && $first_t == $second_t); | ||
| 112 | return undef | ||
| 113 | if($first_t < 0 || | ||
| 114 | $second_t < 0 || | ||
| 115 | $first_t > TURNS_FOR_YEAR || | ||
| 116 | $second_t > TURNS_FOR_YEAR); | ||
| 117 | if($first_y > $second_y || | ||
| 118 | (($first_y == $second_y && $first_t > $second_t))) | ||
| 119 | { | ||
| 120 | return 1; | ||
| 121 | } | ||
| 122 | else | ||
| 123 | { | ||
| 124 | return -1; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | sub evidence_text | ||
| 128 | { | ||
| 129 | my $text = shift; | ||
| 130 | my $key = shift; | ||
| 131 | $text =~ s/($key)/as_evidenced($1)/eg; | ||
| 132 | return $text; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub as_title | ||
| 136 | { | ||
| 137 | my $text = shift; | ||
| 138 | return color("yellow bold") . $text . color("reset"); | ||
| 139 | } | ||
| 140 | sub as_subtitle | ||
| 141 | { | ||
| 142 | my $text = shift; | ||
| 143 | return color("yellow") . $text . color("reset"); | ||
| 144 | } | ||
| 145 | sub as_evidenced | ||
| 146 | { | ||
| 147 | my $text = shift; | ||
| 148 | return color("bold green") . $text . color("reset"); | ||
| 149 | } | ||
| 150 | sub as_active | ||
| 151 | { | ||
| 152 | my $text = shift; | ||
| 153 | return color("bold white") . $text . color("reset"); | ||
| 154 | } | ||
| 155 | |||
| 156 | our @EXPORT_OK = ('prev_turn', 'next_turn', 'random', 'random10', 'get_year_turns', 'as_title', 'from_to_turns', 'compare_turns', 'as_subtitle', 'evidence_text', 'as_evidenced', 'as_active'); | ||
| 157 | |||
| 158 | 1; |