1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3 from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex
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'
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)
23 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
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)
31 game.io.send('CHAT ' + quote(t.nickname + ' entered the map.'))
32 game.io.send('PLAYER_ID %s' % t.id_, connection_id)
34 cmd_LOGIN.argtypes = 'string'
36 def cmd_GET_GAMESTATE(game, connection_id):
37 game.send_gamestate(connection_id)
38 cmd_GET_GAMESTATE.argtypes = ''
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)
51 raise GameError('target user offline')
52 raise GameError('can only query with registered nicknames')
53 cmd_QUERY.argtypes = 'string string'
55 def cmd_PING(game, connection_id):
57 cmd_PING.argtypes = ''
59 def cmd_TURN(game, n):
61 cmd_TURN.argtypes = 'int:nonneg'
63 def cmd_ANNOTATE(game, yx, msg, connection_id):
65 if yx in game.annotations:
66 del game.annotations[yx]
68 game.annotations[yx] = msg
70 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
72 def cmd_PORTAL(game, yx, msg, connection_id):
74 if yx in game.portals:
77 game.portals[yx] = msg
79 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string'
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'
88 def cmd_MAP_LINE(game, y, line):
89 game.map.set_line(y, line)
90 cmd_MAP_LINE.argtypes = 'int:nonneg string'
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'