home · contact · privacy
Fix bug that blocked sound from reaching players.
[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     from plomrogue.mapping import DijkstraMap
26
27     def lower_msg_by_volume(msg, volume, largest_audible_distance):
28         import random
29         factor = largest_audible_distance / 4
30         lowered_msg = ''
31         for c in msg:
32             c = c
33             while random.random() > volume * factor:
34                 if c.isupper():
35                     c = c.lower()
36                 elif c != '.' and c != ' ':
37                     c = '.'
38                 else:
39                     c = ' '
40             lowered_msg += c
41         return lowered_msg
42
43     speaker = game.get_player(connection_id)
44     if not speaker:
45         raise GameError('need to be logged in for this')
46     largest_audible_distance = 20
47     things = [t for t in game.things if t.type_ != 'Player']
48     dijkstra_map = DijkstraMap(things, game.maps, speaker.position,
49                                largest_audible_distance, game.get_map)
50     for c_id in game.sessions:
51         listener = game.get_player(c_id)
52         target_yx = dijkstra_map.target_yx(*listener.position, True)
53         if not target_yx:
54             continue
55         listener_distance = dijkstra_map[target_yx]
56         if listener_distance > largest_audible_distance:
57             continue
58         volume = 1 / max(1, listener_distance)
59         lowered_msg = lower_msg_by_volume(msg, volume, largest_audible_distance)
60         lowered_nick = lower_msg_by_volume(speaker.name, volume,
61                                            largest_audible_distance)
62         game.io.send('CHAT ' +
63                      quote('(volume: %.2f) %s: %s' % (volume, lowered_nick,
64                                                       lowered_msg)),
65                      c_id)
66 cmd_ALL.argtypes = 'string'
67
68 def cmd_SPAWN_POINT(game, big_yx, little_yx):
69     if little_yx.y >= game.map_geometry.size.y or \
70        little_yx.x >= game.map_geometry.size.x:
71         raise GameError('illegal spawn point')
72     game.spawn_point = big_yx, little_yx
73 cmd_SPAWN_POINT.argtypes = 'yx_tuple yx_tuple:nonneg'
74
75 def cmd_LOGIN(game, nick, connection_id):
76     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
77         raise GameError('name already in use')
78     if game.get_player(connection_id):
79         raise GameError('cannot log in twice')
80     t = game.thing_types['Player'](game)
81     t.position = game.spawn_point
82     game.things += [t]  # TODO refactor into Thing.__init__?
83     t.thing_char = game.get_next_player_char()
84     game.sessions[connection_id] = {
85         'thing_id': t.id_,
86         'status': 'player'
87     }
88     game.io.send('LOGIN_OK', connection_id)
89     t.name = nick
90     game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
91     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
92     for s in [s for s in game.things
93               if s.type_ == 'SpawnPoint' and s.name == t.name]:
94         t.position = s.position
95         break
96     game.changed = True
97 cmd_LOGIN.argtypes = 'string'
98
99 def cmd_BECOME_ADMIN(game, password, connection_id):
100     player = game.thing_types['Player'](game)
101     if not player:
102         raise GameError('need to be logged in for this')
103     if password in game.admin_passwords:
104         game.sessions[connection_id]['status'] = 'admin'
105         game.io.send('ADMIN_OK', connection_id)
106     else:
107         raise GameError('wrong password')
108 cmd_BECOME_ADMIN.argtypes = 'string'
109
110 def cmd_ADMIN_PASSWORD(game, password):
111     game.admin_passwords += [password]
112 cmd_ADMIN_PASSWORD.argtypes = 'string'
113
114 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
115     player = game.get_player(connection_id)
116     if not player:
117         raise GameError('need to be logged in for this')
118     if not game.sessions[connection_id]['status'] == 'admin':
119         raise GameError('need to be admin for this')
120     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
121     map_control = game.get_map(big_yx, 'control')
122     map_control[little_yx] = control_char
123     game.changed = True
124 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
125
126 def cmd_THING_PROTECTION(game, thing_id, protection_char, connection_id):
127     player = game.get_player(connection_id)
128     if not player:
129         raise GameError('need to be logged in for this')
130     if not game.sessions[connection_id]['status'] == 'admin':
131         raise GameError('need to be admin for this')
132     t = game.get_thing(thing_id)
133     if not t:
134         raise GameError('thing of ID %s not found' % thing_id)
135     t.protection = protection_char
136     game.changed = True
137 cmd_THING_PROTECTION.argtypes = 'int:pos char'
138
139 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
140     player = game.get_player(connection_id)
141     if not player:
142         raise GameError('need to be logged in for this')
143     if not game.sessions[connection_id]['status'] == 'admin':
144         raise GameError('need to be admin for this')
145     if tile_class == '.':
146         raise GameError('tile class "." must remain unprotected')
147     game.map_control_passwords[tile_class] = password
148     game.changed = True
149 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
150
151 def cmd_NICK(game, nick, connection_id):
152     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
153         raise GameError('name already in use')
154     t = game.get_player(connection_id)
155     if not t:
156         raise GameError('can only rename when already logged in')
157     old_nick = t.name
158     t.name = nick
159     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
160     game.changed = True
161 cmd_NICK.argtypes = 'string'
162
163 def cmd_GET_GAMESTATE(game, connection_id):
164     game.send_gamestate(connection_id)
165 cmd_GET_GAMESTATE.argtypes = ''
166
167 # def cmd_QUERY(game, target_nick, msg, connection_id):
168 #     if not connection_id in game.sessions:
169 #         raise GameError('can only query when logged in')
170 #     t = game.get_thing(game.sessions[connection_id], False)
171 #     source_nick = t.name
172 #     for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
173 #         for c_id in game.sessions:
174 #             if game.sessions[c_id] == t.id_:
175 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
176 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
177 #                 return
178 #         raise GameError('target user offline')
179 #     raise GameError('can only query with registered nicknames')
180 # cmd_QUERY.argtypes = 'string string'
181
182 def cmd_PING(game, connection_id):
183     game.io.send('PONG', connection_id)
184 cmd_PING.argtypes = ''
185
186 def cmd_TURN(game, n):
187     game.turn = n
188 cmd_TURN.argtypes = 'int:nonneg'
189
190 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
191     player = game.get_player(connection_id)
192     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
193     if not player.fov_test(big_yx, little_yx):
194         raise GameError('cannot annotate tile outside field of view')
195     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
196         raise GameError('wrong password for tile')
197     if msg == ' ':
198         if big_yx in game.annotations:
199             if little_yx in game.annotations[big_yx]:
200                 del game.annotations[big_yx][little_yx]
201     else:
202         if big_yx not in game.annotations:
203             game.annotations[big_yx] = {}
204         game.annotations[big_yx][little_yx] = msg
205     game.changed = True
206 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
207
208 def cmd_PORTAL(game, yx, msg, pw, connection_id):
209     player = game.get_player(connection_id)
210     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
211     if not player.fov_test(big_yx, little_yx):
212         raise GameError('cannot edit portal on tile outside field of view')
213     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
214         raise GameError('wrong password for tile')
215     if msg == ' ':
216         if big_yx in game.portals:
217             if little_yx in game.portals[big_yx]:
218                 del game.portals[big_yx][little_yx]
219     else:
220         if big_yx not in game.portals:
221             game.portals[big_yx] = {}
222         game.portals[big_yx][little_yx] = msg
223     game.changed = True
224 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
225
226 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
227     if big_yx not in game.annotations:
228         game.annotations[big_yx] = {}
229     game.annotations[big_yx][little_yx] = msg
230     game.changed = True
231 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
232
233 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
234     if big_yx not in game.portals:
235         game.portals[big_yx] = {}
236     game.portals[big_yx][little_yx] = msg
237     game.changed = True
238 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
239
240 def cmd_GET_ANNOTATION(game, yx, connection_id):
241     player = game.get_player(connection_id)
242     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
243     annotation = '(unknown)'
244     if player.fov_test(big_yx, little_yx):
245         annotation = '(none)'
246         if big_yx in game.annotations:
247             if little_yx in game.annotations[big_yx]:
248                 annotation = game.annotations[big_yx][little_yx]
249     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
250 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
251
252 def cmd_MAP_LINE(game, big_yx, y, line):
253     map_ = game.get_map(big_yx)
254     map_.set_line(y, line)
255 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
256
257 def cmd_MAP(game, geometry, size):
258     from plomrogue.mapping import MapGeometryHex, MapGeometrySquare
259     map_geometry_class = MapGeometrySquare
260     if geometry == 'Hex':
261         map_geometry_class = MapGeometryHex
262     game.new_world(map_geometry_class(size))
263 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
264
265 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
266     map_control = game.get_map(big_yx, 'control')
267     map_control.set_line(y, line)
268 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
269
270 def cmd_MAP_CONTROL_PW(game, tile_class, password):
271     game.map_control_passwords[tile_class] = password
272 cmd_MAP_CONTROL_PW.argtypes = 'char string'
273
274 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
275     if thing_type not in game.thing_types:
276         raise GameError('illegal thing type %s' % thing_type)
277     _ = game.get_map(big_yx)
278     t_old = None
279     if thing_id > 0:
280         t_old = game.get_thing(thing_id)
281     t_new = game.thing_types[thing_type](game, id_=thing_id, position=(big_yx,
282                                                                        little_yx))
283     if t_old:
284         game.things[game.things.index(t_old)] = t_new
285     else:
286         game.things += [t_new]
287     game.changed = True
288 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
289
290 def cmd_THING_NAME(game, thing_id, name, pw, connection_id):
291     # TODO check if thing in FOV
292     t = game.get_thing(thing_id)
293     if not t:
294         raise GameError('thing of ID %s not found' % thing_id)
295     if not game.can_do_thing_with_pw(t, pw):
296         raise GameError('wrong password for tile')
297     t.name = name
298     game.changed = True
299 cmd_THING_NAME.argtypes = 'int:pos string string'
300
301 def cmd_GOD_THING_NAME(game, thing_id, name):
302     t = game.get_thing(thing_id)
303     if not t:
304         raise GameError('thing of ID %s not found' % thing_id)
305     t.name = name
306 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
307
308 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
309     t = game.get_thing(thing_id)
310     if not t:
311         raise GameError('thing of ID %s not found' % thing_id)
312     t.protection = protection_char
313 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
314
315 def cmd_THING_DOOR_CLOSED(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 t.type_ == 'Door':
320         raise GameError('thing of ID %s not door' % thing_id)
321     t.blocking = True
322     t.portable = False
323     t.thing_char = '#'
324 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'