home · contact · privacy
50445e7f9c62f557b098d7e4781723679c11c74d
[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_ALL(game, msg, connection_id):
32     speaker = game.get_player(connection_id)
33     if not speaker:
34         raise GameError('need to be logged in for this')
35     if speaker.drunk > 0:
36         import random
37         drunken_msg = ''
38         for c in msg:
39             if random.random() > 0.85:
40                 c = random.choice(['l', 'll', 'n', 'nhg', 'w', 'wl', 'bw'])
41             drunken_msg += c
42         msg = drunken_msg
43     speaker.sound(speaker.name, msg)
44 cmd_ALL.argtypes = 'string'
45
46 def cmd_SPAWN_POINT(game, big_yx, little_yx):
47     if little_yx.y >= game.map_geometry.size.y or \
48        little_yx.x >= game.map_geometry.size.x:
49         raise GameError('illegal spawn point')
50     game.spawn_point = big_yx, little_yx
51 cmd_SPAWN_POINT.argtypes = 'yx_tuple yx_tuple:nonneg'
52
53 def cmd_LOGIN(game, nick, connection_id):
54     nick = nick.strip()
55     if len(nick) == 0:
56         raise GameError('empty name')
57     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
58         raise GameError('name already in use')
59     if game.get_player(connection_id):
60         raise GameError('cannot log in twice')
61     t = game.add_thing('Player', game.spawn_point)
62     t.name = nick
63     t.thing_char = game.get_next_player_char()
64     game.sessions[connection_id] = {
65         'thing_id': t.id_,
66         'status': 'player'
67     }
68     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
69     game.io.send('LOGIN_OK', connection_id)
70     game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
71     for s in [s for s in game.things
72               if s.type_ == 'SpawnPoint' and s.name == t.name]:
73         t.position = s.position
74         break
75     # game.changed = True  # handled by game.add_thing
76 cmd_LOGIN.argtypes = 'string'
77
78 def cmd_BECOME_ADMIN(game, password, connection_id):
79     player = game.get_player(connection_id)
80     if not player:
81         raise GameError('need to be logged in for this')
82     if password in game.admin_passwords:
83         game.sessions[connection_id]['status'] = 'admin'
84         game.io.send('ADMIN_OK', connection_id)
85     else:
86         raise GameError('wrong password')
87 cmd_BECOME_ADMIN.argtypes = 'string'
88
89 def cmd_ADMIN_PASSWORD(game, password):
90     game.admin_passwords += [password]
91 cmd_ADMIN_PASSWORD.argtypes = 'string'
92
93 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
94     player = game.get_player(connection_id)
95     if not player:
96         raise GameError('need to be logged in for this')
97     if not game.sessions[connection_id]['status'] == 'admin':
98         raise GameError('need to be admin for this')
99     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
100     map_control = game.get_map(big_yx, 'control')
101     map_control[little_yx] = control_char
102     game.changed = True
103     game.record_change((big_yx, little_yx), 'fov')
104 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
105
106 def cmd_THING_PROTECTION(game, protection_char, connection_id):
107     player = game.get_player(connection_id)
108     if not player:
109         raise GameError('need to be logged in for this')
110     if not game.sessions[connection_id]['status'] == 'admin':
111         raise GameError('need to be admin for this')
112     if not player.carrying:
113         raise GameError('need to carry a thing to protect it')
114     player.carrying.protection = protection_char
115     game.changed = True
116     game.record_change(player.carrying.position, 'other')
117 cmd_THING_PROTECTION.argtypes = 'char'
118
119 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
120     player = game.get_player(connection_id)
121     if not player:
122         raise GameError('need to be logged in for this')
123     if not game.sessions[connection_id]['status'] == 'admin':
124         raise GameError('need to be admin for this')
125     if tile_class == '.':
126         raise GameError('tile class "." must remain unprotected')
127     game.map_control_passwords[tile_class] = password
128 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
129
130 def cmd_NICK(game, nick, connection_id):
131     nick = nick.strip()
132     if len(nick) == 0:
133         raise GameError('empty name')
134     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
135         raise GameError('name already in use')
136     t = game.get_player(connection_id)
137     if not t:
138         raise GameError('can only rename when already logged in')
139     old_nick = t.name
140     t.name = nick
141     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
142     game.changed = True
143     game.record_change(t.position, 'other')
144 cmd_NICK.argtypes = 'string'
145
146 def cmd_GET_GAMESTATE(game, connection_id):
147     game.send_gamestate(connection_id)
148 cmd_GET_GAMESTATE.argtypes = ''
149
150 # def cmd_QUERY(game, target_nick, msg, connection_id):
151 #     if not connection_id in game.sessions:
152 #         raise GameError('can only query when logged in')
153 #     t = game.get_thing(game.sessions[connection_id], False)
154 #     source_nick = t.name
155 #     for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
156 #         for c_id in game.sessions:
157 #             if game.sessions[c_id] == t.id_:
158 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
159 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
160 #                 return
161 #         raise GameError('target user offline')
162 #     raise GameError('can only query with registered nicknames')
163 # cmd_QUERY.argtypes = 'string string'
164
165 def cmd_PING(game, connection_id):
166     game.io.send('PONG', connection_id)
167 cmd_PING.argtypes = ''
168
169 def cmd_TURN(game, n):
170     game.turn = n
171 cmd_TURN.argtypes = 'int:nonneg'
172
173 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
174     if len(msg) > 500:
175         raise ArgError('annotation text must be <= 500 characters')
176     player = game.get_player(connection_id)
177     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
178     if not player.fov_test(big_yx, little_yx):
179         raise GameError('cannot annotate tile outside field of view')
180     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
181         raise GameError('wrong password for tile')
182     if msg == ' ':
183         if big_yx in game.annotations:
184             if little_yx in game.annotations[big_yx]:
185                 del game.annotations[big_yx][little_yx]
186     else:
187         if big_yx not in game.annotations:
188             game.annotations[big_yx] = {}
189         game.annotations[big_yx][little_yx] = msg
190     game.record_change([big_yx, little_yx], 'other')
191     game.changed = True
192 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
193
194 def cmd_PORTAL(game, yx, msg, pw, connection_id):
195     player = game.get_player(connection_id)
196     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
197     if not player.fov_test(big_yx, little_yx):
198         raise GameError('cannot edit portal on tile outside field of view')
199     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
200         raise GameError('wrong password for tile')
201     if msg == ' ':
202         if big_yx in game.portals:
203             if little_yx in game.portals[big_yx]:
204                 del game.portals[big_yx][little_yx]
205     else:
206         if big_yx not in game.portals:
207             game.portals[big_yx] = {}
208         game.portals[big_yx][little_yx] = msg
209     game.record_change([big_yx, little_yx], 'other')
210     game.changed = True
211 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
212
213 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
214     if big_yx not in game.annotations:
215         game.annotations[big_yx] = {}
216     game.annotations[big_yx][little_yx] = msg
217     #game.changed = True
218 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
219
220 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
221     if big_yx not in game.portals:
222         game.portals[big_yx] = {}
223     game.portals[big_yx][little_yx] = msg
224     #game.changed = True
225 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
226
227 def cmd_MAP_LINE(game, big_yx, y, line):
228     map_ = game.get_map(big_yx)
229     map_.set_line(y, line)
230 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
231
232 def cmd_MAP(game, geometry, size):
233     from plomrogue.mapping import MapGeometryHex, MapGeometrySquare
234     map_geometry_class = MapGeometrySquare
235     if geometry == 'Hex':
236         map_geometry_class = MapGeometryHex
237     game.new_world(map_geometry_class(size))
238     game.terrains = {}
239 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
240
241 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
242     map_control = game.get_map(big_yx, 'control')
243     map_control.set_line(y, line)
244 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
245
246 def cmd_MAP_CONTROL_PW(game, tile_class, password):
247     game.map_control_passwords[tile_class] = password
248 cmd_MAP_CONTROL_PW.argtypes = 'char string'
249
250 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
251     if thing_type not in game.thing_types:
252         raise GameError('illegal thing type %s' % thing_type)
253     _ = game.get_map(big_yx)
254     game.add_thing(thing_type, (big_yx, little_yx), id_=thing_id)
255 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
256
257 def cmd_THING_NAME(game, name, pw, connection_id):
258     player = game.get_player(connection_id)
259     if not player:
260         raise GameError('need to be logged in for this')
261     if not player.carrying:
262         raise GameError('need to carry a thing to rename it')
263     if not game.can_do_thing_with_pw(player.carrying, pw):
264         raise GameError('wrong password for thing')
265     if name == ' ':
266         if hasattr(player.carrying.__class__, 'name'):
267             raise GameError('cannot un-name things of this type')
268         if hasattr(player.carrying, 'name'):
269             del player.carrying.name
270     else:
271         player.carrying.name = name
272     game.changed = True
273     game.record_change(player.carrying.position, 'other')
274 cmd_THING_NAME.argtypes = 'string string'
275
276 def cmd_GOD_THING_NAME(game, thing_id, name):
277     t = game.get_thing(thing_id)
278     if not t:
279         raise GameError('thing of ID %s not found' % thing_id)
280     t.name = name
281 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
282
283 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
284     t = game.get_thing(thing_id)
285     if not t:
286         raise GameError('thing of ID %s not found' % thing_id)
287     t.protection = protection_char
288 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
289
290 def cmd_THING_DOOR_CLOSED(game, thing_id):
291     t = game.get_thing(thing_id)
292     if not t:
293         raise GameError('thing of ID %s not found' % thing_id)
294     if not t.type_ == 'Door':
295         raise GameError('thing of ID %s not door' % thing_id)
296     t.close()
297 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'
298
299 def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
300     t = game.get_thing(thing_id)
301     if not t:
302         raise GameError('thing of ID %s not found' % thing_id)
303     if not t.type_ == 'MusicPlayer':
304         raise GameError('thing of ID %s not music player' % thing_id)
305     t.playing = playing
306     t.playlist_index = index
307     t.repeat = repeat
308 cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int bool'
309
310 def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
311     t = game.get_thing(thing_id)
312     if not t:
313         raise GameError('thing of ID %s not found' % thing_id)
314     if not t.type_ == 'MusicPlayer':
315         raise GameError('thing of ID %s not music player' % thing_id)
316     t.playlist += [(title, length)]
317 cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'
318
319 def cmd_THING_BOTTLE_EMPTY(game, thing_id):
320     t = game.get_thing(thing_id)
321     if not t:
322         raise GameError('thing of ID %s not found' % thing_id)
323     if not t.type_ == 'Bottle':
324         raise GameError('thing of ID %s not bottle' % thing_id)
325     t.empty()
326 cmd_THING_BOTTLE_EMPTY.argtypes = 'int:pos'
327
328 def cmd_THING_INSTALLED(game, thing_id):
329     t = game.get_thing(thing_id)
330     if not t:
331         raise GameError('thing of ID %s not found' % thing_id)
332     if not hasattr(t, 'installable'):
333         raise GameError('thing of ID %s not installable' % thing_id)
334     t.install()
335 cmd_THING_INSTALLED.argtypes = 'int:pos'
336
337 def cmd_PLAYER_FACE(game, face, connection_id):
338     t = game.get_player(connection_id)
339     if not t:
340         raise GameError('can only draw face when already logged in')
341     if len(face) != 18:
342         raise GameError('wrong face string length')
343     game.faces[t.name] = face
344     game.changed = True
345     game.record_change(t.position, 'other')
346 cmd_PLAYER_FACE.argtypes = 'string'
347
348 def cmd_PLAYER_HAT(game, hat, connection_id):
349     t = game.get_player(connection_id)
350     if not t:
351         raise GameError('can only edit hat when already logged in')
352     if not t.name in game.hats:
353         raise GameError('not currently wearing an editable hat')
354     if len(hat) != 18:
355         raise GameError('wrong hat string length')
356     legal_chars = t.get_cookie_chars()
357     for c in hat:
358         if c not in legal_chars:
359             raise GameError('used illegal character: "%s" – '
360                             'allowed characters: %s'
361                             % (c, legal_chars))
362     game.hats[t.name] = hat
363     game.changed = True
364     game.record_change(t.position, 'other')
365 cmd_PLAYER_HAT.argtypes = 'string'
366
367 def cmd_GOD_PLAYER_FACE(game, name, face):
368     if len(face) != 18:
369         raise GameError('wrong face string length')
370     game.faces[name] = face
371 cmd_GOD_PLAYER_FACE.argtypes = 'string string'
372
373 def cmd_GOD_PLAYER_HAT(game, name, hat):
374     if len(hat) != 18:
375         raise GameError('wrong hat string length')
376     game.hats[name] = hat
377 cmd_GOD_PLAYER_HAT.argtypes = 'string string'
378
379 def cmd_GOD_PLAYERS_HAT_CHARS(game, name, hat_chars):
380     game.players_hat_chars[name] = hat_chars
381 cmd_GOD_PLAYERS_HAT_CHARS.argtypes = 'string string'
382
383 def cmd_THING_HAT_DESIGN(game, thing_id, design):
384     if len(design) != 18:
385         raise GameError('hat design of wrong length')
386     t = game.get_thing(thing_id)
387     if not t:
388         raise GameError('thing of ID %s not found' % thing_id)
389     if t.type_ != 'Hat':
390         raise GameError('thing of ID %s not a hat' % thing_id)
391     t.design = design
392 cmd_THING_HAT_DESIGN.argtypes = 'int:pos string'