1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3 from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex, DijkstraMap
7 # TODO: instead of sending tasks, thing types etc. on request, send them on connection
9 def cmd_TASKS(game, connection_id):
11 game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
12 cmd_TASKS.argtypes = ''
14 def cmd_THING_TYPES(game, connection_id):
15 for t_t in game.thing_types.values():
16 game.io.send('THING_TYPE %s %s' % (t_t.get_type(), quote(t_t.symbol_hint)),
18 cmd_THING_TYPES.argtypes = ''
20 def cmd_TERRAINS(game, connection_id):
21 for t in game.terrains.keys():
22 game.io.send('TERRAIN %s %s' % (quote(t), quote(game.terrains[t])),
24 cmd_TERRAINS.argtypes = ''
26 def cmd_ALL(game, msg, connection_id):
28 def lower_msg_by_volume(msg, volume, largest_audible_distance):
30 factor = largest_audible_distance / 4
34 while random.random() > volume * factor:
37 elif c != '.' and c != ' ':
44 speaker = game.get_player(connection_id)
46 raise GameError('need to be logged in for this')
47 largest_audible_distance = 20
48 dijkstra_map = DijkstraMap(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.player_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)
93 cmd_LOGIN.argtypes = 'string'
95 def cmd_BECOME_ADMIN(game, password, connection_id):
96 player = game.thing_types['Player'](game)
98 raise GameError('need to be logged in for this')
99 if password in game.admin_passwords:
100 game.sessions[connection_id]['status'] = 'admin'
101 game.io.send('ADMIN_OK', connection_id)
103 raise GameError('wrong password')
104 cmd_BECOME_ADMIN.argtypes = 'string'
106 def cmd_ADMIN_PASSWORD(game, password):
107 game.admin_passwords += [password]
108 cmd_ADMIN_PASSWORD.argtypes = 'string'
110 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
111 player = game.get_player(connection_id)
113 raise GameError('need to be logged in for this')
114 if not game.sessions[connection_id]['status'] == 'admin':
115 raise GameError('need to be admin for this')
116 if not (control_char == '.'
117 or control_char in game.map_control_passwords.keys()):
118 raise GameError('no password set for this tile class')
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
123 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
125 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
126 player = game.get_player(connection_id)
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 if tile_class == '.':
132 raise GameError('tile class "." must remain unprotected')
133 game.map_control_passwords[tile_class] = password
135 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
137 def cmd_NICK(game, nick, connection_id):
138 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
139 raise GameError('name already in use')
140 t = game.get_player(connection_id)
142 raise GameError('can only rename when already logged in')
145 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
147 cmd_NICK.argtypes = 'string'
149 def cmd_GET_GAMESTATE(game, connection_id):
150 game.send_gamestate(connection_id)
151 cmd_GET_GAMESTATE.argtypes = ''
153 #def cmd_QUERY(game, target_nick, msg, connection_id):
154 # if not connection_id in game.sessions:
155 # raise GameError('can only query when logged in')
156 # t = game.get_thing(game.sessions[connection_id], False)
157 # source_nick = t.name
158 # for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
159 # for c_id in game.sessions:
160 # if game.sessions[c_id] == t.id_:
161 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
162 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
164 # raise GameError('target user offline')
165 # raise GameError('can only query with registered nicknames')
166 #cmd_QUERY.argtypes = 'string string'
168 def cmd_PING(game, connection_id):
169 game.io.send('PONG', connection_id)
170 cmd_PING.argtypes = ''
172 def cmd_TURN(game, n):
174 cmd_TURN.argtypes = 'int:nonneg'
176 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
177 player = game.get_player(connection_id)
178 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
179 if not player.fov_test(big_yx, little_yx):
180 raise GameError('cannot annotate tile outside field of view')
181 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
182 raise GameError('wrong password for tile')
184 if big_yx in game.annotations:
185 if little_yx in game.annotations[big_yx]:
186 del game.annotations[big_yx][little_yx]
188 if not big_yx in game.annotations:
189 game.annotations[big_yx] = {}
190 game.annotations[big_yx][little_yx] = msg
192 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
194 def cmd_PORTAL(game, yx, msg, pw, connection_id):
195 player = game.get_player(connection_id)
196 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
197 if not player.fov_test(big_yx, little_yx):
198 raise GameError('cannot edit portal on tile outside field of view')
199 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
200 raise GameError('wrong password for tile')
202 if big_yx in game.portals:
203 if little_yx in game.portals[big_yx]:
204 del game.portals[big_yx][little_xy]
206 if not big_yx in game.portals:
207 game.portals[big_yx] = {}
208 game.portals[big_yx][little_yx] = msg
210 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
212 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
213 if not big_yx in game.annotations:
214 game.annotations[big_yx] = {}
215 game.annotations[big_yx][little_yx] = msg
217 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
219 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
220 if not big_yx in game.portals:
221 game.portals[big_yx] = {}
222 game.portals[big_yx][little_yx] = msg
224 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
226 def cmd_GET_ANNOTATION(game, yx, connection_id):
227 player = game.get_player(connection_id)
228 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
229 annotation = '(unknown)';
230 if player.fov_test(big_yx, little_yx):
231 annotation = '(none)';
232 if big_yx in game.annotations:
233 if little_yx in game.annotations[big_yx]:
234 annotation = game.annotations[big_yx][little_yx]
235 game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
236 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
238 def cmd_MAP_LINE(game, big_yx, y, line):
239 map_ = game.get_map(big_yx)
240 map_.set_line(y, line)
241 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
243 def cmd_MAP(game, geometry, size):
244 map_geometry_class = globals()['MapGeometry' + geometry]
245 game.new_world(map_geometry_class(size))
246 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
248 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
249 map_control = game.get_map(big_yx, 'control')
250 map_control.set_line(y, line)
251 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
253 def cmd_MAP_CONTROL_PW(game, tile_class, password):
254 game.map_control_passwords[tile_class] = password
255 cmd_MAP_CONTROL_PW.argtypes = 'char string'
257 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
258 if not thing_type in game.thing_types:
259 raise GameError('illegal thing type %s' % thing_type)
260 map_ = game.get_map(big_yx)
263 t_old = game.get_thing(thing_id)
264 t_new = game.thing_types[thing_type](game, id_=thing_id, position=(big_yx,
267 game.things[game.things.index(t_old)] = t_new
269 game.things += [t_new]
271 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
273 def cmd_THING_NAME(game, thing_id, name):
274 t = game.get_thing(thing_id)
276 raise GameError('thing of ID %s not found' % thing_id)
278 cmd_THING_NAME.argtypes = 'int:pos string'