-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.command.php
More file actions
236 lines (199 loc) · 6.33 KB
/
class.command.php
File metadata and controls
236 lines (199 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
class HelpCommand extends Command {
}
class CommCommand extends Command {
}
class UnknownCommand extends Command {
}
class ExitsCommand extends Command {
public function perform() {
parent::perform();
return "Exits: " . $this->user->room->exits() . "\r\n";
}
}
class WhoCommand extends Command {
public function perform() {
parent::perform();
return "Not implemented\r\n";
}
}
class LookCommand extends Command {
public function perform() {
parent::perform();
return $this->user->room->name() . "\r\n" . $this->user->room->description() . "\r\n[Exits: " . $this->user->room->exits() . "]\r\n\r\n";
}
}
class MoveCommand extends Command {
public function perform() {
parent::perform();
$exits = explode(',', $this->user->room->exits(','));
if( in_array($this->cmds[0], $exits) ) {
$room = $this->user->room->getWorld()->traverse($this->user->room, $this->cmds[0]);
if( is_null($room) ) {
return "You cannot go that direction\r\n";
}
else {
$this->user->setRoom($room);
return $this->user->room->name() . "\r\n" . $this->user->room->description()
. "\r\n[Exits: " . $this->user->room->exits() . "]\r\n\r\n";
}
}
else {
return "You cannot go that way\r\n";
}
}
}
class QuitCommand extends Command {
// it tis what it is
}
class WhoCommand extends Command {
//get list of players online
}
class KillCommand extends Command {
//starts the combat loop
}
class Command {
protected $user, $world, $cmds;
public function __construct(User $user, World $world, Array $cmds) {
$this->user = $user;
$this->world = $world;
$this->cmds = $cmds;
}
public function perform() {
return null;
}
public function argc() {
return count($this->cmds);
}
public function argv($index) {
return isset($this->cmds[$index])?$this->cmds[$index]:null;
}
}
class InputHandler {
// this file maps input commands to command Classes/Objects
private $user, $world;
public function __construct(User $user, World $world) {
$this->user = $user;
$this->world = $world;
}
public function execute($cmd) {
$cmds = $this->explode_ex(' ', $cmd);
// blank commands shouldn't get here, but we'll double-check anyway
//
if( count($cmds) == 0 ) return $this->constructCommand("UnknownCommand", $cmds);
echo "[InputHandler->execute] Got " . json_encode($cmds) . "\n";
switch($cmds[0]) {
case 'quit':
case 'q':
return $this->constructCommand("QuitCommand", $cmds);
break;
case 'look':
case 'l':
return $this->constructCommand("LookCommand", $cmds);
break;
case 'north':
case 'n':
case 'south':
case 's':
case 'east':
case 'e':
case 'west':
case 'w':
case 'up':
case 'u':
case 'down':
case 'd':
return $this->constructCommand("MoveCommand", $cmds);
break;
case 'who':
return $this->constructCommand("WhoCommand", $cmds);
break;
case 'help':
return $this->constructCommand("HelpCommand", $cmds);
break;
case 'say':
case 'shout':
case 'gossip':
case 'tell':
return $this->constructCommand("CommCommand", $cmds);
break;
case 'inventory':
case 'i':
return $this->constructCommand("InventoryCommand", $cmds);
break;
case 'equipment':
case 'eq':
return $this->constructCommand("EquipmentCommand", $cmds);
break;
case 'get':
case 'drop':
case 'wear':
case 'remove':
case 'wield':
case 'hold':
return $this->constructCommand("ActionCommand", $cmds);
break;
case 'score':
return $this->constructCommand("ScoreCommand", $cmds);
break;
case 'sleep':
case 'rest':
case 'wake':
case 'stand':
return $this->constructCommand("StateCommand", $cmds);
break;
case 'consider':
case 'con':
return $this->constructCommand("AssessCommand", $cmds);
break;
case 'kill':
return $this->constructCommand("AttackCommand", $cmds);
break;
case 'assist':
return $this->constructCommand("AssistCommand", $cmds);
break;
case 'flee':
return $this->constructCommand("FleeCommand", $cmds);
break;
case 'cast':
return $this->constructCommand("CastCommand", $cmds);
break;
case 'rent':
return $this->constructCommand("RentCommand", $cmds);
break;
case 'eat':
case 'drink':
case 'quaff':
return $this->constructCommand("DrinkCommand", $cmds);
break;
case 'time':
return $this->constructCommand("ServerCommand", $cmds);
break;
case 'aff':
return $this->constructCommand("AffectsCommand", $cmds);
break;
default:
return $this->constructCommand("UnknownCommand", $cmds);
}
}
private function constructCommand($command_class, Array $cmds) {
return new $command_class($this->user, $this->world, $cmds);
}
private function explode_ex($delimiter, $str) {
$parts = [];
$part = '';
for($i = 0; $i < strlen($str); $i++) {
if( $str[$i] == $delimiter && strlen($part) > 0 ) {
$parts[] = $part;
$part = '';
}
else {
$part .= $str[$i];
}
}
if( strlen($part) > 0 ) {
$parts[] = $part;
}
return $parts;
}
}