home · contact · privacy
Make sign design sizes variable.
[plomrogue2] / plomrogue / commands.py
1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError, ArgError
3 from plomrogue.misc import Terrain
4
5
6
7 # TODO: instead of sending tasks, thing types etc. on request, send them on connection
8
9 def cmd_TASKS(game, connection_id):
10     game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
11 cmd_TASKS.argtypes = ''
12
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)),
16                      connection_id)
17 cmd_THING_TYPES.argtypes = ''
18
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 = ''
24
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'
30
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'
36
37 def cmd_ALL(game, msg, connection_id):
38     speaker = game.get_player(connection_id)
39     if not speaker:
40         raise GameError('need to be logged in for this')
41     if speaker.drunk > 0:
42         import random
43         drunken_msg = ''
44         for c in msg:
45             if random.random() > 0.85:
46                 c = random.choice(['l', 'll', 'n', 'nhg', 'w', 'wl', 'bw'])
47             drunken_msg += c
48         msg = drunken_msg
49     speaker.sound(speaker.name, msg)
50 cmd_ALL.argtypes = 'string'
51
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_point = big_yx, little_yx
57 cmd_SPAWN_POINT.argtypes = 'yx_tuple yx_tuple:nonneg'
58
59 def cmd_LOGIN(game, nick, connection_id):
60     nick = nick.strip()
61     if len(nick) == 0:
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'
67
68 def cmd_BECOME_ADMIN(game, password, connection_id):
69     player = game.get_player(connection_id)
70     if not player:
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)
75     else:
76         raise GameError('wrong password')
77 cmd_BECOME_ADMIN.argtypes = 'string'
78
79 def cmd_ADMIN_PASSWORD(game, password):
80     game.admin_passwords += [password]
81 cmd_ADMIN_PASSWORD.argtypes = 'string'
82
83 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
84     player = game.get_player(connection_id)
85     if not player:
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
92     game.changed = True
93     game.record_change((big_yx, little_yx), 'fov')
94 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
95
96 def cmd_THING_PROTECTION(game, protection_char, connection_id):
97     player = game.get_player(connection_id)
98     if not player:
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
105     game.changed = True
106     game.record_change(player.carrying.position, 'other')
107 cmd_THING_PROTECTION.argtypes = 'char'
108
109 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
110     player = game.get_player(connection_id)
111     if not player:
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'
119
120 def cmd_NICK(game, nick, connection_id):
121     nick = nick.strip()
122     if len(nick) == 0:
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)
127     if not t:
128         raise GameError('can only rename when already logged in')
129     old_nick = t.name
130     t.name = nick
131     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
132     game.changed = True
133     game.record_change(t.position, 'other')
134 cmd_NICK.argtypes = 'string'
135
136 def cmd_GET_GAMESTATE(game, connection_id):
137     game.send_gamestate(connection_id)
138 cmd_GET_GAMESTATE.argtypes = ''
139
140 # def cmd_QUERY(game, target_nick, msg, connection_id):
141 #     if not connection_id in game.sessions:
142 #         raise GameError('can only query when logged in')
143 #     t = game.get_thing(game.sessions[connection_id], False)
144 #     source_nick = t.name
145 #     for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
146 #         for c_id in game.sessions:
147 #             if game.sessions[c_id] == t.id_:
148 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
149 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
150 #                 return
151 #         raise GameError('target user offline')
152 #     raise GameError('can only query with registered nicknames')
153 # cmd_QUERY.argtypes = 'string string'
154
155 def cmd_PING(game, connection_id):
156     game.io.send('PONG', connection_id)
157 cmd_PING.argtypes = ''
158
159 def cmd_TURN(game, n):
160     game.turn = n
161 cmd_TURN.argtypes = 'int:nonneg'
162
163 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
164     if len(msg) > 500:
165         raise ArgError('annotation text must be <= 500 characters')
166     player = game.get_player(connection_id)
167     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
168     if not player.fov_test(big_yx, little_yx):
169         raise GameError('cannot annotate tile outside field of view')
170     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
171         raise GameError('wrong password for tile')
172     if msg == ' ':
173         if big_yx in game.annotations:
174             if little_yx in game.annotations[big_yx]:
175                 del game.annotations[big_yx][little_yx]
176     else:
177         if big_yx not in game.annotations:
178             game.annotations[big_yx] = {}
179         game.annotations[big_yx][little_yx] = msg
180     game.record_change([big_yx, little_yx], 'other')
181     game.changed = True
182 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
183
184 def cmd_PORTAL(game, yx, msg, pw, connection_id):
185     player = game.get_player(connection_id)
186     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
187     if not player.fov_test(big_yx, little_yx):
188         raise GameError('cannot edit portal on tile outside field of view')
189     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
190         raise GameError('wrong password for tile')
191     if msg == ' ':
192         if big_yx in game.portals:
193             if little_yx in game.portals[big_yx]:
194                 del game.portals[big_yx][little_yx]
195     else:
196         if big_yx not in game.portals:
197             game.portals[big_yx] = {}
198         game.portals[big_yx][little_yx] = msg
199     game.record_change([big_yx, little_yx], 'other')
200     game.changed = True
201 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
202
203 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
204     if big_yx not in game.annotations:
205         game.annotations[big_yx] = {}
206     game.annotations[big_yx][little_yx] = msg
207     #game.changed = True
208 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
209
210 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
211     if big_yx not in game.portals:
212         game.portals[big_yx] = {}
213     game.portals[big_yx][little_yx] = msg
214     #game.changed = True
215 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
216
217 def cmd_MAP_LINE(game, big_yx, y, line):
218     map_ = game.get_map(big_yx)
219     map_.set_line(y, line)
220 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
221
222 def cmd_MAP(game, geometry, size):
223     from plomrogue.mapping import MapGeometryHex, MapGeometrySquare
224     map_geometry_class = MapGeometrySquare
225     if geometry == 'Hex':
226         map_geometry_class = MapGeometryHex
227     game.new_world(map_geometry_class(size))
228     game.terrains = {}
229 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
230
231 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
232     map_control = game.get_map(big_yx, 'control')
233     map_control.set_line(y, line)
234 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
235
236 def cmd_MAP_CONTROL_PW(game, tile_class, password):
237     game.map_control_passwords[tile_class] = password
238 cmd_MAP_CONTROL_PW.argtypes = 'char string'
239
240 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
241     if thing_type not in game.thing_types:
242         raise GameError('illegal thing type %s' % thing_type)
243     _ = game.get_map(big_yx)
244     game.add_thing(thing_type, (big_yx, little_yx), id_=thing_id)
245 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
246
247 def cmd_THING_NAME(game, name, pw, connection_id):
248     player = game.get_player(connection_id)
249     if not player:
250         raise GameError('need to be logged in for this')
251     if not player.carrying:
252         raise GameError('need to carry a thing to rename it')
253     if not game.can_do_thing_with_pw(player.carrying, pw):
254         raise GameError('wrong password for thing')
255     if name == ' ':
256         if hasattr(player.carrying.__class__, 'name'):
257             raise GameError('cannot un-name things of this type')
258         if hasattr(player.carrying, 'name'):
259             del player.carrying.name
260     else:
261         player.carrying.name = name
262     game.changed = True
263     game.record_change(player.carrying.position, 'other')
264 cmd_THING_NAME.argtypes = 'string string'
265
266 def cmd_GOD_THING_NAME(game, thing_id, name):
267     t = game.get_thing(thing_id)
268     if not t:
269         raise GameError('thing of ID %s not found' % thing_id)
270     t.name = name
271 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
272
273 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
274     t = game.get_thing(thing_id)
275     if not t:
276         raise GameError('thing of ID %s not found' % thing_id)
277     t.protection = protection_char
278 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
279
280 def cmd_THING_DOOR_CLOSED(game, thing_id, locked):
281     t = game.get_thing(thing_id)
282     if not t:
283         raise GameError('thing of ID %s not found' % thing_id)
284     if not t.type_ == 'Door':
285         raise GameError('thing of ID %s not door' % thing_id)
286     t.close()
287     if locked:
288         t.lock()
289 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos bool'
290
291 def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
292     t = game.get_thing(thing_id)
293     if not t:
294         raise GameError('thing of ID %s not found' % thing_id)
295     if not t.type_ == 'MusicPlayer':
296         raise GameError('thing of ID %s not music player' % thing_id)
297     t.playing = playing
298     t.playlist_index = index
299     t.repeat = repeat
300 cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int bool'
301
302 def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
303     t = game.get_thing(thing_id)
304     if not t:
305         raise GameError('thing of ID %s not found' % thing_id)
306     if not t.type_ == 'MusicPlayer':
307         raise GameError('thing of ID %s not music player' % thing_id)
308     t.playlist += [(title, length)]
309 cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'
310
311 def cmd_THING_BOTTLE_EMPTY(game, thing_id):
312     t = game.get_thing(thing_id)
313     if not t:
314         raise GameError('thing of ID %s not found' % thing_id)
315     if not t.type_ == 'Bottle':
316         raise GameError('thing of ID %s not bottle' % thing_id)
317     t.empty()
318 cmd_THING_BOTTLE_EMPTY.argtypes = 'int:pos'
319
320 def cmd_THING_INSTALLED(game, thing_id):
321     t = game.get_thing(thing_id)
322     if not t:
323         raise GameError('thing of ID %s not found' % thing_id)
324     if not hasattr(t, 'installable'):
325         raise GameError('thing of ID %s not installable' % thing_id)
326     t.install()
327 cmd_THING_INSTALLED.argtypes = 'int:pos'
328
329 def cmd_PLAYER_FACE(game, face, connection_id):
330     t = game.get_player(connection_id)
331     if not t:
332         raise GameError('can only draw face when already logged in')
333     if len(face) != 18:
334         raise GameError('wrong face string length')
335     game.faces[t.name] = face
336     game.changed = True
337     game.record_change(t.position, 'other')
338 cmd_PLAYER_FACE.argtypes = 'string'
339
340 def cmd_GOD_PLAYER_FACE(game, name, face):
341     if len(face) != 18:
342         raise GameError('wrong face string length')
343     game.faces[name] = face
344 cmd_GOD_PLAYER_FACE.argtypes = 'string string'
345
346 def cmd_GOD_PLAYER_HAT(game, name, hat):
347     if len(hat) != 18:
348         raise GameError('wrong hat string length')
349     game.hats[name] = hat
350 cmd_GOD_PLAYER_HAT.argtypes = 'string string'
351
352 def cmd_GOD_PLAYERS_HAT_CHARS(game, name, hat_chars):
353     game.players_hat_chars[name] = hat_chars
354 cmd_GOD_PLAYERS_HAT_CHARS.argtypes = 'string string'
355
356 def cmd_THING_DESIGN(game, design, pw, connection_id):
357     player = game.get_player(connection_id)
358     if not player:
359         raise GameError('need to be logged in for this')
360     if not player.carrying:
361         raise GameError('need to carry a thing to re-draw it')
362     if not game.can_do_thing_with_pw(player.carrying, pw):
363         raise GameError('wrong password for thing')
364     if not hasattr(player.carrying, 'design'):
365         raise GameError('carried thing not designable')
366     if player.carrying.type_ == 'Hat':
367         legal_chars = player.get_cookie_chars()
368         for c in design:
369             if c not in legal_chars:
370                 raise GameError('used illegal character: "%s" – '
371                                 'allowed characters: %s'
372                                 % (c, legal_chars))
373     size = player.carrying.design_size
374     if len(design) != size.y * size.x:
375         raise GameError('design for carried thing of wrong length')
376     player.carrying.design = design
377     game.changed = True
378     game.record_change(player.carrying.position, 'other')
379 cmd_THING_DESIGN.argtypes = 'string string'
380
381 def cmd_THING_DESIGN_SIZE(game, size, pw, connection_id):
382     player = game.get_player(connection_id)
383     if not player:
384         raise GameError('need to be logged in for this')
385     if not player.carrying:
386         raise GameError('need to carry a thing to re-draw it')
387     if not game.can_do_thing_with_pw(player.carrying, pw):
388         raise GameError('wrong password for thing')
389     if not hasattr(player.carrying, 'design'):
390         raise GameError('carried thing not designable')
391     if player.carrying.type_ == 'Hat':
392         raise GameError('may not change Hat size')
393     player.carrying.design_size = size
394 cmd_THING_DESIGN_SIZE.argtypes = 'yx_tuple:nonneg string'
395
396 def cmd_GOD_THING_DESIGN(game, thing_id, design):
397     t = game.get_thing(thing_id)
398     if not t:
399         raise GameError('thing of ID %s not found' % thing_id)
400     if not hasattr(t, 'design'):
401         raise GameError('thing of ID %s not designable' % thing_id)
402     if len(design) != t.design_size.y * t.design_size.x:
403         raise GameError('design for thing of ID %s of wrong length' % thing_id)
404     t.design = design
405 cmd_GOD_THING_DESIGN.argtypes = 'int:pos string'
406
407 def cmd_GOD_THING_DESIGN_SIZE(game, thing_id, size):
408     t = game.get_thing(thing_id)
409     if not t:
410         raise GameError('thing of ID %s not found' % thing_id)
411     if not hasattr(t, 'design'):
412         raise GameError('thing of ID %s not designable' % thing_id)
413     if t.type_ == 'Hat':
414         raise GameError('may not change Hat size')
415     t.design_size = size
416 cmd_GOD_THING_DESIGN_SIZE.argtypes = 'int:pos yx_tuple:nonneg'
417
418 # TODO: refactor similar god and player commands
419
420 def cmd_THING_DOOR_KEY(game, key_id, door_id):
421     key = game.get_thing(key_id)
422     if not key:
423         raise GameError('thing of ID %s not found' % key_id)
424     if key.type_ != 'DoorKey':
425         raise GameError('thing of ID %s not a door key' % key_id)
426     door = game.get_thing(door_id)
427     if not door:
428         raise GameError('thing of ID %s not found' % door_id)
429     if door.type_ != 'Door':
430         raise GameError('thing of ID %s not a door' % key_id)
431     key.door = door
432 cmd_THING_DOOR_KEY.argtypes = 'int:pos int:pos'
433
434 def cmd_THING_CRATE_ITEM(game, crate_id, item_id):
435     crate = game.get_thing(crate_id)
436     if not crate:
437         raise GameError('thing of ID %s not found' % crate_id)
438     if crate.type_ != 'Crate':
439         raise GameError('thing of ID %s not a crate' % crate_id)
440     item = game.get_thing(item_id)
441     if not item:
442         raise GameError('thing of ID %s not found' % item_id)
443     if item.type_ == 'Crate':
444         raise GameError('thing of ID %s is a crate' % item_id)
445     crate.accept(item)
446 cmd_THING_CRATE_ITEM.argtypes = 'int:pos int:pos'
447
448 def cmd_MAP_CONTROL_PRESETS(game, draw_control_presets):
449     game.draw_control_presets = draw_control_presets
450 cmd_MAP_CONTROL_PRESETS.argtypes = 'bool'
451
452 def cmd_THING_SPAWNPOINT_CREATED(game, spawnpoint_id, timestamp):
453     import datetime
454     spawnpoint = game.get_thing(spawnpoint_id)
455     if not spawnpoint:
456         raise GameError('thing of ID %s not found' % spawnpoint_id)
457     if spawnpoint.type_ != 'SpawnPoint':
458         raise GameError('thing of ID %s not a SpawnPoint' % spawnpoint_id)
459     if timestamp == 0:
460         spawnpoint.temporary = False
461     else:
462         spawnpoint.temporary = True
463         spawnpoint.created_at = datetime.datetime.fromtimestamp(timestamp)
464 cmd_THING_SPAWNPOINT_CREATED.argtypes = 'int:pos int:nonneg'