itte/ramenkan.py

140 lines
4.9 KiB
Python
Executable File

from random import randint
from itte import IRC, Util
class Ramen:
"""Requests with a ramen theme."""
def main(self):
"""Instantiate an IRC object and attach the listeners."""
# Load yaml sources used by request handlers
self.util = Util()
self.rand = self.util.rand
self.std = self.util.yml("ramenkan/standard.yml")
self.links = self.util.yml("ramenkan/links.yml")
self.dishes = self.util.yml("ramenkan/dishes.yml")
# Init irc object
self.irc = IRC()
self.cfg = self.irc.config("ramenkan/config.sample.yml")
self.irc.run(self.add_listeners)
def add_listeners(self, cxt):
"""Map triggers to handlers."""
self.irc.listen(cxt, "exit " + self.cfg["admin"]["code"], self.quit, \
admin=True)
self.irc.listen(cxt, "rollcall", self.rollcall)
self.irc.listen(cxt, "help", self.rollcall)
self.irc.listen(cxt, "water " + self.cfg["bot_nick"], self.water)
self.irc.listen(cxt, "botsnack " + self.cfg["bot_nick"], self.botsnack)
self.irc.listen(cxt, "ramen", self.ramen)
self.irc.listen(cxt, "veggieramen", self.ramen_veggie)
self.irc.listen(cxt, "rk", self.ramen)
self.irc.listen(cxt, "rkveg", self.ramen_veggie)
self.irc.listen(cxt, "rklink", self.link)
def quit(self, cxt):
"""Disconnect from the server and quit."""
self.irc.disconnect("Okay, okay, I'll leave. (´・ω・`)", "noodling off")
def rollcall(self, cxt):
"""Handle request for app info."""
self.irc.reply(cxt, self.std["rollcall"])
def water(self, cxt):
"""Handle water offer."""
resp = self.std["water"]
for index, r in enumerate(resp):
if "{{ nick }}" in r:
resp.append(r.replace("{{ nick }}", cxt["msg"]["nick"]))
resp.pop(index)
self.irc.reply(cxt, self.rand(resp))
def botsnack(self, cxt):
"""Handle snack offer."""
self.irc.reply(cxt, self.rand(self.std["botsnack"]))
def make_ramen_combo(self, *args, **kwargs):
"""Generate a ramen dish. Optionally pass `veggie=True` for a
vegetarian dish."""
dish = self.dishes
# Check vegetarian flag
is_veggie = kwargs.get("veggie", False)
combo = "ramen in "
# Noodle type and broth richness
if randint(0, 1):
combo = self.rand(dish["noodle-shape"]) + ", " + \
self.rand(dish["noodle-broth-type"]) + " "
combo = combo.capitalize()
# Broth type
if is_veggie:
combo += self.rand(dish["broth-veggie"])
else:
combo += self.rand(dish["broth"] + dish["broth-veggie"])
combo += " broth"
# Topping
n_top = randint(2, 4)
for n in range(2, n_top+1):
if n == 2:
if is_veggie:
prev_top = self.rand(dish["topping"])
else:
prev_top = self.rand(dish["topping"] + dish["meat"])
combo += " with " + prev_top
else:
if n > 2 and n < n_top:
combo += ", "
else:
combo += " and "
if is_veggie:
next_top = self.rand(dish["topping"])
else:
next_top = self.rand(dish["topping"])
# Check for duplicate
if next_top == prev_top:
next_top = self.rand(dish["topping"])
combo += next_top
prev_top = next_top
# Condiment and side dish
if randint(0, 1):
combo += ", garnished with " + self.rand(dish["condiment"]) + "."
else:
combo += "."
if randint(0, 1):
combo += " Served with "
if is_veggie:
combo += self.rand(dish["tapa-veggie"]) + "."
else:
combo += self.rand(dish["tapa"] + dish["tapa-veggie"]) + "."
return combo
def ramen(self, cxt):
"""Handle ramen request."""
roll = randint(1, 100)
# 1% possibility of regional preset
if roll == 100:
resp = (self.rand(self.dishes["set"] + \
self.dishes["set-veggie"]) + ".").capitalize()
else:
resp = self.make_ramen_combo()
self.irc.reply(cxt, resp)
def ramen_veggie(self, cxt):
"""Handle vegetarian ramen request."""
roll = randint(1, 100)
if roll == 100:
resp = (self.dishes["set-veggie"] + ".").capitalize()
else:
resp = self.make_ramen_combo(veggie=True)
self.irc.reply(cxt, resp)
def link(self, cxt):
"""Handle to display a link."""
index = randint(0, len(self.links)-1)
self.irc.reply(cxt, self.links[index]["title"] + " " + \
self.links[index]["link"])
app = Ramen()
app.main()