1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3 from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex
7 def cmd_TASKS(game, connection_id):
9 game.io.send('TASKS ' + ','.join(game.tasks.keys()), connection_id)
10 cmd_TASKS.argtypes = ''
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'
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)
30 game.io.send('CHAT ' + quote(t.nickname + ' entered the map.'))
31 game.io.send('PLAYER_ID %s' % t.id_, connection_id)
33 cmd_LOGIN.argtypes = 'string'
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)
44 game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
46 cmd_NICK.argtypes = 'string'
48 def cmd_GET_GAMESTATE(game, connection_id):
49 game.send_gamestate(connection_id)
50 cmd_GET_GAMESTATE.argtypes = ''
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)
63 raise GameError('target user offline')
64 raise GameError('can only query with registered nicknames')
65 cmd_QUERY.argtypes = 'string string'
67 def cmd_PING(game, connection_id):
68 game.io.send('PONG', connection_id)
69 cmd_PING.argtypes = ''
71 def cmd_TURN(game, n):
73 cmd_TURN.argtypes = 'int:nonneg'
75 def cmd_ANNOTATE(game, yx, msg, pw=None, connection_id=None):
76 if connection_id and not game.can_do_tile_with_pw(yx, pw):
77 raise GameError('wrong password for tile')
79 if yx in game.annotations:
80 del game.annotations[yx]
82 game.annotations[yx] = msg
84 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string string'
86 def cmd_PORTAL(game, yx, msg, pw=None, connection_id=None):
87 if connection_id and not game.can_do_tile_with_pw(yx, pw):
88 raise GameError('wrong password for tile')
90 if yx in game.portals:
93 game.portals[yx] = msg
95 cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string'
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'
104 def cmd_MAP_LINE(game, y, line):
105 game.map.set_line(y, line)
106 cmd_MAP_LINE.argtypes = 'int:nonneg string'
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'
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'
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'