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.nickname, 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.nickname == 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 game.sessions[connection_id] = t.id_
86 game.io.send('LOGIN_OK', connection_id)
88 game.io.send('CHAT ' + quote(t.nickname + ' entered the map.'))
89 game.io.send('PLAYER_ID %s' % t.id_, connection_id)
91 cmd_LOGIN.argtypes = 'string'
93 def cmd_NICK(game, nick, connection_id):
94 for t in [t for t in game.things if t.type_ == 'Player' and t.nickname == nick]:
95 raise GameError('name already in use')
96 if not connection_id in game.sessions:
97 raise GameError('can only rename when already logged in')
98 t_id = game.sessions[connection_id]
99 t = game.get_thing(t_id)
100 old_nick = t.nickname
102 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
104 cmd_NICK.argtypes = 'string'
106 def cmd_GET_GAMESTATE(game, connection_id):
107 game.send_gamestate(connection_id)
108 cmd_GET_GAMESTATE.argtypes = ''
110 #def cmd_QUERY(game, target_nick, msg, connection_id):
111 # if not connection_id in game.sessions:
112 # raise GameError('can only query when logged in')
113 # t = game.get_thing(game.sessions[connection_id], False)
114 # source_nick = t.nickname
115 # for t in [t for t in game.things if t.type_ == 'Player' and t.nickname == target_nick]:
116 # for c_id in game.sessions:
117 # if game.sessions[c_id] == t.id_:
118 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
119 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
121 # raise GameError('target user offline')
122 # raise GameError('can only query with registered nicknames')
123 #cmd_QUERY.argtypes = 'string string'
125 def cmd_PING(game, connection_id):
126 game.io.send('PONG', connection_id)
127 cmd_PING.argtypes = ''
129 def cmd_TURN(game, n):
131 cmd_TURN.argtypes = 'int:nonneg'
133 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
134 player = game.get_thing(game.sessions[connection_id])
135 if player.fov_stencil[yx] != '.':
136 raise GameError('cannot annotate tile outside field of view')
137 if not game.can_do_tile_with_pw(yx, pw):
138 raise GameError('wrong password for tile')
140 if yx in game.annotations:
141 del game.annotations[yx]
143 game.annotations[yx] = msg
145 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
147 def cmd_PORTAL(game, yx, msg, pw, connection_id):
148 player = game.get_thing(game.sessions[connection_id])
149 if player.fov_stencil[yx] != '.':
150 raise GameError('cannot edit portal on tile outside field of view')
151 if not game.can_do_tile_with_pw(yx, pw):
152 raise GameError('wrong password for tile')
154 if yx in game.portals:
157 game.portals[yx] = msg
159 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
161 def cmd_GOD_ANNOTATE(game, yx, msg):
162 game.annotations[yx] = msg
164 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
166 def cmd_GOD_PORTAL(game, yx, msg):
167 game.portals[yx] = msg
169 cmd_GOD_PORTAL.argtypes = 'yx_tuple:nonneg string'
171 def cmd_GET_ANNOTATION(game, yx, connection_id):
172 player = game.get_thing(game.sessions[connection_id])
173 annotation = '(unknown)';
174 if player.fov_stencil[yx] == '.':
175 annotation = '(none)';
176 if yx in game.annotations:
177 annotation = game.annotations[yx]
178 game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
179 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
181 def cmd_MAP_LINE(game, y, line):
182 game.map.set_line(y, line)
183 cmd_MAP_LINE.argtypes = 'int:nonneg string'
185 def cmd_MAP(game, geometry, size):
186 map_geometry_class = globals()['MapGeometry' + geometry]
187 game.new_world(map_geometry_class(size))
188 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
190 def cmd_MAP_CONTROL_LINE(game, y, line):
191 game.map_control.set_line(y, line)
192 cmd_MAP_CONTROL_LINE.argtypes = 'int:nonneg string'
194 def cmd_MAP_CONTROL_PW(game, tile_class, password):
195 game.map_control_passwords[tile_class] = password
196 cmd_MAP_CONTROL_PW.argtypes = 'char string'
198 def cmd_THING(game, yx, thing_type, thing_id):
199 if not thing_type in game.thing_types:
200 raise GameError('illegal thing type %s' % thing_type)
201 if yx.y < 0 or yx.x < 0 or yx.y >= game.map.size.y or yx.x >= game.map.size.x:
202 raise GameError('illegal position %s' % yx)
205 t_old = game.get_thing(thing_id)
206 t_new = game.thing_types[thing_type](game, id_=thing_id, position=yx)
208 game.things[game.things.index(t_old)] = t_new
210 game.things += [t_new]
212 cmd_THING.argtypes = 'yx_tuple:nonneg string:thing_type int:nonneg'