inky (owner)

Revisions

gist: 218752 Download_button fork
public
Public Clone URL: git://gist.github.com/218752.git
Embed All Files: show embed
boring-adventure.py #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
 
from twisted.internet import protocol, reactor
 
PORT = 3069
 
class BoringAdventure(protocol.Protocol):
    actions = {
        'look': 'You are surrounded by featureless walls.',
        'inventory': 'One light bulb (broken) and four pieces of eight.',
        'north': 'The path is blocked.',
        'south': 'The path is blocked.',
        'east': 'The path is blocked.',
        'west': 'The path is blocked.',
        'up': 'The path is blocked.',
        'down': 'The path is blocked.',
    }
    prompt = '> '
 
    def connectionMade(self):
        self.transport.write('\r\n'.join([
            'BORING ADVENTURE',
            '****************',
            '',
            'Commands are: %s' % ', '.join(sorted(self.actions.keys())),
            self.prompt,
        ]))
 
    def dataReceived(self, line):
        valid = False
        command = line.strip().lower()
        for action, response in self.actions.items():
            if action.startswith(command):
                self.transport.write(self.actions[action] + '\r\n')
                valid = True
                break
        if not valid:
            self.transport.write("What you say!\r\n")
        self.transport.write(self.prompt)
 
def main():
    factory = protocol.Factory()
    factory.protocol = BoringAdventure
    print 'Running on port %d...' % PORT
    reactor.listenTCP(PORT, factory)
    reactor.run()
 
if __name__ == '__main__':
    main()