1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError, ArgError
3 from plomrogue.misc import Terrain
7 # TODO: instead of sending tasks, thing types etc. on request, send them on connection
9 def cmd_TASKS(game, connection_id):
10 game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
11 cmd_TASKS.argtypes = ''
13 def cmd_THING_TYPES(game, connection_id):
14 for t_t in game.thing_types.values():
15 game.io.send('THING_TYPE %s %s' % (t_t.get_type(), quote(t_t.symbol_hint)),
17 cmd_THING_TYPES.argtypes = ''
19 def cmd_TERRAINS(game, connection_id):
20 for t in game.terrains.values():
21 game.io.send('TERRAIN %s %s' % (quote(t.character),
22 quote(t.description)), connection_id)
23 cmd_TERRAINS.argtypes = ''
25 def cmd_TERRAIN(game, character, description,
26 blocks_light, blocks_sound, blocks_movement):
27 game.terrains[character] = Terrain(character, description, blocks_light,
28 blocks_sound, blocks_movement)
29 cmd_TERRAIN.argtypes = 'char string bool bool bool'
31 def cmd_TERRAIN_TAG(game, character, tag):
32 if not character in game.terrains:
33 raise GameError('terrain does not exist: %s' % character)
34 game.terrains[character].tags.add(tag)
35 cmd_TERRAIN_TAG.argtypes = 'char string'
37 def cmd_ALL(game, msg, connection_id):
38 speaker = game.get_player(connection_id)
40 raise GameError('need to be logged in for this')
45 if random.random() > 0.85:
46 c = random.choice(['l', 'll', 'n', 'nhg', 'w', 'wl', 'bw'])
49 speaker.sound(speaker.name, msg)
50 cmd_ALL.argtypes = 'string'
52 def cmd_SPAWN_POINT(game, big_yx, little_yx):
53 if little_yx.y >= game.map_geometry.size.y or \
54 little_yx.x >= game.map_geometry.size.x:
55 raise GameError('illegal spawn point')
56 game.spawn_points += [(big_yx, little_yx)]
57 cmd_SPAWN_POINT.argtypes = 'yx_tuple yx_tuple:nonneg'
59 def cmd_LOGIN(game, nick, connection_id):
62 raise GameError('empty name')
63 if game.get_player(connection_id):
64 raise GameError('cannot log in twice')
65 game.login_requests += [(nick, connection_id)]
66 cmd_LOGIN.argtypes = 'string'
68 def cmd_BECOME_ADMIN(game, password, connection_id):
69 player = game.get_player(connection_id)
71 raise GameError('need to be logged in for this')
72 if password in game.admin_passwords:
73 game.sessions[connection_id]['status'] = 'admin'
74 game.io.send('ADMIN_OK', connection_id)
76 raise GameError('wrong password')
77 cmd_BECOME_ADMIN.argtypes = 'string'
79 def cmd_ADMIN_PASSWORD(game, password):
80 game.admin_passwords += [password]
81 cmd_ADMIN_PASSWORD.argtypes = 'string'
83 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
84 player = game.get_player(connection_id)
86 raise GameError('need to be logged in for this')
87 if not game.sessions[connection_id]['status'] == 'admin':
88 raise GameError('need to be admin for this')
89 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
90 map_control = game.get_map(big_yx, 'control')
91 map_control[little_yx] = control_char
93 game.record_change((big_yx, little_yx), 'fov')
94 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
96 def cmd_THING_PROTECTION(game, protection_char, connection_id):
97 player = game.get_player(connection_id)
99 raise GameError('need to be logged in for this')
100 if not game.sessions[connection_id]['status'] == 'admin':
101 raise GameError('need to be admin for this')
102 if not player.carrying:
103 raise GameError('need to carry a thing to protect it')
104 player.carrying.protection = protection_char
106 game.record_change(player.carrying.position, 'other')
107 cmd_THING_PROTECTION.argtypes = 'char'
109 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
110 player = game.get_player(connection_id)
112 raise GameError('need to be logged in for this')
113 if not game.sessions[connection_id]['status'] == 'admin':
114 raise GameError('need to be admin for this')
115 if tile_class == '.':
116 raise GameError('tile class "." must remain unprotected')
117 game.map_control_passwords[tile_class] = password
118 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
120 def cmd_NICK(game, nick, connection_id):
123 raise GameError('empty name')
124 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
125 raise GameError('name already in use')
126 t = game.get_player(connection_id)
128 raise GameError('can only rename when already logged in')
131 print('DEBUG RENAME %s %s' % (old_nick, nick))
132 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
134 game.record_change(t.position, 'other')
135 cmd_NICK.argtypes = 'string'
137 def cmd_GET_GAMESTATE(game, connection_id):
138 game.send_gamestate(connection_id)
139 cmd_GET_GAMESTATE.argtypes = ''
141 # def cmd_QUERY(game, target_nick, msg, connection_id):
142 # if not connection_id in game.sessions:
143 # raise GameError('can only query when logged in')
144 # t = game.get_thing(game.sessions[connection_id], False)
145 # source_nick = t.name
146 # for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
147 # for c_id in game.sessions:
148 # if game.sessions[c_id] == t.id_:
149 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
150 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
152 # raise GameError('target user offline')
153 # raise GameError('can only query with registered nicknames')
154 # cmd_QUERY.argtypes = 'string string'
156 def cmd_PING(game, connection_id):
157 game.io.send('PONG', connection_id)
158 cmd_PING.argtypes = ''
160 def cmd_TURN(game, n):
162 cmd_TURN.argtypes = 'int:nonneg'
164 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
166 raise ArgError('annotation text must be <= 500 characters')
167 player = game.get_player(connection_id)
168 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
169 if not player.fov_test(big_yx, little_yx):
170 raise GameError('cannot annotate tile outside field of view')
171 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
172 raise GameError('wrong password for tile')
174 if big_yx in game.annotations:
175 if little_yx in game.annotations[big_yx]:
176 del game.annotations[big_yx][little_yx]
178 if big_yx not in game.annotations:
179 game.annotations[big_yx] = {}
180 game.annotations[big_yx][little_yx] = msg
181 game.record_change([big_yx, little_yx], 'other')
183 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
185 def cmd_PORTAL(game, yx, msg, pw, connection_id):
186 player = game.get_player(connection_id)
187 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
188 if not player.fov_test(big_yx, little_yx):
189 raise GameError('cannot edit portal on tile outside field of view')
190 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
191 raise GameError('wrong password for tile')
193 if big_yx in game.portals:
194 if little_yx in game.portals[big_yx]:
195 del game.portals[big_yx][little_yx]
197 if big_yx not in game.portals:
198 game.portals[big_yx] = {}
199 game.portals[big_yx][little_yx] = msg
200 game.record_change([big_yx, little_yx], 'other')
202 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
204 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
205 if big_yx not in game.annotations:
206 game.annotations[big_yx] = {}
207 game.annotations[big_yx][little_yx] = msg
209 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
211 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
212 if big_yx not in game.portals:
213 game.portals[big_yx] = {}
214 game.portals[big_yx][little_yx] = msg
216 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
218 def cmd_MAP_LINE(game, big_yx, y, line):
219 map_ = game.get_map(big_yx)
220 map_.set_line(y, line)
221 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
223 def cmd_MAP(game, geometry, size):
224 from plomrogue.mapping import MapGeometryHex, MapGeometrySquare
225 map_geometry_class = MapGeometrySquare
226 if geometry == 'Hex':
227 map_geometry_class = MapGeometryHex
228 game.new_world(map_geometry_class(size))
230 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
232 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
233 map_control = game.get_map(big_yx, 'control')
234 map_control.set_line(y, line)
235 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
237 def cmd_MAP_CONTROL_PW(game, tile_class, password):
238 game.map_control_passwords[tile_class] = password
239 cmd_MAP_CONTROL_PW.argtypes = 'char string'
241 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
242 if thing_type not in game.thing_types:
243 raise GameError('illegal thing type %s' % thing_type)
244 _ = game.get_map(big_yx)
245 game.add_thing(thing_type, (big_yx, little_yx), id_=thing_id)
246 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
248 def cmd_THING_NAME(game, name, pw, connection_id):
249 player = game.get_player(connection_id)
251 raise GameError('need to be logged in for this')
252 if not player.carrying:
253 raise GameError('need to carry a thing to rename it')
254 if not game.can_do_thing_with_pw(player.carrying, pw):
255 raise GameError('wrong password for thing')
257 if hasattr(player.carrying.__class__, 'name'):
258 raise GameError('cannot un-name things of this type')
259 if hasattr(player.carrying, 'name'):
260 del player.carrying.name
262 player.carrying.name = name
264 game.record_change(player.carrying.position, 'other')
265 cmd_THING_NAME.argtypes = 'string string'
267 def cmd_GOD_THING_NAME(game, thing_id, name):
268 t = game.get_thing(thing_id)
270 raise GameError('thing of ID %s not found' % thing_id)
272 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
274 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
275 t = game.get_thing(thing_id)
277 raise GameError('thing of ID %s not found' % thing_id)
278 t.protection = protection_char
279 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
281 def cmd_THING_DOOR_CLOSED(game, thing_id, locked):
282 t = game.get_thing(thing_id)
284 raise GameError('thing of ID %s not found' % thing_id)
285 if not t.type_ == 'Door':
286 raise GameError('thing of ID %s not door' % thing_id)
290 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos bool'
292 def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
293 t = game.get_thing(thing_id)
295 raise GameError('thing of ID %s not found' % thing_id)
296 if not t.type_ == 'MusicPlayer':
297 raise GameError('thing of ID %s not music player' % thing_id)
299 t.playlist_index = index
301 cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int bool'
303 def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
304 t = game.get_thing(thing_id)
306 raise GameError('thing of ID %s not found' % thing_id)
307 if not t.type_ == 'MusicPlayer':
308 raise GameError('thing of ID %s not music player' % thing_id)
309 t.playlist += [(title, length)]
310 cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'
312 def cmd_THING_BOTTLE_EMPTY(game, thing_id):
313 t = game.get_thing(thing_id)
315 raise GameError('thing of ID %s not found' % thing_id)
316 if not t.type_ == 'Bottle':
317 raise GameError('thing of ID %s not bottle' % thing_id)
319 cmd_THING_BOTTLE_EMPTY.argtypes = 'int:pos'
321 def cmd_THING_INSTALLED(game, thing_id):
322 t = game.get_thing(thing_id)
324 raise GameError('thing of ID %s not found' % thing_id)
325 if not hasattr(t, 'installable'):
326 raise GameError('thing of ID %s not installable' % thing_id)
328 cmd_THING_INSTALLED.argtypes = 'int:pos'
330 def cmd_PLAYER_FACE(game, face, connection_id):
331 t = game.get_player(connection_id)
333 raise GameError('can only draw face when already logged in')
335 raise GameError('wrong face string length')
336 game.faces[t.name] = face
338 game.record_change(t.position, 'other')
339 cmd_PLAYER_FACE.argtypes = 'string'
341 def cmd_GOD_PLAYER_FACE(game, name, face):
343 raise GameError('wrong face string length')
344 game.faces[name] = face
345 cmd_GOD_PLAYER_FACE.argtypes = 'string string'
347 def cmd_GOD_PLAYER_HAT(game, name, hat):
349 raise GameError('wrong hat string length')
350 game.hats[name] = hat
351 cmd_GOD_PLAYER_HAT.argtypes = 'string string'
353 def cmd_GOD_PLAYERS_HAT_CHARS(game, name, hat_chars):
354 game.players_hat_chars[name] = hat_chars
355 cmd_GOD_PLAYERS_HAT_CHARS.argtypes = 'string string'
357 def cmd_THING_DESIGN(game, design, pw, connection_id):
358 player = game.get_player(connection_id)
360 raise GameError('need to be logged in for this')
361 if not player.carrying:
362 raise GameError('need to carry a thing to re-draw it')
363 if not game.can_do_thing_with_pw(player.carrying, pw):
364 raise GameError('wrong password for thing')
365 if not hasattr(player.carrying, 'design'):
366 raise GameError('carried thing not designable')
367 if player.carrying.type_ == 'Hat':
368 legal_chars = player.get_cookie_chars()
370 if c not in legal_chars:
371 raise GameError('used illegal character: "%s" – '
372 'allowed characters: %s'
374 size = player.carrying.design_size
375 if len(design) != size.y * size.x:
376 raise GameError('design for carried thing of wrong length')
377 player.carrying.design = design
379 game.record_change(player.carrying.position, 'other')
380 cmd_THING_DESIGN.argtypes = 'string string'
382 def cmd_THING_DESIGN_SIZE(game, size, pw, connection_id):
383 player = game.get_player(connection_id)
385 raise GameError('need to be logged in for this')
386 if not player.carrying:
387 raise GameError('need to carry a thing to re-draw it')
388 if not game.can_do_thing_with_pw(player.carrying, pw):
389 raise GameError('wrong password for thing')
390 if not hasattr(player.carrying, 'design'):
391 raise GameError('carried thing not designable')
392 if player.carrying.type_ == 'Hat':
393 raise GameError('may not change Hat size')
394 player.carrying.design_size = size
395 cmd_THING_DESIGN_SIZE.argtypes = 'yx_tuple:nonneg string'
397 def cmd_GOD_THING_DESIGN(game, thing_id, design):
398 t = game.get_thing(thing_id)
400 raise GameError('thing of ID %s not found' % thing_id)
401 if not hasattr(t, 'design'):
402 raise GameError('thing of ID %s not designable' % thing_id)
403 if len(design) != t.design_size.y * t.design_size.x:
404 raise GameError('design for thing of ID %s of wrong length' % thing_id)
406 cmd_GOD_THING_DESIGN.argtypes = 'int:pos string'
408 def cmd_GOD_THING_DESIGN_SIZE(game, thing_id, size):
409 t = game.get_thing(thing_id)
411 raise GameError('thing of ID %s not found' % thing_id)
412 if not hasattr(t, 'design'):
413 raise GameError('thing of ID %s not designable' % thing_id)
415 raise GameError('may not change Hat size')
417 cmd_GOD_THING_DESIGN_SIZE.argtypes = 'int:pos yx_tuple:nonneg'
419 # TODO: refactor similar god and player commands
421 def cmd_THING_DOOR_KEY(game, key_id, door_id):
422 key = game.get_thing(key_id)
424 raise GameError('thing of ID %s not found' % key_id)
425 if key.type_ != 'DoorKey':
426 raise GameError('thing of ID %s not a door key' % key_id)
427 door = game.get_thing(door_id)
429 raise GameError('thing of ID %s not found' % door_id)
430 if door.type_ != 'Door':
431 raise GameError('thing of ID %s not a door' % key_id)
433 cmd_THING_DOOR_KEY.argtypes = 'int:pos int:pos'
435 def cmd_THING_CRATE_ITEM(game, crate_id, item_id):
436 crate = game.get_thing(crate_id)
438 raise GameError('thing of ID %s not found' % crate_id)
439 if crate.type_ != 'Crate':
440 raise GameError('thing of ID %s not a crate' % crate_id)
441 item = game.get_thing(item_id)
443 raise GameError('thing of ID %s not found' % item_id)
444 if item.type_ == 'Crate':
445 raise GameError('thing of ID %s is a crate' % item_id)
447 cmd_THING_CRATE_ITEM.argtypes = 'int:pos int:pos'
449 def cmd_MAP_CONTROL_PRESETS(game, draw_control_presets):
450 game.draw_control_presets = draw_control_presets
451 cmd_MAP_CONTROL_PRESETS.argtypes = 'int:nonneg'
453 def cmd_THING_SPAWNPOINT_CREATED(game, spawnpoint_id, timestamp):
455 spawnpoint = game.get_thing(spawnpoint_id)
457 raise GameError('thing of ID %s not found' % spawnpoint_id)
458 if spawnpoint.type_ != 'SpawnPoint':
459 raise GameError('thing of ID %s not a SpawnPoint' % spawnpoint_id)
461 spawnpoint.temporary = False
463 spawnpoint.temporary = True
464 spawnpoint.created_at = datetime.datetime.fromtimestamp(timestamp)
465 cmd_THING_SPAWNPOINT_CREATED.argtypes = 'int:pos int:nonneg'
467 def cmd_INTRO_MSG(game, msg):
468 game.intro_messages += [msg]
469 cmd_INTRO_MSG.argtypes = 'string'