| 1 | package BalanceOfPower::Dice; | ||
| 2 | $BalanceOfPower::Dice::VERSION = '0.200000'; | ||
| 3 | use v5.10; | ||
| 4 | use Moo; | ||
| 5 | use Data::Dumper; | ||
| 6 | use List::Util qw(shuffle); | ||
| 7 | |||
| 8 | with 'BalanceOfPower::Role::Logger'; | ||
| 9 | |||
| 10 | has tricks => ( | ||
| 11 | is => 'rw', | ||
| 12 | default => sub { {} } | ||
| 13 | ); | ||
| 14 | has trick_counters => ( | ||
| 15 | is => 'rw', | ||
| 16 | default => sub { {} } | ||
| 17 | ); | ||
| 18 | has forced_advisor => ( | ||
| 19 | is => 'rw', | ||
| 20 | default => 0 | ||
| 21 | ); | ||
| 22 | has only_one_nation_acting => ( | ||
| 23 | is => 'rw', | ||
| 24 | default => 0 | ||
| 25 | ); | ||
| 26 | |||
| 27 | sub random | ||
| 28 | { | ||
| 29 | my $self = shift; | ||
| 30 | my $min = shift; | ||
| 31 | my $max = shift; | ||
| 32 | my $message = shift || "NO MESSAGE [$min-$max]"; | ||
| 33 | my $out = $self->tricked($message); | ||
| 34 | if(defined $out) | ||
| 35 | { | ||
| 36 | $self->write_log($message, $out, 1); | ||
| 37 | return $out; | ||
| 38 | } | ||
| 39 | else | ||
| 40 | { | ||
| 41 | my $random_range = $max - $min + 1; | ||
| 42 | $out = int(rand($random_range)) + $min; | ||
| 43 | $self->write_log($message, $out, 0); | ||
| 44 | return $out; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | sub random10 | ||
| 49 | { | ||
| 50 | my $self = shift; | ||
| 51 | my $min = shift; | ||
| 52 | my $max = shift; | ||
| 53 | my $message = shift || "NO MESSAGE [$min-$max]"; | ||
| 54 | my $out = $self->tricked($message); | ||
| 55 | if(defined $out) | ||
| 56 | { | ||
| 57 | $self->write_log($message, $out, 1); | ||
| 58 | return $out; | ||
| 59 | } | ||
| 60 | else | ||
| 61 | { | ||
| 62 | my $random_range = (($max - $min) / 10) + 1; | ||
| 63 | $out = (int(rand($random_range)) * 10) + $min; | ||
| 64 | $self->write_log($message, $out, 0); | ||
| 65 | return $out; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | sub random_around_zero | ||
| 70 | { | ||
| 71 | my $self = shift; | ||
| 72 | my $max = shift; | ||
| 73 | my $divider = shift || 1; | ||
| 74 | my $message = shift; | ||
| 75 | my $out = $self->tricked($message); | ||
| 76 | if(defined $out) | ||
| 77 | { | ||
| 78 | $self->write_log($message, $out, 1); | ||
| 79 | return $out; | ||
| 80 | } | ||
| 81 | else | ||
| 82 | { | ||
| 83 | my $random_max = $max * 2; | ||
| 84 | my $r = $self->random(0, $random_max, "Inside dice, from random_around_zero"); | ||
| 85 | $r = $r - $max; | ||
| 86 | $r = $r / $divider; | ||
| 87 | $self->write_log($message, $r, 0); | ||
| 88 | return $r; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | sub shuffle_array | ||
| 92 | { | ||
| 93 | my $self = shift; | ||
| 94 | my $message = shift || "NO MESSAGE IN SHUFFLE"; | ||
| 95 | my @array = @_; | ||
| 96 | if($message =~ /^Choosing advisor for (.*)$/) | ||
| 97 | { | ||
| 98 | my $nation = $1; | ||
| 99 | my @array_back; | ||
| 100 | my $tricked = 0; | ||
| 101 | if($self->forced_advisor()) | ||
| 102 | { | ||
| 103 | @array_back = ( $self->forced_advisor() ); | ||
| 104 | $tricked = 1; | ||
| 105 | } | ||
| 106 | else | ||
| 107 | { | ||
| 108 | @array_back = shuffle @array; | ||
| 109 | } | ||
| 110 | if($self->only_one_nation_acting) | ||
| 111 | { | ||
| 112 | if($self->only_one_nation_acting ne $nation) | ||
| 113 | { | ||
| 114 | @array_back = ("Noone"); | ||
| 115 | $tricked = 1; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | $self->write_log($message, "<<array>>, first result: " . $array_back[0], $tricked); | ||
| 119 | return @array_back | ||
| 120 | } | ||
| 121 | |||
| 122 | if(@array == 0) | ||
| 123 | { | ||
| 124 | $self->write_log($message, "<<array>>, Array empty"); | ||
| 125 | return @array; | ||
| 126 | } | ||
| 127 | @array = shuffle @array; | ||
| 128 | $self->write_log($message, "<<array>>, first result: " . $array[0]); | ||
| 129 | return @array; | ||
| 130 | } | ||
| 131 | sub tricked | ||
| 132 | { | ||
| 133 | my $self = shift; | ||
| 134 | my $message = shift; | ||
| 135 | if(exists $self->tricks->{$message}) | ||
| 136 | { | ||
| 137 | my $index; | ||
| 138 | if(exists $self->trick_counters->{$message}) | ||
| 139 | { | ||
| 140 | $index = $self->trick_counters->{$message}; | ||
| 141 | } | ||
| 142 | else | ||
| 143 | { | ||
| 144 | $index = 0; | ||
| 145 | } | ||
| 146 | my $result; | ||
| 147 | if(exists $self->tricks->{$message}->[$index]) | ||
| 148 | { | ||
| 149 | $result = $self->tricks->{$message}->[$index]; | ||
| 150 | } | ||
| 151 | else | ||
| 152 | { | ||
| 153 | $result = undef; | ||
| 154 | } | ||
| 155 | $self->trick_counters->{$message} = $index + 1; | ||
| 156 | return $result; | ||
| 157 | } | ||
| 158 | else | ||
| 159 | { | ||
| 160 | return undef; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | sub write_log | ||
| 165 | { | ||
| 166 | my $self = shift; | ||
| 167 | my $message = shift; | ||
| 168 | my $result = shift; | ||
| 169 | my $tricked = shift; | ||
| 170 | if($tricked) | ||
| 171 | { | ||
| 172 | $message .= " *TRICKED* "; | ||
| 173 | } | ||
| 174 | $message = "[" . $message . "] $result"; | ||
| 175 | $self->log($message); | ||
| 176 | } | ||
| 177 | |||
| 178 | 1; |