2004-01-20 08:15:37 +01:00
|
|
|
#!/usr/bin/env python
|
2004-01-05 22:26:41 +01:00
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
|
|
# 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.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
|
|
|
This handles interesting things to do with a dictionary (words) database.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import supybot
|
|
|
|
import plugins
|
|
|
|
|
|
|
|
import os
|
2004-01-08 23:31:31 +01:00
|
|
|
import copy
|
2004-01-05 22:26:41 +01:00
|
|
|
import string
|
2004-01-07 04:02:03 +01:00
|
|
|
import time
|
|
|
|
import random
|
2004-01-05 22:26:41 +01:00
|
|
|
|
|
|
|
import sqlite
|
|
|
|
|
|
|
|
import conf
|
|
|
|
import utils
|
|
|
|
import privmsgs
|
|
|
|
import callbacks
|
2004-01-07 04:02:03 +01:00
|
|
|
import ircutils
|
2004-01-22 22:43:07 +01:00
|
|
|
import registry
|
2004-01-05 22:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
class WordsDB(plugins.DBHandler):
|
|
|
|
def makeDb(self, filename):
|
|
|
|
if os.path.exists(filename):
|
|
|
|
db = sqlite.connect(filename)
|
|
|
|
else:
|
|
|
|
db = sqlite.connect(filename, converters={'bool': bool})
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""CREATE TABLE words (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
word TEXT UNIQUE ON CONFLICT IGNORE,
|
|
|
|
sorted_word_id INTEGER)""")
|
|
|
|
cursor.execute("""CREATE TABLE sorted_words (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
word TEXT UNIQUE ON CONFLICT IGNORE)""")
|
|
|
|
cursor.execute("""CREATE INDEX sorted_word_id
|
|
|
|
ON words (sorted_word_id)""")
|
|
|
|
cursor.execute("""CREATE INDEX sorted_words_word
|
|
|
|
ON sorted_words (word)""")
|
|
|
|
db.commit()
|
|
|
|
return db
|
|
|
|
|
|
|
|
def addWord(db, word, commit=False):
|
|
|
|
word = word.strip().lower()
|
|
|
|
if word.translate(string.ascii, string.ascii_letters):
|
|
|
|
raise ValueError, 'Invalid word: %r' % word
|
|
|
|
L = list(word)
|
|
|
|
L.sort()
|
|
|
|
sorted = ''.join(L)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""INSERT INTO sorted_words VALUES (NULL, %s)""", sorted)
|
|
|
|
cursor.execute("""INSERT INTO words VALUES (NULL, %s,
|
|
|
|
(SELECT id FROM sorted_words
|
|
|
|
WHERE word=%s))""", word, sorted)
|
|
|
|
if commit:
|
|
|
|
db.commit()
|
|
|
|
|
2004-01-08 07:44:52 +01:00
|
|
|
|
2004-01-08 06:19:31 +01:00
|
|
|
class HangmanGame:
|
|
|
|
def __init__(self):
|
|
|
|
self.timeout = 0
|
2004-01-08 23:31:31 +01:00
|
|
|
self.timeGuess = 0
|
2004-01-08 06:19:31 +01:00
|
|
|
self.tries = 0
|
|
|
|
self.prefix = ''
|
|
|
|
self.guessed = False
|
2004-01-08 23:31:31 +01:00
|
|
|
self.unused = ''
|
2004-01-08 06:19:31 +01:00
|
|
|
self.hidden = ''
|
|
|
|
self.guess = ''
|
|
|
|
|
2004-01-08 23:31:31 +01:00
|
|
|
def getWord(self, dbHandler):
|
2004-01-08 06:19:31 +01:00
|
|
|
db = dbHandler.getDb()
|
|
|
|
cur = db.cursor()
|
|
|
|
cur.execute("""SELECT word FROM words ORDER BY random() LIMIT 1""")
|
|
|
|
word = cur.fetchone()[0]
|
|
|
|
return word
|
|
|
|
|
2004-01-08 07:44:52 +01:00
|
|
|
def letterPositions(self, letter, word):
|
|
|
|
"""
|
|
|
|
Returns a list containing the positions of letter in word.
|
|
|
|
"""
|
|
|
|
lst = []
|
2004-01-23 14:32:02 +01:00
|
|
|
for (i, c) in enumerate(word):
|
|
|
|
if c == letter:
|
2004-01-08 07:44:52 +01:00
|
|
|
lst.append(i)
|
|
|
|
return lst
|
|
|
|
|
|
|
|
def addLetter(self, letter, word, pos):
|
|
|
|
"""
|
|
|
|
Replaces all characters of word at positions contained in pos
|
|
|
|
by letter.
|
|
|
|
"""
|
|
|
|
newWord = []
|
2004-01-23 14:32:02 +01:00
|
|
|
for (i, c) in enumerate(word):
|
2004-01-08 07:44:52 +01:00
|
|
|
if i in pos:
|
|
|
|
newWord.append(letter)
|
|
|
|
else:
|
2004-01-23 14:32:02 +01:00
|
|
|
newWord.append(c)
|
2004-01-08 07:44:52 +01:00
|
|
|
return ''.join(newWord)
|
|
|
|
|
|
|
|
def triesLeft(self, n):
|
|
|
|
"""
|
|
|
|
Returns the number of tries and the correctly pluralized try/tries
|
|
|
|
"""
|
|
|
|
return utils.nItems('try', n)
|
|
|
|
|
|
|
|
def letterArticle(self, letter):
|
|
|
|
"""
|
|
|
|
Returns 'a' or 'an' to match the letter that will come after
|
|
|
|
"""
|
2004-01-23 14:32:02 +01:00
|
|
|
anLetters = 'aefhilmnorsx'
|
|
|
|
if letter in anLetters:
|
2004-01-08 07:44:52 +01:00
|
|
|
return 'an'
|
|
|
|
else:
|
|
|
|
return 'a'
|
|
|
|
|
2004-01-22 22:43:07 +01:00
|
|
|
conf.registerPlugin('Words')
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Words, 'hangmanMaxTries',
|
2004-01-23 14:32:02 +01:00
|
|
|
registry.Integer(6, """Determines how many oppurtunities users will have to
|
|
|
|
guess letters in the hangman game."""))
|
2004-01-22 22:43:07 +01:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Words, 'hangmanPrefix',
|
2004-01-23 14:32:02 +01:00
|
|
|
registry.StringWithSpaceOnRight('-= HANGMAN -= ', """Determines what prefix
|
|
|
|
string is placed in front of hangman-related messages sent to the
|
|
|
|
channel."""))
|
2004-01-22 22:43:07 +01:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Words, 'hangmanTimeout',
|
2004-01-23 14:32:02 +01:00
|
|
|
registry.Integer(300, """Determines how long a game must be idle before it
|
|
|
|
will be replaced with a new game."""))
|
2004-01-05 22:26:41 +01:00
|
|
|
|
2004-01-23 14:32:02 +01:00
|
|
|
class Words(callbacks.Privmsg):
|
2004-01-05 22:26:41 +01:00
|
|
|
def __init__(self):
|
|
|
|
callbacks.Privmsg.__init__(self)
|
2004-01-18 08:58:26 +01:00
|
|
|
dataDir = conf.supybot.directories.data()
|
|
|
|
self.dbHandler = WordsDB(os.path.join(dataDir, 'Words'))
|
2004-01-05 22:26:41 +01:00
|
|
|
|
|
|
|
def add(self, irc, msg, args):
|
|
|
|
"""<word> [<word>]
|
|
|
|
|
|
|
|
Adds a word or words to the database of words. This database is used
|
|
|
|
for the other commands in this plugin.
|
|
|
|
"""
|
|
|
|
if not args:
|
|
|
|
raise callbacks.ArgumentError
|
|
|
|
for word in args:
|
|
|
|
if word.translate(string.ascii, string.ascii_letters):
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.error('Word must contain only letters.')
|
2004-01-05 22:26:41 +01:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
addWord(self.dbHandler.getDb(), word, commit=True)
|
2004-01-09 00:03:48 +01:00
|
|
|
irc.replySuccess()
|
2004-01-05 22:26:41 +01:00
|
|
|
|
|
|
|
def crossword(self, irc, msg, args):
|
|
|
|
"""<word>
|
|
|
|
|
|
|
|
Gives the possible crossword completions for <word>; use underscores
|
|
|
|
('_') to denote blank spaces.
|
|
|
|
"""
|
|
|
|
word = privmsgs.getArgs(args).lower()
|
|
|
|
db = self.dbHandler.getDb()
|
|
|
|
cursor = db.cursor()
|
|
|
|
if '%' in word:
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.error('"%" isn\'t allowed in the word.')
|
2004-01-05 22:26:41 +01:00
|
|
|
return
|
|
|
|
cursor.execute("""SELECT word FROM words
|
|
|
|
WHERE word LIKE %s
|
|
|
|
ORDER BY word""", word)
|
|
|
|
words = [t[0] for t in cursor.fetchall()]
|
|
|
|
if words:
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.reply(utils.commaAndify(words))
|
2004-01-05 22:26:41 +01:00
|
|
|
else:
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.reply('No matching words were found.')
|
2004-01-05 22:26:41 +01:00
|
|
|
|
|
|
|
def anagram(self, irc, msg, args):
|
|
|
|
"""<word>
|
|
|
|
|
|
|
|
Using the words database, determines if a word has any anagrams.
|
|
|
|
"""
|
|
|
|
word = privmsgs.getArgs(args).strip().lower()
|
|
|
|
db = self.dbHandler.getDb()
|
|
|
|
cursor = db.cursor()
|
|
|
|
L = list(word.lower())
|
|
|
|
L.sort()
|
|
|
|
sorted = ''.join(L)
|
|
|
|
cursor.execute("""SELECT id FROM sorted_words WHERE word=%s""", sorted)
|
|
|
|
if cursor.rowcount == 0:
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.reply('That word has no anagrams I could find.')
|
2004-01-05 22:26:41 +01:00
|
|
|
else:
|
|
|
|
id = cursor.fetchone()[0]
|
|
|
|
cursor.execute("""SELECT words.word FROM words
|
|
|
|
WHERE sorted_word_id=%s""", id)
|
|
|
|
if cursor.rowcount > 1:
|
|
|
|
words = [t[0] for t in cursor.fetchall()]
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.reply(utils.commaAndify(words))
|
2004-01-05 22:26:41 +01:00
|
|
|
else:
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.reply('That word has no anagrams I could find.')
|
2004-01-07 04:02:03 +01:00
|
|
|
|
|
|
|
###
|
|
|
|
# HANGMAN
|
|
|
|
###
|
|
|
|
games = ircutils.IrcDict()
|
2004-01-23 14:32:02 +01:00
|
|
|
validLetters = list(string.ascii_lowercase)
|
2004-01-07 04:02:03 +01:00
|
|
|
|
|
|
|
def endGame(self, channel):
|
2004-01-14 22:10:42 +01:00
|
|
|
self.games[channel] = None
|
2004-01-07 04:02:03 +01:00
|
|
|
|
|
|
|
def letters(self, irc, msg, args):
|
2004-01-23 14:32:02 +01:00
|
|
|
"""[<channel>]
|
2004-01-07 04:02:03 +01:00
|
|
|
|
2004-01-23 14:32:02 +01:00
|
|
|
Returns the unused letters that can be guessed in the hangman game
|
|
|
|
in <channel>. <channel> is only necessary if the message isn't sent in
|
|
|
|
the channel itself.
|
2004-01-07 04:02:03 +01:00
|
|
|
"""
|
2004-01-23 14:32:02 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2004-01-13 07:43:58 +01:00
|
|
|
if channel in self.games:
|
|
|
|
game = self.games[channel]
|
2004-01-14 22:10:42 +01:00
|
|
|
if game is not None:
|
|
|
|
irc.reply('%s%s' % (game.prefix, ' '.join(game.unused)))
|
2004-01-13 07:43:58 +01:00
|
|
|
return
|
|
|
|
irc.error('There is no hangman game going on right now.')
|
2004-01-07 04:02:03 +01:00
|
|
|
|
2004-01-08 06:19:31 +01:00
|
|
|
def hangman(self, irc, msg, args):
|
2004-01-23 14:32:02 +01:00
|
|
|
"""[<channel>]
|
2004-01-07 04:02:03 +01:00
|
|
|
|
2004-01-23 14:32:02 +01:00
|
|
|
Creates a new game of hangman in <channel>. <channel> is only
|
|
|
|
necessary if the message isn't sent in the channel itself.
|
2004-01-07 04:02:03 +01:00
|
|
|
"""
|
2004-01-23 14:32:02 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2004-01-07 04:02:03 +01:00
|
|
|
# Fill our dictionary of games
|
|
|
|
if channel not in self.games:
|
2004-01-14 22:10:42 +01:00
|
|
|
self.games[channel] = None
|
2004-01-07 04:02:03 +01:00
|
|
|
# We only start a new game if no other game is going on right now
|
2004-01-14 22:10:42 +01:00
|
|
|
if self.games[channel] is None:
|
|
|
|
self.games[channel] = HangmanGame()
|
|
|
|
game = self.games[channel]
|
2004-01-23 14:32:02 +01:00
|
|
|
game.timeout = self.registryValue('hangmanTimeout', channel)
|
2004-01-08 23:31:31 +01:00
|
|
|
game.timeGuess = time.time()
|
2004-01-23 14:32:02 +01:00
|
|
|
game.tries = self.registryValue('hangmanMaxTries', channel)
|
|
|
|
game.prefix = self.registryValue('hangmanPrefix', channel)
|
2004-01-08 23:31:31 +01:00
|
|
|
game.guessed = False
|
|
|
|
game.unused = copy.copy(self.validLetters)
|
|
|
|
game.hidden = game.getWord(self.dbHandler)
|
2004-01-08 06:19:31 +01:00
|
|
|
game.guess = '_' * len(game.hidden)
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.reply('%sOkay ladies and gentlemen, you have '
|
2004-01-07 04:02:03 +01:00
|
|
|
'a %s-letter word to find, you have %s!' %
|
2004-01-08 06:19:31 +01:00
|
|
|
(game.prefix, len(game.hidden),
|
2004-01-08 07:44:52 +01:00
|
|
|
game.triesLeft(game.tries)), prefixName=False)
|
2004-01-07 04:02:03 +01:00
|
|
|
# So, a game is going on, but let's see if it's timed out. If it is
|
|
|
|
# we create a new one, otherwise we inform the user
|
|
|
|
else:
|
2004-01-14 22:10:42 +01:00
|
|
|
game = self.games[channel]
|
2004-01-08 06:19:31 +01:00
|
|
|
secondsEllapsed = time.time() - game.timeGuess
|
|
|
|
if secondsEllapsed > game.timeout:
|
2004-01-07 04:02:03 +01:00
|
|
|
self.endGame(channel)
|
2004-01-09 23:01:54 +01:00
|
|
|
self.hangman(irc, msg, args)
|
2004-01-07 04:02:03 +01:00
|
|
|
else:
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.error('Sorry, there is already a game going on. '
|
2004-01-23 14:32:02 +01:00
|
|
|
'%s left before timeout.' % \
|
|
|
|
utils.nItems('second',
|
|
|
|
int(game.timeout - secondsEllapsed)))
|
2004-01-07 04:02:03 +01:00
|
|
|
|
|
|
|
def guess(self, irc, msg, args):
|
2004-01-23 14:32:02 +01:00
|
|
|
"""[<channel>] <letter|word>
|
2004-01-07 04:02:03 +01:00
|
|
|
|
|
|
|
Try to guess a single letter or the whole word. If you try to guess
|
|
|
|
the whole word and you are wrong, you automatically lose.
|
|
|
|
"""
|
2004-01-23 14:32:02 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2004-01-13 07:43:58 +01:00
|
|
|
try:
|
|
|
|
game = self.games[channel]
|
2004-01-23 14:32:02 +01:00
|
|
|
if game is None:
|
|
|
|
raise KeyError
|
2004-01-13 07:43:58 +01:00
|
|
|
except KeyError:
|
|
|
|
irc.error('There is no hangman game going on right now.')
|
|
|
|
return
|
2004-01-07 04:02:03 +01:00
|
|
|
letter = privmsgs.getArgs(args)
|
2004-01-08 06:19:31 +01:00
|
|
|
game.timeGuess = time.time()
|
2004-01-07 04:02:03 +01:00
|
|
|
# User input a valid letter that hasn't been already tried
|
2004-01-08 06:19:31 +01:00
|
|
|
if letter in game.unused:
|
|
|
|
del game.unused[game.unused.index(letter)]
|
|
|
|
if letter in game.hidden:
|
2004-01-23 14:32:02 +01:00
|
|
|
irc.reply('%sYes, there is %s %r' % (game.prefix,
|
|
|
|
game.letterArticle(letter), letter), prefixName=False)
|
2004-01-08 07:44:52 +01:00
|
|
|
game.guess = game.addLetter(letter, game.guess,
|
|
|
|
game.letterPositions(letter, game.hidden))
|
2004-01-08 06:19:31 +01:00
|
|
|
if game.guess == game.hidden:
|
|
|
|
game.guessed = True
|
2004-01-07 04:02:03 +01:00
|
|
|
else:
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.reply('%sNo, there is no %s' % (game.prefix,`letter`),
|
2004-01-07 04:02:03 +01:00
|
|
|
prefixName=False)
|
2004-01-08 06:19:31 +01:00
|
|
|
game.tries -= 1
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.reply('%s%s (%s left)' % (game.prefix, game.guess,
|
2004-01-08 07:44:52 +01:00
|
|
|
game.triesLeft(game.tries)), prefixName=False)
|
2004-01-07 04:02:03 +01:00
|
|
|
# User input a valid character that has already been tried
|
|
|
|
elif letter in self.validLetters:
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.error('That letter has already been tried.')
|
2004-01-07 04:02:03 +01:00
|
|
|
# User tries to guess the whole word or entered an invalid input
|
|
|
|
else:
|
|
|
|
# The length of the word tried by the user and that of the hidden
|
2004-01-08 04:12:14 +01:00
|
|
|
# word are same, so we assume the user wants to guess the whole
|
|
|
|
# word
|
2004-01-08 06:19:31 +01:00
|
|
|
if len(letter) == len(game.hidden):
|
|
|
|
if letter == game.hidden:
|
|
|
|
game.guessed = True
|
2004-01-07 04:02:03 +01:00
|
|
|
else:
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.reply('%syou did not guess the correct word '
|
2004-01-08 06:19:31 +01:00
|
|
|
'and you lose a try' % game.prefix, prefixName=False)
|
|
|
|
game.tries -= 1
|
2004-01-07 04:02:03 +01:00
|
|
|
else:
|
|
|
|
# User input an invalid character
|
|
|
|
if len(letter) == 1:
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.error('That is not a valid character.')
|
2004-01-08 06:19:31 +01:00
|
|
|
# User input an invalid word (len(try) != len(hidden))
|
2004-01-07 04:02:03 +01:00
|
|
|
else:
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.error('That is not a valid word guess.')
|
2004-01-07 04:02:03 +01:00
|
|
|
# Verify if the user won or lost
|
2004-01-08 06:19:31 +01:00
|
|
|
if game.guessed and game.tries > 0:
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.reply('%sYou win! The word was indeed %s' %
|
2004-01-08 06:19:31 +01:00
|
|
|
(game.prefix, game.hidden), prefixName=False)
|
2004-01-07 04:02:03 +01:00
|
|
|
self.endGame(channel)
|
2004-01-08 06:19:31 +01:00
|
|
|
elif not game.guessed and game.tries == 0:
|
2004-01-13 07:43:58 +01:00
|
|
|
irc.reply('%sYou lose! The word was %s' %
|
2004-01-08 06:19:31 +01:00
|
|
|
(game.prefix, game.hidden), prefixName=False)
|
2004-01-07 04:02:03 +01:00
|
|
|
self.endGame(channel)
|
|
|
|
###
|
|
|
|
# END HANGMAN
|
|
|
|
###
|
2004-01-05 22:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
Class = Words
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
### TODO: Write a script to make the database.
|
2004-01-05 22:26:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|