home · contact · privacy
Send game messages on player logins/renames.
[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         old_nick = t.nickname
21         t.nickname = nick
22         game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
23     else:
24         t = game.thing_types['player'](game)
25         t.position = YX(game.map.size.y // 2, game.map.size.x // 2)
26         game.things += [t]  # TODO refactor into Thing.__init__?
27         game.sessions[connection_id] = t.id_
28         game.io.send('LOGIN_OK')
29         t.nickname = nick
30         game.io.send('CHAT ' + quote(t.nickname + ' entered the map.'))
31         game.io.send('PLAYER_ID %s' % t.id_, connection_id)
32     game.changed = True
33 cmd_LOGIN.argtypes = 'string'
34
35 def cmd_GET_GAMESTATE(game, connection_id):
36     game.send_gamestate(connection_id)
37 cmd_GET_GAMESTATE.argtypes = ''
38
39 def cmd_QUERY(game, target_nick, msg, connection_id):
40     if not connection_id in game.sessions:
41         raise GameError('can only query when logged in')
42     t = game.get_thing(game.sessions[connection_id], False)
43     source_nick = t.nickname
44     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == target_nick]:
45         for c_id in game.sessions:
46             if game.sessions[c_id] == t.id_:
47                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
48                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
49                 return
50         raise GameError('target user offline')
51     raise GameError('can only query with registered nicknames')
52 cmd_QUERY.argtypes = 'string string'
53
54 def cmd_PING(game, connection_id):
55     game.io.send('PONG')
56 cmd_PING.argtypes = ''
57
58 def cmd_TURN(game, n):
59     game.turn = n
60 cmd_TURN.argtypes = 'int:nonneg'
61
62 def cmd_ANNOTATE(game, yx, msg, connection_id):
63     if msg == ' ':
64         if yx in game.annotations:
65             del game.annotations[yx]
66     else:
67         game.annotations[yx] = msg
68     game.changed = True
69 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
70
71 def cmd_GET_ANNOTATION(game, yx, connection_id):
72     annotation = '(none)';
73     if yx in game.annotations:
74         annotation = game.annotations[yx]
75     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
76 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
77
78 def cmd_MAP_LINE(game, y, line):
79     game.map.set_line(y, line)
80 cmd_MAP_LINE.argtypes = 'int:nonneg string'
81
82 def cmd_MAP(game, size):
83     game.new_world(size)
84 cmd_MAP.argtypes = 'yx_tuple:pos'