home · contact · privacy
c0795991666f941cb85ecfe11d446b61d8dab9f8
[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 = 20
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_LOGIN(game, nick, connection_id):
69     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
70         raise GameError('name already in use')
71     if game.get_player(connection_id):
72         raise GameError('cannot log in twice')
73     t = game.thing_types['Player'](game)
74     t.position = (YX(0,0),
75                   YX(game.map_geometry.size.y // 2, game.map_geometry.size.x // 2))
76     game.things += [t]  # TODO refactor into Thing.__init__?
77     t.player_char = game.get_next_player_char()
78     game.sessions[connection_id] = {
79         'thing_id': t.id_,
80         'status': 'player'
81     }
82     game.io.send('LOGIN_OK', connection_id)
83     t.name = nick
84     game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
85     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
86     game.changed = True
87 cmd_LOGIN.argtypes = 'string'
88
89 def cmd_BECOME_ADMIN(game, password, connection_id):
90     player = game.thing_types['Player'](game)
91     if not player:
92         raise GameError('need to be logged in for this')
93     if password in game.admin_passwords:
94         game.sessions[connection_id]['status'] = 'admin'
95     else:
96         raise GameError('wrong password')
97 cmd_BECOME_ADMIN.argtypes = 'string'
98
99 def cmd_ADMIN_PASSWORD(game, password):
100     game.admin_passwords += [password]
101 cmd_ADMIN_PASSWORD.argtypes = 'string'
102
103 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
104     player = game.get_player(connection_id)
105     if not player:
106         raise GameError('need to be logged in for this')
107     if not game.sessions[connection_id]['status'] == 'admin':
108         raise GameError('need to be admin for this')
109     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
110     map_control = game.get_map(big_yx, 'control')
111     map_control[little_yx] = control_char
112 cmd_SET_TILE_CONTROL.argtypes = 'yx:nonneg char'
113
114 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
115     player = game.get_player(connection_id)
116     if not player:
117         raise GameError('need to be logged in for this')
118     if not game.sessions[connection_id]['status'] == 'admin':
119         raise GameError('need to be admin for this')
120     game.map_control_passwords[tile_class] = password
121     game.changed = True
122 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
123
124 def cmd_NICK(game, nick, connection_id):
125     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
126         raise GameError('name already in use')
127     t = game.get_player(connection_id)
128     if not t:
129         raise GameError('can only rename when already logged in')
130     old_nick = t.name
131     t.name = nick
132     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
133     game.changed = True
134 cmd_NICK.argtypes = 'string'
135
136 def cmd_GET_GAMESTATE(game, connection_id):
137     game.send_gamestate(connection_id)
138 cmd_GET_GAMESTATE.argtypes = ''
139
140 #def cmd_QUERY(game, target_nick, msg, connection_id):
141 #    if not connection_id in game.sessions:
142 #        raise GameError('can only query when logged in')
143 #    t = game.get_thing(game.sessions[connection_id], False)
144 #    source_nick = t.name
145 #    for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
146 #        for c_id in game.sessions:
147 #            if game.sessions[c_id] == t.id_:
148 #                game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
149 #                game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
150 #                return
151 #        raise GameError('target user offline')
152 #    raise GameError('can only query with registered nicknames')
153 #cmd_QUERY.argtypes = 'string string'
154
155 def cmd_PING(game, connection_id):
156     game.io.send('PONG', connection_id)
157 cmd_PING.argtypes = ''
158
159 def cmd_TURN(game, n):
160     game.turn = n
161 cmd_TURN.argtypes = 'int:nonneg'
162
163 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
164     player = game.get_player(connection_id)
165     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
166     if not player.fov_test(big_yx, little_yx):
167         raise GameError('cannot annotate tile outside field of view')
168     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
169         raise GameError('wrong password for tile')
170     if msg == ' ':
171         if big_yx in game.annotations:
172             if little_yx in game.annotations[big_yx]:
173                 del game.annotations[big_yx][little_yx]
174     else:
175         if not big_yx in game.annotations:
176             game.annotations[big_yx] = {}
177         game.annotations[big_yx][little_yx] = msg
178     game.changed = True
179 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
180
181 def cmd_PORTAL(game, yx, msg, pw, connection_id):
182     player = game.get_player(connection_id)
183     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
184     if not player.fov_test(big_yx, little_yx):
185         raise GameError('cannot edit portal on tile outside field of view')
186     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
187         raise GameError('wrong password for tile')
188     if msg == ' ':
189         if big_yx in game.portals:
190             if little_yx in game.portals[big_yx]:
191                 del game.portals[big_yx][little_xy]
192     else:
193         if not big_yx in game.portals:
194             game.portals[big_yx] = {}
195         game.portals[big_yx][little_yx] = msg
196     game.changed = True
197 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
198
199 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
200     if not big_yx in game.annotations:
201         game.annotations[big_yx] = {}
202     game.annotations[big_yx][little_yx] = msg
203     game.changed = True
204 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
205
206 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
207     if not big_yx in game.portals:
208         game.portals[big_yx] = {}
209     game.portals[big_yx][little_yx] = msg
210     game.changed = True
211 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
212
213 def cmd_GET_ANNOTATION(game, yx, connection_id):
214     player = game.get_player(connection_id)
215     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
216     annotation = '(unknown)';
217     if player.fov_test(big_yx, little_yx):
218         annotation = '(none)';
219         if big_yx in game.annotations:
220             if little_yx in game.annotations[big_yx]:
221                 annotation = game.annotations[big_yx][little_yx]
222     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
223 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
224
225 def cmd_MAP_LINE(game, big_yx, y, line):
226     map_ = game.get_map(big_yx)
227     map_.set_line(y, line)
228 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
229
230 def cmd_MAP(game, geometry, size):
231     map_geometry_class = globals()['MapGeometry' + geometry]
232     game.new_world(map_geometry_class(size))
233 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
234
235 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
236     map_control = game.get_map(big_yx, 'control')
237     map_control.set_line(y, line)
238 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
239
240 def cmd_MAP_CONTROL_PW(game, tile_class, password):
241     game.map_control_passwords[tile_class] = password
242 cmd_MAP_CONTROL_PW.argtypes = 'char string'
243
244 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
245     if not thing_type in game.thing_types:
246         raise GameError('illegal thing type %s' % thing_type)
247     map_ = game.get_map(big_yx)
248     t_old = None
249     if thing_id > 0:
250         t_old = game.get_thing(thing_id)
251     t_new = game.thing_types[thing_type](game, id_=thing_id, position=(big_yx,
252                                                                        little_yx))
253     if t_old:
254         game.things[game.things.index(t_old)] = t_new
255     else:
256         game.things += [t_new]
257     game.changed = True
258 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
259
260 def cmd_THING_NAME(game, thing_id, name):
261     t = game.get_thing(thing_id)
262     if not t:
263         raise GameError('thing of ID %s not found' % thing_id)
264     t.name = name
265 cmd_THING_NAME.argtypes = 'int:pos string'