home · contact · privacy
Optimize send_gamestate, don't send any invisible state changes.
[plomrogue2] / plomrogue / commands.py
1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError, ArgError
3
4
5
6 # TODO: instead of sending tasks, thing types etc. on request, send them on connection
7
8 def cmd_TASKS(game, connection_id):
9     game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
10 cmd_TASKS.argtypes = ''
11
12 def cmd_THING_TYPES(game, connection_id):
13     for t_t in game.thing_types.values():
14         game.io.send('THING_TYPE %s %s' % (t_t.get_type(), quote(t_t.symbol_hint)),
15                      connection_id)
16 cmd_THING_TYPES.argtypes = ''
17
18 def cmd_TERRAINS(game, connection_id):
19     for t in game.terrains.keys():
20         game.io.send('TERRAIN %s %s' % (quote(t), quote(game.terrains[t])),
21                      connection_id)
22 cmd_TERRAINS.argtypes = ''
23
24 def cmd_ALL(game, msg, connection_id):
25     speaker = game.get_player(connection_id)
26     if not speaker:
27         raise GameError('need to be logged in for this')
28     speaker.sound(speaker.name, msg)
29 cmd_ALL.argtypes = 'string'
30
31 def cmd_SPAWN_POINT(game, big_yx, little_yx):
32     if little_yx.y >= game.map_geometry.size.y or \
33        little_yx.x >= game.map_geometry.size.x:
34         raise GameError('illegal spawn point')
35     game.spawn_point = big_yx, little_yx
36 cmd_SPAWN_POINT.argtypes = 'yx_tuple yx_tuple:nonneg'
37
38 def cmd_LOGIN(game, nick, connection_id):
39     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
40         raise GameError('name already in use')
41     if game.get_player(connection_id):
42         raise GameError('cannot log in twice')
43     t = game.add_thing('Player', game.spawn_point)
44     t.thing_char = game.get_next_player_char()
45     game.sessions[connection_id] = {
46         'thing_id': t.id_,
47         'status': 'player'
48     }
49     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
50     game.io.send('LOGIN_OK', connection_id)
51     t.name = nick
52     game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
53     for s in [s for s in game.things
54               if s.type_ == 'SpawnPoint' and s.name == t.name]:
55         t.position = s.position
56         break
57     # game.changed = True  # handled by game.add_thing
58 cmd_LOGIN.argtypes = 'string'
59
60 def cmd_BECOME_ADMIN(game, password, connection_id):
61     player = game.thing_types['Player'](game)
62     if not player:
63         raise GameError('need to be logged in for this')
64     if password in game.admin_passwords:
65         game.sessions[connection_id]['status'] = 'admin'
66         game.io.send('ADMIN_OK', connection_id)
67     else:
68         raise GameError('wrong password')
69 cmd_BECOME_ADMIN.argtypes = 'string'
70
71 def cmd_ADMIN_PASSWORD(game, password):
72     game.admin_passwords += [password]
73 cmd_ADMIN_PASSWORD.argtypes = 'string'
74
75 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
76     player = game.get_player(connection_id)
77     if not player:
78         raise GameError('need to be logged in for this')
79     if not game.sessions[connection_id]['status'] == 'admin':
80         raise GameError('need to be admin for this')
81     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
82     map_control = game.get_map(big_yx, 'control')
83     map_control[little_yx] = control_char
84     game.changed = True
85     game.record_fov_change((big_yx, little_yx))
86 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
87
88 def cmd_THING_PROTECTION(game, thing_id, protection_char, connection_id):
89     player = game.get_player(connection_id)
90     if not player:
91         raise GameError('need to be logged in for this')
92     if not game.sessions[connection_id]['status'] == 'admin':
93         raise GameError('need to be admin for this')
94     t = game.get_thing(thing_id)
95     if not t:
96         raise GameError('thing of ID %s not found' % thing_id)
97     t.protection = protection_char
98     game.changed = True
99     # FIXME: pseudo-FOV-change actually
100     game.record_fov_change(t.position)
101 cmd_THING_PROTECTION.argtypes = 'int:pos char'
102
103 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, 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     if tile_class == '.':
110         raise GameError('tile class "." must remain unprotected')
111     game.map_control_passwords[tile_class] = password
112     #game.changed = True
113 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
114
115 def cmd_NICK(game, nick, connection_id):
116     for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
117         raise GameError('name already in use')
118     t = game.get_player(connection_id)
119     if not t:
120         raise GameError('can only rename when already logged in')
121     old_nick = t.name
122     t.name = nick
123     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
124     game.changed = True
125     # FIXME: pseudo-FOV-change actually
126     game.record_fov_change(t.position)
127 cmd_NICK.argtypes = 'string'
128
129 def cmd_GET_GAMESTATE(game, connection_id):
130     game.send_gamestate(connection_id)
131 cmd_GET_GAMESTATE.argtypes = ''
132
133 # def cmd_QUERY(game, target_nick, msg, connection_id):
134 #     if not connection_id in game.sessions:
135 #         raise GameError('can only query when logged in')
136 #     t = game.get_thing(game.sessions[connection_id], False)
137 #     source_nick = t.name
138 #     for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
139 #         for c_id in game.sessions:
140 #             if game.sessions[c_id] == t.id_:
141 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
142 #                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
143 #                 return
144 #         raise GameError('target user offline')
145 #     raise GameError('can only query with registered nicknames')
146 # cmd_QUERY.argtypes = 'string string'
147
148 def cmd_PING(game, connection_id):
149     game.io.send('PONG', connection_id)
150 cmd_PING.argtypes = ''
151
152 def cmd_TURN(game, n):
153     game.turn = n
154 cmd_TURN.argtypes = 'int:nonneg'
155
156 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
157     if len(msg) > 500:
158         raise ArgError('annotation text must be <= 500 characters')
159     player = game.get_player(connection_id)
160     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
161     if not player.fov_test(big_yx, little_yx):
162         raise GameError('cannot annotate tile outside field of view')
163     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
164         raise GameError('wrong password for tile')
165     if msg == ' ':
166         if big_yx in game.annotations:
167             if little_yx in game.annotations[big_yx]:
168                 del game.annotations[big_yx][little_yx]
169     else:
170         if big_yx not in game.annotations:
171             game.annotations[big_yx] = {}
172         game.annotations[big_yx][little_yx] = msg
173     # FIXME: pseudo-FOV-change actually
174     game.record_fov_change([big_yx, little_yx])
175     game.changed = True
176 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
177
178 def cmd_PORTAL(game, yx, msg, pw, connection_id):
179     player = game.get_player(connection_id)
180     big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
181     if not player.fov_test(big_yx, little_yx):
182         raise GameError('cannot edit portal on tile outside field of view')
183     if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
184         raise GameError('wrong password for tile')
185     if msg == ' ':
186         if big_yx in game.portals:
187             if little_yx in game.portals[big_yx]:
188                 del game.portals[big_yx][little_yx]
189     else:
190         if big_yx not in game.portals:
191             game.portals[big_yx] = {}
192         game.portals[big_yx][little_yx] = msg
193     # FIXME: pseudo-FOV-change actually
194     game.record_fov_change([big_yx, little_yx])
195     game.changed = True
196 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
197
198 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
199     if big_yx not in game.annotations:
200         game.annotations[big_yx] = {}
201     game.annotations[big_yx][little_yx] = msg
202     #game.changed = True
203 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
204
205 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
206     if big_yx not in game.portals:
207         game.portals[big_yx] = {}
208     game.portals[big_yx][little_yx] = msg
209     #game.changed = True
210 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
211
212 def cmd_MAP_LINE(game, big_yx, y, line):
213     map_ = game.get_map(big_yx)
214     map_.set_line(y, line)
215 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
216
217 def cmd_MAP(game, geometry, size):
218     from plomrogue.mapping import MapGeometryHex, MapGeometrySquare
219     map_geometry_class = MapGeometrySquare
220     if geometry == 'Hex':
221         map_geometry_class = MapGeometryHex
222     game.new_world(map_geometry_class(size))
223 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
224
225 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
226     map_control = game.get_map(big_yx, 'control')
227     map_control.set_line(y, line)
228 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
229
230 def cmd_MAP_CONTROL_PW(game, tile_class, password):
231     game.map_control_passwords[tile_class] = password
232 cmd_MAP_CONTROL_PW.argtypes = 'char string'
233
234 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
235     if thing_type not in game.thing_types:
236         raise GameError('illegal thing type %s' % thing_type)
237     _ = game.get_map(big_yx)
238     game.add_thing(thing_type, (big_yx, little_yx), id_=thing_id)
239     # game.changed = True  handled by add_thing
240 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
241
242 def cmd_THING_NAME(game, thing_id, name, pw, connection_id):
243     # TODO check if thing in FOV
244     t = game.get_thing(thing_id)
245     if not t:
246         raise GameError('thing of ID %s not found' % thing_id)
247     if not game.can_do_thing_with_pw(t, pw):
248         raise GameError('wrong password for thing')
249     t.name = name
250     game.changed = True
251     # FIXME: pseudo-FOV-change actually
252     game.record_fov_change(t.position)
253 cmd_THING_NAME.argtypes = 'int:pos string string'
254
255 def cmd_GOD_THING_NAME(game, thing_id, name):
256     t = game.get_thing(thing_id)
257     if not t:
258         raise GameError('thing of ID %s not found' % thing_id)
259     t.name = name
260 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
261
262 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
263     t = game.get_thing(thing_id)
264     if not t:
265         raise GameError('thing of ID %s not found' % thing_id)
266     t.protection = protection_char
267 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
268
269 def cmd_THING_DOOR_CLOSED(game, thing_id):
270     t = game.get_thing(thing_id)
271     if not t:
272         raise GameError('thing of ID %s not found' % thing_id)
273     if not t.type_ == 'Door':
274         raise GameError('thing of ID %s not door' % thing_id)
275     t.blocking = True
276     t.portable = False
277     t.thing_char = '#'
278 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'
279
280 def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
281     t = game.get_thing(thing_id)
282     if not t:
283         raise GameError('thing of ID %s not found' % thing_id)
284     if not t.type_ == 'MusicPlayer':
285         raise GameError('thing of ID %s not music player' % thing_id)
286     t.playing = playing
287     t.playlist_index = index
288     t.repeat = repeat
289 cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int bool'
290
291 def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
292     t = game.get_thing(thing_id)
293     if not t:
294         raise GameError('thing of ID %s not found' % thing_id)
295     if not t.type_ == 'MusicPlayer':
296         raise GameError('thing of ID %s not music player' % thing_id)
297     t.playlist += [(title, length)]
298 cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'
299
300 def cmd_THING_BOTTLE_EMPTY(game, thing_id):
301     t = game.get_thing(thing_id)
302     if not t:
303         raise GameError('thing of ID %s not found' % thing_id)
304     if not t.type_ == 'Bottle':
305         raise GameError('thing of ID %s not bottle' % thing_id)
306     t.empty()
307 cmd_THING_BOTTLE_EMPTY.argtypes = 'int:pos'
308
309 def cmd_THING_INSTALLED(game, thing_id):
310     t = game.get_thing(thing_id)
311     if not t:
312         raise GameError('thing of ID %s not found' % thing_id)
313     if not hasattr(t, 'installable'):
314         raise GameError('thing of ID %s not installable' % thing_id)
315     t.install()
316 cmd_THING_INSTALLED.argtypes = 'int:pos'
317
318 def cmd_PLAYER_FACE(game, face, connection_id):
319     t = game.get_player(connection_id)
320     if not t:
321         raise GameError('can only draw face when already logged in')
322     if len(face) != 18:
323         raise GameError('wrong face string length')
324     game.faces[t.name] = face
325     game.changed = True
326     # FIXME: pseudo-FOV-change actually
327     game.record_fov_change(t.position)
328 cmd_PLAYER_FACE.argtypes = 'string'
329
330 def cmd_GOD_PLAYER_FACE(game, name, face):
331     if len(face) != 18:
332         raise GameError('wrong face string length')
333     game.faces[name] = face
334 cmd_GOD_PLAYER_FACE.argtypes = 'string string'
335
336 def cmd_GOD_PLAYER_HAT(game, name, hat):
337     if len(hat) != 18:
338         raise GameError('wrong hat string length')
339     game.hats[name] = hat
340 cmd_GOD_PLAYER_HAT.argtypes = 'string string'
341
342 def cmd_THING_HAT_DESIGN(game, thing_id, design):
343     if len(design) != 18:
344         raise GameError('hat design of wrong length')
345     t = game.get_thing(thing_id)
346     if not t:
347         raise GameError('thing of ID %s not found' % thing_id)
348     if t.type_ != 'Hat':
349         raise GameError('thing of ID %s not a hat' % thing_id)
350     t.design = design
351 cmd_THING_HAT_DESIGN.argtypes = 'int:pos string'