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_TERRAIN_TAG(game, character, tag):
32 if not character in game.terrains:
33 raise GameError('terrain does not exist: %s' % character)
34 game.terrains[character].tags.add(tag)
35 cmd_TERRAIN_TAG.argtypes = 'char string'
37 def cmd_ALL(game, msg, connection_id):
38 speaker = game.get_player(connection_id)
40 raise GameError('need to be logged in for this')
45 if random.random() > 0.85:
46 c = random.choice(['l', 'll', 'n', 'nhg', 'w', 'wl', 'bw'])
49 speaker.sound(speaker.name, msg)
50 cmd_ALL.argtypes = 'string'
52 def cmd_SPAWN_POINT(game, big_yx, little_yx):
53 if little_yx.y >= game.map_geometry.size.y or \
54 little_yx.x >= game.map_geometry.size.x:
55 raise GameError('illegal spawn point')
56 game.spawn_point = big_yx, little_yx
57 cmd_SPAWN_POINT.argtypes = 'yx_tuple yx_tuple:nonneg'
59 def cmd_LOGIN(game, nick, connection_id):
62 raise GameError('empty name')
63 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
64 raise GameError('name already in use')
65 if game.get_player(connection_id):
66 raise GameError('cannot log in twice')
67 t = game.add_thing('Player', game.spawn_point)
69 t.thing_char = game.get_next_player_char()
70 game.sessions[connection_id] = {
74 game.io.send('PLAYER_ID %s' % t.id_, connection_id)
75 game.io.send('LOGIN_OK', connection_id)
76 game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
77 for s in [s for s in game.things
78 if s.type_ == 'SpawnPoint' and s.name == t.name]:
79 t.position = s.position
81 # game.changed = True # handled by game.add_thing
82 cmd_LOGIN.argtypes = 'string'
84 def cmd_BECOME_ADMIN(game, password, connection_id):
85 player = game.get_player(connection_id)
87 raise GameError('need to be logged in for this')
88 if password in game.admin_passwords:
89 game.sessions[connection_id]['status'] = 'admin'
90 game.io.send('ADMIN_OK', connection_id)
92 raise GameError('wrong password')
93 cmd_BECOME_ADMIN.argtypes = 'string'
95 def cmd_ADMIN_PASSWORD(game, password):
96 game.admin_passwords += [password]
97 cmd_ADMIN_PASSWORD.argtypes = 'string'
99 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
100 player = game.get_player(connection_id)
102 raise GameError('need to be logged in for this')
103 if not game.sessions[connection_id]['status'] == 'admin':
104 raise GameError('need to be admin for this')
105 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
106 map_control = game.get_map(big_yx, 'control')
107 map_control[little_yx] = control_char
109 game.record_change((big_yx, little_yx), 'fov')
110 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
112 def cmd_THING_PROTECTION(game, protection_char, 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 not player.carrying:
119 raise GameError('need to carry a thing to protect it')
120 player.carrying.protection = protection_char
122 game.record_change(player.carrying.position, 'other')
123 cmd_THING_PROTECTION.argtypes = 'char'
125 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
126 player = game.get_player(connection_id)
128 raise GameError('need to be logged in for this')
129 if not game.sessions[connection_id]['status'] == 'admin':
130 raise GameError('need to be admin for this')
131 if tile_class == '.':
132 raise GameError('tile class "." must remain unprotected')
133 game.map_control_passwords[tile_class] = password
134 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
136 def cmd_NICK(game, nick, connection_id):
139 raise GameError('empty name')
140 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
141 raise GameError('name already in use')
142 t = game.get_player(connection_id)
144 raise GameError('can only rename when already logged in')
147 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
149 game.record_change(t.position, 'other')
150 cmd_NICK.argtypes = 'string'
152 def cmd_GET_GAMESTATE(game, connection_id):
153 game.send_gamestate(connection_id)
154 cmd_GET_GAMESTATE.argtypes = ''
156 # def cmd_QUERY(game, target_nick, msg, connection_id):
157 # if not connection_id in game.sessions:
158 # raise GameError('can only query when logged in')
159 # t = game.get_thing(game.sessions[connection_id], False)
160 # source_nick = t.name
161 # for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
162 # for c_id in game.sessions:
163 # if game.sessions[c_id] == t.id_:
164 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
165 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
167 # raise GameError('target user offline')
168 # raise GameError('can only query with registered nicknames')
169 # cmd_QUERY.argtypes = 'string string'
171 def cmd_PING(game, connection_id):
172 game.io.send('PONG', connection_id)
173 cmd_PING.argtypes = ''
175 def cmd_TURN(game, n):
177 cmd_TURN.argtypes = 'int:nonneg'
179 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
181 raise ArgError('annotation text must be <= 500 characters')
182 player = game.get_player(connection_id)
183 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
184 if not player.fov_test(big_yx, little_yx):
185 raise GameError('cannot annotate tile outside field of view')
186 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
187 raise GameError('wrong password for tile')
189 if big_yx in game.annotations:
190 if little_yx in game.annotations[big_yx]:
191 del game.annotations[big_yx][little_yx]
193 if big_yx not in game.annotations:
194 game.annotations[big_yx] = {}
195 game.annotations[big_yx][little_yx] = msg
196 game.record_change([big_yx, little_yx], 'other')
198 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
200 def cmd_PORTAL(game, yx, msg, pw, connection_id):
201 player = game.get_player(connection_id)
202 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
203 if not player.fov_test(big_yx, little_yx):
204 raise GameError('cannot edit portal on tile outside field of view')
205 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
206 raise GameError('wrong password for tile')
208 if big_yx in game.portals:
209 if little_yx in game.portals[big_yx]:
210 del game.portals[big_yx][little_yx]
212 if big_yx not in game.portals:
213 game.portals[big_yx] = {}
214 game.portals[big_yx][little_yx] = msg
215 game.record_change([big_yx, little_yx], 'other')
217 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
219 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
220 if big_yx not in game.annotations:
221 game.annotations[big_yx] = {}
222 game.annotations[big_yx][little_yx] = msg
224 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
226 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
227 if big_yx not in game.portals:
228 game.portals[big_yx] = {}
229 game.portals[big_yx][little_yx] = msg
231 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
233 def cmd_MAP_LINE(game, big_yx, y, line):
234 map_ = game.get_map(big_yx)
235 map_.set_line(y, line)
236 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
238 def cmd_MAP(game, geometry, size):
239 from plomrogue.mapping import MapGeometryHex, MapGeometrySquare
240 map_geometry_class = MapGeometrySquare
241 if geometry == 'Hex':
242 map_geometry_class = MapGeometryHex
243 game.new_world(map_geometry_class(size))
245 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
247 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
248 map_control = game.get_map(big_yx, 'control')
249 map_control.set_line(y, line)
250 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
252 def cmd_MAP_CONTROL_PW(game, tile_class, password):
253 game.map_control_passwords[tile_class] = password
254 cmd_MAP_CONTROL_PW.argtypes = 'char string'
256 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
257 if thing_type not in game.thing_types:
258 raise GameError('illegal thing type %s' % thing_type)
259 _ = game.get_map(big_yx)
260 game.add_thing(thing_type, (big_yx, little_yx), id_=thing_id)
261 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
263 def cmd_THING_NAME(game, name, pw, connection_id):
264 player = game.get_player(connection_id)
266 raise GameError('need to be logged in for this')
267 if not player.carrying:
268 raise GameError('need to carry a thing to rename it')
269 if not game.can_do_thing_with_pw(player.carrying, pw):
270 raise GameError('wrong password for thing')
272 if hasattr(player.carrying.__class__, 'name'):
273 raise GameError('cannot un-name things of this type')
274 if hasattr(player.carrying, 'name'):
275 del player.carrying.name
277 player.carrying.name = name
279 game.record_change(player.carrying.position, 'other')
280 cmd_THING_NAME.argtypes = 'string string'
282 def cmd_GOD_THING_NAME(game, thing_id, name):
283 t = game.get_thing(thing_id)
285 raise GameError('thing of ID %s not found' % thing_id)
287 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
289 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
290 t = game.get_thing(thing_id)
292 raise GameError('thing of ID %s not found' % thing_id)
293 t.protection = protection_char
294 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
296 def cmd_THING_DOOR_CLOSED(game, thing_id):
297 t = game.get_thing(thing_id)
299 raise GameError('thing of ID %s not found' % thing_id)
300 if not t.type_ == 'Door':
301 raise GameError('thing of ID %s not door' % thing_id)
303 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'
305 def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
306 t = game.get_thing(thing_id)
308 raise GameError('thing of ID %s not found' % thing_id)
309 if not t.type_ == 'MusicPlayer':
310 raise GameError('thing of ID %s not music player' % thing_id)
312 t.playlist_index = index
314 cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int bool'
316 def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
317 t = game.get_thing(thing_id)
319 raise GameError('thing of ID %s not found' % thing_id)
320 if not t.type_ == 'MusicPlayer':
321 raise GameError('thing of ID %s not music player' % thing_id)
322 t.playlist += [(title, length)]
323 cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'
325 def cmd_THING_BOTTLE_EMPTY(game, thing_id):
326 t = game.get_thing(thing_id)
328 raise GameError('thing of ID %s not found' % thing_id)
329 if not t.type_ == 'Bottle':
330 raise GameError('thing of ID %s not bottle' % thing_id)
332 cmd_THING_BOTTLE_EMPTY.argtypes = 'int:pos'
334 def cmd_THING_INSTALLED(game, thing_id):
335 t = game.get_thing(thing_id)
337 raise GameError('thing of ID %s not found' % thing_id)
338 if not hasattr(t, 'installable'):
339 raise GameError('thing of ID %s not installable' % thing_id)
341 cmd_THING_INSTALLED.argtypes = 'int:pos'
343 def cmd_PLAYER_FACE(game, face, connection_id):
344 t = game.get_player(connection_id)
346 raise GameError('can only draw face when already logged in')
348 raise GameError('wrong face string length')
349 game.faces[t.name] = face
351 game.record_change(t.position, 'other')
352 cmd_PLAYER_FACE.argtypes = 'string'
354 def cmd_PLAYER_HAT(game, hat, connection_id):
355 t = game.get_player(connection_id)
357 raise GameError('can only edit hat when already logged in')
358 if not t.name in game.hats:
359 raise GameError('not currently wearing an editable hat')
361 raise GameError('wrong hat string length')
362 legal_chars = t.get_cookie_chars()
364 if c not in legal_chars:
365 raise GameError('used illegal character: "%s" – '
366 'allowed characters: %s'
368 game.hats[t.name] = hat
370 game.record_change(t.position, 'other')
371 cmd_PLAYER_HAT.argtypes = 'string'
373 def cmd_GOD_PLAYER_FACE(game, name, face):
375 raise GameError('wrong face string length')
376 game.faces[name] = face
377 cmd_GOD_PLAYER_FACE.argtypes = 'string string'
379 def cmd_GOD_PLAYER_HAT(game, name, hat):
381 raise GameError('wrong hat string length')
382 game.hats[name] = hat
383 cmd_GOD_PLAYER_HAT.argtypes = 'string string'
385 def cmd_GOD_PLAYERS_HAT_CHARS(game, name, hat_chars):
386 game.players_hat_chars[name] = hat_chars
387 cmd_GOD_PLAYERS_HAT_CHARS.argtypes = 'string string'
389 def cmd_THING_HAT_DESIGN(game, thing_id, design):
390 if len(design) != 18:
391 raise GameError('hat design of wrong length')
392 t = game.get_thing(thing_id)
394 raise GameError('thing of ID %s not found' % thing_id)
396 raise GameError('thing of ID %s not a hat' % thing_id)
398 cmd_THING_HAT_DESIGN.argtypes = 'int:pos string'