home · contact · privacy
8ea4ff34a738615c53b65f9349f905e483d39169
[plomrogue2] / plomrogue / commands.py
1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError, ArgError
3
4
5
6 # TODO: instead of sending tasks, thing types etc. on request, send them on connection
7
8 def cmd_TASKS(game, connection_id):
9     game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
10 cmd_TASKS.argtypes = ''
11
12 def cmd_THING_TYPES(game, connection_id):
13     for t_t in game.thing_types.values():
14         game.io.send('THING_TYPE %s %s' % (t_t.get_type(), quote(t_t.symbol_hint)),
15                      connection_id)
16 cmd_THING_TYPES.argtypes = ''
17
18 def cmd_TERRAINS(game, connection_id):
19     for t in game.terrains.keys():
20         game.io.send('TERRAIN %s %s' % (quote(t), quote(game.terrains[t])),
21                      connection_id)
22 cmd_TERRAINS.argtypes = ''
23
24 def cmd_ALL(game, msg, connection_id):
25     speaker = game.get_player(connection_id)
26     if not speaker:
27         raise GameError('need to be logged in for this')
28     speaker.sound(speaker.name, msg)
29 cmd_ALL.argtypes = 'string'
30
31 def cmd_SPAWN_POINT(game, big_yx, little_yx):
32     if little_yx.y >= game.map_geometry.size.y or \
33        little_yx.x >= game.map_geometry.size.x:
34         raise GameError('illegal spawn point')
35     game.spawn_point = big_yx, little_yx
36 cmd_SPAWN_POINT.argtypes = 'yx_tuple yx_tuple:nonneg'
37
38 def cmd_LOGIN(game, nick, connection_id):
39     nick = nick.strip()
40     if len(nick) == 0:
41         raise GameError('empty name')
42     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
43         raise GameError('name already in use')
44     if game.get_player(connection_id):
45         raise GameError('cannot log in twice')
46     t = game.add_thing('Player', game.spawn_point)
47     t.name = nick
48     t.thing_char = game.get_next_player_char()
49     game.sessions[connection_id] = {
50         'thing_id': t.id_,
51         'status': 'player'
52     }
53     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
54     game.io.send('LOGIN_OK', connection_id)
55     game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
56     for s in [s for s in game.things
57               if s.type_ == 'SpawnPoint' and s.name == t.name]:
58         t.position = s.position
59         break
60     # game.changed = True  # handled by game.add_thing
61 cmd_LOGIN.argtypes = 'string'
62
63 def cmd_BECOME_ADMIN(game, password, connection_id):
64     player = game.thing_types['Player'](game)
65     if not player:
66         raise GameError('need to be logged in for this')
67     if password in game.admin_passwords:
68         game.sessions[connection_id]['status'] = 'admin'
69         game.io.send('ADMIN_OK', connection_id)
70     else:
71         raise GameError('wrong password')
72 cmd_BECOME_ADMIN.argtypes = 'string'
73
74 def cmd_ADMIN_PASSWORD(game, password):
75     game.admin_passwords += [password]
76 cmd_ADMIN_PASSWORD.argtypes = 'string'
77
78 def cmd_SET_TILE_CONTROL(game, yx, control_char, 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 not game.sessions[connection_id]['status'] == 'admin':
83         raise GameError('need to be admin for this')
84     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
85     map_control = game.get_map(big_yx, 'control')
86     map_control[little_yx] = control_char
87     game.changed = True
88     game.record_fov_change((big_yx, little_yx))
89 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
90
91 def cmd_THING_PROTECTION(game, thing_id, protection_char, connection_id):
92     player = game.get_player(connection_id)
93     if not player:
94         raise GameError('need to be logged in for this')
95     if not game.sessions[connection_id]['status'] == 'admin':
96         raise GameError('need to be admin for this')
97     t = game.get_thing(thing_id)
98     if not t:
99         raise GameError('thing of ID %s not found' % thing_id)
100     t.protection = protection_char
101     game.changed = True
102     # FIXME: pseudo-FOV-change actually
103     game.record_fov_change(t.position)
104 cmd_THING_PROTECTION.argtypes = 'int:pos char'
105
106 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, 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 tile_class == '.':
113         raise GameError('tile class "." must remain unprotected')
114     game.map_control_passwords[tile_class] = password
115     #game.changed = True
116 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
117
118 def cmd_NICK(game, nick, connection_id):
119     nick = nick.strip()
120     if len(nick) == 0:
121         raise GameError('empty name')
122     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
123         raise GameError('name already in use')
124     t = game.get_player(connection_id)
125     if not t:
126         raise GameError('can only rename when already logged in')
127     old_nick = t.name
128     t.name = nick
129     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
130     game.changed = True
131     # FIXME: pseudo-FOV-change actually
132     game.record_fov_change(t.position)
133 cmd_NICK.argtypes = 'string'
134
135 def cmd_GET_GAMESTATE(game, connection_id):
136     game.send_gamestate(connection_id)
137 cmd_GET_GAMESTATE.argtypes = ''
138
139 # def cmd_QUERY(game, target_nick, msg, connection_id):
140 #     if not connection_id in game.sessions:
141 #         raise GameError('can only query when logged in')
142 #     t = game.get_thing(game.sessions[connection_id], False)
143 #     source_nick = t.name
144 #     for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
145 #         for c_id in game.sessions:
146 #             if game.sessions[c_id] == t.id_:
147 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
148 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
149 #                 return
150 #         raise GameError('target user offline')
151 #     raise GameError('can only query with registered nicknames')
152 # cmd_QUERY.argtypes = 'string string'
153
154 def cmd_PING(game, connection_id):
155     game.io.send('PONG', connection_id)
156 cmd_PING.argtypes = ''
157
158 def cmd_TURN(game, n):
159     game.turn = n
160 cmd_TURN.argtypes = 'int:nonneg'
161
162 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
163     if len(msg) > 500:
164         raise ArgError('annotation text must be <= 500 characters')
165     player = game.get_player(connection_id)
166     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
167     if not player.fov_test(big_yx, little_yx):
168         raise GameError('cannot annotate tile outside field of view')
169     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
170         raise GameError('wrong password for tile')
171     if msg == ' ':
172         if big_yx in game.annotations:
173             if little_yx in game.annotations[big_yx]:
174                 del game.annotations[big_yx][little_yx]
175     else:
176         if big_yx not in game.annotations:
177             game.annotations[big_yx] = {}
178         game.annotations[big_yx][little_yx] = msg
179     # FIXME: pseudo-FOV-change actually
180     game.record_fov_change([big_yx, little_yx])
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     # FIXME: pseudo-FOV-change actually
200     game.record_fov_change([big_yx, little_yx])
201     game.changed = True
202 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
203
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
208     #game.changed = True
209 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
210
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
215     #game.changed = True
216 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
217
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'
222
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))
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     # game.changed = True  handled by add_thing
246 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
247
248 def cmd_THING_NAME(game, thing_id, name, pw, connection_id):
249     # TODO check if thing in FOV
250     t = game.get_thing(thing_id)
251     if not t:
252         raise GameError('thing of ID %s not found' % thing_id)
253     if not game.can_do_thing_with_pw(t, pw):
254         raise GameError('wrong password for thing')
255     t.name = name
256     game.changed = True
257     # FIXME: pseudo-FOV-change actually
258     game.record_fov_change(t.position)
259 cmd_THING_NAME.argtypes = 'int:pos string string'
260
261 def cmd_GOD_THING_NAME(game, thing_id, name):
262     t = game.get_thing(thing_id)
263     if not t:
264         raise GameError('thing of ID %s not found' % thing_id)
265     t.name = name
266 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
267
268 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
269     t = game.get_thing(thing_id)
270     if not t:
271         raise GameError('thing of ID %s not found' % thing_id)
272     t.protection = protection_char
273 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
274
275 def cmd_THING_DOOR_CLOSED(game, thing_id):
276     t = game.get_thing(thing_id)
277     if not t:
278         raise GameError('thing of ID %s not found' % thing_id)
279     if not t.type_ == 'Door':
280         raise GameError('thing of ID %s not door' % thing_id)
281     t.blocking = True
282     t.portable = False
283     t.thing_char = '#'
284 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'
285
286 def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
287     t = game.get_thing(thing_id)
288     if not t:
289         raise GameError('thing of ID %s not found' % thing_id)
290     if not t.type_ == 'MusicPlayer':
291         raise GameError('thing of ID %s not music player' % thing_id)
292     t.playing = playing
293     t.playlist_index = index
294     t.repeat = repeat
295 cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int bool'
296
297 def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
298     t = game.get_thing(thing_id)
299     if not t:
300         raise GameError('thing of ID %s not found' % thing_id)
301     if not t.type_ == 'MusicPlayer':
302         raise GameError('thing of ID %s not music player' % thing_id)
303     t.playlist += [(title, length)]
304 cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'
305
306 def cmd_THING_BOTTLE_EMPTY(game, thing_id):
307     t = game.get_thing(thing_id)
308     if not t:
309         raise GameError('thing of ID %s not found' % thing_id)
310     if not t.type_ == 'Bottle':
311         raise GameError('thing of ID %s not bottle' % thing_id)
312     t.empty()
313 cmd_THING_BOTTLE_EMPTY.argtypes = 'int:pos'
314
315 def cmd_THING_INSTALLED(game, thing_id):
316     t = game.get_thing(thing_id)
317     if not t:
318         raise GameError('thing of ID %s not found' % thing_id)
319     if not hasattr(t, 'installable'):
320         raise GameError('thing of ID %s not installable' % thing_id)
321     t.install()
322 cmd_THING_INSTALLED.argtypes = 'int:pos'
323
324 def cmd_PLAYER_FACE(game, face, connection_id):
325     t = game.get_player(connection_id)
326     if not t:
327         raise GameError('can only draw face when already logged in')
328     if len(face) != 18:
329         raise GameError('wrong face string length')
330     game.faces[t.name] = face
331     game.changed = True
332     # FIXME: pseudo-FOV-change actually
333     game.record_fov_change(t.position)
334 cmd_PLAYER_FACE.argtypes = 'string'
335
336 def cmd_GOD_PLAYER_FACE(game, name, face):
337     if len(face) != 18:
338         raise GameError('wrong face string length')
339     game.faces[name] = face
340 cmd_GOD_PLAYER_FACE.argtypes = 'string string'
341
342 def cmd_GOD_PLAYER_HAT(game, name, hat):
343     if len(hat) != 18:
344         raise GameError('wrong hat string length')
345     game.hats[name] = hat
346 cmd_GOD_PLAYER_HAT.argtypes = 'string string'
347
348 def cmd_THING_HAT_DESIGN(game, thing_id, design):
349     if len(design) != 18:
350         raise GameError('hat design of wrong length')
351     t = game.get_thing(thing_id)
352     if not t:
353         raise GameError('thing of ID %s not found' % thing_id)
354     if t.type_ != 'Hat':
355         raise GameError('thing of ID %s not a hat' % thing_id)
356     t.design = design
357 cmd_THING_HAT_DESIGN.argtypes = 'int:pos string'