home · contact · privacy
Check server disconnect right on websocket.onclose.
[plomrogue2-experiments] / new2 / plomrogue / commands.py
1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3 from plomrogue.mapping import YX
4
5
6
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'
13
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)
21     else:
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('LOGIN_OK ' + quote('you are now: ' + nick), connection_id)
27     t.nickname = nick
28     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
29 cmd_LOGIN.argtypes = 'string'
30
31 def cmd_GET_GAMESTATE(game, connection_id):
32     game.send_gamestate(connection_id)
33 cmd_GET_GAMESTATE.argtypes = ''
34
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)
45                 return
46         raise GameError('target user offline')
47     raise GameError('can only query with registered nicknames')
48 cmd_QUERY.argtypes = 'string string'
49
50 def cmd_PING(game, connection_id):
51     game.io.send('PONG')
52 cmd_PING.argtypes = ''
53
54 def cmd_TURN(game, n):
55     game.turn = n
56 cmd_TURN.argtypes = 'int:nonneg'
57
58 def cmd_ANNOTATE(game, yx, msg, connection_id):
59     if msg == ' ':
60         if yx in game.annotations:
61             del game.annotations[yx]
62     else:
63         game.annotations[yx] = msg
64     game.changed = True
65 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
66
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'
73
74 def cmd_MAP_LINE(game, y, line):
75     game.map.set_line(y, line)
76 cmd_MAP_LINE.argtypes = 'int:nonneg string'
77
78 def cmd_MAP(game, size):
79     game.new_world(size)
80 cmd_MAP.argtypes = 'yx_tuple:pos'