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