1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
6 # TODO: instead of sending tasks, thing types etc. on request, send them on connection
8 def cmd_TASKS(game, connection_id):
9 game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
10 cmd_TASKS.argtypes = ''
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)),
16 cmd_THING_TYPES.argtypes = ''
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])),
22 cmd_TERRAINS.argtypes = ''
24 def cmd_ALL(game, msg, connection_id):
25 from plomrogue.mapping import DijkstraMap
27 def lower_msg_by_volume(msg, volume, largest_audible_distance):
29 factor = largest_audible_distance / 4
33 while random.random() > volume * factor:
36 elif c != '.' and c != ' ':
43 speaker = game.get_player(connection_id)
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)
55 listener_distance = dijkstra_map[target_yx]
56 if listener_distance > largest_audible_distance:
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,
66 cmd_ALL.argtypes = 'string'
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'
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] = {
88 game.io.send('LOGIN_OK', connection_id)
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
97 cmd_LOGIN.argtypes = 'string'
99 def cmd_BECOME_ADMIN(game, password, connection_id):
100 player = game.thing_types['Player'](game)
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)
107 raise GameError('wrong password')
108 cmd_BECOME_ADMIN.argtypes = 'string'
110 def cmd_ADMIN_PASSWORD(game, password):
111 game.admin_passwords += [password]
112 cmd_ADMIN_PASSWORD.argtypes = 'string'
114 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
115 player = game.get_player(connection_id)
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
124 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
126 def cmd_THING_PROTECTION(game, thing_id, protection_char, connection_id):
127 player = game.get_player(connection_id)
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)
134 raise GameError('thing of ID %s not found' % thing_id)
135 t.protection = protection_char
137 cmd_THING_PROTECTION.argtypes = 'int:pos char'
139 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
140 player = game.get_player(connection_id)
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
149 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
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)
156 raise GameError('can only rename when already logged in')
159 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
161 cmd_NICK.argtypes = 'string'
163 def cmd_GET_GAMESTATE(game, connection_id):
164 game.send_gamestate(connection_id)
165 cmd_GET_GAMESTATE.argtypes = ''
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)
178 # raise GameError('target user offline')
179 # raise GameError('can only query with registered nicknames')
180 # cmd_QUERY.argtypes = 'string string'
182 def cmd_PING(game, connection_id):
183 game.io.send('PONG', connection_id)
184 cmd_PING.argtypes = ''
186 def cmd_TURN(game, n):
188 cmd_TURN.argtypes = 'int:nonneg'
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')
198 if big_yx in game.annotations:
199 if little_yx in game.annotations[big_yx]:
200 del game.annotations[big_yx][little_yx]
202 if big_yx not in game.annotations:
203 game.annotations[big_yx] = {}
204 game.annotations[big_yx][little_yx] = msg
206 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
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')
216 if big_yx in game.portals:
217 if little_yx in game.portals[big_yx]:
218 del game.portals[big_yx][little_yx]
220 if big_yx not in game.portals:
221 game.portals[big_yx] = {}
222 game.portals[big_yx][little_yx] = msg
224 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
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
231 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
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
238 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
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'
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'
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'
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'
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'
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)
280 t_old = game.get_thing(thing_id)
281 t_new = game.thing_types[thing_type](game, id_=thing_id, position=(big_yx,
284 game.things[game.things.index(t_old)] = t_new
286 game.things += [t_new]
288 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
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)
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')
299 cmd_THING_NAME.argtypes = 'int:pos string string'
301 def cmd_GOD_THING_NAME(game, thing_id, name):
302 t = game.get_thing(thing_id)
304 raise GameError('thing of ID %s not found' % thing_id)
306 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
308 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
309 t = game.get_thing(thing_id)
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'
315 def cmd_THING_DOOR_CLOSED(game, thing_id):
316 t = game.get_thing(thing_id)
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)
324 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'