home · contact · privacy
Lower volume of messages by obfuscating them.
[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 def cmd_TASKS(game, connection_id):
8     tasks = []
9     game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
10 cmd_TASKS.argtypes = ''
11
12 def cmd_ALL(game, msg, connection_id):
13
14     def lower_msg_by_volume(msg, volume):
15         lowered_msg = ''
16         for c in msg:
17             c = c
18             while random.random() > volume * 4:
19                 if c.isupper():
20                     c = c.lower()
21                 elif c != '.':
22                     c = '.'
23                 else:
24                     c = ' '
25             lowered_msg += c
26         return lowered_msg
27
28     import random
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)
33     n_max = 256
34     dijkstra_map.terrain = [n_max for i in range(dijkstra_map.size_i)]
35     dijkstra_map[speaker.position] = 0
36     shrunk = True
37     while shrunk:
38         shrunk = False
39         for pos in dijkstra_map:
40             if game.map[pos] == 'X':
41                 continue
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
47                     shrunk = True
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,
55                                                       lowered_msg)),
56                      c_id)
57 cmd_ALL.argtypes = 'string'
58
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)
69     t.nickname = nick
70     game.io.send('CHAT ' + quote(t.nickname + ' entered the map.'))
71     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
72     game.changed = True
73 cmd_LOGIN.argtypes = 'string'
74
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)
82     old_nick = t.nickname
83     t.nickname = nick
84     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
85     game.changed = True
86 cmd_NICK.argtypes = 'string'
87
88 def cmd_GET_GAMESTATE(game, connection_id):
89     game.send_gamestate(connection_id)
90 cmd_GET_GAMESTATE.argtypes = ''
91
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)
102                 return
103         raise GameError('target user offline')
104     raise GameError('can only query with registered nicknames')
105 cmd_QUERY.argtypes = 'string string'
106
107 def cmd_PING(game, connection_id):
108     game.io.send('PONG', connection_id)
109 cmd_PING.argtypes = ''
110
111 def cmd_TURN(game, n):
112     game.turn = n
113 cmd_TURN.argtypes = 'int:nonneg'
114
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')
121     if msg == ' ':
122         if yx in game.annotations:
123             del game.annotations[yx]
124     else:
125         game.annotations[yx] = msg
126     game.changed = True
127 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
128
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')
135     if msg == ' ':
136         if yx in game.portals:
137             del game.portals[yx]
138     else:
139         game.portals[yx] = msg
140     game.changed = True
141 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
142
143 def cmd_GOD_ANNOTATE(game, yx, msg):
144     game.annotations[yx] = msg
145     game.changed = True
146 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
147
148 def cmd_GOD_PORTAL(game, yx, msg):
149     game.portals[yx] = msg
150     game.changed = True
151 cmd_GOD_PORTAL.argtypes = 'yx_tuple:nonneg string'
152
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'
162
163 def cmd_MAP_LINE(game, y, line):
164     game.map.set_line(y, line)
165 cmd_MAP_LINE.argtypes = 'int:nonneg string'
166
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'
171
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'
175
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'