Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###
- # Copyright (c) 2013, Steven
- # All rights reserved.
- #
- # Redistribution and use in source and binary forms, with or without
- # modification, are permitted provided that the following conditions are met:
- #
- # * Redistributions of source code must retain the above copyright notice,
- # this list of conditions, and the following disclaimer.
- # * Redistributions in binary form must reproduce the above copyright notice,
- # this list of conditions, and the following disclaimer in the
- # documentation and/or other materials provided with the distribution.
- # * Neither the name of the author of this software nor the name of
- # contributors to this software may be used to endorse or promote products
- # derived from this software without specific prior written consent.
- #
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- # POSSIBILITY OF SUCH DAMAGE.
- ###
- import os
- import time
- import random
- import inflect
- #open is probably overshadowed by the imports
- _open=open
- import supybot.conf as conf
- import supybot.ircdb as ircdb
- import supybot.utils as utils
- from supybot.commands import *
- import supybot.plugins as plugins
- import supybot.ircutils as ircutils
- import supybot.callbacks as callbacks
- from supybot.conf import supybot
- try:
- import sqlite3
- except ImportError:
- from pysqlite2 import dbapi2 as sqlite3 # for python2.4
- color="12"
- def _is_determiner(string):
- determiners = ['the', 'a', 'an', 'another', 'no', 'the', 'a', 'an',
- 'no', 'another', 'some', 'any', 'my', 'our', 'their',
- 'her', 'his', 'its', 'another', 'no', 'each', 'every',
- 'certain', 'its', 'another', 'no', 'this', 'that',
- 'all']
- if string in determiners:
- return True
- elif string[-2:] == "'s":
- return True
- #any and __builtins__ are overshadowed
- #elif __builtins__.any(c.isdigit() for c in string):
- elif len([c for c in string if c in "1234567890"]) > 0:
- return True
- else:
- return False
- class BakeIt(callbacks.Plugin, plugins.ChannelDBHandler):
- """bake [food]: bakes a food and adds it to the database
- eat [username]: consumes a food to remove it from the database"""
- def __init__(self, irc):
- callbacks.Plugin.__init__(self, irc)
- plugins.ChannelDBHandler.__init__(self)
- exec open("plugins/BakeIt/foods.txt", "r").read()
- exec open("plugins/BakeIt/goods.txt", "r").read()
- def makeDb(self, filename):
- if os.path.exists(filename):
- db = sqlite3.connect(filename)
- db.text_factory = str
- return db
- db = sqlite3.connect(filename)
- db.text_factory = str
- cursor = db.cursor()
- cursor.execute("""CREATE TABLE foods (
- id INTEGER PRIMARY KEY,
- foodname TEXT,
- added_at TIMESTAMP,
- baker TEXT,
- poisoned BOOLEAN,
- eater TEXT,
- cookverb TEXT
- )""")
- cursor.execute("""CREATE TABLE bakers (
- id INTEGER PRIMARY KEY,
- username TEXT UNIQUE ON CONFLICT REPLACE
- )""")
- db.commit()
- return db
- def bake(self, irc, msg, args, channel, foodname):
- """[food]: bakes a food and adds it to the database.
- A randomly selected default food choice is selected if one is not
- provided."""
- db = self.getDb(channel)
- cursor = db.cursor()
- p=inflect.engine()
- username = msg.nick
- if not foodname:
- foodname = random.choice(self.goods)
- capitals = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- lowers = "abcdefghijklmnopqrstuvwxyz"
- #if there is only one capitalized letter in the food name
- if len([c for c in foodname if c in capitals]) == 1:
- #if it is the first letter
- if foodname[0] in capitals:
- #lowercase it
- foodname = (lowers[capitals.index(foodname[0])]
- + foodname[1:])
- foodname = foodname.strip()
- #start the food name with "a" or "an" if it is singular and this ins't already done
- if p.singular_noun(foodname):
- plural=True
- else:
- if not _is_determiner(foodname.split()[0]):
- foodname = p.a(foodname)
- plural=False
- if random.random()<0.25:
- problem = random.choice(("knocked the food onto the floor",
- "overcooked the dough", "added salt instead of sugar",
- "let the plastic wrapping melt into the food",
- "sneezed onto the food"))
- response = ("You baked %s%s. Unfortunately, you %s and had to" +
- " discard it") % (color, foodname, problem)
- irc.reply(response)
- return
- poisoned = random.randint(0, 1)
- cursor.execute("""INSERT INTO foods VALUES
- (NULL, ?, ?, ?, ?, NULL, 'baked')""",
- (foodname, int(time.time()), username, poisoned))
- cursor.execute("""INSERT INTO bakers VALUES
- (NULL, ?)""", [username])
- response = ("You baked %s%s. But whether or not %s poisonous " +
- "remains to be seen.") % (color, foodname, "they are" if plural else "it is")
- irc.reply(response)
- bake = wrap(bake, ['channel', additional('text')])
- def cook(self, irc, msg, args, channel, foodname):
- """[food]: cooks a food and adds it to the database.
- A randomly selected default food choice is selected if one is not
- provided."""
- db = self.getDb(channel)
- cursor = db.cursor()
- p=inflect.engine()
- username = msg.nick
- if not foodname:
- foodname = random.choice(self.foods)
- capitals = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- lowers = "abcdefghijklmnopqrstuvwxyz"
- #if there is only one capitalized letter in the food name
- if len([c for c in foodname if c in capitals]) == 1:
- #if it is the first letter
- if foodname[0] in capitals:
- #lowercase it
- foodname = (lowers[capitals.index(foodname[0])]
- + foodname[1:])
- foodname = foodname.strip()
- #start the food name with "a" or "an" if it is singular and this ins't already done
- if p.singular_noun(foodname):
- plural=True
- else:
- if not _is_determiner(foodname.split()[0]):
- foodname = p.a(foodname)
- plural=False
- if random.random()<0.25:
- problem = random.choice(("knocked the food onto the floor",
- "overcooked the dough", "added sugar instead of salt",
- "let the plastic wrapping melt into the food",
- "sneezed onto the food"))
- response = ("You cooked %s%s. Unfortunately, you %s and had to" +
- " discard it") % (color, foodname, problem)
- irc.reply(response)
- return
- poisoned = random.randint(0, 1)
- cursor.execute("""INSERT INTO foods VALUES
- (NULL, ?, ?, ?, ?, NULL, 'cooked')""",
- (foodname, int(time.time()), username, poisoned))
- #not useful to call it the cooks table because that only
- #makes the code more complex
- cursor.execute("""INSERT INTO bakers VALUES
- (NULL, ?)""", [username])
- response = ("You cooked %s%s. But whether or not %s poisonous " +
- "remains to be seen.") % (color, foodname, "they are" if plural else "it is")
- irc.reply(response)
- cook = wrap(cook, ['channel', additional('text')])
- def eat(self, irc, msg, args, channel, other):
- """[other]: consumes a food to remove it from the database.
- Consumes a random food baked by other. If other is not provided,
- a randomly selected food not made by the consumer is eaten."""
- db = self.getDb(channel)
- cursor = db.cursor()
- p=inflect.engine()
- username = msg.nick
- if other:
- if username.lower() == other.lower():
- irc.reply("You can't eat your own food! " +
- "What if you poisoned yourself?!")
- return
- else:
- cursor.execute(
- """SELECT baker FROM foods WHERE eater IS NULL AND
- LOWER(baker) <> ?""",
- [username.lower()])
- others = [i[0] for i in cursor.fetchall()]
- #If the user selected nobody but there are still no goods
- #made by someone else
- if others == []:
- irc.reply("There are no goods available for you " +
- "to eat. Perhaps try baking something yourself.")
- return
- other = random.choice(others)
- cursor.execute("""SELECT * FROM foods WHERE LOWER(baker) = ?
- AND eater IS NULL""", [other.lower()])
- entries = cursor.fetchall()
- #If the the person provided in the argument doesn't have anything
- #baked
- if entries == []:
- cursor.execute(
- """SELECT count(*) as cnt FROM bakers WHERE LOWER(username)
- = ?""",
- [other.lower()])
- other_has_baked = cursor.fetchone()[0] == (1,)
- #If the user selected someone who has never baked or cooked
- if other_has_baked:
- irc.reply(other + " has never prepared any food. " +
- "Perhaps you can try cooking or baking" +
- " something yourself.")
- return
- #If the user selected someone who has baked in the past, but
- #everything made by the person has been eaten
- else:
- irc.reply(other + " does not currently have any foods " +
- "available for you to eat. Perhaps you " +
- "can try baking or cooking something yourself.")
- return
- selected_entry = random.choice(entries)
- (id, foodname, added_at, baker, poisoned, eater, verb) = selected_entry
- timediff = int(time.time()) - added_at
- remarkprefix = "It seems that the" if poisoned else "The"
- remark = ("very deadly and you were poisoned" if poisoned==1 else
- "very delicious and you wanted to eat more")
- #fpr the word after "which"
- inflection1 = "were" if p.singular_noun(foodname) else "was"
- #for "goods were" or "good was"?
- #no longer used
- #inflection2 = "s were" if p.singular_noun(foodname) else " was"
- SECONDS_PER_DAY = 86400.
- SECONDS_PER_HOUR = 3600.
- SECONDS_PER_MINUTE = 60.
- days = int(timediff / SECONDS_PER_DAY)
- hours = int((timediff - SECONDS_PER_DAY*days) / SECONDS_PER_HOUR)
- minutes = int((timediff - (days*SECONDS_PER_DAY) - \
- (hours*SECONDS_PER_HOUR)) / SECONDS_PER_MINUTE)
- seconds = int(timediff % 60)
- timenames = (" day", " hour", " minute", " second")
- timeparts = tuple(str(value) + p.plural(timenames[index], value)
- for index, value in enumerate((days, hours, minutes, seconds)))
- if random.random()<0.25:
- problem = random.choice(("fell onto the floor",
- "had ants in it", "was moldy",
- "was recalled by the government",
- "got squished in the fridge"))
- response = ("You were going to eat %s%s which %s %s by %s about"
- + " %s, %s, %s, and %s ago. Unfortunately, the" +
- " food %s and you threw it away.") % \
- ((color, foodname, inflection1, verb, other) +
- timeparts + (problem,))
- cursor.execute("""UPDATE foods SET eater = 'dumpster' WHERE id = ?""",
- [id])
- else:
- response = ("You just ate %s%s which %s %s by %s about" +
- " %s, %s, %s, and %s ago." +
- " %s food was %s!") % \
- ((color, foodname, inflection1, verb, other) +
- timeparts + (remarkprefix, remark))
- cursor.execute("""UPDATE foods SET eater = ? WHERE id = ?""",
- [username, id])
- irc.reply(response)
- eat = wrap(eat, ['channel', additional('something')])
- def food(self, irc, msg, args, channel, other):
- """: shows how much food you, or if entered, other
- has eaten and baked and how much of it is poisonous. """
- db = self.getDb(channel)
- cursor = db.cursor()
- p=inflect.engine()
- username = msg.nick.lower()
- if other:
- other = other.lower()
- if other:
- if username != other:
- irc.reply("Using this command on another person is" +
- " currently not supported.")
- return
- else:
- cursor.execute(
- """SELECT COUNT(*) as cnt FROM foods WHERE LOWER(baker) = ?
- AND verb = 'baked'""",
- [username])
- baked = cursor.fetchone()[0]
- cursor.execute(
- """SELECT COUNT(*) as cnt FROM foods WHERE LOWER(baker) = ? AND
- poisoned = 1 AND verb = 'baked'""",
- [username])
- baked_poison = cursor.fetchone()[0]
- cursor.execute(
- """SELECT COUNT(*) as cnt FROM foods WHERE LOWER(baker) = ?
- AND verb = 'cooked'""",
- [username])
- cooked = cursor.fetchone()[0]
- cursor.execute(
- """SELECT COUNT(*) as cnt FROM foods WHERE LOWER(baker) = ? AND
- poisoned = 1 AND verb = 'cooked'""",
- [username])
- cooked_poison = cursor.fetchone()[0]
- cursor.execute(
- """SELECT COUNT(*) as cnt FROM foods WHERE LOWER(eater) = ?""",
- [username])
- eaten = cursor.fetchone()[0]
- cursor.execute(
- """SELECT COUNT(*) as cnt FROM foods WHERE LOWER(eater) = ? AND
- poisoned = 1""",
- [username])
- eaten_poison = cursor.fetchone()[0]
- irc.reply("You have baked %s foods and %s of them were poisoned" %
- (baked, baked_poison))
- irc.reply("You have cooked %s foods and %s of them were poisoned" %
- (cooked, cooked_poison))
- irc.reply("You have eaten %s foods and %s of them were poisoned" %
- (eaten, eaten_poison))
- food = wrap(food, ['channel', additional('something')])
- Class = BakeIt
- # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement