home · contact · privacy
Add basic flood protection to server.
[plomrogue2] / plomrogue / commands.py
1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3 from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex, Map
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):
29         lowered_msg = ''
30         for c in msg:
31             c = c
32             while random.random() > volume * 8:
33                 if c.isupper():
34                     c = c.lower()
35                 elif c != '.' and c != ' ':
36                     c = '.'
37                 else:
38                     c = ' '
39             lowered_msg += c
40         return lowered_msg
41
42     import random
43     if not connection_id in game.sessions:
44         raise GameError('need to be logged in for this')
45     speaker = game.get_thing(game.sessions[connection_id])
46     n_max = 255
47     map_size = game.map.size_i
48     dijkstra_map = [n_max for i in range(game.map.size_i)]
49     dijkstra_map[game.map.get_position_index(speaker.position)] = 0
50     shrunk = True
51     while shrunk:
52         shrunk = False
53         for i in range(map_size):
54             if game.map.terrain[i] == 'X':
55                 continue
56             neighbors = game.map_geometry.get_neighbors_i(i)
57             for direction in [d for d in neighbors if neighbors[d]]:
58                 j = neighbors[direction]
59                 if dijkstra_map[j] < dijkstra_map[i] - 1:
60                     dijkstra_map[i] = dijkstra_map[j] + 1
61                     shrunk = True
62     #print('DEBUG')
63     #line_to_print = []
64     #x = 0
65     #for n in dijkstra_map:
66     #    line_to_print += ['%3s' % n]
67     #    x += 1
68     #    if x >= game.map.size.x:
69     #        x = 0
70     #        print(' '.join(line_to_print))
71     for c_id in game.sessions:
72         listener = game.get_thing(game.sessions[c_id])
73         listener_vol = dijkstra_map[game.map.get_position_index(listener.position)]
74         volume = 1 / max(1, listener_vol)
75         lowered_msg = lower_msg_by_volume(msg, volume)
76         lowered_nick = lower_msg_by_volume(speaker.name, volume)
77         game.io.send('CHAT ' +
78                      quote('(volume: %.2f) %s: %s' % (volume, lowered_nick,
79                                                       lowered_msg)),
80                      c_id)
81 cmd_ALL.argtypes = 'string'
82
83 def cmd_LOGIN(game, nick, connection_id):
84     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
85         raise GameError('name already in use')
86     if connection_id in game.sessions:
87         raise GameError('cannot log in twice')
88     t = game.thing_types['Player'](game)
89     t.position = YX(game.map.size.y // 2, game.map.size.x // 2)
90     game.things += [t]  # TODO refactor into Thing.__init__?
91     t.player_char = game.get_next_player_char()
92     game.sessions[connection_id] = t.id_
93     game.io.send('LOGIN_OK', connection_id)
94     t.name = nick
95     game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
96     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
97     game.changed = True
98 cmd_LOGIN.argtypes = 'string'
99
100 def cmd_NICK(game, nick, connection_id):
101     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
102         raise GameError('name already in use')
103     if not connection_id in game.sessions:
104         raise GameError('can only rename when already logged in')
105     t_id = game.sessions[connection_id]
106     t = game.get_thing(t_id)
107     old_nick = t.name
108     t.name = nick
109     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
110     game.changed = True
111 cmd_NICK.argtypes = 'string'
112
113 def cmd_GET_GAMESTATE(game, connection_id):
114     game.send_gamestate(connection_id)
115 cmd_GET_GAMESTATE.argtypes = ''
116
117 #def cmd_QUERY(game, target_nick, msg, connection_id):
118 #    if not connection_id in game.sessions:
119 #        raise GameError('can only query when logged in')
120 #    t = game.get_thing(game.sessions[connection_id], False)
121 #    source_nick = t.name
122 #    for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
123 #        for c_id in game.sessions:
124 #            if game.sessions[c_id] == t.id_:
125 #                game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
126 #                game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
127 #                return
128 #        raise GameError('target user offline')
129 #    raise GameError('can only query with registered nicknames')
130 #cmd_QUERY.argtypes = 'string string'
131
132 def cmd_PING(game, connection_id):
133     game.io.send('PONG', connection_id)
134 cmd_PING.argtypes = ''
135
136 def cmd_TURN(game, n):
137     game.turn = n
138 cmd_TURN.argtypes = 'int:nonneg'
139
140 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
141     player = game.get_thing(game.sessions[connection_id])
142     if player.fov_stencil[yx] != '.':
143         raise GameError('cannot annotate tile outside field of view')
144     if not game.can_do_tile_with_pw(yx, pw):
145         raise GameError('wrong password for tile')
146     if msg == ' ':
147         if yx in game.annotations:
148             del game.annotations[yx]
149     else:
150         game.annotations[yx] = msg
151     game.changed = True
152 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
153
154 def cmd_PORTAL(game, yx, msg, pw, connection_id):
155     player = game.get_thing(game.sessions[connection_id])
156     if player.fov_stencil[yx] != '.':
157         raise GameError('cannot edit portal on tile outside field of view')
158     if not game.can_do_tile_with_pw(yx, pw):
159         raise GameError('wrong password for tile')
160     if msg == ' ':
161         if yx in game.portals:
162             del game.portals[yx]
163     else:
164         game.portals[yx] = msg
165     game.changed = True
166 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
167
168 def cmd_GOD_ANNOTATE(game, yx, msg):
169     game.annotations[yx] = msg
170     game.changed = True
171 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
172
173 def cmd_GOD_PORTAL(game, yx, msg):
174     game.portals[yx] = msg
175     game.changed = True
176 cmd_GOD_PORTAL.argtypes = 'yx_tuple:nonneg string'
177
178 def cmd_GET_ANNOTATION(game, yx, connection_id):
179     player = game.get_thing(game.sessions[connection_id])
180     annotation = '(unknown)';
181     if player.fov_stencil[yx] == '.':
182         annotation = '(none)';
183         if yx in game.annotations:
184             annotation = game.annotations[yx]
185     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
186 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
187
188 def cmd_MAP_LINE(game, y, line):
189     game.map.set_line(y, line)
190 cmd_MAP_LINE.argtypes = 'int:nonneg string'
191
192 def cmd_MAP(game, geometry, size):
193     map_geometry_class = globals()['MapGeometry' + geometry]
194     game.new_world(map_geometry_class(size))
195 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
196
197 def cmd_MAP_CONTROL_LINE(game, y, line):
198     game.map_control.set_line(y, line)
199 cmd_MAP_CONTROL_LINE.argtypes = 'int:nonneg string'
200
201 def cmd_MAP_CONTROL_PW(game, tile_class, password):
202     game.map_control_passwords[tile_class] = password
203 cmd_MAP_CONTROL_PW.argtypes = 'char string'
204
205 def cmd_THING(game, yx, thing_type, thing_id):
206     if not thing_type in game.thing_types:
207         raise GameError('illegal thing type %s' % thing_type)
208     if yx.y < 0 or yx.x < 0 or yx.y >= game.map.size.y or yx.x >= game.map.size.x:
209         raise GameError('illegal position %s' % yx)
210     t_old = None
211     if thing_id > 0:
212         t_old = game.get_thing(thing_id)
213     t_new = game.thing_types[thing_type](game, id_=thing_id, position=yx)
214     if t_old:
215         game.things[game.things.index(t_old)] = t_new
216     else:
217         game.things += [t_new]
218     game.changed = True
219 cmd_THING.argtypes = 'yx_tuple:nonneg string:thing_type int:nonneg'
220
221 def cmd_THING_NAME(game, thing_id, name):
222     t = game.get_thing(thing_id)
223     if not t:
224         raise GameError('thing of ID %s not found' % thing_id)
225     t.name = name
226 cmd_THING_NAME.argtypes = 'int:pos string'