| 1 | package Games::Go::Rank; |
|---|
| 2 | |
|---|
| 3 | use warnings; |
|---|
| 4 | use strict; |
|---|
| 5 | use Moose; |
|---|
| 6 | |
|---|
| 7 | our $VERSION = '0.05'; |
|---|
| 8 | |
|---|
| 9 | use overload |
|---|
| 10 | '""' => 'stringify', |
|---|
| 11 | '<=>' => 'num_cmp'; |
|---|
| 12 | |
|---|
| 13 | has 'rank' => (is => 'rw'); |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | sub stringify { |
|---|
| 17 | my $self = shift; |
|---|
| 18 | $self->rank; |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | sub as_value { |
|---|
| 23 | my $self = shift; |
|---|
| 24 | my $rank = $self->rank; |
|---|
| 25 | return unless defined $rank; # so m// doesn't complain |
|---|
| 26 | |
|---|
| 27 | if ($rank =~ /^\s*(\d+)[\s-]*k/) { return -$1 + 1 } |
|---|
| 28 | elsif ($rank =~ /^\s*(\d+)[\s-]*d/) { return $1 } |
|---|
| 29 | elsif ($rank =~ /^\s*(\d+)[\s-]*p/) { return $1 + 7 } |
|---|
| 30 | else { return } |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | sub from_value { |
|---|
| 35 | my ($self, $value) = @_; |
|---|
| 36 | if ($value <= 0) { |
|---|
| 37 | $self->rank(sprintf "%sk" => -$value + 1) |
|---|
| 38 | } elsif ($value > 0 && $value <= 7) { |
|---|
| 39 | $self->rank(sprintf "%sd" => $value) |
|---|
| 40 | } elsif ($value > 7) { |
|---|
| 41 | $self->rank(sprintf "%sp" => $value - 7) |
|---|
| 42 | } else { |
|---|
| 43 | $self->clear_rank; |
|---|
| 44 | } |
|---|
| 45 | return $self; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | sub num_cmp { |
|---|
| 50 | my ($lhs, $rhs, $reversed) = @_; |
|---|
| 51 | for ($lhs, $rhs) { |
|---|
| 52 | if (ref $_ eq __PACKAGE__) { |
|---|
| 53 | $_ = $_->as_value; |
|---|
| 54 | } else { |
|---|
| 55 | $_ = __PACKAGE__->new(rank => $_)->as_value; |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | ($lhs, $rhs) = ($rhs, $lhs) if $reversed; |
|---|
| 59 | for ($lhs, $rhs) { $_ = -99 unless defined($_) } |
|---|
| 60 | $lhs <=> $rhs; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | 1; |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | __END__ |
|---|
| 68 | |
|---|
| 69 | {% USE p = PodGenerated %} |
|---|
| 70 | |
|---|
| 71 | =head1 NAME |
|---|
| 72 | |
|---|
| 73 | Games::Go::Rank - represents a player's rank in the game of Go |
|---|
| 74 | |
|---|
| 75 | =head1 SYNOPSIS |
|---|
| 76 | |
|---|
| 77 | use Games::Go::Rank; |
|---|
| 78 | |
|---|
| 79 | my $black_rank = Games::Go::Rank->new(rank => '1k'); |
|---|
| 80 | my $white_rank = Games::Go::Rank->new(rank => '2d'); |
|---|
| 81 | if ($white_rank > $black_rank) { ... } |
|---|
| 82 | |
|---|
| 83 | =head1 DESCRIPTION |
|---|
| 84 | |
|---|
| 85 | This class represents a player's rank in the game of Go. Rank objects can be |
|---|
| 86 | compared to see whether two ranks are equal or whether one rank is higher than |
|---|
| 87 | the other. Rank objects stringify to the rank notation. |
|---|
| 88 | |
|---|
| 89 | Use the standard notation for ranks such as C<30k>, C<5k>, C<1d>, C<2p> and so |
|---|
| 90 | on. You can also use other common formats such as C<6-dan> or C<2 dan>. |
|---|
| 91 | Anything after the first C<k>, C<d> or C<p> is ignored. So C<6-dan*> is the |
|---|
| 92 | same as C<6-dan>, which is the same as C<6d>. |
|---|
| 93 | |
|---|
| 94 | =head1 METHODS |
|---|
| 95 | |
|---|
| 96 | =over 4 |
|---|
| 97 | |
|---|
| 98 | {% p.write_methods %} |
|---|
| 99 | |
|---|
| 100 | =item as_value |
|---|
| 101 | |
|---|
| 102 | Returns a number representing the rank. C<1k> is returned as C<0>, lower kyu |
|---|
| 103 | ranks are returned as negative numbers (C<2K> is C<-1>, C<3k> is C<-2> etc.). |
|---|
| 104 | Dan ranks are returned as positive numbers, with pro ranks coming immediately |
|---|
| 105 | after dan ranks. For example, C<1d> == C<1>, C<7d> == C<7>, C<1p> == C<8>, |
|---|
| 106 | C<2p> == C<9>. Only dan ranks up to 7d are recognized as amateur ranks - |
|---|
| 107 | that is, C<8d> == C<1p>. |
|---|
| 108 | |
|---|
| 109 | =item from_value |
|---|
| 110 | |
|---|
| 111 | Sets the rank from a numerical value that is interpreted as described above. |
|---|
| 112 | |
|---|
| 113 | =back |
|---|
| 114 | |
|---|
| 115 | {% p.write_inheritance %} |
|---|
| 116 | |
|---|
| 117 | {% PROCESS standard_pod %} |
|---|
| 118 | |
|---|
| 119 | =cut |
|---|
| 120 | |
|---|