home · contact · privacy
a3d1f4ce685cfabb9f6108ff8bd7f0f154e8434b
[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     import math
14     if not connection_id in game.sessions:
15         raise GameError('need to be logged in for this')
16     speaker = game.get_thing(game.sessions[connection_id], False)
17     for c_id in game.sessions:
18         listener = game.get_thing(game.sessions[c_id], create_unfound=False)
19         d_y = abs(speaker.position.y - listener.position.y)
20         d_x = abs(speaker.position.x - listener.position.x)
21         d = math.sqrt(d_y ** 2 + d_x ** 2)
22         distance = '(close)' if d < 3 else '(distant)'
23         game.io.send('CHAT ' +
24                      quote('%s %s: %s' % (distance, speaker.nickname, msg)),
25                      c_id)
26 cmd_ALL.argtypes = 'string'
27
28 def cmd_LOGIN(game, nick, connection_id):
29     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
30         raise GameError('name already in use')
31     if connection_id in game.sessions:
32         raise GameError('cannot log in twice')
33     t = game.thing_types['player'](game)
34     t.position = YX(game.map.size.y // 2, game.map.size.x // 2)
35     game.things += [t]  # TODO refactor into Thing.__init__?
36     game.sessions[connection_id] = t.id_
37     game.io.send('LOGIN_OK', connection_id)
38     t.nickname = nick
39     game.io.send('CHAT ' + quote(t.nickname + ' entered the map.'))
40     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
41     game.changed = True
42 cmd_LOGIN.argtypes = 'string'
43
44 def cmd_NICK(game, nick, connection_id):
45     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
46         raise GameError('name already in use')
47     if not connection_id in game.sessions:
48         raise GameError('can only rename when already logged in')
49     t_id = game.sessions[connection_id]
50     t = game.get_thing(t_id, False)
51     old_nick = t.nickname
52     t.nickname = nick
53     game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
54     game.changed = True
55 cmd_NICK.argtypes = 'string'
56
57 def cmd_GET_GAMESTATE(game, connection_id):
58     game.send_gamestate(connection_id)
59 cmd_GET_GAMESTATE.argtypes = ''
60
61 def cmd_QUERY(game, target_nick, msg, connection_id):
62     if not connection_id in game.sessions:
63         raise GameError('can only query when logged in')
64     t = game.get_thing(game.sessions[connection_id], False)
65     source_nick = t.nickname
66     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == target_nick]:
67         for c_id in game.sessions:
68             if game.sessions[c_id] == t.id_:
69                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
70                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
71                 return
72         raise GameError('target user offline')
73     raise GameError('can only query with registered nicknames')
74 cmd_QUERY.argtypes = 'string string'
75
76 def cmd_PING(game, connection_id):
77     game.io.send('PONG', connection_id)
78 cmd_PING.argtypes = ''
79
80 def cmd_TURN(game, n):
81     game.turn = n
82 cmd_TURN.argtypes = 'int:nonneg'
83
84 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
85     player = game.get_thing(game.sessions[connection_id], False)
86     if player.fov_stencil[yx] == '.':
87         raise GameError('cannot annotate tile outside field of view')
88     if not game.can_do_tile_with_pw(yx, pw):
89         raise GameError('wrong password for tile')
90     if msg == ' ':
91         if yx in game.annotations:
92             del game.annotations[yx]
93     else:
94         game.annotations[yx] = msg
95     game.changed = True
96 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
97
98 def cmd_PORTAL(game, yx, msg, pw, connection_id):
99     player = game.get_thing(game.sessions[connection_id], False)
100     if player.fov_stencil[yx] == '.':
101         raise GameError('cannot edit portal on tile outside field of view')
102     if not game.can_do_tile_with_pw(yx, pw):
103         raise GameError('wrong password for tile')
104     if msg == ' ':
105         if yx in game.portals:
106             del game.portals[yx]
107     else:
108         game.portals[yx] = msg
109     game.changed = True
110 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
111
112 def cmd_GOD_ANNOTATE(game, yx, msg):
113     game.annotations[yx] = msg
114     game.changed = True
115 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
116
117 def cmd_GOD_PORTAL(game, yx, msg):
118     game.portals[yx] = msg
119     game.changed = True
120 cmd_GOD_PORTAL.argtypes = 'yx_tuple:nonneg string'
121
122 def cmd_GET_ANNOTATION(game, yx, connection_id):
123     player = game.get_thing(game.sessions[connection_id], False)
124     annotation = '(unknown)';
125     if player.fov_stencil[yx] == '.':
126         annotation = '(none)';
127         if yx in game.annotations:
128             annotation = game.annotations[yx]
129     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
130 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
131
132 def cmd_MAP_LINE(game, y, line):
133     game.map.set_line(y, line)
134 cmd_MAP_LINE.argtypes = 'int:nonneg string'
135
136 def cmd_MAP(game, geometry, size):
137     map_geometry_class = globals()['MapGeometry' + geometry]
138     game.new_world(map_geometry_class(size))
139 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
140
141 def cmd_MAP_CONTROL_LINE(game, y, line):
142     game.map_control.set_line(y, line)
143 cmd_MAP_CONTROL_LINE.argtypes = 'int:nonneg string'
144
145 def cmd_MAP_CONTROL_PW(game, tile_class, password):
146     game.map_control_passwords[tile_class] = password
147 cmd_MAP_CONTROL_PW.argtypes = 'char string'