home · contact · privacy
Re-instate (better) flood protection.
[plomrogue2] / plomrogue / commands.py
1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
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     # TODO filter newlines
40     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
41         raise GameError('name already in use')
42     if game.get_player(connection_id):
43         raise GameError('cannot log in twice')
44     t = game.thing_types['Player'](game)
45     t.position = game.spawn_point
46     game.things += [t]  # TODO refactor into Thing.__init__?
47     t.thing_char = game.get_next_player_char()
48     game.sessions[connection_id] = {
49         'thing_id': t.id_,
50         'status': 'player'
51     }
52     game.io.send('LOGIN_OK', connection_id)
53     t.name = nick
54     game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
55     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
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
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 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
89
90 def cmd_THING_PROTECTION(game, thing_id, protection_char, connection_id):
91     player = game.get_player(connection_id)
92     if not player:
93         raise GameError('need to be logged in for this')
94     if not game.sessions[connection_id]['status'] == 'admin':
95         raise GameError('need to be admin for this')
96     t = game.get_thing(thing_id)
97     if not t:
98         raise GameError('thing of ID %s not found' % thing_id)
99     t.protection = protection_char
100     #game.changed = True
101 cmd_THING_PROTECTION.argtypes = 'int:pos char'
102
103 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
104     player = game.get_player(connection_id)
105     if not player:
106         raise GameError('need to be logged in for this')
107     if not game.sessions[connection_id]['status'] == 'admin':
108         raise GameError('need to be admin for this')
109     if tile_class == '.':
110         raise GameError('tile class "." must remain unprotected')
111     game.map_control_passwords[tile_class] = password
112     #game.changed = True
113 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
114
115 def cmd_NICK(game, nick, connection_id):
116     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
117         raise GameError('name already in use')
118     t = game.get_player(connection_id)
119     if not t:
120         raise GameError('can only rename when already logged in')
121     old_nick = t.name
122     t.name = nick
123     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
124     game.changed = True
125 cmd_NICK.argtypes = 'string'
126
127 def cmd_GET_GAMESTATE(game, connection_id):
128     game.send_gamestate(connection_id)
129 cmd_GET_GAMESTATE.argtypes = ''
130
131 # def cmd_QUERY(game, target_nick, msg, connection_id):
132 #     if not connection_id in game.sessions:
133 #         raise GameError('can only query when logged in')
134 #     t = game.get_thing(game.sessions[connection_id], False)
135 #     source_nick = t.name
136 #     for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
137 #         for c_id in game.sessions:
138 #             if game.sessions[c_id] == t.id_:
139 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
140 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
141 #                 return
142 #         raise GameError('target user offline')
143 #     raise GameError('can only query with registered nicknames')
144 # cmd_QUERY.argtypes = 'string string'
145
146 def cmd_PING(game, connection_id):
147     game.io.send('PONG', connection_id)
148 cmd_PING.argtypes = ''
149
150 def cmd_TURN(game, n):
151     game.turn = n
152 cmd_TURN.argtypes = 'int:nonneg'
153
154 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
155     player = game.get_player(connection_id)
156     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
157     if not player.fov_test(big_yx, little_yx):
158         raise GameError('cannot annotate tile outside field of view')
159     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
160         raise GameError('wrong password for tile')
161     if msg == ' ':
162         if big_yx in game.annotations:
163             if little_yx in game.annotations[big_yx]:
164                 del game.annotations[big_yx][little_yx]
165     else:
166         if big_yx not in game.annotations:
167             game.annotations[big_yx] = {}
168         game.annotations[big_yx][little_yx] = msg
169     game.changed = True
170 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
171
172 def cmd_PORTAL(game, yx, msg, pw, connection_id):
173     player = game.get_player(connection_id)
174     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
175     if not player.fov_test(big_yx, little_yx):
176         raise GameError('cannot edit portal on tile outside field of view')
177     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
178         raise GameError('wrong password for tile')
179     if msg == ' ':
180         if big_yx in game.portals:
181             if little_yx in game.portals[big_yx]:
182                 del game.portals[big_yx][little_yx]
183     else:
184         if big_yx not in game.portals:
185             game.portals[big_yx] = {}
186         game.portals[big_yx][little_yx] = msg
187     game.changed = True
188 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
189
190 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
191     if big_yx not in game.annotations:
192         game.annotations[big_yx] = {}
193     game.annotations[big_yx][little_yx] = msg
194     game.changed = True
195 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
196
197 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
198     if big_yx not in game.portals:
199         game.portals[big_yx] = {}
200     game.portals[big_yx][little_yx] = msg
201     game.changed = True
202 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
203
204 def cmd_GET_ANNOTATION(game, yx, connection_id):
205     player = game.get_player(connection_id)
206     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
207     annotation = '(unknown)'
208     if player.fov_test(big_yx, little_yx):
209         annotation = '(none)'
210         if big_yx in game.annotations:
211             if little_yx in game.annotations[big_yx]:
212                 annotation = game.annotations[big_yx][little_yx]
213     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
214 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
215
216 def cmd_MAP_LINE(game, big_yx, y, line):
217     map_ = game.get_map(big_yx)
218     map_.set_line(y, line)
219 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
220
221 def cmd_MAP(game, geometry, size):
222     from plomrogue.mapping import MapGeometryHex, MapGeometrySquare
223     map_geometry_class = MapGeometrySquare
224     if geometry == 'Hex':
225         map_geometry_class = MapGeometryHex
226     game.new_world(map_geometry_class(size))
227 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
228
229 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
230     map_control = game.get_map(big_yx, 'control')
231     map_control.set_line(y, line)
232 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
233
234 def cmd_MAP_CONTROL_PW(game, tile_class, password):
235     game.map_control_passwords[tile_class] = password
236 cmd_MAP_CONTROL_PW.argtypes = 'char string'
237
238 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
239     if thing_type not in game.thing_types:
240         raise GameError('illegal thing type %s' % thing_type)
241     _ = game.get_map(big_yx)
242     t_old = None
243     if thing_id > 0:
244         t_old = game.get_thing(thing_id)
245     t_new = game.thing_types[thing_type](game, id_=thing_id, position=(big_yx,
246                                                                        little_yx))
247     if t_old:
248         game.things[game.things.index(t_old)] = t_new
249     else:
250         game.things += [t_new]
251     game.changed = True
252 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
253
254 def cmd_THING_NAME(game, thing_id, name, pw, connection_id):
255     # TODO check if thing in FOV
256     t = game.get_thing(thing_id)
257     if not t:
258         raise GameError('thing of ID %s not found' % thing_id)
259     if not game.can_do_thing_with_pw(t, pw):
260         raise GameError('wrong password for tile')
261     t.name = name
262     game.changed = True
263 cmd_THING_NAME.argtypes = 'int:pos string string'
264
265 def cmd_GOD_THING_NAME(game, thing_id, name):
266     t = game.get_thing(thing_id)
267     if not t:
268         raise GameError('thing of ID %s not found' % thing_id)
269     t.name = name
270 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
271
272 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
273     t = game.get_thing(thing_id)
274     if not t:
275         raise GameError('thing of ID %s not found' % thing_id)
276     t.protection = protection_char
277 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
278
279 def cmd_THING_DOOR_CLOSED(game, thing_id):
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_ == 'Door':
284         raise GameError('thing of ID %s not door' % thing_id)
285     t.blocking = True
286     t.portable = False
287     t.thing_char = '#'
288 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'
289
290 def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
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.playing = playing
297     t.playlist_index = index
298     t.repeat = repeat
299 cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int:nonneg bool'
300
301 def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
302     t = game.get_thing(thing_id)
303     if not t:
304         raise GameError('thing of ID %s not found' % thing_id)
305     if not t.type_ == 'MusicPlayer':
306         raise GameError('thing of ID %s not music player' % thing_id)
307     t.playlist += [(title, length)]
308 cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'
309
310 def cmd_THING_BOTTLE_EMPTY(game, thing_id):
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_ == 'Bottle':
315         raise GameError('thing of ID %s not bottle' % thing_id)
316     t.empty()
317 cmd_THING_BOTTLE_EMPTY.argtypes = 'int:pos'