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')
39 if random.random() > 0.85:
40 c = random.choice(['l', 'll', 'n', 'nhg', 'w', 'wl', 'bw'])
43 speaker.sound(speaker.name, msg)
44 cmd_ALL.argtypes = 'string'
46 def cmd_SPAWN_POINT(game, big_yx, little_yx):
47 if little_yx.y >= game.map_geometry.size.y or \
48 little_yx.x >= game.map_geometry.size.x:
49 raise GameError('illegal spawn point')
50 game.spawn_point = big_yx, little_yx
51 cmd_SPAWN_POINT.argtypes = 'yx_tuple yx_tuple:nonneg'
53 def cmd_LOGIN(game, nick, connection_id):
56 raise GameError('empty name')
57 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
58 raise GameError('name already in use')
59 if game.get_player(connection_id):
60 raise GameError('cannot log in twice')
61 t = game.add_thing('Player', game.spawn_point)
63 t.thing_char = game.get_next_player_char()
64 game.sessions[connection_id] = {
68 game.io.send('PLAYER_ID %s' % t.id_, connection_id)
69 game.io.send('LOGIN_OK', connection_id)
70 game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
71 for s in [s for s in game.things
72 if s.type_ == 'SpawnPoint' and s.name == t.name]:
73 t.position = s.position
75 # game.changed = True # handled by game.add_thing
76 cmd_LOGIN.argtypes = 'string'
78 def cmd_BECOME_ADMIN(game, password, connection_id):
79 player = game.get_player(connection_id)
81 raise GameError('need to be logged in for this')
82 if password in game.admin_passwords:
83 game.sessions[connection_id]['status'] = 'admin'
84 game.io.send('ADMIN_OK', connection_id)
86 raise GameError('wrong password')
87 cmd_BECOME_ADMIN.argtypes = 'string'
89 def cmd_ADMIN_PASSWORD(game, password):
90 game.admin_passwords += [password]
91 cmd_ADMIN_PASSWORD.argtypes = 'string'
93 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
94 player = game.get_player(connection_id)
96 raise GameError('need to be logged in for this')
97 if not game.sessions[connection_id]['status'] == 'admin':
98 raise GameError('need to be admin for this')
99 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
100 map_control = game.get_map(big_yx, 'control')
101 map_control[little_yx] = control_char
103 game.record_change((big_yx, little_yx), 'fov')
104 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
106 def cmd_THING_PROTECTION(game, protection_char, connection_id):
107 player = game.get_player(connection_id)
109 raise GameError('need to be logged in for this')
110 if not game.sessions[connection_id]['status'] == 'admin':
111 raise GameError('need to be admin for this')
112 if not player.carrying:
113 raise GameError('need to carry a thing to protect it')
114 player.carrying.protection = protection_char
116 game.record_change(player.carrying.position, 'other')
117 cmd_THING_PROTECTION.argtypes = 'char'
119 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
120 player = game.get_player(connection_id)
122 raise GameError('need to be logged in for this')
123 if not game.sessions[connection_id]['status'] == 'admin':
124 raise GameError('need to be admin for this')
125 if tile_class == '.':
126 raise GameError('tile class "." must remain unprotected')
127 game.map_control_passwords[tile_class] = password
128 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
130 def cmd_NICK(game, nick, connection_id):
133 raise GameError('empty name')
134 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
135 raise GameError('name already in use')
136 t = game.get_player(connection_id)
138 raise GameError('can only rename when already logged in')
141 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
143 game.record_change(t.position, 'other')
144 cmd_NICK.argtypes = 'string'
146 def cmd_GET_GAMESTATE(game, connection_id):
147 game.send_gamestate(connection_id)
148 cmd_GET_GAMESTATE.argtypes = ''
150 # def cmd_QUERY(game, target_nick, msg, connection_id):
151 # if not connection_id in game.sessions:
152 # raise GameError('can only query when logged in')
153 # t = game.get_thing(game.sessions[connection_id], False)
154 # source_nick = t.name
155 # for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
156 # for c_id in game.sessions:
157 # if game.sessions[c_id] == t.id_:
158 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
159 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
161 # raise GameError('target user offline')
162 # raise GameError('can only query with registered nicknames')
163 # cmd_QUERY.argtypes = 'string string'
165 def cmd_PING(game, connection_id):
166 game.io.send('PONG', connection_id)
167 cmd_PING.argtypes = ''
169 def cmd_TURN(game, n):
171 cmd_TURN.argtypes = 'int:nonneg'
173 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
175 raise ArgError('annotation text must be <= 500 characters')
176 player = game.get_player(connection_id)
177 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
178 if not player.fov_test(big_yx, little_yx):
179 raise GameError('cannot annotate tile outside field of view')
180 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
181 raise GameError('wrong password for tile')
183 if big_yx in game.annotations:
184 if little_yx in game.annotations[big_yx]:
185 del game.annotations[big_yx][little_yx]
187 if big_yx not in game.annotations:
188 game.annotations[big_yx] = {}
189 game.annotations[big_yx][little_yx] = msg
190 game.record_change([big_yx, little_yx], 'other')
192 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
194 def cmd_PORTAL(game, yx, msg, pw, connection_id):
195 player = game.get_player(connection_id)
196 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
197 if not player.fov_test(big_yx, little_yx):
198 raise GameError('cannot edit portal on tile outside field of view')
199 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
200 raise GameError('wrong password for tile')
202 if big_yx in game.portals:
203 if little_yx in game.portals[big_yx]:
204 del game.portals[big_yx][little_yx]
206 if big_yx not in game.portals:
207 game.portals[big_yx] = {}
208 game.portals[big_yx][little_yx] = msg
209 game.record_change([big_yx, little_yx], 'other')
211 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
213 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
214 if big_yx not in game.annotations:
215 game.annotations[big_yx] = {}
216 game.annotations[big_yx][little_yx] = msg
218 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
220 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
221 if big_yx not in game.portals:
222 game.portals[big_yx] = {}
223 game.portals[big_yx][little_yx] = msg
225 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
227 def cmd_MAP_LINE(game, big_yx, y, line):
228 map_ = game.get_map(big_yx)
229 map_.set_line(y, line)
230 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
232 def cmd_MAP(game, geometry, size):
233 from plomrogue.mapping import MapGeometryHex, MapGeometrySquare
234 map_geometry_class = MapGeometrySquare
235 if geometry == 'Hex':
236 map_geometry_class = MapGeometryHex
237 game.new_world(map_geometry_class(size))
239 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
241 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
242 map_control = game.get_map(big_yx, 'control')
243 map_control.set_line(y, line)
244 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
246 def cmd_MAP_CONTROL_PW(game, tile_class, password):
247 game.map_control_passwords[tile_class] = password
248 cmd_MAP_CONTROL_PW.argtypes = 'char string'
250 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
251 if thing_type not in game.thing_types:
252 raise GameError('illegal thing type %s' % thing_type)
253 _ = game.get_map(big_yx)
254 game.add_thing(thing_type, (big_yx, little_yx), id_=thing_id)
255 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
257 def cmd_THING_NAME(game, name, pw, connection_id):
258 player = game.get_player(connection_id)
260 raise GameError('need to be logged in for this')
261 if not player.carrying:
262 raise GameError('need to carry a thing to rename it')
263 if not game.can_do_thing_with_pw(player.carrying, pw):
264 raise GameError('wrong password for thing')
266 if hasattr(player.carrying.__class__, 'name'):
267 raise GameError('cannot un-name things of this type')
268 if hasattr(player.carrying, 'name'):
269 del player.carrying.name
271 player.carrying.name = name
273 game.record_change(player.carrying.position, 'other')
274 cmd_THING_NAME.argtypes = 'string string'
276 def cmd_GOD_THING_NAME(game, thing_id, name):
277 t = game.get_thing(thing_id)
279 raise GameError('thing of ID %s not found' % thing_id)
281 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
283 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
284 t = game.get_thing(thing_id)
286 raise GameError('thing of ID %s not found' % thing_id)
287 t.protection = protection_char
288 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
290 def cmd_THING_DOOR_CLOSED(game, thing_id):
291 t = game.get_thing(thing_id)
293 raise GameError('thing of ID %s not found' % thing_id)
294 if not t.type_ == 'Door':
295 raise GameError('thing of ID %s not door' % thing_id)
297 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'
299 def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
300 t = game.get_thing(thing_id)
302 raise GameError('thing of ID %s not found' % thing_id)
303 if not t.type_ == 'MusicPlayer':
304 raise GameError('thing of ID %s not music player' % thing_id)
306 t.playlist_index = index
308 cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int bool'
310 def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
311 t = game.get_thing(thing_id)
313 raise GameError('thing of ID %s not found' % thing_id)
314 if not t.type_ == 'MusicPlayer':
315 raise GameError('thing of ID %s not music player' % thing_id)
316 t.playlist += [(title, length)]
317 cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'
319 def cmd_THING_BOTTLE_EMPTY(game, thing_id):
320 t = game.get_thing(thing_id)
322 raise GameError('thing of ID %s not found' % thing_id)
323 if not t.type_ == 'Bottle':
324 raise GameError('thing of ID %s not bottle' % thing_id)
326 cmd_THING_BOTTLE_EMPTY.argtypes = 'int:pos'
328 def cmd_THING_INSTALLED(game, thing_id):
329 t = game.get_thing(thing_id)
331 raise GameError('thing of ID %s not found' % thing_id)
332 if not hasattr(t, 'installable'):
333 raise GameError('thing of ID %s not installable' % thing_id)
335 cmd_THING_INSTALLED.argtypes = 'int:pos'
337 def cmd_PLAYER_FACE(game, face, connection_id):
338 t = game.get_player(connection_id)
340 raise GameError('can only draw face when already logged in')
342 raise GameError('wrong face string length')
343 game.faces[t.name] = face
345 game.record_change(t.position, 'other')
346 cmd_PLAYER_FACE.argtypes = 'string'
348 def cmd_PLAYER_HAT(game, hat, connection_id):
349 t = game.get_player(connection_id)
351 raise GameError('can only edit hat when already logged in')
352 if not t.name in game.hats:
353 raise GameError('not currently wearing an editable hat')
355 raise GameError('wrong hat string length')
356 legal_chars = t.get_cookie_chars()
358 if c not in legal_chars:
359 raise GameError('used illegal character: "%s" – '
360 'allowed characters: %s'
362 game.hats[t.name] = hat
364 game.record_change(t.position, 'other')
365 cmd_PLAYER_HAT.argtypes = 'string'
367 def cmd_GOD_PLAYER_FACE(game, name, face):
369 raise GameError('wrong face string length')
370 game.faces[name] = face
371 cmd_GOD_PLAYER_FACE.argtypes = 'string string'
373 def cmd_GOD_PLAYER_HAT(game, name, hat):
375 raise GameError('wrong hat string length')
376 game.hats[name] = hat
377 cmd_GOD_PLAYER_HAT.argtypes = 'string string'
379 def cmd_GOD_PLAYERS_HAT_CHARS(game, name, hat_chars):
380 game.players_hat_chars[name] = hat_chars
381 cmd_GOD_PLAYERS_HAT_CHARS.argtypes = 'string string'
383 def cmd_THING_HAT_DESIGN(game, thing_id, design):
384 if len(design) != 18:
385 raise GameError('hat design of wrong length')
386 t = game.get_thing(thing_id)
388 raise GameError('thing of ID %s not found' % thing_id)
390 raise GameError('thing of ID %s not a hat' % thing_id)
392 cmd_THING_HAT_DESIGN.argtypes = 'int:pos string'