1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
6 # TODO: instead of sending tasks, thing types etc. on request, send them on connection
8 def cmd_TASKS(game, connection_id):
9 game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
10 cmd_TASKS.argtypes = ''
12 def cmd_THING_TYPES(game, connection_id):
13 for t_t in game.thing_types.values():
14 game.io.send('THING_TYPE %s %s' % (t_t.get_type(), quote(t_t.symbol_hint)),
16 cmd_THING_TYPES.argtypes = ''
18 def cmd_TERRAINS(game, connection_id):
19 for t in game.terrains.keys():
20 game.io.send('TERRAIN %s %s' % (quote(t), quote(game.terrains[t])),
22 cmd_TERRAINS.argtypes = ''
24 def cmd_ALL(game, msg, connection_id):
25 speaker = game.get_player(connection_id)
27 raise GameError('need to be logged in for this')
28 speaker.sound(speaker.name, msg)
29 cmd_ALL.argtypes = 'string'
31 def cmd_SPAWN_POINT(game, big_yx, little_yx):
32 if little_yx.y >= game.map_geometry.size.y or \
33 little_yx.x >= game.map_geometry.size.x:
34 raise GameError('illegal spawn point')
35 game.spawn_point = big_yx, little_yx
36 cmd_SPAWN_POINT.argtypes = 'yx_tuple yx_tuple:nonneg'
38 def cmd_LOGIN(game, nick, connection_id):
39 # TODO filter newlines
40 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
41 raise GameError('name already in use')
42 if game.get_player(connection_id):
43 raise GameError('cannot log in twice')
44 t = game.thing_types['Player'](game)
45 t.position = game.spawn_point
46 game.things += [t] # TODO refactor into Thing.__init__?
47 t.thing_char = game.get_next_player_char()
48 game.sessions[connection_id] = {
52 game.io.send('LOGIN_OK', connection_id)
54 game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
55 game.io.send('PLAYER_ID %s' % t.id_, connection_id)
56 for s in [s for s in game.things
57 if s.type_ == 'SpawnPoint' and s.name == t.name]:
58 t.position = s.position
61 cmd_LOGIN.argtypes = 'string'
63 def cmd_BECOME_ADMIN(game, password, connection_id):
64 player = game.thing_types['Player'](game)
66 raise GameError('need to be logged in for this')
67 if password in game.admin_passwords:
68 game.sessions[connection_id]['status'] = 'admin'
69 game.io.send('ADMIN_OK', connection_id)
71 raise GameError('wrong password')
72 cmd_BECOME_ADMIN.argtypes = 'string'
74 def cmd_ADMIN_PASSWORD(game, password):
75 game.admin_passwords += [password]
76 cmd_ADMIN_PASSWORD.argtypes = 'string'
78 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
79 player = game.get_player(connection_id)
81 raise GameError('need to be logged in for this')
82 if not game.sessions[connection_id]['status'] == 'admin':
83 raise GameError('need to be admin for this')
84 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
85 map_control = game.get_map(big_yx, 'control')
86 map_control[little_yx] = control_char
88 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
90 def cmd_THING_PROTECTION(game, thing_id, protection_char, connection_id):
91 player = game.get_player(connection_id)
93 raise GameError('need to be logged in for this')
94 if not game.sessions[connection_id]['status'] == 'admin':
95 raise GameError('need to be admin for this')
96 t = game.get_thing(thing_id)
98 raise GameError('thing of ID %s not found' % thing_id)
99 t.protection = protection_char
101 cmd_THING_PROTECTION.argtypes = 'int:pos char'
103 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
104 player = game.get_player(connection_id)
106 raise GameError('need to be logged in for this')
107 if not game.sessions[connection_id]['status'] == 'admin':
108 raise GameError('need to be admin for this')
109 if tile_class == '.':
110 raise GameError('tile class "." must remain unprotected')
111 game.map_control_passwords[tile_class] = password
113 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
115 def cmd_NICK(game, nick, connection_id):
116 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
117 raise GameError('name already in use')
118 t = game.get_player(connection_id)
120 raise GameError('can only rename when already logged in')
123 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
125 cmd_NICK.argtypes = 'string'
127 def cmd_GET_GAMESTATE(game, connection_id):
128 game.send_gamestate(connection_id)
129 cmd_GET_GAMESTATE.argtypes = ''
131 # def cmd_QUERY(game, target_nick, msg, connection_id):
132 # if not connection_id in game.sessions:
133 # raise GameError('can only query when logged in')
134 # t = game.get_thing(game.sessions[connection_id], False)
135 # source_nick = t.name
136 # for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
137 # for c_id in game.sessions:
138 # if game.sessions[c_id] == t.id_:
139 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
140 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
142 # raise GameError('target user offline')
143 # raise GameError('can only query with registered nicknames')
144 # cmd_QUERY.argtypes = 'string string'
146 def cmd_PING(game, connection_id):
147 game.io.send('PONG', connection_id)
148 cmd_PING.argtypes = ''
150 def cmd_TURN(game, n):
152 cmd_TURN.argtypes = 'int:nonneg'
154 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
155 player = game.get_player(connection_id)
156 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
157 if not player.fov_test(big_yx, little_yx):
158 raise GameError('cannot annotate tile outside field of view')
159 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
160 raise GameError('wrong password for tile')
162 if big_yx in game.annotations:
163 if little_yx in game.annotations[big_yx]:
164 del game.annotations[big_yx][little_yx]
166 if big_yx not in game.annotations:
167 game.annotations[big_yx] = {}
168 game.annotations[big_yx][little_yx] = msg
170 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
172 def cmd_PORTAL(game, yx, msg, pw, connection_id):
173 player = game.get_player(connection_id)
174 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
175 if not player.fov_test(big_yx, little_yx):
176 raise GameError('cannot edit portal on tile outside field of view')
177 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
178 raise GameError('wrong password for tile')
180 if big_yx in game.portals:
181 if little_yx in game.portals[big_yx]:
182 del game.portals[big_yx][little_yx]
184 if big_yx not in game.portals:
185 game.portals[big_yx] = {}
186 game.portals[big_yx][little_yx] = msg
188 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
190 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
191 if big_yx not in game.annotations:
192 game.annotations[big_yx] = {}
193 game.annotations[big_yx][little_yx] = msg
195 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
197 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
198 if big_yx not in game.portals:
199 game.portals[big_yx] = {}
200 game.portals[big_yx][little_yx] = msg
202 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
204 def cmd_GET_ANNOTATION(game, yx, connection_id):
205 player = game.get_player(connection_id)
206 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
207 annotation = '(unknown)'
208 if player.fov_test(big_yx, little_yx):
209 annotation = '(none)'
210 if big_yx in game.annotations:
211 if little_yx in game.annotations[big_yx]:
212 annotation = game.annotations[big_yx][little_yx]
213 game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
214 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
216 def cmd_MAP_LINE(game, big_yx, y, line):
217 map_ = game.get_map(big_yx)
218 map_.set_line(y, line)
219 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
221 def cmd_MAP(game, geometry, size):
222 from plomrogue.mapping import MapGeometryHex, MapGeometrySquare
223 map_geometry_class = MapGeometrySquare
224 if geometry == 'Hex':
225 map_geometry_class = MapGeometryHex
226 game.new_world(map_geometry_class(size))
227 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
229 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
230 map_control = game.get_map(big_yx, 'control')
231 map_control.set_line(y, line)
232 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
234 def cmd_MAP_CONTROL_PW(game, tile_class, password):
235 game.map_control_passwords[tile_class] = password
236 cmd_MAP_CONTROL_PW.argtypes = 'char string'
238 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
239 if thing_type not in game.thing_types:
240 raise GameError('illegal thing type %s' % thing_type)
241 _ = game.get_map(big_yx)
244 t_old = game.get_thing(thing_id)
245 t_new = game.thing_types[thing_type](game, id_=thing_id, position=(big_yx,
248 game.things[game.things.index(t_old)] = t_new
250 game.things += [t_new]
252 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
254 def cmd_THING_NAME(game, thing_id, name, pw, connection_id):
255 # TODO check if thing in FOV
256 t = game.get_thing(thing_id)
258 raise GameError('thing of ID %s not found' % thing_id)
259 if not game.can_do_thing_with_pw(t, pw):
260 raise GameError('wrong password for tile')
263 cmd_THING_NAME.argtypes = 'int:pos string string'
265 def cmd_GOD_THING_NAME(game, thing_id, name):
266 t = game.get_thing(thing_id)
268 raise GameError('thing of ID %s not found' % thing_id)
270 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
272 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
273 t = game.get_thing(thing_id)
275 raise GameError('thing of ID %s not found' % thing_id)
276 t.protection = protection_char
277 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
279 def cmd_THING_DOOR_CLOSED(game, thing_id):
280 t = game.get_thing(thing_id)
282 raise GameError('thing of ID %s not found' % thing_id)
283 if not t.type_ == 'Door':
284 raise GameError('thing of ID %s not door' % thing_id)
288 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'
290 def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
291 t = game.get_thing(thing_id)
293 raise GameError('thing of ID %s not found' % thing_id)
294 if not t.type_ == 'MusicPlayer':
295 raise GameError('thing of ID %s not music player' % thing_id)
297 t.playlist_index = index
299 cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int:nonneg bool'
301 def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
302 t = game.get_thing(thing_id)
304 raise GameError('thing of ID %s not found' % thing_id)
305 if not t.type_ == 'MusicPlayer':
306 raise GameError('thing of ID %s not music player' % thing_id)
307 t.playlist += [(title, length)]
308 cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'
310 def cmd_THING_BOTTLE_EMPTY(game, thing_id):
311 t = game.get_thing(thing_id)
313 raise GameError('thing of ID %s not found' % thing_id)
314 if not t.type_ == 'Bottle':
315 raise GameError('thing of ID %s not bottle' % thing_id)
317 cmd_THING_BOTTLE_EMPTY.argtypes = 'int:pos'