~stefanor/ibid/dict-398764

« back to all changes in this revision

Viewing changes to ibid/plugins/misc.py

  • Committer: Stefano Rivera
  • Date: 2009-06-02 11:15:19 UTC
  • mfrom: (643.6.9 coffee-374028)
  • Revision ID: stefano@rivera.za.net-20090602111519-y775bmt9ovhuedo1
Rework coffee to use twisted callLater rather than sleep (and provide infrastructure for other plugins to do the same)
https://code.edge.launchpad.net/~stefanor/ibid/coffee-374028/+merge/6926

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import logging
 
2
from random import choice
1
3
from time import sleep
2
4
 
 
5
import ibid
3
6
from ibid.plugins import Processor, match
4
7
from ibid.config import IntOption
5
8
from ibid.utils import ibid_version
6
9
 
7
10
help = {}
 
11
log = logging.getLogger('plugins.misc')
8
12
 
9
13
help['coffee'] = u"Times coffee brewing and reserves cups for people"
10
14
class Coffee(Processor):
11
15
    u"""coffee (on|please)"""
12
16
    feature = 'coffee'
13
17
 
14
 
    pot = None
 
18
    pots = {}
15
19
 
16
20
    time = IntOption('coffee_time', u'Brewing time in seconds', 240)
17
21
    cups = IntOption('coffee_cups', u'Maximum number of cups', 4)
18
22
    
 
23
    def coffee_announce(self, event):
 
24
        event.addresponse(u"Coffee's ready for %s!", u', '.join(self.pots[(event.source, event.channel)]))
 
25
        del self.pots[(event.source, event.channel)]
 
26
 
19
27
    @match(r'^coffee\s+on$')
20
28
    def coffee_on(self, event):
21
 
        # Hi ... race condition.
22
 
        if self.pot:
23
 
            event.addresponse(u"There's already a pot on")
 
29
        if (event.source, event.channel) in self.pots:
 
30
            if len(self.pots[(event.source, event.channel)]) >= self.cups:
 
31
                event.addresponse(u"There's already a pot on, and it's all reserved")
 
32
            elif event.sender['nick'] in self.pots[(event.source, event.channel)]:
 
33
                event.addresponse(u"You already have a pot on the go")
 
34
            else:
 
35
                event.addresponse(u"There's already a pot on. If you ask nicely, maybe you can have a cup")
24
36
            return
25
37
        
26
 
        self.pot = [event.sender['nick']]
27
 
        sleep(self.time)
28
 
        event.addresponse(u"Coffee's ready for %s!", u', '.join(self.pot))
29
 
        self.pot = None
 
38
        self.pots[(event.source, event.channel)] = [event.sender['nick']]
 
39
        ibid.dispatcher.call_later(self.time, self.coffee_announce, event)
 
40
 
 
41
        event.addresponse({
 
42
            'action': True,
 
43
            'reply': choice((
 
44
                u'puts the kettle on',
 
45
                u'starts grinding coffee',
 
46
                u'flips the salt-timer',
 
47
                u'washes some mugs',
 
48
            )),
 
49
        })
30
50
    
31
51
    @match('^coffee\s+(?:please|pls)$')
32
52
    def coffee_accept(self, event):
33
 
        if not self.pot:
 
53
        if (event.source, event.channel) not in self.pots:
34
54
            event.addresponse(u"There isn't a pot on")
35
55
 
36
 
        elif len(self.pot) >= self.cups:
 
56
        elif len(self.pots[(event.source, event.channel)]) >= self.cups:
37
57
            event.addresponse(u"Sorry, there aren't any more cups left")
38
58
 
 
59
        elif event.sender['nick'] in self.pots[(event.source, event.channel)]:
 
60
            event.addresponse(u"Now now, we don't want anyone getting caffine overdoses")
 
61
 
39
62
        else:
40
 
            self.pot.append(event.sender['nick'])
 
63
            self.pots[(event.source, event.channel)].append(event.sender['nick'])
41
64
            event.addresponse(True)
42
65
 
43
66
help['version'] = u"Show the Ibid version currently running"