home · contact · privacy
c2bcc0a4858076568f0f7609a4f69f1efa3528d4
[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, pw, yx, msg, connection_id):
76     if connection_id and not game.can_do_tile_with_pw(yx, pw):
77         raise GameError('wrong password for tile')
78     if msg == ' ':
79         if yx in game.annotations:
80             del game.annotations[yx]
81     else:
82         game.annotations[yx] = msg
83     game.changed = True
84 cmd_ANNOTATE.argtypes = 'string yx_tuple:nonneg string'
85
86 def cmd_PORTAL(game, pw, yx, msg, connection_id):
87     if connection_id and not game.can_do_tile_with_pw(yx, pw):
88         raise GameError('wrong password for tile')
89     if msg == ' ':
90         if yx in game.portals:
91             del game.portals[yx]
92     else:
93         game.portals[yx] = msg
94     game.changed = True
95 cmd_PORTAL.argtypes = 'string yx_tuple:nonneg string'
96
97 def cmd_GET_ANNOTATION(game, yx, connection_id):
98     annotation = '(none)';
99     if yx in game.annotations:
100         annotation = game.annotations[yx]
101     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
102 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
103
104 def cmd_MAP_LINE(game, y, line):
105     game.map.set_line(y, line)
106 cmd_MAP_LINE.argtypes = 'int:nonneg string'
107
108 def cmd_MAP(game, geometry, size):
109     map_geometry_class = globals()['MapGeometry' + geometry]
110     game.new_world(map_geometry_class(size))
111 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
112
113 def cmd_MAP_CONTROL_LINE(game, y, line):
114     game.map_control.set_line(y, line)
115 cmd_MAP_CONTROL_LINE.argtypes = 'int:nonneg string'
116
117 def cmd_MAP_CONTROL_PW(game, tile_class, password):
118     game.map_control_passwords[tile_class] = password
119 cmd_MAP_CONTROL_PW.argtypes = 'char string'