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