1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3 from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex, Map
7 def cmd_TASKS(game, connection_id):
9 game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
10 cmd_TASKS.argtypes = ''
12 def cmd_ALL(game, msg, connection_id):
14 def lower_msg_by_volume(msg, volume):
18 while random.random() > volume * 8:
21 elif c != '.' and c != ' ':
29 if not connection_id in game.sessions:
30 raise GameError('need to be logged in for this')
31 speaker = game.get_thing(game.sessions[connection_id], False)
33 map_size = game.map.size_i
34 dijkstra_map = [n_max for i in range(game.map.size_i)]
35 dijkstra_map[game.map.get_position_index(speaker.position)] = 0
39 for i in range(map_size):
40 if game.map.terrain[i] == 'X':
42 neighbors = game.map_geometry.get_neighbors_i(i)
43 for direction in [d for d in neighbors if neighbors[d]]:
44 j = neighbors[direction]
45 if dijkstra_map[j] < dijkstra_map[i] - 1:
46 dijkstra_map[i] = dijkstra_map[j] + 1
51 #for n in dijkstra_map:
52 # line_to_print += ['%3s' % n]
54 # if x >= game.map.size.x:
56 # print(' '.join(line_to_print))
57 for c_id in game.sessions:
58 listener = game.get_thing(game.sessions[c_id], create_unfound=False)
59 listener_vol = dijkstra_map[game.map.get_position_index(listener.position)]
60 volume = 1 / max(1, listener_vol)
61 lowered_msg = lower_msg_by_volume(msg, volume)
62 lowered_nick = lower_msg_by_volume(speaker.nickname, volume)
63 game.io.send('CHAT ' +
64 quote('(volume: %.2f) %s: %s' % (volume, lowered_nick,
67 cmd_ALL.argtypes = 'string'
69 def cmd_LOGIN(game, nick, connection_id):
70 for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
71 raise GameError('name already in use')
72 if connection_id in game.sessions:
73 raise GameError('cannot log in twice')
74 t = game.thing_types['player'](game)
75 t.position = YX(game.map.size.y // 2, game.map.size.x // 2)
76 game.things += [t] # TODO refactor into Thing.__init__?
77 game.sessions[connection_id] = t.id_
78 game.io.send('LOGIN_OK', connection_id)
80 game.io.send('CHAT ' + quote(t.nickname + ' entered the map.'))
81 game.io.send('PLAYER_ID %s' % t.id_, connection_id)
83 cmd_LOGIN.argtypes = 'string'
85 def cmd_NICK(game, nick, connection_id):
86 for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
87 raise GameError('name already in use')
88 if not connection_id in game.sessions:
89 raise GameError('can only rename when already logged in')
90 t_id = game.sessions[connection_id]
91 t = game.get_thing(t_id, False)
94 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
96 cmd_NICK.argtypes = 'string'
98 def cmd_GET_GAMESTATE(game, connection_id):
99 game.send_gamestate(connection_id)
100 cmd_GET_GAMESTATE.argtypes = ''
102 #def cmd_QUERY(game, target_nick, msg, connection_id):
103 # if not connection_id in game.sessions:
104 # raise GameError('can only query when logged in')
105 # t = game.get_thing(game.sessions[connection_id], False)
106 # source_nick = t.nickname
107 # for t in [t for t in game.things if t.type_ == 'player' and t.nickname == target_nick]:
108 # for c_id in game.sessions:
109 # if game.sessions[c_id] == t.id_:
110 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
111 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
113 # raise GameError('target user offline')
114 # raise GameError('can only query with registered nicknames')
115 #cmd_QUERY.argtypes = 'string string'
117 def cmd_PING(game, connection_id):
118 game.io.send('PONG', connection_id)
119 cmd_PING.argtypes = ''
121 def cmd_TURN(game, n):
123 cmd_TURN.argtypes = 'int:nonneg'
125 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
126 player = game.get_thing(game.sessions[connection_id], False)
127 if player.fov_stencil[yx] != '.':
128 raise GameError('cannot annotate tile outside field of view')
129 if not game.can_do_tile_with_pw(yx, pw):
130 raise GameError('wrong password for tile')
132 if yx in game.annotations:
133 del game.annotations[yx]
135 game.annotations[yx] = msg
137 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
139 def cmd_PORTAL(game, yx, msg, pw, connection_id):
140 player = game.get_thing(game.sessions[connection_id], False)
141 if player.fov_stencil[yx] != '.':
142 raise GameError('cannot edit portal on tile outside field of view')
143 if not game.can_do_tile_with_pw(yx, pw):
144 raise GameError('wrong password for tile')
146 if yx in game.portals:
149 game.portals[yx] = msg
151 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
153 def cmd_GOD_ANNOTATE(game, yx, msg):
154 game.annotations[yx] = msg
156 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
158 def cmd_GOD_PORTAL(game, yx, msg):
159 game.portals[yx] = msg
161 cmd_GOD_PORTAL.argtypes = 'yx_tuple:nonneg string'
163 def cmd_GET_ANNOTATION(game, yx, connection_id):
164 player = game.get_thing(game.sessions[connection_id], False)
165 annotation = '(unknown)';
166 if player.fov_stencil[yx] == '.':
167 annotation = '(none)';
168 if yx in game.annotations:
169 annotation = game.annotations[yx]
170 game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
171 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
173 def cmd_MAP_LINE(game, y, line):
174 game.map.set_line(y, line)
175 cmd_MAP_LINE.argtypes = 'int:nonneg string'
177 def cmd_MAP(game, geometry, size):
178 map_geometry_class = globals()['MapGeometry' + geometry]
179 game.new_world(map_geometry_class(size))
180 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
182 def cmd_MAP_CONTROL_LINE(game, y, line):
183 game.map_control.set_line(y, line)
184 cmd_MAP_CONTROL_LINE.argtypes = 'int:nonneg string'
186 def cmd_MAP_CONTROL_PW(game, tile_class, password):
187 game.map_control_passwords[tile_class] = password
188 cmd_MAP_CONTROL_PW.argtypes = 'char string'