| 1 | package Game::Terminal::Kit::History; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Object::Proto::Sugar -types; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use State::Store; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | has [qw/save load/] => ( | ||||||||||||||||||||||||||||||||||||||||||
| 14 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 15 | isa => CodeRef | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | has limit => ( | ||||||||||||||||||||||||||||||||||||||||||
| 19 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 20 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | default => 200 | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | has store => ( | ||||||||||||||||||||||||||||||||||||||||||
| 25 | is => 'rw' | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | has begun => ( | ||||||||||||||||||||||||||||||||||||||||||
| 29 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | isa => Bool, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | default => 0 | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 16 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 16 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 16 | ▶ | Carp::croak('Game::Terminal::Kit::History: save and load are both needed') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->save and $self->load
MC/DC50%1/2
$self->save and $self->load
$self->save
$self->load
Truth table66%2/3
A and B
A= $self->saveB= $self->load
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | unless $self->save && $self->load; | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 15 | ▶ | $self->store(State::Store->new(undef, history => $self->limit > 0 ? $self->limit : undef)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | unless $self->store; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 15 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 344 | ▶ | sub _snapshot { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_snapshot: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 344 | my ($self, $game) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 344 | my $text = $self->save->($game); | |||||||||||||||||||||||||||||||||||||||||
| 46 | 344 | ▶ | Carp::croak('Game::Terminal::Kit::History: save gave ' . (defined $text ? 'a reference' : 'nothing') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition80%4/5
not defined $text or ref $text
ref $game || 'a game'
MC/DC100%2/2
not defined $text or ref $text
not defined $text
ref $text
MC/DC0%0/1
ref $game || 'a game'
ref $game
Truth table100%3/3
not(A) or B
A= not defined $textB= ref $text
Truth table50%1/2
A
A= ref $game
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | . ' for ' . (ref $game || 'a game') . ', where a string is needed') | ||||||||||||||||||||||||||||||||||||||||||
| 48 | if !defined $text || ref $text; | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 342 | return $text; | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 17 | ▶ | sub start { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 17 | my ($self, $game) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 17 | $self->store->set($self->_snapshot($game)); | |||||||||||||||||||||||||||||||||||||||||
| 55 | 16 | $self->store->clear_history; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 16 | $self->begun(0); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 16 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 327 | ▶ | sub record { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
record: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 327 | my ($self, $game) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 327 | $self->store->set($self->_snapshot($game)); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 326 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 3 | ▶ | sub turn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
turn: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 3 | my ($self, $game) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 3 | ▶ | return $self->record($game) if $self->begun; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | $self->start($game); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 2 | $self->begun(1); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 4 | ▶ | sub text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 4 | return $self->store->current; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 18 | ▶ | sub undo { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
undo: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 18 | my ($self, $steps) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 18 | ▶ | return undef unless $self->store->undo(defined $steps ? $steps : 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 15 | return $self->_rebuild; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 8 | ▶ | sub redo { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
redo: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 8 | my ($self, $steps) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 8 | ▶ | return undef unless $self->store->redo(defined $steps ? $steps : 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 6 | return $self->_rebuild; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 21 | ▶ | sub _rebuild { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_rebuild: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 21 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 21 | my $game = $self->load->($self->store->current); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 21 | ▶ | Carp::croak('Game::Terminal::Kit::History: load gave nothing back for a saved game') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | unless defined $game; | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 20 | return $game; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 6 | ▶ | sub can_undo { return $_[0]->store->can_undo ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
can_undo: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 6 | ▶ | sub can_redo { return $_[0]->store->can_redo ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
can_redo: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 13 | ▶ | sub undo_count { return $_[0]->store->undo_count } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
undo_count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 7 | ▶ | sub redo_count { return $_[0]->store->redo_count } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
redo_count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 2 | ▶ | sub clear { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 2 | $self->store->clear_history; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | sub game_keys { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
game_keys: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | return { u => 'undo', r => 'redo' }; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 5 | ▶ | sub legend { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
legend: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 5 | ▶ | return join ', ', ($self->can_undo ? 'u undo' : ()), ($self->can_redo ? 'r redo' : ()); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||||||||||||||||