blib/lib/Games/Axmud/Obj/Repeat.pm untested

filter: Statementstmt 0.0% 0 / 32 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 94 CC 5 SCAR 18.9
CC
5
Cov
0%
new
12.0
sendCmd
12.0
cmd
2.0
CRAP 6.6
1 # Copyright (C) 2011-2024 A S Lewis
2 #
3 # This program is free software: you can redistribute it and/or modify it under the terms of the GNU
4 # General Public License as published by the Free Software Foundation, either version 3 of the
5 # License, or (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License along with this program. If not,
12 # see <http://www.gnu.org/licenses/>.
13 #
14 #
15 # Games::Axmud::Obj::Repeat
16 # Handles repeating commands (created with ';intervalrepeat')
17  
18 { package Games::Axmud::Obj::Repeat;
19  
20 use strict;
21 use warnings;
22 # use diagnostics;
23  
24 use Glib qw(TRUE FALSE);
25  
26 our @ISA = qw(Games::Axmud);
27  
28 ##################
29 # Constructors
30  
31 sub new {
32  
33 # Called by GA::Cmd::AddQuest->do
34 # Creates a new instance of the quest object
35 #
36 # Expected arguments
37 # $session - The GA::Session which called this function (not stored as an IV)
38 # $cmd - The command to send
39 # $number - How often to send it (minimum value 1)
40 # $waitTime - How long to wait between sending each command, in seconds (minimum 1
41 # second)
42 #
43 # Return values
44 # 'undef' on improper arguments or if $number or $time are invalid
45 # Blessed reference to the newly-created object on success
46  
47 my ($class, $session, $cmd, $number, $waitTime, $check) = @_;
48  
49 # Check for improper arguments
50 if (
51 ! defined $class || ! defined $session || ! defined $cmd || ! defined $number
52 || ! defined $waitTime || defined $check
53 ) {
54 return $axmud::CLIENT->writeImproper($class . '->new', @_);
55 }
56  
57 # Check that $number and $waitTime are valid positive integers
58 if ($number =~ /\D/ || $number < 1 || $waitTime =~ /\D/ || $waitTime < 1) {
59  
60 return undef;
61 }
62  
63 # Setup
64 my $self = {
65 _objName => $cmd,
66 _objClass => $class,
67 _parentFile => undef, # No parent file object
68 _parentWorld => undef, # No parent file object
69 _privFlag => TRUE, # All IVs are private
70  
71 # IVs
72 # ---
73  
74 # Which command to send
75 cmd => $cmd,
76 # How many more times to send it
77 number => $number,
78 # How long to wait between sending each command (in seconds)
79 waitTime => $waitTime,
80 # The time at which to send the next command, in seconds (matches
81 # GA::Session->sessionTime). The first command is sent on the next session loop
82 nextCheckTime => 0,
83 };
84  
85 # Bless the object into existence
86 bless $self, $class;
87 return $self;
88 }
89  
90 ##################
91 # Methods
92  
93 sub sendCmd {
94  
95 # Called by GA::Session->taskLoop when it's time to re-send the command
96 # Sends the command and updates IVs
97 #
98 # Expected arguments
99 # $session - The parent GA::Session
100 #
101 # Return values
102 # 'undef' on improper arguments or if this object has now finished sending commands
103 # 1 otherwise
104  
105 my ($self, $session, $check) = @_;
106  
107 # Check for improper arguments
108 if (! defined $session || defined $check) {
109  
110 return $axmud::CLIENT->writeImproper($self->_objClass . '->sendCmd', @_);
111 }
112  
113 # Send the command
114 $session->worldCmd($self->cmd);
115 if ($self->number <= 1) {
116  
117 # Don't send the command again
118 return undef;
119  
120 } else {
121  
122 # Set the time at which the command will be sent again
123 $self->ivDecrement('number');
124 $self->ivPoke('nextCheckTime', ($session->sessionTime + $self->waitTime));
125  
126 return 1;
127 }
128 }
129  
130 ##################
131 # Accessors - set
132  
133 ##################
134 # Accessors - get
135  
136 sub cmd
137 { $_[0]->{cmd} }
138 sub number
139 { $_[0]->{number} }
140 sub waitTime
141 { $_[0]->{waitTime} }
142 sub nextCheckTime
143 { $_[0]->{nextCheckTime} }
144 }
145  
146 # Package must return a true value
147 1