1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError, ArgError
3 from plomrogue.misc import Terrain
7 # TODO: instead of sending tasks, thing types etc. on request, send them on connection
9 def cmd_TASKS(game, connection_id):
10 game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
11 cmd_TASKS.argtypes = ''
13 def cmd_THING_TYPES(game, connection_id):
14 for t_t in game.thing_types.values():
15 game.io.send('THING_TYPE %s %s' % (t_t.get_type(), quote(t_t.symbol_hint)),
17 cmd_THING_TYPES.argtypes = ''
19 def cmd_TERRAINS(game, connection_id):
20 for t in game.terrains.values():
21 game.io.send('TERRAIN %s %s' % (quote(t.character),
22 quote(t.description)), connection_id)
23 cmd_TERRAINS.argtypes = ''
25 def cmd_TERRAIN(game, character, description,
26 blocks_light, blocks_sound, blocks_movement):
27 game.terrains[character] = Terrain(character, description, blocks_light,
28 blocks_sound, blocks_movement)
29 cmd_TERRAIN.argtypes = 'char string bool bool bool'
31 def cmd_ALL(game, msg, connection_id):
32 speaker = game.get_player(connection_id)
34 raise GameError('need to be logged in for this')
35 speaker.sound(speaker.name, msg)
36 cmd_ALL.argtypes = 'string'
38 def cmd_SPAWN_POINT(game, big_yx, little_yx):
39 if little_yx.y >= game.map_geometry.size.y or \
40 little_yx.x >= game.map_geometry.size.x:
41 raise GameError('illegal spawn point')
42 game.spawn_point = big_yx, little_yx
43 cmd_SPAWN_POINT.argtypes = 'yx_tuple yx_tuple:nonneg'
45 def cmd_LOGIN(game, nick, connection_id):
48 raise GameError('empty name')
49 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
50 raise GameError('name already in use')
51 if game.get_player(connection_id):
52 raise GameError('cannot log in twice')
53 t = game.add_thing('Player', game.spawn_point)
55 t.thing_char = game.get_next_player_char()
56 game.sessions[connection_id] = {
60 game.io.send('PLAYER_ID %s' % t.id_, connection_id)
61 game.io.send('LOGIN_OK', connection_id)
62 game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
63 for s in [s for s in game.things
64 if s.type_ == 'SpawnPoint' and s.name == t.name]:
65 t.position = s.position
67 # game.changed = True # handled by game.add_thing
68 cmd_LOGIN.argtypes = 'string'
70 def cmd_BECOME_ADMIN(game, password, connection_id):
71 player = game.thing_types['Player'](game)
73 raise GameError('need to be logged in for this')
74 if password in game.admin_passwords:
75 game.sessions[connection_id]['status'] = 'admin'
76 game.io.send('ADMIN_OK', connection_id)
78 raise GameError('wrong password')
79 cmd_BECOME_ADMIN.argtypes = 'string'
81 def cmd_ADMIN_PASSWORD(game, password):
82 game.admin_passwords += [password]
83 cmd_ADMIN_PASSWORD.argtypes = 'string'
85 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
86 player = game.get_player(connection_id)
88 raise GameError('need to be logged in for this')
89 if not game.sessions[connection_id]['status'] == 'admin':
90 raise GameError('need to be admin for this')
91 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
92 map_control = game.get_map(big_yx, 'control')
93 map_control[little_yx] = control_char
95 game.record_change((big_yx, little_yx), 'fov')
96 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
98 def cmd_THING_PROTECTION(game, thing_id, protection_char, connection_id):
99 player = game.get_player(connection_id)
101 raise GameError('need to be logged in for this')
102 if not game.sessions[connection_id]['status'] == 'admin':
103 raise GameError('need to be admin for this')
104 t = game.get_thing(thing_id)
106 raise GameError('thing of ID %s not found' % thing_id)
107 t.protection = protection_char
109 game.record_change(t.position, 'other')
110 cmd_THING_PROTECTION.argtypes = 'int:pos char'
112 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
113 player = game.get_player(connection_id)
115 raise GameError('need to be logged in for this')
116 if not game.sessions[connection_id]['status'] == 'admin':
117 raise GameError('need to be admin for this')
118 if tile_class == '.':
119 raise GameError('tile class "." must remain unprotected')
120 game.map_control_passwords[tile_class] = password
122 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
124 def cmd_NICK(game, nick, connection_id):
127 raise GameError('empty name')
128 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
129 raise GameError('name already in use')
130 t = game.get_player(connection_id)
132 raise GameError('can only rename when already logged in')
135 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
137 game.record_change(t.position, 'other')
138 cmd_NICK.argtypes = 'string'
140 def cmd_GET_GAMESTATE(game, connection_id):
141 game.send_gamestate(connection_id)
142 cmd_GET_GAMESTATE.argtypes = ''
144 # def cmd_QUERY(game, target_nick, msg, connection_id):
145 # if not connection_id in game.sessions:
146 # raise GameError('can only query when logged in')
147 # t = game.get_thing(game.sessions[connection_id], False)
148 # source_nick = t.name
149 # for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
150 # for c_id in game.sessions:
151 # if game.sessions[c_id] == t.id_:
152 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
153 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
155 # raise GameError('target user offline')
156 # raise GameError('can only query with registered nicknames')
157 # cmd_QUERY.argtypes = 'string string'
159 def cmd_PING(game, connection_id):
160 game.io.send('PONG', connection_id)
161 cmd_PING.argtypes = ''
163 def cmd_TURN(game, n):
165 cmd_TURN.argtypes = 'int:nonneg'
167 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
169 raise ArgError('annotation text must be <= 500 characters')
170 player = game.get_player(connection_id)
171 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
172 if not player.fov_test(big_yx, little_yx):
173 raise GameError('cannot annotate tile outside field of view')
174 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
175 raise GameError('wrong password for tile')
177 if big_yx in game.annotations:
178 if little_yx in game.annotations[big_yx]:
179 del game.annotations[big_yx][little_yx]
181 if big_yx not in game.annotations:
182 game.annotations[big_yx] = {}
183 game.annotations[big_yx][little_yx] = msg
184 game.record_change([big_yx, little_yx], 'other')
186 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
188 def cmd_PORTAL(game, yx, msg, pw, connection_id):
189 player = game.get_player(connection_id)
190 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
191 if not player.fov_test(big_yx, little_yx):
192 raise GameError('cannot edit portal on tile outside field of view')
193 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
194 raise GameError('wrong password for tile')
196 if big_yx in game.portals:
197 if little_yx in game.portals[big_yx]:
198 del game.portals[big_yx][little_yx]
200 if big_yx not in game.portals:
201 game.portals[big_yx] = {}
202 game.portals[big_yx][little_yx] = msg
203 game.record_change([big_yx, little_yx], 'other')
205 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
207 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
208 if big_yx not in game.annotations:
209 game.annotations[big_yx] = {}
210 game.annotations[big_yx][little_yx] = msg
212 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
214 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
215 if big_yx not in game.portals:
216 game.portals[big_yx] = {}
217 game.portals[big_yx][little_yx] = msg
219 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
221 def cmd_MAP_LINE(game, big_yx, y, line):
222 map_ = game.get_map(big_yx)
223 map_.set_line(y, line)
224 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
226 def cmd_MAP(game, geometry, size):
227 from plomrogue.mapping import MapGeometryHex, MapGeometrySquare
228 map_geometry_class = MapGeometrySquare
229 if geometry == 'Hex':
230 map_geometry_class = MapGeometryHex
231 game.new_world(map_geometry_class(size))
233 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
235 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
236 map_control = game.get_map(big_yx, 'control')
237 map_control.set_line(y, line)
238 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
240 def cmd_MAP_CONTROL_PW(game, tile_class, password):
241 game.map_control_passwords[tile_class] = password
242 cmd_MAP_CONTROL_PW.argtypes = 'char string'
244 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
245 if thing_type not in game.thing_types:
246 raise GameError('illegal thing type %s' % thing_type)
247 _ = game.get_map(big_yx)
248 game.add_thing(thing_type, (big_yx, little_yx), id_=thing_id)
249 # game.changed = True handled by add_thing
250 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
252 def cmd_THING_NAME(game, thing_id, name, pw, connection_id):
253 # TODO check if thing in FOV
254 t = game.get_thing(thing_id)
256 raise GameError('thing of ID %s not found' % thing_id)
257 if not game.can_do_thing_with_pw(t, pw):
258 raise GameError('wrong password for thing')
260 if hasattr(t.__class__, 'name'):
261 raise GameError('cannot un-name things of this type')
262 if hasattr(t, 'name'):
267 game.record_change(t.position, 'other')
268 cmd_THING_NAME.argtypes = 'int:pos string string'
270 def cmd_GOD_THING_NAME(game, thing_id, name):
271 t = game.get_thing(thing_id)
273 raise GameError('thing of ID %s not found' % thing_id)
275 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
277 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
278 t = game.get_thing(thing_id)
280 raise GameError('thing of ID %s not found' % thing_id)
281 t.protection = protection_char
282 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
284 def cmd_THING_DOOR_CLOSED(game, thing_id):
285 t = game.get_thing(thing_id)
287 raise GameError('thing of ID %s not found' % thing_id)
288 if not t.type_ == 'Door':
289 raise GameError('thing of ID %s not door' % thing_id)
291 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'
293 def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
294 t = game.get_thing(thing_id)
296 raise GameError('thing of ID %s not found' % thing_id)
297 if not t.type_ == 'MusicPlayer':
298 raise GameError('thing of ID %s not music player' % thing_id)
300 t.playlist_index = index
302 cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int bool'
304 def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
305 t = game.get_thing(thing_id)
307 raise GameError('thing of ID %s not found' % thing_id)
308 if not t.type_ == 'MusicPlayer':
309 raise GameError('thing of ID %s not music player' % thing_id)
310 t.playlist += [(title, length)]
311 cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'
313 def cmd_THING_BOTTLE_EMPTY(game, thing_id):
314 t = game.get_thing(thing_id)
316 raise GameError('thing of ID %s not found' % thing_id)
317 if not t.type_ == 'Bottle':
318 raise GameError('thing of ID %s not bottle' % thing_id)
320 cmd_THING_BOTTLE_EMPTY.argtypes = 'int:pos'
322 def cmd_THING_INSTALLED(game, thing_id):
323 t = game.get_thing(thing_id)
325 raise GameError('thing of ID %s not found' % thing_id)
326 if not hasattr(t, 'installable'):
327 raise GameError('thing of ID %s not installable' % thing_id)
329 cmd_THING_INSTALLED.argtypes = 'int:pos'
331 def cmd_PLAYER_FACE(game, face, connection_id):
332 t = game.get_player(connection_id)
334 raise GameError('can only draw face when already logged in')
336 raise GameError('wrong face string length')
337 game.faces[t.name] = face
339 game.record_change(t.position, 'other')
340 cmd_PLAYER_FACE.argtypes = 'string'
342 def cmd_PLAYER_HAT(game, hat, connection_id):
343 t = game.get_player(connection_id)
345 raise GameError('can only edit hat when already logged in')
346 if not t.name in game.hats:
347 raise GameError('not currently wearing an editable hat')
349 raise GameError('wrong hat string length')
350 legal_chars = t.get_cookie_chars()
352 if c not in legal_chars:
353 raise GameError('used illegal character: "%s" – '
354 'allowed characters: %s'
356 game.hats[t.name] = hat
358 game.record_change(t.position, 'other')
359 cmd_PLAYER_HAT.argtypes = 'string'
361 def cmd_GOD_PLAYER_FACE(game, name, face):
363 raise GameError('wrong face string length')
364 game.faces[name] = face
365 cmd_GOD_PLAYER_FACE.argtypes = 'string string'
367 def cmd_GOD_PLAYER_HAT(game, name, hat):
369 raise GameError('wrong hat string length')
370 game.hats[name] = hat
371 cmd_GOD_PLAYER_HAT.argtypes = 'string string'
373 def cmd_GOD_PLAYERS_HAT_CHARS(game, name, hat_chars):
374 game.players_hat_chars[name] = hat_chars
375 cmd_GOD_PLAYERS_HAT_CHARS.argtypes = 'string string'
377 def cmd_THING_HAT_DESIGN(game, thing_id, design):
378 if len(design) != 18:
379 raise GameError('hat design of wrong length')
380 t = game.get_thing(thing_id)
382 raise GameError('thing of ID %s not found' % thing_id)
384 raise GameError('thing of ID %s not a hat' % thing_id)
386 cmd_THING_HAT_DESIGN.argtypes = 'int:pos string'