home · contact · privacy
9e67ce02600682eaa12823c3d63bf7c6ffaed6a8
[plomrogue2-experiments] / new2 / plomrogue / commands.py
1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3
4
5
6 def cmd_ALL(game, msg, connection_id):
7     if not connection_id in game.sessions:
8         raise GameError('need to be logged in for this')
9     t = game.get_thing(game.sessions[connection_id], False)
10     game.io.send('CHAT ' + quote(t.nickname + ': ' + msg))
11 cmd_ALL.argtypes = 'string'
12
13 def cmd_LOGIN(game, nick, connection_id):
14     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
15         raise GameError('name already in use')
16     if connection_id in game.sessions:
17         t_id = game.sessions[connection_id]
18         t = game.get_thing(t_id, False)
19         game.io.send('META ' + quote('you rename yourself to: ' + nick), connection_id)
20     else:
21         t = game.thing_types['player'](game)
22         game.things += [t]  # TODO refactor into Thing.__init__?
23         game.sessions[connection_id] = t.id_
24         game.io.send('META ' + quote('you are now: ' + nick), connection_id)
25     t.nickname = nick
26     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
27 cmd_LOGIN.argtypes = 'string'
28
29 def cmd_QUERY(game, target_nick, msg, connection_id):
30     if not connection_id in game.sessions:
31         raise GameError('can only query when logged in')
32     t = game.get_thing(game.sessions[connection_id], False)
33     source_nick = t.nickname
34     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == target_nick]:
35         for c_id in game.sessions:
36             if game.sessions[c_id] == t.id_:
37                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
38                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
39                 return
40         raise GameError('target user offline')
41     raise GameError('can only query with registered nicknames')
42 cmd_QUERY.argtypes = 'string string'
43
44 def cmd_PING(game, connection_id):
45     game.io.send('PONG')
46 cmd_PING.argtypes = ''
47
48 def cmd_TURN(game, n):
49     game.turn = n
50 cmd_TURN.argtypes = 'int:nonneg'
51
52 #def cmd_ANNOTATE(game, yx, connection_id):
53 def cmd_ANNOTATE(game, yx, msg):
54     if msg == ' ':
55         if yx in game.annotations:
56             del game.annotations[yx]
57     else:
58         game.annotations[yx] = msg
59     game.changed = True
60 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
61
62 def cmd_GET_ANNOTATION(game, yx, connection_id):
63     annotation = '(none)';
64     if yx in game.annotations:
65         annotation = game.annotations[yx]
66     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
67 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
68
69 # TODO: disallow these commands from clients? (maybe by failing on connection_id?)
70 def cmd_MAP_LINE(game, y, line):
71     game.map.set_line(y, line)
72 cmd_MAP_LINE.argtypes = 'int:nonneg string'
73
74 def cmd_MAP(game, size):
75     game.new_world(size)
76 cmd_MAP.argtypes = 'yx_tuple:pos'