home · contact · privacy
Start new players in middle of map instead of topleft corner.
[plomrogue2-experiments] / new2 / plomrogue / commands.py
1 from plomrogue.misc import quote
2 from plomrogue.errors import GameError
3 from plomrogue.mapping import YX
4
5
6
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'
13
14 def cmd_LOGIN(game, nick, connection_id):
15     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
16         raise GameError('name already in use')
17     if connection_id in game.sessions:
18         t_id = game.sessions[connection_id]
19         t = game.get_thing(t_id, False)
20         game.io.send('META ' + quote('you rename yourself to: ' + nick), connection_id)
21     else:
22         t = game.thing_types['player'](game)
23         t.position = YX(game.map.size.y // 2, game.map.size.x // 2)
24         game.things += [t]  # TODO refactor into Thing.__init__?
25         game.sessions[connection_id] = t.id_
26         game.io.send('META ' + quote('you are now: ' + nick), connection_id)
27     t.nickname = nick
28     game.io.send('PLAYER_ID %s' % t.id_, connection_id)
29 cmd_LOGIN.argtypes = 'string'
30
31 def cmd_QUERY(game, target_nick, msg, connection_id):
32     if not connection_id in game.sessions:
33         raise GameError('can only query when logged in')
34     t = game.get_thing(game.sessions[connection_id], False)
35     source_nick = t.nickname
36     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == target_nick]:
37         for c_id in game.sessions:
38             if game.sessions[c_id] == t.id_:
39                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
40                 game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
41                 return
42         raise GameError('target user offline')
43     raise GameError('can only query with registered nicknames')
44 cmd_QUERY.argtypes = 'string string'
45
46 def cmd_PING(game, connection_id):
47     game.io.send('PONG')
48 cmd_PING.argtypes = ''
49
50 def cmd_TURN(game, n):
51     game.turn = n
52 cmd_TURN.argtypes = 'int:nonneg'
53
54 def cmd_ANNOTATE(game, yx, msg, connection_id):
55     if msg == ' ':
56         if yx in game.annotations:
57             del game.annotations[yx]
58     else:
59         game.annotations[yx] = msg
60     game.changed = True
61 cmd_ANNOTATE.argtypes = 'yx_tuple:nonneg string'
62
63 def cmd_GET_ANNOTATION(game, yx, connection_id):
64     annotation = '(none)';
65     if yx in game.annotations:
66         annotation = game.annotations[yx]
67     game.io.send('ANNOTATION %s %s' % (yx, quote(annotation)))
68 cmd_GET_ANNOTATION.argtypes = 'yx_tuple:nonneg'
69
70 def cmd_MAP_LINE(game, y, line):
71     game.map.set_line(y, line)
72 cmd_MAP_LINE.argtypes = 'int:nonneg string'
73
74 def cmd_MAP(game, size):
75     game.new_world(size)
76 cmd_MAP.argtypes = 'yx_tuple:pos'