home · contact · privacy
26576d052649b9b3f903650729ad760c79f6bf3c
[plomrogue2] / plomrogue / commands.py
1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3 from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex, DijkstraMap
4
5
6
7 # TODO: instead of sending tasks, thing types etc. on request, send them on connection
8
9 def cmd_TASKS(game, connection_id):
10     tasks = []
11     game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
12 cmd_TASKS.argtypes = ''
13
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)),
17                      connection_id)
18 cmd_THING_TYPES.argtypes = ''
19
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])),
23                      connection_id)
24 cmd_TERRAINS.argtypes = ''
25
26 def cmd_ALL(game, msg, connection_id):
27
28     def lower_msg_by_volume(msg, volume, largest_audible_distance):
29         import random
30         factor = largest_audible_distance / 8
31         lowered_msg = ''
32         for c in msg:
33             c = c
34             while random.random() > volume * factor:
35                 if c.isupper():
36                     c = c.lower()
37                 elif c != '.' and c != ' ':
38                     c = '.'
39                 else:
40                     c = ' '
41             lowered_msg += c
42         return lowered_msg
43
44     speaker = game.get_player(connection_id)
45     if not speaker:
46         raise GameError('need to be logged in for this')
47     largest_audible_distance = 40
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)
53         if not target_yx:
54             continue
55         listener_distance = dijkstra_map[target_yx]
56         if listener_distance > largest_audible_distance:
57             continue
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,
64                                                       lowered_msg)),
65                      c_id)
66 cmd_ALL.argtypes = 'string'
67
68 def cmd_SPAWN_POINT(game, big_yx, little_yx):
69     if little_yx.y >= game.map_geometry.size.y or \
70        little_yx.x >= game.map_geometry.size.x:
71         raise GameError('illegal spawn point')
72     game.spawn_point = big_yx, little_yx
73 cmd_SPAWN_POINT.argtypes = 'yx_tuple yx_tuple:nonneg'
74
75 def cmd_LOGIN(game, nick, connection_id):
76     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
77         raise GameError('name already in use')
78     if game.get_player(connection_id):
79         raise GameError('cannot log in twice')
80     t = game.thing_types['Player'](game)
81     t.position = game.spawn_point
82     game.things += [t]  # TODO refactor into Thing.__init__?
83     t.player_char = game.get_next_player_char()
84     game.sessions[connection_id] = {
85         'thing_id': t.id_,
86         'status': 'player'
87     }
88     game.io.send('LOGIN_OK', connection_id)
89     t.name = nick
90     game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
91     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
92     game.changed = True
93 cmd_LOGIN.argtypes = 'string'
94
95 def cmd_BECOME_ADMIN(game, password, connection_id):
96     player = game.thing_types['Player'](game)
97     if not player:
98         raise GameError('need to be logged in for this')
99     if password in game.admin_passwords:
100         game.sessions[connection_id]['status'] = 'admin'
101     else:
102         raise GameError('wrong password')
103 cmd_BECOME_ADMIN.argtypes = 'string'
104
105 def cmd_ADMIN_PASSWORD(game, password):
106     game.admin_passwords += [password]
107 cmd_ADMIN_PASSWORD.argtypes = 'string'
108
109 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
110     player = game.get_player(connection_id)
111     if not player:
112         raise GameError('need to be logged in for this')
113     if not game.sessions[connection_id]['status'] == 'admin':
114         raise GameError('need to be admin for this')
115     if not (control_char == '.'
116             or control_char in game.map_control_passwords.keys()):
117         raise GameError('no password set for this tile class')
118     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
119     map_control = game.get_map(big_yx, 'control')
120     map_control[little_yx] = control_char
121     game.changed = True
122 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
123
124 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
125     player = game.get_player(connection_id)
126     if not player:
127         raise GameError('need to be logged in for this')
128     if not game.sessions[connection_id]['status'] == 'admin':
129         raise GameError('need to be admin for this')
130     if tile_class == '.':
131         raise GameError('tile class "." must remain unprotected')
132     game.map_control_passwords[tile_class] = password
133     game.changed = True
134 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
135
136 def cmd_NICK(game, nick, connection_id):
137     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
138         raise GameError('name already in use')
139     t = game.get_player(connection_id)
140     if not t:
141         raise GameError('can only rename when already logged in')
142     old_nick = t.name
143     t.name = nick
144     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
145     game.changed = True
146 cmd_NICK.argtypes = 'string'
147
148 def cmd_GET_GAMESTATE(game, connection_id):
149     game.send_gamestate(connection_id)
150 cmd_GET_GAMESTATE.argtypes = ''
151
152 #def cmd_QUERY(game, target_nick, msg, connection_id):
153 #    if not connection_id in game.sessions:
154 #        raise GameError('can only query when logged in')
155 #    t = game.get_thing(game.sessions[connection_id], False)
156 #    source_nick = t.name
157 #    for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
158 #        for c_id in game.sessions:
159 #            if game.sessions[c_id] == t.id_:
160 #                game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
161 #                game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
162 #                return
163 #        raise GameError('target user offline')
164 #    raise GameError('can only query with registered nicknames')
165 #cmd_QUERY.argtypes = 'string string'
166
167 def cmd_PING(game, connection_id):
168     game.io.send('PONG', connection_id)
169 cmd_PING.argtypes = ''
170
171 def cmd_TURN(game, n):
172     game.turn = n
173 cmd_TURN.argtypes = 'int:nonneg'
174
175 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
176     player = game.get_player(connection_id)
177     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
178     if not player.fov_test(big_yx, little_yx):
179         raise GameError('cannot annotate tile outside field of view')
180     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
181         raise GameError('wrong password for tile')
182     if msg == ' ':
183         if big_yx in game.annotations:
184             if little_yx in game.annotations[big_yx]:
185                 del game.annotations[big_yx][little_yx]
186     else:
187         if not big_yx in game.annotations:
188             game.annotations[big_yx] = {}
189         game.annotations[big_yx][little_yx] = msg
190     game.changed = True
191 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
192
193 def cmd_PORTAL(game, yx, msg, pw, connection_id):
194     player = game.get_player(connection_id)
195     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
196     if not player.fov_test(big_yx, little_yx):
197         raise GameError('cannot edit portal on tile outside field of view')
198     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
199         raise GameError('wrong password for tile')
200     if msg == ' ':
201         if big_yx in game.portals:
202             if little_yx in game.portals[big_yx]:
203                 del game.portals[big_yx][little_xy]
204     else:
205         if not big_yx in game.portals:
206             game.portals[big_yx] = {}
207         game.portals[big_yx][little_yx] = msg
208     game.changed = True
209 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
210
211 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
212     if not big_yx in game.annotations:
213         game.annotations[big_yx] = {}
214     game.annotations[big_yx][little_yx] = msg
215     game.changed = True
216 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
217
218 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
219     if not big_yx in game.portals:
220         game.portals[big_yx] = {}
221     game.portals[big_yx][little_yx] = msg
222     game.changed = True
223 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
224
225 def cmd_GET_ANNOTATION(game, yx, connection_id):
226     player = game.get_player(connection_id)
227     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
228     annotation = '(unknown)';
229     if player.fov_test(big_yx, little_yx):
230         annotation = '(none)';
231         if big_yx in game.annotations:
232             if little_yx in game.annotations[big_yx]:
233                 annotation = game.annotations[big_yx][little_yx]
234     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
235 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
236
237 def cmd_MAP_LINE(game, big_yx, y, line):
238     map_ = game.get_map(big_yx)
239     map_.set_line(y, line)
240 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
241
242 def cmd_MAP(game, geometry, size):
243     map_geometry_class = globals()['MapGeometry' + geometry]
244     game.new_world(map_geometry_class(size))
245 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
246
247 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
248     map_control = game.get_map(big_yx, 'control')
249     map_control.set_line(y, line)
250 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
251
252 def cmd_MAP_CONTROL_PW(game, tile_class, password):
253     game.map_control_passwords[tile_class] = password
254 cmd_MAP_CONTROL_PW.argtypes = 'char string'
255
256 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
257     if not thing_type in game.thing_types:
258         raise GameError('illegal thing type %s' % thing_type)
259     map_ = game.get_map(big_yx)
260     t_old = None
261     if thing_id > 0:
262         t_old = game.get_thing(thing_id)
263     t_new = game.thing_types[thing_type](game, id_=thing_id, position=(big_yx,
264                                                                        little_yx))
265     if t_old:
266         game.things[game.things.index(t_old)] = t_new
267     else:
268         game.things += [t_new]
269     game.changed = True
270 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
271
272 def cmd_THING_NAME(game, thing_id, name):
273     t = game.get_thing(thing_id)
274     if not t:
275         raise GameError('thing of ID %s not found' % thing_id)
276     t.name = name
277 cmd_THING_NAME.argtypes = 'int:pos string'