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