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