home · contact · privacy
Split LOGIN into NICK and LOGIN.
[plomrogue2] / plomrogue / commands.py
1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3 from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex
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         raise GameError('cannot log in twice')
19     t = game.thing_types['player'](game)
20     t.position = YX(game.map.size.y // 2, game.map.size.x // 2)
21     game.things += [t]  # TODO refactor into Thing.__init__?
22     game.sessions[connection_id] = t.id_
23     game.io.send('LOGIN_OK', connection_id)
24     t.nickname = nick
25     game.io.send('CHAT ' + quote(t.nickname + ' entered the map.'))
26     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
27     game.changed = True
28 cmd_LOGIN.argtypes = 'string'
29
30 def cmd_NICK(game, nick, connection_id):
31     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
32         raise GameError('name already in use')
33     if not connection_id in game.sessions:
34         raise GameError('can only rename when already logged in')
35     t_id = game.sessions[connection_id]
36     t = game.get_thing(t_id, False)
37     old_nick = t.nickname
38     t.nickname = nick
39     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
40     game.changed = True
41 cmd_NICK.argtypes = 'string'
42
43 def cmd_GET_GAMESTATE(game, connection_id):
44     game.send_gamestate(connection_id)
45 cmd_GET_GAMESTATE.argtypes = ''
46
47 def cmd_QUERY(game, target_nick, msg, connection_id):
48     if not connection_id in game.sessions:
49         raise GameError('can only query when logged in')
50     t = game.get_thing(game.sessions[connection_id], False)
51     source_nick = t.nickname
52     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == target_nick]:
53         for c_id in game.sessions:
54             if game.sessions[c_id] == t.id_:
55                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
56                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
57                 return
58         raise GameError('target user offline')
59     raise GameError('can only query with registered nicknames')
60 cmd_QUERY.argtypes = 'string string'
61
62 def cmd_PING(game, connection_id):
63     game.io.send('PONG', connection_id)
64 cmd_PING.argtypes = ''
65
66 def cmd_TURN(game, n):
67     game.turn = n
68 cmd_TURN.argtypes = 'int:nonneg'
69
70 def cmd_ANNOTATE(game, yx, msg, connection_id):
71     if msg == ' ':
72         if yx in game.annotations:
73             del game.annotations[yx]
74     else:
75         game.annotations[yx] = msg
76     game.changed = True
77 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
78
79 def cmd_PORTAL(game, yx, msg, connection_id):
80     if msg == ' ':
81         if yx in game.portals:
82             del game.portals[yx]
83     else:
84         game.portals[yx] = msg
85     game.changed = True
86 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string'
87
88 def cmd_GET_ANNOTATION(game, yx, connection_id):
89     annotation = '(none)';
90     if yx in game.annotations:
91         annotation = game.annotations[yx]
92     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
93 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
94
95 def cmd_MAP_LINE(game, y, line):
96     game.map.set_line(y, line)
97 cmd_MAP_LINE.argtypes = 'int:nonneg string'
98
99 def cmd_MAP(game, geometry, size):
100     map_geometry_class = globals()['MapGeometry' + geometry]
101     game.new_world(map_geometry_class(size))
102 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'