home · contact · privacy
Add TASK command to request available commands.
[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_TASKS(game, connection_id):
8     tasks = []
9     game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
10 cmd_TASKS.argtypes = ''
11
12 def cmd_ALL(game, msg, connection_id):
13     if not connection_id in game.sessions:
14         raise GameError('need to be logged in for this')
15     t = game.get_thing(game.sessions[connection_id], False)
16     game.io.send('CHAT ' + quote(t.nickname + ': ' + msg))
17 cmd_ALL.argtypes = 'string'
18
19 def cmd_LOGIN(game, nick, connection_id):
20     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
21         raise GameError('name already in use')
22     if connection_id in game.sessions:
23         raise GameError('cannot log in twice')
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', connection_id)
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_NICK(game, nick, connection_id):
36     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
37         raise GameError('name already in use')
38     if not connection_id in game.sessions:
39         raise GameError('can only rename when already logged in')
40     t_id = game.sessions[connection_id]
41     t = game.get_thing(t_id, False)
42     old_nick = t.nickname
43     t.nickname = nick
44     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
45     game.changed = True
46 cmd_NICK.argtypes = 'string'
47
48 def cmd_GET_GAMESTATE(game, connection_id):
49     game.send_gamestate(connection_id)
50 cmd_GET_GAMESTATE.argtypes = ''
51
52 def cmd_QUERY(game, target_nick, msg, connection_id):
53     if not connection_id in game.sessions:
54         raise GameError('can only query when logged in')
55     t = game.get_thing(game.sessions[connection_id], False)
56     source_nick = t.nickname
57     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == target_nick]:
58         for c_id in game.sessions:
59             if game.sessions[c_id] == t.id_:
60                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
61                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
62                 return
63         raise GameError('target user offline')
64     raise GameError('can only query with registered nicknames')
65 cmd_QUERY.argtypes = 'string string'
66
67 def cmd_PING(game, connection_id):
68     game.io.send('PONG', connection_id)
69 cmd_PING.argtypes = ''
70
71 def cmd_TURN(game, n):
72     game.turn = n
73 cmd_TURN.argtypes = 'int:nonneg'
74
75 def cmd_ANNOTATE(game, yx, msg, connection_id):
76     if msg == ' ':
77         if yx in game.annotations:
78             del game.annotations[yx]
79     else:
80         game.annotations[yx] = msg
81     game.changed = True
82 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
83
84 def cmd_PORTAL(game, yx, msg, connection_id):
85     if msg == ' ':
86         if yx in game.portals:
87             del game.portals[yx]
88     else:
89         game.portals[yx] = msg
90     game.changed = True
91 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string'
92
93 def cmd_GET_ANNOTATION(game, yx, connection_id):
94     annotation = '(none)';
95     if yx in game.annotations:
96         annotation = game.annotations[yx]
97     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
98 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
99
100 def cmd_MAP_LINE(game, y, line):
101     game.map.set_line(y, line)
102 cmd_MAP_LINE.argtypes = 'int:nonneg string'
103
104 def cmd_MAP(game, geometry, size):
105     map_geometry_class = globals()['MapGeometry' + geometry]
106     game.new_world(map_geometry_class(size))
107 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'