home · contact · privacy
7544c06e03c94018ae975bf65a0ace44e2f4253f
[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 cmd_LOGIN.argtypes = 'string'
61
62 def cmd_BECOME_ADMIN(game, password, connection_id):
63     player = game.thing_types['Player'](game)
64     if not player:
65         raise GameError('need to be logged in for this')
66     if password in game.admin_passwords:
67         game.sessions[connection_id]['status'] = 'admin'
68         game.io.send('ADMIN_OK', connection_id)
69     else:
70         raise GameError('wrong password')
71 cmd_BECOME_ADMIN.argtypes = 'string'
72
73 def cmd_ADMIN_PASSWORD(game, password):
74     game.admin_passwords += [password]
75 cmd_ADMIN_PASSWORD.argtypes = 'string'
76
77 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
78     player = game.get_player(connection_id)
79     if not player:
80         raise GameError('need to be logged in for this')
81     if not game.sessions[connection_id]['status'] == 'admin':
82         raise GameError('need to be admin for this')
83     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
84     map_control = game.get_map(big_yx, 'control')
85     map_control[little_yx] = control_char
86     game.changed = True
87 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
88
89 def cmd_THING_PROTECTION(game, thing_id, protection_char, connection_id):
90     player = game.get_player(connection_id)
91     if not player:
92         raise GameError('need to be logged in for this')
93     if not game.sessions[connection_id]['status'] == 'admin':
94         raise GameError('need to be admin for this')
95     t = game.get_thing(thing_id)
96     if not t:
97         raise GameError('thing of ID %s not found' % thing_id)
98     t.protection = protection_char
99     #game.changed = True
100 cmd_THING_PROTECTION.argtypes = 'int:pos char'
101
102 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
103     player = game.get_player(connection_id)
104     if not player:
105         raise GameError('need to be logged in for this')
106     if not game.sessions[connection_id]['status'] == 'admin':
107         raise GameError('need to be admin for this')
108     if tile_class == '.':
109         raise GameError('tile class "." must remain unprotected')
110     game.map_control_passwords[tile_class] = password
111     #game.changed = True
112 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
113
114 def cmd_NICK(game, nick, connection_id):
115     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
116         raise GameError('name already in use')
117     t = game.get_player(connection_id)
118     if not t:
119         raise GameError('can only rename when already logged in')
120     old_nick = t.name
121     t.name = nick
122     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
123     game.changed = True
124 cmd_NICK.argtypes = 'string'
125
126 def cmd_GET_GAMESTATE(game, connection_id):
127     game.send_gamestate(connection_id)
128 cmd_GET_GAMESTATE.argtypes = ''
129
130 # def cmd_QUERY(game, target_nick, msg, connection_id):
131 #     if not connection_id in game.sessions:
132 #         raise GameError('can only query when logged in')
133 #     t = game.get_thing(game.sessions[connection_id], False)
134 #     source_nick = t.name
135 #     for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
136 #         for c_id in game.sessions:
137 #             if game.sessions[c_id] == t.id_:
138 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
139 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
140 #                 return
141 #         raise GameError('target user offline')
142 #     raise GameError('can only query with registered nicknames')
143 # cmd_QUERY.argtypes = 'string string'
144
145 def cmd_PING(game, connection_id):
146     game.io.send('PONG', connection_id)
147 cmd_PING.argtypes = ''
148
149 def cmd_TURN(game, n):
150     game.turn = n
151 cmd_TURN.argtypes = 'int:nonneg'
152
153 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
154     if len(msg) > 500:
155         raise ArgError('annotation text must be <= 500 characters')
156     player = game.get_player(connection_id)
157     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
158     if not player.fov_test(big_yx, little_yx):
159         raise GameError('cannot annotate tile outside field of view')
160     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
161         raise GameError('wrong password for tile')
162     if msg == ' ':
163         if big_yx in game.annotations:
164             if little_yx in game.annotations[big_yx]:
165                 del game.annotations[big_yx][little_yx]
166     else:
167         if big_yx not in game.annotations:
168             game.annotations[big_yx] = {}
169         game.annotations[big_yx][little_yx] = msg
170     game.changed = True
171 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
172
173 def cmd_PORTAL(game, yx, msg, pw, connection_id):
174     player = game.get_player(connection_id)
175     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
176     if not player.fov_test(big_yx, little_yx):
177         raise GameError('cannot edit portal on tile outside field of view')
178     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
179         raise GameError('wrong password for tile')
180     if msg == ' ':
181         if big_yx in game.portals:
182             if little_yx in game.portals[big_yx]:
183                 del game.portals[big_yx][little_yx]
184     else:
185         if big_yx not in game.portals:
186             game.portals[big_yx] = {}
187         game.portals[big_yx][little_yx] = msg
188     game.changed = True
189 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
190
191 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
192     if big_yx not in game.annotations:
193         game.annotations[big_yx] = {}
194     game.annotations[big_yx][little_yx] = msg
195     game.changed = True
196 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
197
198 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
199     if big_yx not in game.portals:
200         game.portals[big_yx] = {}
201     game.portals[big_yx][little_yx] = msg
202     game.changed = True
203 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
204
205 def cmd_MAP_LINE(game, big_yx, y, line):
206     map_ = game.get_map(big_yx)
207     map_.set_line(y, line)
208 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
209
210 def cmd_MAP(game, geometry, size):
211     from plomrogue.mapping import MapGeometryHex, MapGeometrySquare
212     map_geometry_class = MapGeometrySquare
213     if geometry == 'Hex':
214         map_geometry_class = MapGeometryHex
215     game.new_world(map_geometry_class(size))
216 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
217
218 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
219     map_control = game.get_map(big_yx, 'control')
220     map_control.set_line(y, line)
221 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
222
223 def cmd_MAP_CONTROL_PW(game, tile_class, password):
224     game.map_control_passwords[tile_class] = password
225 cmd_MAP_CONTROL_PW.argtypes = 'char string'
226
227 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
228     if thing_type not in game.thing_types:
229         raise GameError('illegal thing type %s' % thing_type)
230     _ = game.get_map(big_yx)
231     t_old = None
232     if thing_id > 0:
233         t_old = game.get_thing(thing_id)
234     t_new = game.thing_types[thing_type](game, id_=thing_id, position=(big_yx,
235                                                                        little_yx))
236     if t_old:
237         game.things[game.things.index(t_old)] = t_new
238     else:
239         game.things += [t_new]
240     game.changed = True
241 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
242
243 def cmd_THING_NAME(game, thing_id, name, pw, connection_id):
244     # TODO check if thing in FOV
245     t = game.get_thing(thing_id)
246     if not t:
247         raise GameError('thing of ID %s not found' % thing_id)
248     if not game.can_do_thing_with_pw(t, pw):
249         raise GameError('wrong password for tile')
250     t.name = name
251     game.changed = True
252 cmd_THING_NAME.argtypes = 'int:pos string string'
253
254 def cmd_GOD_THING_NAME(game, thing_id, name):
255     t = game.get_thing(thing_id)
256     if not t:
257         raise GameError('thing of ID %s not found' % thing_id)
258     t.name = name
259 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
260
261 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
262     t = game.get_thing(thing_id)
263     if not t:
264         raise GameError('thing of ID %s not found' % thing_id)
265     t.protection = protection_char
266 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
267
268 def cmd_THING_DOOR_CLOSED(game, thing_id):
269     t = game.get_thing(thing_id)
270     if not t:
271         raise GameError('thing of ID %s not found' % thing_id)
272     if not t.type_ == 'Door':
273         raise GameError('thing of ID %s not door' % thing_id)
274     t.blocking = True
275     t.portable = False
276     t.thing_char = '#'
277 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'
278
279 def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
280     t = game.get_thing(thing_id)
281     if not t:
282         raise GameError('thing of ID %s not found' % thing_id)
283     if not t.type_ == 'MusicPlayer':
284         raise GameError('thing of ID %s not music player' % thing_id)
285     t.playing = playing
286     t.playlist_index = index
287     t.repeat = repeat
288 cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int bool'
289
290 def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
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_ == 'MusicPlayer':
295         raise GameError('thing of ID %s not music player' % thing_id)
296     t.playlist += [(title, length)]
297 cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'
298
299 def cmd_THING_BOTTLE_EMPTY(game, thing_id):
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_ == 'Bottle':
304         raise GameError('thing of ID %s not bottle' % thing_id)
305     t.empty()
306 cmd_THING_BOTTLE_EMPTY.argtypes = 'int:pos'
307
308 def cmd_THING_INSTALLED(game, thing_id):
309     t = game.get_thing(thing_id)
310     if not t:
311         raise GameError('thing of ID %s not found' % thing_id)
312     if not hasattr(t, 'installable'):
313         raise GameError('thing of ID %s not installable' % thing_id)
314     t.install()
315 cmd_THING_INSTALLED.argtypes = 'int:pos'
316
317 def cmd_PLAYER_FACE(game, face, connection_id):
318     t = game.get_player(connection_id)
319     if not t:
320         raise GameError('can only draw face when already logged in')
321     if len(face) != 18:
322         raise GameError('wrong face string length')
323     game.faces[t.name] = face
324     game.changed = True
325 cmd_PLAYER_FACE.argtypes = 'string'
326
327 def cmd_GOD_PLAYER_FACE(game, name, face):
328     if len(face) != 18:
329         raise GameError('wrong face string length')
330     game.faces[name] = face
331 cmd_GOD_PLAYER_FACE.argtypes = 'string string'
332
333 def cmd_GOD_PLAYER_HAT(game, name, hat):
334     if len(hat) != 18:
335         raise GameError('wrong hat string length')
336     game.hats[name] = hat
337 cmd_GOD_PLAYER_HAT.argtypes = 'string string'
338
339 def cmd_THING_HAT_DESIGN(game, thing_id, design):
340     if len(design) != 18:
341         raise GameError('hat design of wrong length')
342     t = game.get_thing(thing_id)
343     if not t:
344         raise GameError('thing of ID %s not found' % thing_id)
345     if t.type_ != 'Hat':
346         raise GameError('thing of ID %s not a hat' % thing_id)
347     t.design = design
348 cmd_THING_HAT_DESIGN.argtypes = 'int:pos string'