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