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 / 8
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_LOGIN(game, nick, connection_id):
69 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
70 raise GameError('name already in use')
71 if game.get_player(connection_id):
72 raise GameError('cannot log in twice')
73 t = game.thing_types['Player'](game)
74 t.position = (YX(0,0),
75 YX(game.map_geometry.size.y // 2, game.map_geometry.size.x // 2))
76 game.things += [t] # TODO refactor into Thing.__init__?
77 t.player_char = game.get_next_player_char()
78 game.sessions[connection_id] = {
82 game.io.send('LOGIN_OK', connection_id)
84 game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
85 game.io.send('PLAYER_ID %s' % t.id_, connection_id)
87 cmd_LOGIN.argtypes = 'string'
89 def cmd_BECOME_ADMIN(game, password, connection_id):
90 player = game.thing_types['Player'](game)
92 raise GameError('need to be logged in for this')
93 if password in game.admin_passwords:
94 game.sessions[connection_id]['status'] = 'admin'
96 raise GameError('wrong password')
97 cmd_BECOME_ADMIN.argtypes = 'string'
99 def cmd_ADMIN_PASSWORD(game, password):
100 game.admin_passwords += [password]
101 cmd_ADMIN_PASSWORD.argtypes = 'string'
103 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
104 player = game.get_player(connection_id)
106 raise GameError('need to be logged in for this')
107 if not game.sessions[connection_id]['status'] == 'admin':
108 raise GameError('need to be admin for this')
109 if not (control_char == '.'
110 or control_char in game.map_control_passwords.keys()):
111 raise GameError('no password set for this tile class')
112 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
113 map_control = game.get_map(big_yx, 'control')
114 map_control[little_yx] = control_char
116 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
118 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
119 player = game.get_player(connection_id)
121 raise GameError('need to be logged in for this')
122 if not game.sessions[connection_id]['status'] == 'admin':
123 raise GameError('need to be admin for this')
124 if tile_class == '.':
125 raise GameError('tile class "." must remain unprotected')
126 game.map_control_passwords[tile_class] = password
128 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
130 def cmd_NICK(game, nick, connection_id):
131 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
132 raise GameError('name already in use')
133 t = game.get_player(connection_id)
135 raise GameError('can only rename when already logged in')
138 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
140 cmd_NICK.argtypes = 'string'
142 def cmd_GET_GAMESTATE(game, connection_id):
143 game.send_gamestate(connection_id)
144 cmd_GET_GAMESTATE.argtypes = ''
146 #def cmd_QUERY(game, target_nick, msg, connection_id):
147 # if not connection_id in game.sessions:
148 # raise GameError('can only query when logged in')
149 # t = game.get_thing(game.sessions[connection_id], False)
150 # source_nick = t.name
151 # for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
152 # for c_id in game.sessions:
153 # if game.sessions[c_id] == t.id_:
154 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
155 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
157 # raise GameError('target user offline')
158 # raise GameError('can only query with registered nicknames')
159 #cmd_QUERY.argtypes = 'string string'
161 def cmd_PING(game, connection_id):
162 game.io.send('PONG', connection_id)
163 cmd_PING.argtypes = ''
165 def cmd_TURN(game, n):
167 cmd_TURN.argtypes = 'int:nonneg'
169 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
170 player = game.get_player(connection_id)
171 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
172 if not player.fov_test(big_yx, little_yx):
173 raise GameError('cannot annotate tile outside field of view')
174 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
175 raise GameError('wrong password for tile')
177 if big_yx in game.annotations:
178 if little_yx in game.annotations[big_yx]:
179 del game.annotations[big_yx][little_yx]
181 if not big_yx in game.annotations:
182 game.annotations[big_yx] = {}
183 game.annotations[big_yx][little_yx] = msg
185 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
187 def cmd_PORTAL(game, yx, msg, pw, connection_id):
188 player = game.get_player(connection_id)
189 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
190 if not player.fov_test(big_yx, little_yx):
191 raise GameError('cannot edit portal on tile outside field of view')
192 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
193 raise GameError('wrong password for tile')
195 if big_yx in game.portals:
196 if little_yx in game.portals[big_yx]:
197 del game.portals[big_yx][little_xy]
199 if not big_yx in game.portals:
200 game.portals[big_yx] = {}
201 game.portals[big_yx][little_yx] = msg
203 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
205 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
206 if not big_yx in game.annotations:
207 game.annotations[big_yx] = {}
208 game.annotations[big_yx][little_yx] = msg
210 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
212 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
213 if not big_yx in game.portals:
214 game.portals[big_yx] = {}
215 game.portals[big_yx][little_yx] = msg
217 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
219 def cmd_GET_ANNOTATION(game, yx, connection_id):
220 player = game.get_player(connection_id)
221 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
222 annotation = '(unknown)';
223 if player.fov_test(big_yx, little_yx):
224 annotation = '(none)';
225 if big_yx in game.annotations:
226 if little_yx in game.annotations[big_yx]:
227 annotation = game.annotations[big_yx][little_yx]
228 game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
229 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
231 def cmd_MAP_LINE(game, big_yx, y, line):
232 map_ = game.get_map(big_yx)
233 map_.set_line(y, line)
234 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
236 def cmd_MAP(game, geometry, size):
237 map_geometry_class = globals()['MapGeometry' + geometry]
238 game.new_world(map_geometry_class(size))
239 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
241 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
242 map_control = game.get_map(big_yx, 'control')
243 map_control.set_line(y, line)
244 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
246 def cmd_MAP_CONTROL_PW(game, tile_class, password):
247 game.map_control_passwords[tile_class] = password
248 cmd_MAP_CONTROL_PW.argtypes = 'char string'
250 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
251 if not thing_type in game.thing_types:
252 raise GameError('illegal thing type %s' % thing_type)
253 map_ = game.get_map(big_yx)
256 t_old = game.get_thing(thing_id)
257 t_new = game.thing_types[thing_type](game, id_=thing_id, position=(big_yx,
260 game.things[game.things.index(t_old)] = t_new
262 game.things += [t_new]
264 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
266 def cmd_THING_NAME(game, thing_id, name):
267 t = game.get_thing(thing_id)
269 raise GameError('thing of ID %s not found' % thing_id)
271 cmd_THING_NAME.argtypes = 'int:pos string'