From 0c3ba15173f789de47d5176ea0f10a8fb401d6b8 Mon Sep 17 00:00:00 2001 From: mio Date: Mon, 17 Sep 2018 05:22:18 +0000 Subject: [PATCH] Add veggie ramen handler, move other handler text to yml --- itte.py | 1 + ramenkan.py | 80 ++++++++++++++++++++++---------------- ramenkan/config.sample.yml | 2 +- ramenkan/standard.yml | 23 +++++++++++ 4 files changed, 72 insertions(+), 34 deletions(-) create mode 100644 ramenkan/standard.yml diff --git a/itte.py b/itte.py index 9f388a3..28b0bdc 100644 --- a/itte.py +++ b/itte.py @@ -19,6 +19,7 @@ class Util: """Return a random item from a given list.""" return lst[randint(0, len(lst)-1)] + class IRC: """Methods for basic IRC communication.""" diff --git a/ramenkan.py b/ramenkan.py index 28cb9eb..c908609 100644 --- a/ramenkan.py +++ b/ramenkan.py @@ -11,6 +11,7 @@ class Ramen: # 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 @@ -27,7 +28,9 @@ class Ramen: 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): @@ -35,41 +38,28 @@ class Ramen: self.irc.disconnect("Okay, okay, I'll leave. (´・ω・`)", "noodling off") def rollcall(self, cxt): - resp = ( - "一、二、三、らーめん缶! " - "Hello, I am a ramen vending machine. " - "Please type a code for service: " - "!help !ramen !rklink - " - "Support: +81 012-700-1MIO どうぞめしあがれ。" - ) - self.irc.reply(cxt, resp) + """Handle request for app info.""" + self.irc.reply(cxt, self.std["rollcall"]) def water(self, cxt): - resp = [ - ("\x01ACTION happily pours the hot liquid into a bowl of noodles " - "and offers it to ") + cxt["msg"]["nick"] + "\x01", - "Cheers! 自o(^_^ )", - "Water Level [/////////] 200% - Thanks! (^▽^ )", - ("Thanks. Some people say I have a CRABby tempeRAMENt. " - "I wonder why. (´-` )"), - ("Q. What do you call noodles made from the core of a tree? " - "A. DuRAMEN. *ba dum tss* ┐('∀`;)┌"), - ("Q. What do you call noodles eaten on bulletin boards? " - "A. FoRAMEN. *ba dum tss* ┐('∀`;)┌"), - ("Q. What kind of noodles can be found at TV stations? " - "A. CameRAMEN. *ba dum tss* ┐('∀`;)┌") - ] + """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): - resp = [ - "CHIKIN RAAAAAMEN━━━(゜∀゜)━━━!!!!!", - "Ramen time anytime! 自o(´▽` )/" - ] - self.irc.reply(cxt, self.rand(resp)) + """Handle snack offer.""" + self.irc.reply(cxt, self.rand(self.std["botsnack"])) - def make_ramen_combo(self): + 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): @@ -77,19 +67,29 @@ class Ramen: self.rand(dish["noodle-broth-type"]) + " " combo = combo.capitalize() # Broth type - combo += self.rand(dish["broth"] + dish["broth-veggie"]) + " broth" + 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: - prev_top = self.rand(dish["topping"] + dish["meat"]) + 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 " - next_top = self.rand(dish["topping"] + dish["meat"]) + 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"]) @@ -101,11 +101,15 @@ class Ramen: else: combo += "." if randint(0, 1): - combo += " Served with " + self.rand(dish["tapa"] + \ - dish["tapa-veggie"]) + "." + 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: @@ -115,7 +119,17 @@ class Ramen: 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"]) diff --git a/ramenkan/config.sample.yml b/ramenkan/config.sample.yml index d57e373..f4add1d 100644 --- a/ramenkan/config.sample.yml +++ b/ramenkan/config.sample.yml @@ -17,4 +17,4 @@ admin: req_prefix: "!" # Print messages to stdout -debug: True +debug: False diff --git a/ramenkan/standard.yml b/ramenkan/standard.yml new file mode 100644 index 0000000..db0dec6 --- /dev/null +++ b/ramenkan/standard.yml @@ -0,0 +1,23 @@ +rollcall: + "一、二、三、らーめん缶! + Hello, I am a ramen vending machine. Please type a code for service: + !help !ramen !veggieramen !rklink + - Support: +81 012-700-1MIO どうぞめしあがれ。" + +water: + - "\x01ACTION happily pours the hot liquid into a bowl of noodles + and offers it to {{ nick }}\x01" + - "Cheers! 自o(^_^ )" + - "Water Level [/////////] 200% - Thanks! (^▽^ )" + - "Thanks. Some people say I have a CRABby tempeRAMENt. + I wonder why. (´-` )" + - "Q. What do you call noodles made from the core of a tree? + A. DuRAMEN. *ba dum tss* ┐('∀`;)┌" + - "Q. What do you call noodles eaten on bulletin boards? + A. FoRAMEN. *ba dum tss* ┐('∀`;)┌" + - "Q. What kind of noodles can be found at TV stations? + A. CameRAMEN. *ba dum tss* ┐('∀`;)┌" + +botsnack: + - "CHIKIN RAAAAAMEN━━━(゜∀゜)━━━!!!!!" + - "Ramen time anytime! 自o(´▽` )/"