home · contact · privacy
dea07173c6181f2233260823977c95766732bd60
[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     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
40         raise GameError('name already in use')
41     if game.get_player(connection_id):
42         raise GameError('cannot log in twice')
43     t = game.thing_types['Player'](game)
44     t.position = game.spawn_point
45     game.things += [t]  # TODO refactor into Thing.__init__?
46     t.thing_char = game.get_next_player_char()
47     game.sessions[connection_id] = {
48         'thing_id': t.id_,
49         'status': 'player'
50     }
51     game.io.send('LOGIN_OK', connection_id)
52     t.name = nick
53     game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
54     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
55     for s in [s for s in game.things
56               if s.type_ == 'SpawnPoint' and s.name == t.name]:
57         t.position = s.position
58         break
59     game.changed = True
60     game.changed_fovs = True
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.changed_fovs = True
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 cmd_THING_PROTECTION.argtypes = 'int:pos char'
103
104 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
105     player = game.get_player(connection_id)
106     if not player:
107         raise GameError('need to be logged in for this')
108     if not game.sessions[connection_id]['status'] == 'admin':
109         raise GameError('need to be admin for this')
110     if tile_class == '.':
111         raise GameError('tile class "." must remain unprotected')
112     game.map_control_passwords[tile_class] = password
113     #game.changed = True
114 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
115
116 def cmd_NICK(game, nick, connection_id):
117     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
118         raise GameError('name already in use')
119     t = game.get_player(connection_id)
120     if not t:
121         raise GameError('can only rename when already logged in')
122     old_nick = t.name
123     t.name = nick
124     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
125     game.changed = True
126 cmd_NICK.argtypes = 'string'
127
128 def cmd_GET_GAMESTATE(game, connection_id):
129     game.send_gamestate(connection_id)
130 cmd_GET_GAMESTATE.argtypes = ''
131
132 # def cmd_QUERY(game, target_nick, msg, connection_id):
133 #     if not connection_id in game.sessions:
134 #         raise GameError('can only query when logged in')
135 #     t = game.get_thing(game.sessions[connection_id], False)
136 #     source_nick = t.name
137 #     for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
138 #         for c_id in game.sessions:
139 #             if game.sessions[c_id] == t.id_:
140 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
141 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
142 #                 return
143 #         raise GameError('target user offline')
144 #     raise GameError('can only query with registered nicknames')
145 # cmd_QUERY.argtypes = 'string string'
146
147 def cmd_PING(game, connection_id):
148     game.io.send('PONG', connection_id)
149 cmd_PING.argtypes = ''
150
151 def cmd_TURN(game, n):
152     game.turn = n
153 cmd_TURN.argtypes = 'int:nonneg'
154
155 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
156     if len(msg) > 500:
157         raise ArgError('annotation text must be <= 500 characters')
158     player = game.get_player(connection_id)
159     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
160     if not player.fov_test(big_yx, little_yx):
161         raise GameError('cannot annotate tile outside field of view')
162     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
163         raise GameError('wrong password for tile')
164     if msg == ' ':
165         if big_yx in game.annotations:
166             if little_yx in game.annotations[big_yx]:
167                 del game.annotations[big_yx][little_yx]
168     else:
169         if big_yx not in game.annotations:
170             game.annotations[big_yx] = {}
171         game.annotations[big_yx][little_yx] = msg
172     game.changed = True
173 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
174
175 def cmd_PORTAL(game, yx, msg, pw, connection_id):
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 edit portal on 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.portals:
184             if little_yx in game.portals[big_yx]:
185                 del game.portals[big_yx][little_yx]
186     else:
187         if big_yx not in game.portals:
188             game.portals[big_yx] = {}
189         game.portals[big_yx][little_yx] = msg
190     game.changed = True
191 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
192
193 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
194     if big_yx not in game.annotations:
195         game.annotations[big_yx] = {}
196     game.annotations[big_yx][little_yx] = msg
197     #game.changed = True
198 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
199
200 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
201     if big_yx not in game.portals:
202         game.portals[big_yx] = {}
203     game.portals[big_yx][little_yx] = msg
204     #game.changed = True
205 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
206
207 def cmd_MAP_LINE(game, big_yx, y, line):
208     map_ = game.get_map(big_yx)
209     map_.set_line(y, line)
210 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
211
212 def cmd_MAP(game, geometry, size):
213     from plomrogue.mapping import MapGeometryHex, MapGeometrySquare
214     map_geometry_class = MapGeometrySquare
215     if geometry == 'Hex':
216         map_geometry_class = MapGeometryHex
217     game.new_world(map_geometry_class(size))
218 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
219
220 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
221     map_control = game.get_map(big_yx, 'control')
222     map_control.set_line(y, line)
223 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
224
225 def cmd_MAP_CONTROL_PW(game, tile_class, password):
226     game.map_control_passwords[tile_class] = password
227 cmd_MAP_CONTROL_PW.argtypes = 'char string'
228
229 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
230     if thing_type not in game.thing_types:
231         raise GameError('illegal thing type %s' % thing_type)
232     _ = game.get_map(big_yx)
233     t_old = None
234     if thing_id > 0:
235         t_old = game.get_thing(thing_id)
236     t_new = game.thing_types[thing_type](game, id_=thing_id, position=(big_yx,
237                                                                        little_yx))
238     if t_old:
239         game.things[game.things.index(t_old)] = t_new
240     else:
241         game.things += [t_new]
242     game.changed = True
243     game.changed_fovs = True
244 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
245
246 def cmd_THING_NAME(game, thing_id, name, pw, connection_id):
247     # TODO check if thing in FOV
248     t = game.get_thing(thing_id)
249     if not t:
250         raise GameError('thing of ID %s not found' % thing_id)
251     if not game.can_do_thing_with_pw(t, pw):
252         raise GameError('wrong password for tile')
253     t.name = name
254     game.changed = True
255 cmd_THING_NAME.argtypes = 'int:pos string string'
256
257 def cmd_GOD_THING_NAME(game, thing_id, name):
258     t = game.get_thing(thing_id)
259     if not t:
260         raise GameError('thing of ID %s not found' % thing_id)
261     t.name = name
262 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
263
264 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
265     t = game.get_thing(thing_id)
266     if not t:
267         raise GameError('thing of ID %s not found' % thing_id)
268     t.protection = protection_char
269 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
270
271 def cmd_THING_DOOR_CLOSED(game, thing_id):
272     t = game.get_thing(thing_id)
273     if not t:
274         raise GameError('thing of ID %s not found' % thing_id)
275     if not t.type_ == 'Door':
276         raise GameError('thing of ID %s not door' % thing_id)
277     t.blocking = True
278     t.portable = False
279     t.thing_char = '#'
280 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'
281
282 def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
283     t = game.get_thing(thing_id)
284     if not t:
285         raise GameError('thing of ID %s not found' % thing_id)
286     if not t.type_ == 'MusicPlayer':
287         raise GameError('thing of ID %s not music player' % thing_id)
288     t.playing = playing
289     t.playlist_index = index
290     t.repeat = repeat
291 cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int bool'
292
293 def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
294     t = game.get_thing(thing_id)
295     if not t:
296         raise GameError('thing of ID %s not found' % thing_id)
297     if not t.type_ == 'MusicPlayer':
298         raise GameError('thing of ID %s not music player' % thing_id)
299     t.playlist += [(title, length)]
300 cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'
301
302 def cmd_THING_BOTTLE_EMPTY(game, thing_id):
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_ == 'Bottle':
307         raise GameError('thing of ID %s not bottle' % thing_id)
308     t.empty()
309 cmd_THING_BOTTLE_EMPTY.argtypes = 'int:pos'
310
311 def cmd_THING_INSTALLED(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 hasattr(t, 'installable'):
316         raise GameError('thing of ID %s not installable' % thing_id)
317     t.install()
318 cmd_THING_INSTALLED.argtypes = 'int:pos'
319
320 def cmd_PLAYER_FACE(game, face, connection_id):
321     t = game.get_player(connection_id)
322     if not t:
323         raise GameError('can only draw face when already logged in')
324     if len(face) != 18:
325         raise GameError('wrong face string length')
326     game.faces[t.name] = face
327     game.changed = True
328 cmd_PLAYER_FACE.argtypes = 'string'
329
330 def cmd_GOD_PLAYER_FACE(game, name, face):
331     if len(face) != 18:
332         raise GameError('wrong face string length')
333     game.faces[name] = face
334 cmd_GOD_PLAYER_FACE.argtypes = 'string string'
335
336 def cmd_GOD_PLAYER_HAT(game, name, hat):
337     if len(hat) != 18:
338         raise GameError('wrong hat string length')
339     game.hats[name] = hat
340 cmd_GOD_PLAYER_HAT.argtypes = 'string string'
341
342 def cmd_THING_HAT_DESIGN(game, thing_id, design):
343     if len(design) != 18:
344         raise GameError('hat design of wrong length')
345     t = game.get_thing(thing_id)
346     if not t:
347         raise GameError('thing of ID %s not found' % thing_id)
348     if t.type_ != 'Hat':
349         raise GameError('thing of ID %s not a hat' % thing_id)
350     t.design = design
351 cmd_THING_HAT_DESIGN.argtypes = 'int:pos string'