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 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
40 raise GameError('name already in use')
41 if game.get_player(connection_id):
42 raise GameError('cannot log in twice')
43 t = game.thing_types['Player'](game)
44 t.position = game.spawn_point
45 game.things += [t] # TODO refactor into Thing.__init__?
46 t.thing_char = game.get_next_player_char()
47 game.sessions[connection_id] = {
51 game.io.send('LOGIN_OK', connection_id)
53 game.io.send('CHAT ' + quote(t.name + ' entered the map.'))
54 game.io.send('PLAYER_ID %s' % t.id_, connection_id)
55 for s in [s for s in game.things
56 if s.type_ == 'SpawnPoint' and s.name == t.name]:
57 t.position = s.position
60 cmd_LOGIN.argtypes = 'string'
62 def cmd_BECOME_ADMIN(game, password, connection_id):
63 player = game.thing_types['Player'](game)
65 raise GameError('need to be logged in for this')
66 if password in game.admin_passwords:
67 game.sessions[connection_id]['status'] = 'admin'
68 game.io.send('ADMIN_OK', connection_id)
70 raise GameError('wrong password')
71 cmd_BECOME_ADMIN.argtypes = 'string'
73 def cmd_ADMIN_PASSWORD(game, password):
74 game.admin_passwords += [password]
75 cmd_ADMIN_PASSWORD.argtypes = 'string'
77 def cmd_SET_TILE_CONTROL(game, yx, control_char, connection_id):
78 player = game.get_player(connection_id)
80 raise GameError('need to be logged in for this')
81 if not game.sessions[connection_id]['status'] == 'admin':
82 raise GameError('need to be admin for this')
83 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
84 map_control = game.get_map(big_yx, 'control')
85 map_control[little_yx] = control_char
87 cmd_SET_TILE_CONTROL.argtypes = 'yx_tuple:nonneg char'
89 def cmd_THING_PROTECTION(game, thing_id, protection_char, connection_id):
90 player = game.get_player(connection_id)
92 raise GameError('need to be logged in for this')
93 if not game.sessions[connection_id]['status'] == 'admin':
94 raise GameError('need to be admin for this')
95 t = game.get_thing(thing_id)
97 raise GameError('thing of ID %s not found' % thing_id)
98 t.protection = protection_char
100 cmd_THING_PROTECTION.argtypes = 'int:pos char'
102 def cmd_SET_MAP_CONTROL_PASSWORD(game, tile_class, password, connection_id):
103 player = game.get_player(connection_id)
105 raise GameError('need to be logged in for this')
106 if not game.sessions[connection_id]['status'] == 'admin':
107 raise GameError('need to be admin for this')
108 if tile_class == '.':
109 raise GameError('tile class "." must remain unprotected')
110 game.map_control_passwords[tile_class] = password
112 cmd_SET_MAP_CONTROL_PASSWORD.argtypes = 'char string'
114 def cmd_NICK(game, nick, connection_id):
115 for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]:
116 raise GameError('name already in use')
117 t = game.get_player(connection_id)
119 raise GameError('can only rename when already logged in')
122 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
124 cmd_NICK.argtypes = 'string'
126 def cmd_GET_GAMESTATE(game, connection_id):
127 game.send_gamestate(connection_id)
128 cmd_GET_GAMESTATE.argtypes = ''
130 # def cmd_QUERY(game, target_nick, msg, connection_id):
131 # if not connection_id in game.sessions:
132 # raise GameError('can only query when logged in')
133 # t = game.get_thing(game.sessions[connection_id], False)
134 # source_nick = t.name
135 # for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]:
136 # for c_id in game.sessions:
137 # if game.sessions[c_id] == t.id_:
138 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
139 # game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
141 # raise GameError('target user offline')
142 # raise GameError('can only query with registered nicknames')
143 # cmd_QUERY.argtypes = 'string string'
145 def cmd_PING(game, connection_id):
146 game.io.send('PONG', connection_id)
147 cmd_PING.argtypes = ''
149 def cmd_TURN(game, n):
151 cmd_TURN.argtypes = 'int:nonneg'
153 def cmd_ANNOTATE(game, yx, msg, pw, connection_id):
154 player = game.get_player(connection_id)
155 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
156 if not player.fov_test(big_yx, little_yx):
157 raise GameError('cannot annotate tile outside field of view')
158 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
159 raise GameError('wrong password for tile')
161 if big_yx in game.annotations:
162 if little_yx in game.annotations[big_yx]:
163 del game.annotations[big_yx][little_yx]
165 if big_yx not in game.annotations:
166 game.annotations[big_yx] = {}
167 game.annotations[big_yx][little_yx] = msg
169 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
171 def cmd_PORTAL(game, yx, msg, pw, connection_id):
172 player = game.get_player(connection_id)
173 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
174 if not player.fov_test(big_yx, little_yx):
175 raise GameError('cannot edit portal on tile outside field of view')
176 if not game.can_do_tile_with_pw(big_yx, little_yx, pw):
177 raise GameError('wrong password for tile')
179 if big_yx in game.portals:
180 if little_yx in game.portals[big_yx]:
181 del game.portals[big_yx][little_yx]
183 if big_yx not in game.portals:
184 game.portals[big_yx] = {}
185 game.portals[big_yx][little_yx] = msg
187 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
189 def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg):
190 if big_yx not in game.annotations:
191 game.annotations[big_yx] = {}
192 game.annotations[big_yx][little_yx] = msg
194 cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string'
196 def cmd_GOD_PORTAL(game, big_yx, little_yx, msg):
197 if big_yx not in game.portals:
198 game.portals[big_yx] = {}
199 game.portals[big_yx][little_yx] = msg
201 cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string'
203 def cmd_GET_ANNOTATION(game, yx, connection_id):
204 player = game.get_player(connection_id)
205 big_yx, little_yx = player.fov_stencil.source_yxyx(yx)
206 annotation = '(unknown)'
207 if player.fov_test(big_yx, little_yx):
208 annotation = '(none)'
209 if big_yx in game.annotations:
210 if little_yx in game.annotations[big_yx]:
211 annotation = game.annotations[big_yx][little_yx]
212 game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
213 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
215 def cmd_MAP_LINE(game, big_yx, y, line):
216 map_ = game.get_map(big_yx)
217 map_.set_line(y, line)
218 cmd_MAP_LINE.argtypes = 'yx_tuple int:nonneg string'
220 def cmd_MAP(game, geometry, size):
221 from plomrogue.mapping import MapGeometryHex, MapGeometrySquare
222 map_geometry_class = MapGeometrySquare
223 if geometry == 'Hex':
224 map_geometry_class = MapGeometryHex
225 game.new_world(map_geometry_class(size))
226 cmd_MAP.argtypes = 'string:map_geometry yx_tuple:pos'
228 def cmd_MAP_CONTROL_LINE(game, big_yx, y, line):
229 map_control = game.get_map(big_yx, 'control')
230 map_control.set_line(y, line)
231 cmd_MAP_CONTROL_LINE.argtypes = 'yx_tuple int:nonneg string'
233 def cmd_MAP_CONTROL_PW(game, tile_class, password):
234 game.map_control_passwords[tile_class] = password
235 cmd_MAP_CONTROL_PW.argtypes = 'char string'
237 def cmd_THING(game, big_yx, little_yx, thing_type, thing_id):
238 if thing_type not in game.thing_types:
239 raise GameError('illegal thing type %s' % thing_type)
240 _ = game.get_map(big_yx)
243 t_old = game.get_thing(thing_id)
244 t_new = game.thing_types[thing_type](game, id_=thing_id, position=(big_yx,
247 game.things[game.things.index(t_old)] = t_new
249 game.things += [t_new]
251 cmd_THING.argtypes = 'yx_tuple yx_tuple:nonneg string:thing_type int:nonneg'
253 def cmd_THING_NAME(game, thing_id, name, pw, connection_id):
254 # TODO check if thing in FOV
255 t = game.get_thing(thing_id)
257 raise GameError('thing of ID %s not found' % thing_id)
258 if not game.can_do_thing_with_pw(t, pw):
259 raise GameError('wrong password for tile')
262 cmd_THING_NAME.argtypes = 'int:pos string string'
264 def cmd_GOD_THING_NAME(game, thing_id, name):
265 t = game.get_thing(thing_id)
267 raise GameError('thing of ID %s not found' % thing_id)
269 cmd_GOD_THING_NAME.argtypes = 'int:pos string'
271 def cmd_GOD_THING_PROTECTION(game, thing_id, protection_char):
272 t = game.get_thing(thing_id)
274 raise GameError('thing of ID %s not found' % thing_id)
275 t.protection = protection_char
276 cmd_GOD_THING_PROTECTION.argtypes = 'int:pos char'
278 def cmd_THING_DOOR_CLOSED(game, thing_id):
279 t = game.get_thing(thing_id)
281 raise GameError('thing of ID %s not found' % thing_id)
282 if not t.type_ == 'Door':
283 raise GameError('thing of ID %s not door' % thing_id)
287 cmd_THING_DOOR_CLOSED.argtypes = 'int:pos'
289 def cmd_THING_MUSICPLAYER_SETTINGS(game, thing_id, playing, index, repeat):
290 t = game.get_thing(thing_id)
292 raise GameError('thing of ID %s not found' % thing_id)
293 if not t.type_ == 'MusicPlayer':
294 raise GameError('thing of ID %s not music player' % thing_id)
296 t.playlist_index = index
298 cmd_THING_MUSICPLAYER_SETTINGS.argtypes = 'int:pos bool int:nonneg bool'
300 def cmd_THING_MUSICPLAYER_PLAYLIST_ITEM(game, thing_id, title, length):
301 t = game.get_thing(thing_id)
303 raise GameError('thing of ID %s not found' % thing_id)
304 if not t.type_ == 'MusicPlayer':
305 raise GameError('thing of ID %s not music player' % thing_id)
306 t.playlist += [(title, length)]
307 cmd_THING_MUSICPLAYER_PLAYLIST_ITEM.argtypes = 'int:pos string int:pos'