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 * 4:
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)
32 dijkstra_map = Map(game.map_geometry.size)
34 dijkstra_map.terrain = [n_max for i in range(dijkstra_map.size_i)]
35 dijkstra_map[speaker.position] = 0
39 for pos in dijkstra_map:
40 if game.map[pos] == 'X':
42 neighbors = game.map_geometry.get_neighbors(pos)
43 for direction in [d for d in neighbors if neighbors[d]]:
44 yx = neighbors[direction]
45 if dijkstra_map[yx] < dijkstra_map[pos] - 1:
46 dijkstra_map[pos] = dijkstra_map[yx] + 1
48 for c_id in game.sessions:
49 listener = game.get_thing(game.sessions[c_id], create_unfound=False)
50 volume = 1 / max(1, dijkstra_map[listener.position])
51 lowered_msg = lower_msg_by_volume(msg, volume)
52 lowered_nick = lower_msg_by_volume(speaker.nickname, volume)
53 game.io.send('CHAT ' +
54 quote('(volume: %.2f) %s: %s' % (volume, lowered_nick,
57 cmd_ALL.argtypes = 'string'
59 def cmd_LOGIN(game, nick, connection_id):
60 for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
61 raise GameError('name already in use')
62 if connection_id in game.sessions:
63 raise GameError('cannot log in twice')
64 t = game.thing_types['player'](game)
65 t.position = YX(game.map.size.y // 2, game.map.size.x // 2)
66 game.things += [t] # TODO refactor into Thing.__init__?
67 game.sessions[connection_id] = t.id_
68 game.io.send('LOGIN_OK', connection_id)
70 game.io.send('CHAT ' + quote(t.nickname + ' entered the map.'))
71 game.io.send('PLAYER_ID %s' % t.id_, connection_id)
73 cmd_LOGIN.argtypes = 'string'
75 def cmd_NICK(game, nick, connection_id):
76 for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
77 raise GameError('name already in use')
78 if not connection_id in game.sessions:
79 raise GameError('can only rename when already logged in')
80 t_id = game.sessions[connection_id]
81 t = game.get_thing(t_id, False)
84 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
86 cmd_NICK.argtypes = 'string'
88 def cmd_GET_GAMESTATE(game, connection_id):
89 game.send_gamestate(connection_id)
90 cmd_GET_GAMESTATE.argtypes = ''
92 def cmd_QUERY(game, target_nick, msg, connection_id):
93 if not connection_id in game.sessions:
94 raise GameError('can only query when logged in')
95 t = game.get_thing(game.sessions[connection_id], False)
96 source_nick = t.nickname
97 for t in [t for t in game.things if t.type_ == 'player' and t.nickname == target_nick]:
98 for c_id in game.sessions:
99 if game.sessions[c_id] == t.id_:
100 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
101 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
103 raise GameError('target user offline')
104 raise GameError('can only query with registered nicknames')
105 cmd_QUERY.argtypes = 'string string'
107 def cmd_PING(game, connection_id):
108 game.io.send('PONG', connection_id)
109 cmd_PING.argtypes = ''
111 def cmd_TURN(game, n):
113 cmd_TURN.argtypes = 'int:nonneg'
115 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
116 player = game.get_thing(game.sessions[connection_id], False)
117 if player.fov_stencil[yx] != '.':
118 raise GameError('cannot annotate tile outside field of view')
119 if not game.can_do_tile_with_pw(yx, pw):
120 raise GameError('wrong password for tile')
122 if yx in game.annotations:
123 del game.annotations[yx]
125 game.annotations[yx] = msg
127 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
129 def cmd_PORTAL(game, yx, msg, pw, connection_id):
130 player = game.get_thing(game.sessions[connection_id], False)
131 if player.fov_stencil[yx] != '.':
132 raise GameError('cannot edit portal on tile outside field of view')
133 if not game.can_do_tile_with_pw(yx, pw):
134 raise GameError('wrong password for tile')
136 if yx in game.portals:
139 game.portals[yx] = msg
141 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
143 def cmd_GOD_ANNOTATE(game, yx, msg):
144 game.annotations[yx] = msg
146 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
148 def cmd_GOD_PORTAL(game, yx, msg):
149 game.portals[yx] = msg
151 cmd_GOD_PORTAL.argtypes = 'yx_tuple:nonneg string'
153 def cmd_GET_ANNOTATION(game, yx, connection_id):
154 player = game.get_thing(game.sessions[connection_id], False)
155 annotation = '(unknown)';
156 if player.fov_stencil[yx] == '.':
157 annotation = '(none)';
158 if yx in game.annotations:
159 annotation = game.annotations[yx]
160 game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
161 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
163 def cmd_MAP_LINE(game, y, line):
164 game.map.set_line(y, line)
165 cmd_MAP_LINE.argtypes = 'int:nonneg string'
167 def cmd_MAP(game, geometry, size):
168 map_geometry_class = globals()['MapGeometry' + geometry]
169 game.new_world(map_geometry_class(size))
170 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
172 def cmd_MAP_CONTROL_LINE(game, y, line):
173 game.map_control.set_line(y, line)
174 cmd_MAP_CONTROL_LINE.argtypes = 'int:nonneg string'
176 def cmd_MAP_CONTROL_PW(game, tile_class, password):
177 game.map_control_passwords[tile_class] = password
178 cmd_MAP_CONTROL_PW.argtypes = 'char string'