1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3 from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex, Map
7 # TODO: instead of sending tasks and thing types 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(), t_t.symbol_hint),
18 cmd_THING_TYPES.argtypes = ''
20 def cmd_ALL(game, msg, connection_id):
22 def lower_msg_by_volume(msg, volume):
26 while random.random() > volume * 8:
29 elif c != '.' and c != ' ':
37 if not connection_id in game.sessions:
38 raise GameError('need to be logged in for this')
39 speaker = game.get_thing(game.sessions[connection_id])
41 map_size = game.map.size_i
42 dijkstra_map = [n_max for i in range(game.map.size_i)]
43 dijkstra_map[game.map.get_position_index(speaker.position)] = 0
47 for i in range(map_size):
48 if game.map.terrain[i] == 'X':
50 neighbors = game.map_geometry.get_neighbors_i(i)
51 for direction in [d for d in neighbors if neighbors[d]]:
52 j = neighbors[direction]
53 if dijkstra_map[j] < dijkstra_map[i] - 1:
54 dijkstra_map[i] = dijkstra_map[j] + 1
59 #for n in dijkstra_map:
60 # line_to_print += ['%3s' % n]
62 # if x >= game.map.size.x:
64 # print(' '.join(line_to_print))
65 for c_id in game.sessions:
66 listener = game.get_thing(game.sessions[c_id])
67 listener_vol = dijkstra_map[game.map.get_position_index(listener.position)]
68 volume = 1 / max(1, listener_vol)
69 lowered_msg = lower_msg_by_volume(msg, volume)
70 lowered_nick = lower_msg_by_volume(speaker.name, volume)
71 game.io.send('CHAT ' +
72 quote('(volume: %.2f) %s: %s' % (volume, lowered_nick,
75 cmd_ALL.argtypes = 'string'
77 def cmd_LOGIN(game, nick, connection_id):
78 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
79 raise GameError('name already in use')
80 if connection_id in game.sessions:
81 raise GameError('cannot log in twice')
82 t = game.thing_types['Player'](game)
83 t.position = YX(game.map.size.y // 2, game.map.size.x // 2)
84 game.things += [t] # TODO refactor into Thing.__init__?
85 t.player_char = game.get_next_player_char()
86 game.sessions[connection_id] = t.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_NICK(game, nick, connection_id):
95 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
96 raise GameError('name already in use')
97 if not connection_id in game.sessions:
98 raise GameError('can only rename when already logged in')
99 t_id = game.sessions[connection_id]
100 t = game.get_thing(t_id)
103 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
105 cmd_NICK.argtypes = 'string'
107 def cmd_GET_GAMESTATE(game, connection_id):
108 game.send_gamestate(connection_id)
109 cmd_GET_GAMESTATE.argtypes = ''
111 #def cmd_QUERY(game, target_nick, msg, connection_id):
112 # if not connection_id in game.sessions:
113 # raise GameError('can only query when logged in')
114 # t = game.get_thing(game.sessions[connection_id], False)
115 # source_nick = t.name
116 # for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
117 # for c_id in game.sessions:
118 # if game.sessions[c_id] == t.id_:
119 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
120 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
122 # raise GameError('target user offline')
123 # raise GameError('can only query with registered nicknames')
124 #cmd_QUERY.argtypes = 'string string'
126 def cmd_PING(game, connection_id):
127 game.io.send('PONG', connection_id)
128 cmd_PING.argtypes = ''
130 def cmd_TURN(game, n):
132 cmd_TURN.argtypes = 'int:nonneg'
134 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
135 player = game.get_thing(game.sessions[connection_id])
136 if player.fov_stencil[yx] != '.':
137 raise GameError('cannot annotate tile outside field of view')
138 if not game.can_do_tile_with_pw(yx, pw):
139 raise GameError('wrong password for tile')
141 if yx in game.annotations:
142 del game.annotations[yx]
144 game.annotations[yx] = msg
146 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
148 def cmd_PORTAL(game, yx, msg, pw, connection_id):
149 player = game.get_thing(game.sessions[connection_id])
150 if player.fov_stencil[yx] != '.':
151 raise GameError('cannot edit portal on tile outside field of view')
152 if not game.can_do_tile_with_pw(yx, pw):
153 raise GameError('wrong password for tile')
155 if yx in game.portals:
158 game.portals[yx] = msg
160 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
162 def cmd_GOD_ANNOTATE(game, yx, msg):
163 game.annotations[yx] = msg
165 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
167 def cmd_GOD_PORTAL(game, yx, msg):
168 game.portals[yx] = msg
170 cmd_GOD_PORTAL.argtypes = 'yx_tuple:nonneg string'
172 def cmd_GET_ANNOTATION(game, yx, connection_id):
173 player = game.get_thing(game.sessions[connection_id])
174 annotation = '(unknown)';
175 if player.fov_stencil[yx] == '.':
176 annotation = '(none)';
177 if yx in game.annotations:
178 annotation = game.annotations[yx]
179 game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
180 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
182 def cmd_MAP_LINE(game, y, line):
183 game.map.set_line(y, line)
184 cmd_MAP_LINE.argtypes = 'int:nonneg string'
186 def cmd_MAP(game, geometry, size):
187 map_geometry_class = globals()['MapGeometry' + geometry]
188 game.new_world(map_geometry_class(size))
189 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
191 def cmd_MAP_CONTROL_LINE(game, y, line):
192 game.map_control.set_line(y, line)
193 cmd_MAP_CONTROL_LINE.argtypes = 'int:nonneg string'
195 def cmd_MAP_CONTROL_PW(game, tile_class, password):
196 game.map_control_passwords[tile_class] = password
197 cmd_MAP_CONTROL_PW.argtypes = 'char string'
199 def cmd_THING(game, yx, thing_type, thing_id):
200 if not thing_type in game.thing_types:
201 raise GameError('illegal thing type %s' % thing_type)
202 if yx.y < 0 or yx.x < 0 or yx.y >= game.map.size.y or yx.x >= game.map.size.x:
203 raise GameError('illegal position %s' % yx)
206 t_old = game.get_thing(thing_id)
207 t_new = game.thing_types[thing_type](game, id_=thing_id, position=yx)
209 game.things[game.things.index(t_old)] = t_new
211 game.things += [t_new]
213 cmd_THING.argtypes = 'yx_tuple:nonneg string:thing_type int:nonneg'
215 def cmd_THING_NAME(game, thing_id, name):
216 t = game.get_thing(thing_id)
218 raise GameError('thing of ID %s not found' % thing_id)
220 cmd_THING_NAME.argtypes = 'int:pos string'