blib/lib/Games/Axmud/Mcp.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 189 CC 11 SCAR 32.0
CC
11
Cov
0%
msg
72.0
new
20.0
name
2.0
CRAP 24.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::Mcp::Obj::MultiLine
16 # The code that stores MCP multiline data until the server has finished sending them
17 # Games::Axmud::Mcp::XXX
18 # The code that handles an MCP package object
19  
20 { package Games::Axmud::Mcp::Obj::MultiLine;
21  
22 use strict;
23 use warnings;
24 # use diagnostics;
25  
26 use Glib qw(TRUE FALSE);
27  
28 our @ISA = qw(Games::Axmud::Generic::Atcp Games::Axmud);
29  
30 ##################
31 # Constructors
32  
33 sub new {
34  
35 # Called by GA::Session->extractMcpArgs
36 # MCP multiline messages comprise a message, spread over one or more lines. The lines occur
37 # in order, but not necessarily consecutively, so the lines' contents are stored here
38 # until a multiline termination message is received
39 #
40 # Expected arguments
41 # $name - The name of this object is the MCP data tag specified in the MCP
42 # message
43 # $msg - The MCP message, e.g. 'mcp-negotiate-can'
44 # $normalListRef - Reference to a list of normal (i.e. single-line) key-value pairs
45 # which can't be updated by multiline continuation lines. List in the
46 # form (key, value, key, value...)
47 # $multiListRef - Reference to a list of multiline key-value pairs which can be updated
48 # by multiline continuation lines. List in the form
49 # (key, value, key, value...), where each value is an empty string
50 #
51 # Return values
52 # 'undef' on improper arguments
53 # Blessed reference to the newly-created object on success
54  
55 my ($class, $name, $msg, $normalListRef, $multiListRef, $check) = @_;
56  
57 # Local variables
58 my (%normalHash, %multiHash);
59  
60 # Check for improper arguments
61 if (
62 ! defined $class || ! defined $name || ! defined $msg || ! defined $normalListRef
63 || ! defined $multiListRef || defined $check
64 ) {
65 return $axmud::CLIENT->writeImproper($class . '->new', @_);
66 }
67  
68 # Convert the list references to hashes (the calling code has already checked for
69 # duplicate keys)
70 if (@$normalListRef) {
71  
72 do {
73  
74 my ($key, $value);
75  
76 $key = shift @$normalListRef;
77 $value = shift @$normalListRef;
78  
79 $normalHash{$key} = $value;
80  
81 } until (! @$normalListRef);
82 }
83  
84 if (@$multiListRef) {
85  
86 do {
87  
88 my ($key, $value);
89  
90 $key = shift @$multiListRef;
91 $value = shift @$multiListRef;
92  
93 $multiHash{$key} = $value;
94  
95 } until (! @$multiListRef);
96 }
97  
98 # Setup
99 my $self = {
100 _objName => $name,
101 _objClass => $class,
102 _parentFile => undef,
103 _parentWorld => undef,
104 _privFlag => TRUE, # All IVs are private
105  
106 # IVs
107 # ---
108  
109 # The name of this object is the MCP data tag specified in the MCP message
110 name => $name,
111 # The MCP message, e.g. 'mcp-negotiate-can'
112 msg => $msg,
113  
114 # Hash of key-value pairs with single-line values that can't be modified in MCP
115 # multiline continuation lines
116 normalHash => {%normalHash},
117 # Hash of key-value pairs with multi-line values that can be modified in MCP multiline
118 # continuation lines
119 multiHash => {%multiHash},
120 };
121  
122 # Bless the object into existence
123 bless $self, $class;
124 return $self;
125 }
126  
127 ##################
128 # Methods
129  
130 ##################
131 # Accessors - set
132  
133 ##################
134 # Accessors - get
135  
136 sub name
137 { $_[0]->{name} }
138 sub msg
139 { $_[0]->{msg} }
140  
141 sub normalHash
142 { my $self = shift; return %{$self->{normalHash}}; }
143 sub multiHash
144 { my $self = shift; return %{$self->{multiHash}}; }
145 }
146  
147 { package Games::Axmud::Mcp::Cord;
148  
149 # This package provides no functions of its own, as all 'mcp-cord' messages are handled by
150 # GA::Session->processMcpMsg
151  
152 use strict;
153 use warnings;
154 # use diagnostics;
155  
156 use Glib qw(TRUE FALSE);
157  
158 our @ISA = qw(Games::Axmud::Generic::Mcp Games::Axmud::Generic::Atcp Games::Axmud);
159  
160 ##################
161 # Constructors
162  
163 ##################
164 # Methods
165  
166 ##################
167 # Accessors - set
168  
169 ##################
170 # Accessors - get
171 }
172  
173 { package Games::Axmud::Mcp::NegotiateCan;
174  
175 # This package provides no functions of its own, as all 'mcp-negotiate' messages are handled by
176 # GA::Session->processMcpMsg
177  
178 use strict;
179 use warnings;
180 # use diagnostics;
181  
182 use Glib qw(TRUE FALSE);
183  
184 our @ISA = qw(Games::Axmud::Generic::Mcp Games::Axmud::Generic::Atcp Games::Axmud);
185  
186 ##################
187 # Constructors
188  
189 ##################
190 # Methods
191  
192 ##################
193 # Accessors - set
194  
195 ##################
196 # Accessors - get
197 }
198  
199 { package Games::Axmud::Mcp::SimpleEdit;
200  
201 # Implementation of 'dns-org-mud-moo-simpleedit', along with
202 # Games::Axmud::OtherWin::McpSimpleEdit
203  
204 use strict;
205 use warnings;
206 # use diagnostics;
207  
208 use Glib qw(TRUE FALSE);
209  
210 our @ISA = qw(Games::Axmud::Generic::Mcp Games::Axmud::Generic::Atcp Games::Axmud);
211  
212 ##################
213 # Constructors
214  
215 ##################
216 # Methods
217  
218 sub msg {
219  
220 # Called by GA::Session->processMcpMsg
221 # Stores values and opens a window for the user to edit one or more lines of text
222 #
223 # Expected arguments
224 # $session - The calling GA::Session
225 # $msg - The MCP message name, e.g. 'mcp-negotiate-can'
226 #
227 # Optional arguments
228 # %hash - A hash of key-value pairs which accompany the MCP message
229 # - Could conceivably be an empty hash, but we expect the keys 'reference',
230 # 'name', 'content' and 'type'
231 #
232 # Return values
233 # 'undef' on improper arguments or if there's an error processing the message
234 # 1 if the message is processed successfully
235  
236 my ($self, $session, $msg, %hash) = @_;
237  
238 # Local variables
239 my ($name, $text, $winObj);
240  
241 # Check for improper arguments
242 if (! defined $session || ! defined $msg) {
243  
244 return $axmud::CLIENT->writeImproper($self->_objClass . '->msg', @_);
245 }
246  
247 # Check that the MCP message actually belongs to this package
248 $name = $self->name;
249 if (! ($msg =~ m/^$name\-/)) {
250  
251 return undef;
252 }
253  
254 # Check that the message is recognised by this package
255 if ($msg ne 'dns-org-mud-moo-simpleedit-content') {
256  
257 return undef;
258 }
259  
260 # Check that the required key-value pairs are all present; ignore any unexpected keys
261 if (
262 ! exists $hash{'reference'}
263 || ! exists $hash{'name'}
264 || ! exists $hash{'content'}
265 || ! exists $hash{'type'}
266 || (
267 $hash{'type'} ne 'string'
268 && $hash{'type'} ne 'string-list'
269 && $hash{'type'} ne 'moo-code'
270 )
271 ) {
272 return undef;
273 }
274  
275 if ($hash{'type'} eq 'string') {
276  
277 # $hash{'content'} represents a single-line value
278  
279 # Prompt the user to edit the value
280 $text = $session->mainWin->showEntryDialogue(
281 'MCP Edit',
282 $hash{'name'},
283 undef, # No max chars
284 $hash{'content'},
285 FALSE, # Not obscure
286 );
287  
288 if (! defined $text) {
289  
290 # Send back the unedited value
291 $text = $hash{'content'};
292 }
293  
294 # Send the MCP message (only one multiline part)
295 $session->mcpSendMultiLine(
296 'dns-org-mud-moo-simpleedit-set',
297 'reference',
298 $hash{'reference'},
299 'content',
300 $text,
301 'type',
302 $hash{'type'},
303 );
304  
305 } else {
306  
307 # $hash{'content'} represents one or more lines of text
308  
309 # Open a window to edit the text (an 'other' window). Pass it all the key-value pairs,
310 # so it can call GA::Session->mcpSendMultiLine() when the user has finished editing
311 $winObj = $session->mainWin->quickFreeWin(
312 'Games::Axmud::OtherWin::McpSimpleEdit',
313 $session,
314 # Config
315 'mcp_reference' => $hash{'reference'},
316 'mcp_name' => $hash{'name'},
317 'mcp_content' => $hash{'content'},
318 'mcp_type' => $hash{'type'},
319 );
320  
321 if (! $winObj) {
322  
323 return undef;
324  
325 } else {
326  
327 return 1;
328 }
329 }
330 }
331  
332 ##################
333 # Accessors - set
334  
335 ##################
336 # Accessors - get
337 }
338  
339 # Package must return a true value
340 1