Merge pull request #9 from dngfx/master
Fix decoys stacking up and attempting to take over irc.
This commit is contained in:
commit
8aad8080e1
1 changed files with 88 additions and 29 deletions
109
modules/ducks.py
109
modules/ducks.py
|
@ -1,3 +1,4 @@
|
||||||
|
from operator import itemgetter
|
||||||
import datetime
|
import datetime
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
@ -12,6 +13,7 @@ class Module(object):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.events = events
|
self.events = events
|
||||||
self.active_duck = 0
|
self.active_duck = 0
|
||||||
|
self.decoy_hooked = 0
|
||||||
|
|
||||||
events.on("received.command.bef").hook(self.duck_bef,
|
events.on("received.command.bef").hook(self.duck_bef,
|
||||||
help="Befriend a duck!")
|
help="Befriend a duck!")
|
||||||
|
@ -20,8 +22,8 @@ class Module(object):
|
||||||
events.on("received.command.decoy").hook(self.set_decoy,
|
events.on("received.command.decoy").hook(self.set_decoy,
|
||||||
help="Be a sneaky fellow and make a decoy duck.")
|
help="Be a sneaky fellow and make a decoy duck.")
|
||||||
|
|
||||||
# events.on("received.command.friends").hook(self.duck_friends,
|
events.on("received.command.friends").hook(self.duck_friends,
|
||||||
# help="See who the friendliest people to ducks are!")
|
help="See who the friendliest people to ducks are!")
|
||||||
# events.on("received.command.killers").hook(self.duck_killers,
|
# events.on("received.command.killers").hook(self.duck_killers,
|
||||||
# help="See who shoots the most amount of ducks.")
|
# help="See who shoots the most amount of ducks.")
|
||||||
# events.on("received.command.ducks").hook(self.duck_list,
|
# events.on("received.command.ducks").hook(self.duck_list,
|
||||||
|
@ -57,7 +59,8 @@ class Module(object):
|
||||||
for server in self.bot.servers.values():
|
for server in self.bot.servers.values():
|
||||||
for channel in server.channels.values():
|
for channel in server.channels.values():
|
||||||
ducks_enabled = channel.get_setting("ducks-enabled", 0)
|
ducks_enabled = channel.get_setting("ducks-enabled", 0)
|
||||||
ducks_enabled = int(ducks_enabled) if isinstance(ducks_enabled, str) else ducks_enabled
|
ducks_enabled = int(ducks_enabled) if isinstance(ducks_enabled,
|
||||||
|
str) else ducks_enabled
|
||||||
|
|
||||||
min_time = "min-duck-time-%s" % channel.name
|
min_time = "min-duck-time-%s" % channel.name
|
||||||
max_time = "max-duck-time-%s" % channel.name
|
max_time = "max-duck-time-%s" % channel.name
|
||||||
|
@ -65,8 +68,10 @@ class Module(object):
|
||||||
min_duck_time = channel.get_setting("min-duck-time", 240)
|
min_duck_time = channel.get_setting("min-duck-time", 240)
|
||||||
max_duck_time = channel.get_setting("max-duck-time", 1200)
|
max_duck_time = channel.get_setting("max-duck-time", 1200)
|
||||||
|
|
||||||
min_duck_time = int(min_duck_time) if isinstance(min_duck_time, str) else min_duck_time
|
min_duck_time = int(min_duck_time) if isinstance(min_duck_time,
|
||||||
max_duck_time = int(max_duck_time) if isinstance(max_duck_time, str) else max_duck_time
|
str) else min_duck_time
|
||||||
|
max_duck_time = int(max_duck_time) if isinstance(max_duck_time,
|
||||||
|
str) else max_duck_time
|
||||||
|
|
||||||
self.duck_times[min_time] = min_duck_time
|
self.duck_times[min_time] = min_duck_time
|
||||||
self.duck_times[max_time] = max_duck_time
|
self.duck_times[max_time] = max_duck_time
|
||||||
|
@ -83,65 +88,111 @@ class Module(object):
|
||||||
min = "min-duck-time-%s" % (channel_name)
|
min = "min-duck-time-%s" % (channel_name)
|
||||||
max = "max-duck-time-%s" % (channel_name)
|
max = "max-duck-time-%s" % (channel_name)
|
||||||
|
|
||||||
self.bot.log.debug("Attempting to set %s to %s", [str(min), str(self.duck_times[min])]);
|
self.bot.log.debug("Attempting to set %s to %s",
|
||||||
self.bot.log.debug("Attempting to set %s to %s", [str(max), str(self.duck_times[max])]);
|
[str(min), str(self.duck_times[min])]);
|
||||||
|
self.bot.log.debug("Attempting to set %s to %s",
|
||||||
|
[str(max), str(self.duck_times[max])]);
|
||||||
|
|
||||||
return random.randint(self.duck_times[min], self.duck_times[max])
|
return random.randint(self.duck_times[min], self.duck_times[max])
|
||||||
|
|
||||||
def decoy_time(self):
|
def decoy_time(self):
|
||||||
return random.randint(10, 20)
|
return random.randint(10, 20)
|
||||||
|
|
||||||
|
def duck_friends(self, event):
|
||||||
|
friends = event["server"].find_all_user_channel_settings(
|
||||||
|
"ducks-befriended")
|
||||||
|
|
||||||
|
friendliest = {}
|
||||||
|
friend_nicks = []
|
||||||
|
friend_ducks = []
|
||||||
|
|
||||||
|
for i in friends:
|
||||||
|
if i[1] in friendliest.keys():
|
||||||
|
friendliest[i[1]] += i[2]
|
||||||
|
else:
|
||||||
|
friendliest[i[1]] = i[2]
|
||||||
|
|
||||||
|
for user, friends in sorted(friendliest.items(), key = itemgetter(1),
|
||||||
|
reverse = True):
|
||||||
|
friend_nicks.append(user)
|
||||||
|
friend_ducks.append(friends)
|
||||||
|
|
||||||
|
sentence = "Friendliest Users -- "
|
||||||
|
|
||||||
|
length = len(friend_nicks) if len(friend_nicks) < 11 else 11
|
||||||
|
|
||||||
|
for i in range(0, length):
|
||||||
|
sentence += friend_nicks[i] + " (" + str(friend_ducks[i]) + ")"
|
||||||
|
if i < 10:
|
||||||
|
sentence += ", "
|
||||||
|
|
||||||
|
sentence = sentence[0:-2]
|
||||||
|
|
||||||
|
event["target"].send_message(sentence)
|
||||||
|
|
||||||
def duck_bef(self, event):
|
def duck_bef(self, event):
|
||||||
target = event["user"].nickname
|
user = event["user"]
|
||||||
|
target = user.nickname
|
||||||
|
id = user.id
|
||||||
|
|
||||||
active_duck = event["target"].get_setting("active-duck", 0)
|
active_duck = event["target"].get_setting("active-duck", 0)
|
||||||
active_duck = int(active_duck) if isinstance(active_duck, str) else active_duck
|
active_duck = int(active_duck) if isinstance(active_duck,
|
||||||
|
str) else active_duck
|
||||||
|
|
||||||
if active_duck == 0:
|
if active_duck == 0:
|
||||||
event["stderr"].set_prefix("Kick")
|
event["stderr"].set_prefix("Kick")
|
||||||
if event["server"].has_user(target):
|
if event["server"].has_user(target):
|
||||||
if not event["server"].is_own_nickname(target):
|
if not event["server"].is_own_nickname(target):
|
||||||
event["target"].send_kick(target, "You tried befriending a non-existent duck. Creepy!")
|
event["target"].send_kick(target,
|
||||||
|
"You tried befriending a non-existent duck. Creepy!")
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("Nope.")
|
event["stderr"].write("Nope.")
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("That user is not in this channel")
|
event["stderr"].write("That user is not in this channel")
|
||||||
else:
|
else:
|
||||||
setting = "bef-ducks-%s-%s" % (target, event["target"].name)
|
befriended_ducks = event["target"].get_user_setting(id,
|
||||||
befriended_ducks = event["server"].get_setting(setting, 0)
|
"ducks-befriended",
|
||||||
event["server"].set_setting(setting, befriended_ducks + 1)
|
0)
|
||||||
|
event["target"].set_user_setting(id,
|
||||||
|
"ducks-befriended",
|
||||||
|
befriended_ducks + 1)
|
||||||
event["target"].set_setting("active-duck", 0)
|
event["target"].set_setting("active-duck", 0)
|
||||||
|
|
||||||
grammar = "" if befriended_ducks == 0 else "s"
|
grammar = "" if befriended_ducks == 0 else "s"
|
||||||
|
|
||||||
event["stdout"].write(
|
event["stdout"].write(
|
||||||
target + ", you've befriended " + str(befriended_ducks + 1) + " duck" + grammar + " in " + event[
|
target + ", you've befriended " + str(
|
||||||
"target"].name);
|
befriended_ducks + 1) + " duck" + grammar + " in " + event[
|
||||||
|
"target"].name)
|
||||||
|
|
||||||
next_duck_time = self.duck_time(event)
|
next_duck_time = self.duck_time(event)
|
||||||
self.bot.add_timer("duck-appear", next_duck_time, persist=False)
|
self.bot.add_timer("duck-appear", next_duck_time, persist=False)
|
||||||
|
|
||||||
def duck_bang(self, event):
|
def duck_bang(self, event):
|
||||||
target = event["user"].nickname
|
user = event["user"]
|
||||||
|
target = user.nickname
|
||||||
|
id = user.id
|
||||||
if event["target"].get_setting("active-duck", 0) == 0:
|
if event["target"].get_setting("active-duck", 0) == 0:
|
||||||
event["stderr"].set_prefix("Kick")
|
event["stderr"].set_prefix("Kick")
|
||||||
if event["server"].has_user(target):
|
if event["server"].has_user(target):
|
||||||
if not event["server"].is_own_nickname(target):
|
if not event["server"].is_own_nickname(target):
|
||||||
event["target"].send_kick(target, "You tried shooting a non-existent duck. Creepy!")
|
event["target"].send_kick(target,
|
||||||
|
"You tried shooting a non-existent duck. Creepy!")
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("Nope.")
|
event["stderr"].write("Nope.")
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("That user is not in this channel")
|
event["stderr"].write("That user is not in this channel")
|
||||||
else:
|
else:
|
||||||
setting = "shot-ducks-%s-%s" % (target, event["target"].name)
|
shot_ducks = event["target"].get_user_setting(id, "ducks-shot", 0)
|
||||||
shot_ducks = event["server"].get_setting(setting, 0)
|
event["target"].set_user_setting(id, "ducks-shot", shot_ducks + 1)
|
||||||
event["server"].set_setting(setting, shot_ducks + 1)
|
|
||||||
event["target"].set_setting("active-duck", 0)
|
event["target"].set_setting("active-duck", 0)
|
||||||
|
|
||||||
grammar = "" if shot_ducks == 0 else "s"
|
grammar = "" if shot_ducks == 0 else "s"
|
||||||
|
|
||||||
event["stdout"].write(
|
event["stdout"].write(
|
||||||
target + ", you've shot " + str(shot_ducks + 1) + " duck" + grammar + " in " + event["target"].name);
|
target + ", you've shot " + str(
|
||||||
|
shot_ducks + 1) + " duck" + grammar + " in " + event[
|
||||||
|
"target"].name)
|
||||||
|
|
||||||
next_duck_time = self.duck_time(event)
|
next_duck_time = self.duck_time(event)
|
||||||
self.bot.add_timer("duck-appear", next_duck_time, persist=False)
|
self.bot.add_timer("duck-appear", next_duck_time, persist=False)
|
||||||
|
@ -150,13 +201,16 @@ class Module(object):
|
||||||
for server in self.bot.servers.values():
|
for server in self.bot.servers.values():
|
||||||
for channel in server.channels.values():
|
for channel in server.channels.values():
|
||||||
ducks_enabled = channel.get_setting("ducks-enabled", 0)
|
ducks_enabled = channel.get_setting("ducks-enabled", 0)
|
||||||
ducks_enabled = int(ducks_enabled) if isinstance(ducks_enabled, str) else ducks_enabled
|
ducks_enabled = int(ducks_enabled) if isinstance(ducks_enabled,
|
||||||
|
str) else ducks_enabled
|
||||||
if ducks_enabled == 0:
|
if ducks_enabled == 0:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self.bot.log.info("Ducks enabled for %s: %s", [str(channel.name), str(ducks_enabled)])
|
self.bot.log.info("Ducks enabled for %s: %s",
|
||||||
|
[str(channel.name), str(ducks_enabled)])
|
||||||
active_duck = channel.get_setting("active-duck", 0)
|
active_duck = channel.get_setting("active-duck", 0)
|
||||||
active_duck = int(active_duck) if isinstance(active_duck, str) else active_duck
|
active_duck = int(active_duck) if isinstance(active_duck,
|
||||||
|
str) else active_duck
|
||||||
|
|
||||||
if ducks_enabled == 1 and active_duck == 0:
|
if ducks_enabled == 1 and active_duck == 0:
|
||||||
ducks = [
|
ducks = [
|
||||||
|
@ -180,7 +234,8 @@ class Module(object):
|
||||||
channel.set_setting("active-duck", 0)
|
channel.set_setting("active-duck", 0)
|
||||||
|
|
||||||
next_duck_time = self.duck_time(channel.name)
|
next_duck_time = self.duck_time(channel.name)
|
||||||
self.bot.add_timer("duck-appear", next_duck_time, persist=False)
|
self.bot.add_timer("duck-appear", next_duck_time,
|
||||||
|
persist=False)
|
||||||
|
|
||||||
def duck_decoy(self, event):
|
def duck_decoy(self, event):
|
||||||
ducks = [
|
ducks = [
|
||||||
|
@ -199,7 +254,11 @@ class Module(object):
|
||||||
channel = event["target"]
|
channel = event["target"]
|
||||||
|
|
||||||
next_decoy_time = self.decoy_time()
|
next_decoy_time = self.decoy_time()
|
||||||
|
|
||||||
|
if self.decoy_hooked == 0:
|
||||||
self.events.on("timer").on("duck-decoy").hook(self.duck_decoy)
|
self.events.on("timer").on("duck-decoy").hook(self.duck_decoy)
|
||||||
|
self.decoy_hooked = 1
|
||||||
|
|
||||||
self.bot.add_timer("duck-decoy", next_decoy_time, None, None, False,
|
self.bot.add_timer("duck-decoy", next_decoy_time, None, None, False,
|
||||||
channel=channel)
|
channel=channel)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue