1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3 from plomrogue.mapping import YX
7 def cmd_ALL(game, msg, connection_id):
8 if not connection_id in game.sessions:
9 raise GameError('need to be logged in for this')
10 t = game.get_thing(game.sessions[connection_id], False)
11 game.io.send('CHAT ' + quote(t.nickname + ': ' + msg))
12 cmd_ALL.argtypes = 'string'
14 def cmd_LOGIN(game, nick, connection_id):
15 for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
16 raise GameError('name already in use')
17 if connection_id in game.sessions:
18 t_id = game.sessions[connection_id]
19 t = game.get_thing(t_id, False)
20 game.io.send('META ' + quote('you rename yourself to: ' + nick), connection_id)
22 t = game.thing_types['player'](game)
23 t.position = YX(game.map.size.y // 2, game.map.size.x // 2)
24 game.things += [t] # TODO refactor into Thing.__init__?
25 game.sessions[connection_id] = t.id_
26 game.io.send('META ' + quote('you are now: ' + nick), connection_id)
28 game.io.send('PLAYER_ID %s' % t.id_, connection_id)
29 cmd_LOGIN.argtypes = 'string'
31 def cmd_GET_GAMESTATE(game, connection_id):
32 game.send_gamestate(connection_id)
33 cmd_GET_GAMESTATE.argtypes = ''
35 def cmd_QUERY(game, target_nick, msg, connection_id):
36 if not connection_id in game.sessions:
37 raise GameError('can only query when logged in')
38 t = game.get_thing(game.sessions[connection_id], False)
39 source_nick = t.nickname
40 for t in [t for t in game.things if t.type_ == 'player' and t.nickname == target_nick]:
41 for c_id in game.sessions:
42 if game.sessions[c_id] == t.id_:
43 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
44 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
46 raise GameError('target user offline')
47 raise GameError('can only query with registered nicknames')
48 cmd_QUERY.argtypes = 'string string'
50 def cmd_PING(game, connection_id):
52 cmd_PING.argtypes = ''
54 def cmd_TURN(game, n):
56 cmd_TURN.argtypes = 'int:nonneg'
58 def cmd_ANNOTATE(game, yx, msg, connection_id):
60 if yx in game.annotations:
61 del game.annotations[yx]
63 game.annotations[yx] = msg
65 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
67 def cmd_GET_ANNOTATION(game, yx, connection_id):
68 annotation = '(none)';
69 if yx in game.annotations:
70 annotation = game.annotations[yx]
71 game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
72 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
74 def cmd_MAP_LINE(game, y, line):
75 game.map.set_line(y, line)
76 cmd_MAP_LINE.argtypes = 'int:nonneg string'
78 def cmd_MAP(game, size):
80 cmd_MAP.argtypes = 'yx_tuple:pos'