home · contact · privacy
Add basic save file mechanism.
[plomrogue2-experiments] / new2 / plomrogue / commands.py
1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3
4
5
6 def cmd_ALL(game, msg, connection_id):
7     if not connection_id in game.sessions:
8         raise GameError('need to be logged in for this')
9     t = game.get_thing(game.sessions[connection_id], False)
10     game.io.send('LOG ' + quote(t.nickname + ': ' + msg))
11 cmd_ALL.argtypes = 'string'
12
13 def cmd_LOGIN(game, nick, connection_id):
14     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
15         raise GameError('name already in use')
16     t = game.thing_types['player'](game)
17     t.nickname = nick
18     game.things += [t]  # TODO refactor into Thing.__init__?
19     game.sessions[connection_id] = t.id_ 
20     game.io.send('META ' + quote('you are now: ' + nick), connection_id)
21 cmd_LOGIN.argtypes = 'string'
22
23 def cmd_QUERY(game, target_nick, msg, connection_id):
24     if not connection_id in game.sessions:
25         raise GameError('can only query when logged in')
26     t = game.get_thing(game.sessions[connection_id], False)
27     source_nick = t.nickname
28     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == target_nick]:
29         for c_id in game.sessions:
30             if game.sessions[c_id] == t.id_:
31                 game.io.send('LOG ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
32                 game.io.send('LOG ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
33                 return
34         raise GameError('target user offline')
35     raise GameError('can only query with registered nicknames')
36 cmd_QUERY.argtypes = 'string string'
37
38 def cmd_PING(game, connection_id):
39     game.io.send('PONG')
40 cmd_PING.argtypes = ''
41
42 def cmd_SAVE(game):
43
44     def write(f, msg):
45         f.write(msg + '\n')
46
47     with open(game.io.save_file, 'w') as f:
48         write(f, 'TURN %s' % game.turn)
49         for y, line in game.map.lines():
50             write(f, 'MAP_LINE %5s %s' % (y, quote(line)))
51 cmd_SAVE.argtypes = ''
52
53 def cmd_TURN(game, n):
54     game.turn = n
55 cmd_TURN.argtypes = 'int:nonneg'
56
57 def cmd_MAP_LINE(game, y, line):
58     game.map.set_line(y, line)
59 cmd_MAP_LINE.argtypes = 'int:nonneg string'