home · contact · privacy
ed7d3ddeb2b041181a14fdd3ef29f09d50d9b3f1
[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     if not connection_id in game.sessions:
45         raise GameError('need to be logged in for this')
46     speaker = game.get_thing(game.sessions[connection_id])
47     largest_audible_distance = 20
48     dijkstra_map_class = game.map_geometry.dijkstra_map_class
49     dijkstra_map = dijkstra_map_class(game.map, speaker.position,
50                                       largest_audible_distance)
51     for c_id in game.sessions:
52         listener = game.get_thing(game.sessions[c_id])
53         target_yx = dijkstra_map.target_yx(listener.position, True)
54         if not target_yx:
55             continue
56         listener_distance = dijkstra_map[target_yx]
57         if listener_distance > largest_audible_distance:
58             continue
59         volume = 1 / max(1, listener_distance)
60         lowered_msg = lower_msg_by_volume(msg, volume, largest_audible_distance)
61         lowered_nick = lower_msg_by_volume(speaker.name, volume,
62                                            largest_audible_distance)
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.name == 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     t.player_char = game.get_next_player_char()
78     game.sessions[connection_id] = t.id_
79     game.io.send('LOGIN_OK', connection_id)
80     t.name = nick
81     game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
82     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
83     game.changed = True
84 cmd_LOGIN.argtypes = 'string'
85
86 def cmd_NICK(game, nick, connection_id):
87     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
88         raise GameError('name already in use')
89     if not connection_id in game.sessions:
90         raise GameError('can only rename when already logged in')
91     t_id = game.sessions[connection_id]
92     t = game.get_thing(t_id)
93     old_nick = t.name
94     t.name = nick
95     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
96     game.changed = True
97 cmd_NICK.argtypes = 'string'
98
99 def cmd_GET_GAMESTATE(game, connection_id):
100     game.send_gamestate(connection_id)
101 cmd_GET_GAMESTATE.argtypes = ''
102
103 #def cmd_QUERY(game, target_nick, msg, connection_id):
104 #    if not connection_id in game.sessions:
105 #        raise GameError('can only query when logged in')
106 #    t = game.get_thing(game.sessions[connection_id], False)
107 #    source_nick = t.name
108 #    for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
109 #        for c_id in game.sessions:
110 #            if game.sessions[c_id] == t.id_:
111 #                game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
112 #                game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
113 #                return
114 #        raise GameError('target user offline')
115 #    raise GameError('can only query with registered nicknames')
116 #cmd_QUERY.argtypes = 'string string'
117
118 def cmd_PING(game, connection_id):
119     game.io.send('PONG', connection_id)
120 cmd_PING.argtypes = ''
121
122 def cmd_TURN(game, n):
123     game.turn = n
124 cmd_TURN.argtypes = 'int:nonneg'
125
126 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
127     player = game.get_thing(game.sessions[connection_id])
128     source_yx = player.fov_stencil.source_yx(yx)
129     if not player.fov_test(source_yx):
130         raise GameError('cannot annotate tile outside field of view')
131     if not game.can_do_tile_with_pw(source_yx, pw):
132         raise GameError('wrong password for tile')
133     if msg == ' ':
134         if source_yx in game.annotations:
135             del game.annotations[source_yx]
136     else:
137         game.annotations[source_yx] = msg
138     game.changed = True
139 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
140
141 def cmd_PORTAL(game, yx, msg, pw, connection_id):
142     player = game.get_thing(game.sessions[connection_id])
143     source_yx = player.fov_stencil.source_yx(yx)
144     if not player.fov_test(source_yx):
145         raise GameError('cannot edit portal on tile outside field of view')
146     if not game.can_do_tile_with_pw(source_yx, pw):
147         raise GameError('wrong password for tile')
148     if msg == ' ':
149         if source_yx in game.portals:
150             del game.portals[source_yx]
151     else:
152         game.portals[source_yx] = msg
153     game.changed = True
154 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
155
156 def cmd_GOD_ANNOTATE(game, yx, msg):
157     game.annotations[yx] = msg
158     game.changed = True
159 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
160
161 def cmd_GOD_PORTAL(game, yx, msg):
162     game.portals[yx] = msg
163     game.changed = True
164 cmd_GOD_PORTAL.argtypes = 'yx_tuple:nonneg string'
165
166 def cmd_GET_ANNOTATION(game, yx, connection_id):
167     player = game.get_thing(game.sessions[connection_id])
168     source_yx = player.fov_stencil.source_yx(yx)
169     annotation = '(unknown)';
170     if player.fov_test(source_yx):
171         annotation = '(none)';
172         if source_yx in game.annotations:
173             annotation = game.annotations[source_yx]
174     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
175 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
176
177 def cmd_MAP_LINE(game, y, line):
178     game.map.set_line(y, line)
179 cmd_MAP_LINE.argtypes = 'int:nonneg string'
180
181 def cmd_MAP(game, geometry, size):
182     map_geometry_class = globals()['MapGeometry' + geometry]
183     game.new_world(map_geometry_class(size))
184 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
185
186 def cmd_MAP_CONTROL_LINE(game, y, line):
187     game.map_control.set_line(y, line)
188 cmd_MAP_CONTROL_LINE.argtypes = 'int:nonneg string'
189
190 def cmd_MAP_CONTROL_PW(game, tile_class, password):
191     game.map_control_passwords[tile_class] = password
192 cmd_MAP_CONTROL_PW.argtypes = 'char string'
193
194 def cmd_THING(game, yx, thing_type, thing_id):
195     if not thing_type in game.thing_types:
196         raise GameError('illegal thing type %s' % thing_type)
197     if not game.map.inside(yx):
198         raise GameError('illegal position %s' % yx)
199     t_old = None
200     if thing_id > 0:
201         t_old = game.get_thing(thing_id)
202     t_new = game.thing_types[thing_type](game, id_=thing_id, position=yx)
203     if t_old:
204         game.things[game.things.index(t_old)] = t_new
205     else:
206         game.things += [t_new]
207     game.changed = True
208 cmd_THING.argtypes = 'yx_tuple:nonneg string:thing_type int:nonneg'
209
210 def cmd_THING_NAME(game, thing_id, name):
211     t = game.get_thing(thing_id)
212     if not t:
213         raise GameError('thing of ID %s not found' % thing_id)
214     t.name = name
215 cmd_THING_NAME.argtypes = 'int:pos string'