aboutsummaryrefslogtreecommitdiff
path: root/modules/ducks.py
blob: d9081ffb002f10081325efce815fc95bd25562ff (about) (plain) (blame)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import random
from src import EventManager, ModuleManager, utils

DUCK = "・゜゜・。。・゜゜\_o< QUACK!"
NO_DUCK = "There was no duck!"

DEFAUL_MIN_MESSAGES = 100

@utils.export("channelset", {"setting": "ducks-enabled",
    "help": "Whether or not to spawn ducks", "validate": utils.bool_or_none})
@utils.export("channelset", {"setting": "ducks-min-messages",
    "help": "Minimum messages between ducks spawning",
    "validate": utils.int_or_none})
@utils.export("channelset", {"setting": "ducks-kick",
    "help": "Whether or not to kick someone talking to non-existent ducks",
    "validate": utils.bool_or_none})
class Module(ModuleManager.BaseModule):
    @utils.hook("new.channel")
    def new_channel(self, event):
        self.bootstrap_channel(event["channel"])

    def bootstrap_channel(self, channel):
        if not hasattr(channel, "duck_active"):
            channel.duck_active = False
            channel.duck_lines = 0

    def _activity(self, channel):
        self.bootstrap_channel(channel)

        ducks_enabled = channel.get_setting("ducks-enabled", False)

        if ducks_enabled and not channel.duck_active:
            channel.duck_lines += 1
            min_lines = channel.get_setting("ducks-min-messages",
                DEFAULT_MIN_MESSAGES)

            if channel.duck_lines >= min_lines:
                show_duck = random.SystemRandom().randint(1, 20) == 1

                if show_duck:
                    channel.duck_lines = 0
                    self._trigger_duck(channel)

    @utils.hook("received.join")
    def join(self, event):
        self._activity(event["channel"])
    @utils.hook("received.message.channel",
        priority=EventManager.PRIORITY_MONITOR)
    def channel_message(self, event):
        self._activity(event["channel"])

    def _trigger_duck(self, channel):
        channel.duck_active = True
        channel.send_message(DUCK)

    def _duck_action(self, channel, user, action, setting):
        channel.duck_active = False

        user_id = user.get_id()
        action_count = channel.get_user_setting(user_id, setting, 0)
        action_count += 1
        channel.set_user_setting(user_id, setting, action_count)

        return "%s %s a duck! You've %s %d ducks in %s!" % (
            user.nickname, action, action, action_count, channel.name)

    def _no_duck(self, channel, user, stderr, action):
        if channel.get_setting("ducks-kick"):
            channel.send_kick(user.nickname, NO_DUCK)
        else:
            stderr.write(NO_DUCK)

    @utils.hook("received.command.bef", alias_of="befriend")
    @utils.hook("received.command.befriend", channel_only=True)
    def befriend(self, event):
        if event["target"].duck_active:
            action = self._duck_action(event["target"], event["user"], "saved",
                "ducks-befriended")
            event["stdout"].write(action)
        else:
            self._no_duck(event["target"], event["user"], event["stderr"],
                "befriend")

    @utils.hook("received.command.bang", channel_only=True)
    def bang(self, event):
        if event["target"].duck_active:
            action = self._duck_action(event["target"], event["user"], "shot",
                "ducks-shot")
            event["stdout"].write(action)
        else:
            self._no_duck(event["target"], event["user"], event["stderr"],
                "shoot")

    @utils.hook("received.command.friends")
    def friends(self, event):
        stats = self._duck_stats(event["server"], "ducks-befriended", "friends",
            event["args_split"][0] if event["args"] else None)
        event["stdout"].write(stats)
    @utils.hook("received.command.enemies")
    def enemies(self, event):
        stats = self._duck_stats(event["server"], "ducks-shot", "enemies",
            event["args_split"][0] if event["args"] else None)
        event["stdout"].write(stats)

    def _duck_stats(self, server, setting, description, channel_query):
        channel_query_str = ""
        if not channel_query == None:
            channel_query = server.irc_lower(channel_query)
            channel_query_str = " in %s" % channel_query

        stats = server.find_all_user_channel_settings(setting)

        user_stats = {}
        for channel, nickname, value in stats:
            if not channel_query or channel_query == channel:
                if not nickname in user_stats:
                    user_stats[nickname] = 0
                user_stats[nickname] += value

        top_10 = utils.top_10(user_stats,
            convert_key=lambda nickname: server.get_user(nickname).nickname)
        return "Top duck %s%s: %s" % (description, channel_query_str,
            ", ".join(top_10))