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 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)
54 listener_distance = dijkstra_map[target_yx]
55 if listener_distance > largest_audible_distance:
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,
65 cmd_ALL.argtypes = 'string'
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'
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] = {
87 game.io.send('LOGIN_OK', connection_id)
89 game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
90 game.io.send('PLAYER_ID %s' % t.id_, connection_id)
92 cmd_LOGIN.argtypes = 'string'
94 def cmd_BECOME_ADMIN(game, password, connection_id):
95 player = game.thing_types['Player'](game)
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)
102 raise GameError('wrong password')
103 cmd_BECOME_ADMIN.argtypes = 'string'
105 def cmd_ADMIN_PASSWORD(game, password):
106 game.admin_passwords += [password]
107 cmd_ADMIN_PASSWORD.argtypes = 'string'
109 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
110 player = game.get_player(connection_id)
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
119 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
121 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
122 player = game.get_player(connection_id)
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 if tile_class == '.':
128 raise GameError('tile class "." must remain unprotected')
129 game.map_control_passwords[tile_class] = password
131 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
133 def cmd_NICK(game, nick, connection_id):
134 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
135 raise GameError('name already in use')
136 t = game.get_player(connection_id)
138 raise GameError('can only rename when already logged in')
141 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
143 cmd_NICK.argtypes = 'string'
145 def cmd_GET_GAMESTATE(game, connection_id):
146 game.send_gamestate(connection_id)
147 cmd_GET_GAMESTATE.argtypes = ''
149 #def cmd_QUERY(game, target_nick, msg, connection_id):
150 # if not connection_id in game.sessions:
151 # raise GameError('can only query when logged in')
152 # t = game.get_thing(game.sessions[connection_id], False)
153 # source_nick = t.name
154 # for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
155 # for c_id in game.sessions:
156 # if game.sessions[c_id] == t.id_:
157 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
158 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
160 # raise GameError('target user offline')
161 # raise GameError('can only query with registered nicknames')
162 #cmd_QUERY.argtypes = 'string string'
164 def cmd_PING(game, connection_id):
165 game.io.send('PONG', connection_id)
166 cmd_PING.argtypes = ''
168 def cmd_TURN(game, n):
170 cmd_TURN.argtypes = 'int:nonneg'
172 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
173 player = game.get_player(connection_id)
174 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
175 if not player.fov_test(big_yx, little_yx):
176 raise GameError('cannot annotate tile outside field of view')
177 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
178 raise GameError('wrong password for tile')
180 if big_yx in game.annotations:
181 if little_yx in game.annotations[big_yx]:
182 del game.annotations[big_yx][little_yx]
184 if not big_yx in game.annotations:
185 game.annotations[big_yx] = {}
186 game.annotations[big_yx][little_yx] = msg
188 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
190 def cmd_PORTAL(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 edit portal on 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.portals:
199 if little_yx in game.portals[big_yx]:
200 del game.portals[big_yx][little_yx]
202 if not big_yx in game.portals:
203 game.portals[big_yx] = {}
204 game.portals[big_yx][little_yx] = msg
206 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
208 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
209 if not big_yx in game.annotations:
210 game.annotations[big_yx] = {}
211 game.annotations[big_yx][little_yx] = msg
213 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
215 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
216 if not big_yx in game.portals:
217 game.portals[big_yx] = {}
218 game.portals[big_yx][little_yx] = msg
220 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
222 def cmd_GET_ANNOTATION(game, yx, connection_id):
223 player = game.get_player(connection_id)
224 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
225 annotation = '(unknown)';
226 if player.fov_test(big_yx, little_yx):
227 annotation = '(none)';
228 if big_yx in game.annotations:
229 if little_yx in game.annotations[big_yx]:
230 annotation = game.annotations[big_yx][little_yx]
231 game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
232 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
234 def cmd_MAP_LINE(game, big_yx, y, line):
235 map_ = game.get_map(big_yx)
236 map_.set_line(y, line)
237 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
239 def cmd_MAP(game, geometry, size):
240 map_geometry_class = globals()['MapGeometry' + geometry]
241 game.new_world(map_geometry_class(size))
242 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
244 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
245 map_control = game.get_map(big_yx, 'control')
246 map_control.set_line(y, line)
247 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
249 def cmd_MAP_CONTROL_PW(game, tile_class, password):
250 game.map_control_passwords[tile_class] = password
251 cmd_MAP_CONTROL_PW.argtypes = 'char string'
253 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
254 if not thing_type in game.thing_types:
255 raise GameError('illegal thing type %s' % thing_type)
256 _ = game.get_map(big_yx)
259 t_old = game.get_thing(thing_id)
260 t_new = game.thing_types[thing_type](game, id_=thing_id, position=(big_yx,
263 game.things[game.things.index(t_old)] = t_new
265 game.things += [t_new]
267 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
269 def cmd_THING_NAME(game, thing_id, name):
270 t = game.get_thing(thing_id)
272 raise GameError('thing of ID %s not found' % thing_id)
274 cmd_THING_NAME.argtypes = 'int:pos string'