home · contact · privacy
Add basic multiplayer roguelike chat example.
[plomrogue2-experiments] / new2 / plomrogue / commands.py
1 from plomrogue.misc import quote
2
3
4
5 def cmd_ALL(game, msg, connection_id):
6     if not connection_id in game.sessions:
7         game.io.send('LOG' + quote('need to be logged in for this'), connection_id)
8         return
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         game.io.send('LOG ' + quote('name already in use'), connection_id)
16         return
17     t = game.thing_types['player'](game)
18     t.nickname = nick
19     game.things += [t]  # TODO refactor into Thing.__init__?
20     game.sessions[connection_id] = t.id_ 
21     game.io.send('LOG ' + quote('your are now: ' + nick), connection_id)
22 cmd_LOGIN.argtypes = 'string'
23
24 def cmd_QUERY(game, target_nick, msg, connection_id):
25     if not connection_id in game.sessions:
26         game.io.send('LOG ' + quote('can only query when logged in'), connection_id)
27     t = game.get_thing(game.sessions[connection_id], False)
28     source_nick = t.nickname
29     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == target_nick]:
30         for c_id in game.sessions:
31             if game.sessions[c_id] == t.id_:
32                 game.io.send('LOG ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id)
33                 game.io.send('LOG ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id)
34                 return
35         game.io.send('LOG ' + quote('target user offline?'))
36     game.io.send('LOG ' + quote('can only query with registered nicknames'))
37 cmd_QUERY.argtypes = 'string string'