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

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 105 CC 8 SCAR 17.9
CC
8
Cov
0%
new
12.0
set_char
12.0
set_connectedTime
6.0
CRAP 6.0
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::ConnectHistory
16 # Stores basic information about previous connections to a world (stored in the world profile)
17  
18 { package Games::Axmud::Obj::ConnectHistory;
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::Session->doConnect
34 # Prepare a new instance of the connection history object (stored in the world profile),
35 # which stores details for each connection (or attempted connection), but not for 'connect
36 # offline' sessions
37 #
38 # Expected arguments
39 # $session - The parent GA::Session (not stored as an IV)
40 #
41 # Return values
42 # 'undef' on improper arguments
43 # Blessed reference to the newly-created object on success
44  
45 my ($class, $session, $check) = @_;
46  
47 # Local variables
48 my ($connectingTime, $char);
49  
50 # Check for improper arguments
51 if (! defined $class || ! defined $session || defined $check) {
52  
53 return $axmud::CLIENT->writeImproper($class . '->new', @_);
54 }
55  
56 # Set initial IV values
57 $connectingTime = int($axmud::CLIENT->getTime());
58 if ($session->currentChar) {
59  
60 $char = $session->currentChar->name;
61 }
62  
63 # Setup
64 my $self = {
65 _objName => 'connect_history_obj',
66 _objClass => $class,
67 _parentFile => $session->currentWorld->name,
68 _parentWorld => $session->currentWorld->name,
69 _privFlag => TRUE, # All IVs are private
70  
71 # Object IVs
72 # ----------
73  
74 # System times (all matching Time::HiRes::gettimeofday() )
75 # Time at which a connection to the world was attempted (GA::Session->status was set to
76 # 'connecting')
77 connectingTime => $connectingTime,
78 # Time at which a connection to the world was completed (GA::Session->status was set to
79 # 'connected')
80 connectedTime => undef,
81 # Time at which a disconnection occured (GA::Session->status was set to 'disconnected')
82 # NB Not set when GA::Session->doTempDisconnect is called to temporarily mark the
83 # sesson 'disconnected' during an MXP crosslinking operation
84 disconnectedTime => undef,
85 # Axmud may crash, or be terminated suddenly, in which case the end of the connection
86 # won't be recorded. This IV is updated by the GA::Session every second, so that after
87 # a crash, we at least have the time that the world profile's data file was last saved
88 currentTime => $connectingTime,
89 # The length of the connetion, set to 0 when GA::Session->status is set to 'connected',
90 # updated every time ->currentTime is updated, and given a final value when
91 # GA::Session->status is set to 'disconnected'
92 connectionLength => undef,
93 # The character profile used by this connection. 'undef' if no current character is set
94 # when the connection is opened (GA::Session->status was set to 'connecting', so
95 # using ';setchar' won't change its value)
96 # NB If the user connects to a world without specifying a character, but later types
97 # ';setchar' while still connected, GA::Generic::Cmd->setProfile calls $self->set_char
98 # to set this IV. The IV is not updated in other circumstances, for example if the
99 # current character profile is changed from one to another
100 char => $char,
101 };
102  
103 # Bless the object into existence
104 bless $self, $class;
105 return $self;
106 }
107  
108 ##################
109 # Methods
110  
111 ##################
112 # Accessors - set
113  
114 sub set_char {
115  
116 # Called by GA::Generic::Cmd->setProfile. Can only be used if $self->char is currently
117 # undefined
118  
119 my ($self, $char, $check) = @_;
120  
121 # Check for improper arguments
122 if (! defined $char || defined $check) {
123  
124 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_char', @_);
125 }
126  
127 # Update IVs
128 if (! defined $self->char) {
129  
130 $self->ivPoke('char', $char);
131 }
132  
133 return 1;
134 }
135  
136 sub set_connectedTime {
137  
138 # Called by Session->connectionComplete
139  
140 my ($self, $check) = @_;
141  
142 # Check for improper arguments
143 if (defined $check) {
144  
145 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_connectedTime', @_);
146 }
147  
148 # Update IVs
149 $self->ivPoke('connectedTime', int($axmud::CLIENT->getTime()));
150 $self->ivPoke('connectionLength', 0);
151  
152 return 1;
153 }
154  
155 sub set_currentTime {
156  
157 # Called by Session->spinMaintainLoop
158  
159 my ($self, $check) = @_;
160  
161 # Check for improper arguments
162 if (defined $check) {
163  
164 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_currentTime', @_);
165 }
166  
167 # Update IVs
168 $self->ivPoke('currentTime', int($axmud::CLIENT->getTime()));
169 $self->ivPoke('connectionLength', int($self->currentTime) - int($self->connectingTime));
170  
171 return 1;
172 }
173  
174 sub set_disconnectedTime {
175  
176 # Called by Session->stop, ->doDisconnect, ->reactDisconnect
177  
178 my ($self, $check) = @_;
179  
180 # Check for improper arguments
181 if (defined $check) {
182  
183 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_disconnectedTime', @_);
184 }
185  
186 # Update IVs
187 $self->ivPoke('disconnectedTime', int($axmud::CLIENT->getTime()));
188 $self->ivPoke('currentTime', $self->disconnectedTime);
189 $self->ivPoke('connectionLength', int($self->currentTime) - int($self->connectingTime));
190  
191 return 1;
192 }
193  
194 ##################
195 # Accessors - get
196  
197 sub connectingTime
198 { $_[0]->{connectingTime} }
199 sub connectedTime
200 { $_[0]->{connectedTime} }
201 sub disconnectedTime
202 { $_[0]->{disconnectedTime} }
203 sub currentTime
204 { $_[0]->{currentTime} }
205 sub connectionLength
206 { $_[0]->{connectionLength} }
207 sub char
208 { $_[0]->{char} }
209 }
210  
211 # Package must return a true value
212 1