Fixed a bug

This commit is contained in:
Vincent Foley 2004-01-13 06:43:58 +00:00
parent 18c49c9a56
commit 20984842f3
1 changed files with 30 additions and 25 deletions

View File

@ -180,7 +180,7 @@ class Words(callbacks.Privmsg, configurable.Mixin):
raise callbacks.ArgumentError raise callbacks.ArgumentError
for word in args: for word in args:
if word.translate(string.ascii, string.ascii_letters): if word.translate(string.ascii, string.ascii_letters):
irc.error(msg, 'Word must contain only letters.') irc.error('Word must contain only letters.')
return return
else: else:
addWord(self.dbHandler.getDb(), word, commit=True) addWord(self.dbHandler.getDb(), word, commit=True)
@ -196,16 +196,16 @@ class Words(callbacks.Privmsg, configurable.Mixin):
db = self.dbHandler.getDb() db = self.dbHandler.getDb()
cursor = db.cursor() cursor = db.cursor()
if '%' in word: if '%' in word:
irc.error(msg, '"%" isn\'t allowed in the word.') irc.error('"%" isn\'t allowed in the word.')
return return
cursor.execute("""SELECT word FROM words cursor.execute("""SELECT word FROM words
WHERE word LIKE %s WHERE word LIKE %s
ORDER BY word""", word) ORDER BY word""", word)
words = [t[0] for t in cursor.fetchall()] words = [t[0] for t in cursor.fetchall()]
if words: if words:
irc.reply(msg, utils.commaAndify(words)) irc.reply(utils.commaAndify(words))
else: else:
irc.reply(msg, 'No matching words were found.') irc.reply('No matching words were found.')
def anagram(self, irc, msg, args): def anagram(self, irc, msg, args):
"""<word> """<word>
@ -220,16 +220,16 @@ class Words(callbacks.Privmsg, configurable.Mixin):
sorted = ''.join(L) sorted = ''.join(L)
cursor.execute("""SELECT id FROM sorted_words WHERE word=%s""", sorted) cursor.execute("""SELECT id FROM sorted_words WHERE word=%s""", sorted)
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.reply(msg, 'That word has no anagrams I could find.') irc.reply('That word has no anagrams I could find.')
else: else:
id = cursor.fetchone()[0] id = cursor.fetchone()[0]
cursor.execute("""SELECT words.word FROM words cursor.execute("""SELECT words.word FROM words
WHERE sorted_word_id=%s""", id) WHERE sorted_word_id=%s""", id)
if cursor.rowcount > 1: if cursor.rowcount > 1:
words = [t[0] for t in cursor.fetchall()] words = [t[0] for t in cursor.fetchall()]
irc.reply(msg, utils.commaAndify(words)) irc.reply(utils.commaAndify(words))
else: else:
irc.reply(msg, 'That word has no anagrams I could find.') irc.reply('That word has no anagrams I could find.')
### ###
# HANGMAN # HANGMAN
@ -253,11 +253,12 @@ class Words(callbacks.Privmsg, configurable.Mixin):
Returns unused letters Returns unused letters
""" """
channel = msg.args[0] channel = msg.args[0]
game = self.games[channel] if channel in self.games:
if not game.gameOn: game = self.games[channel]
irc.error(msg, 'There is no hangman game going on right now.') if game.gameOn:
return irc.reply('%s %s' % (game.prefix, ' '.join(game.unused)))
irc.reply(msg, '%s %s' % (game.prefix, ' '.join(game.unused))) return
irc.error('There is no hangman game going on right now.')
def hangman(self, irc, msg, args): def hangman(self, irc, msg, args):
"""takes no arguments """takes no arguments
@ -280,7 +281,7 @@ class Words(callbacks.Privmsg, configurable.Mixin):
game.unused = copy.copy(self.validLetters) game.unused = copy.copy(self.validLetters)
game.hidden = game.getWord(self.dbHandler) game.hidden = game.getWord(self.dbHandler)
game.guess = '_' * len(game.hidden) game.guess = '_' * len(game.hidden)
irc.reply(msg, '%sOkay ladies and gentlemen, you have ' irc.reply('%sOkay ladies and gentlemen, you have '
'a %s-letter word to find, you have %s!' % 'a %s-letter word to find, you have %s!' %
(game.prefix, len(game.hidden), (game.prefix, len(game.hidden),
game.triesLeft(game.tries)), prefixName=False) game.triesLeft(game.tries)), prefixName=False)
@ -292,7 +293,7 @@ class Words(callbacks.Privmsg, configurable.Mixin):
self.endGame(channel) self.endGame(channel)
self.hangman(irc, msg, args) self.hangman(irc, msg, args)
else: else:
irc.error(msg, 'Sorry, there is already a game going on. ' irc.error('Sorry, there is already a game going on. '
'%s left before timeout.' % utils.nItems('second', '%s left before timeout.' % utils.nItems('second',
int(game.timeout - secondsEllapsed))) int(game.timeout - secondsEllapsed)))
@ -303,9 +304,13 @@ class Words(callbacks.Privmsg, configurable.Mixin):
the whole word and you are wrong, you automatically lose. the whole word and you are wrong, you automatically lose.
""" """
channel = msg.args[0] channel = msg.args[0]
game = self.games[channel] try:
game = self.games[channel]
except KeyError:
irc.error('There is no hangman game going on right now.')
return
if not game.gameOn: if not game.gameOn:
irc.error(msg, 'There is no hangman game going on right now.') irc.error('There is no hangman game going on right now.')
return return
letter = privmsgs.getArgs(args) letter = privmsgs.getArgs(args)
game.timeGuess = time.time() game.timeGuess = time.time()
@ -313,21 +318,21 @@ class Words(callbacks.Privmsg, configurable.Mixin):
if letter in game.unused: if letter in game.unused:
del game.unused[game.unused.index(letter)] del game.unused[game.unused.index(letter)]
if letter in game.hidden: if letter in game.hidden:
irc.reply(msg, '%sYes, there is %s %s' % (game.prefix, irc.reply('%sYes, there is %s %s' % (game.prefix,
game.letterArticle(letter), `letter`), prefixName=False) game.letterArticle(letter), `letter`), prefixName=False)
game.guess = game.addLetter(letter, game.guess, game.guess = game.addLetter(letter, game.guess,
game.letterPositions(letter, game.hidden)) game.letterPositions(letter, game.hidden))
if game.guess == game.hidden: if game.guess == game.hidden:
game.guessed = True game.guessed = True
else: else:
irc.reply(msg,'%sNo, there is no %s' % (game.prefix,`letter`), irc.reply('%sNo, there is no %s' % (game.prefix,`letter`),
prefixName=False) prefixName=False)
game.tries -= 1 game.tries -= 1
irc.reply(msg, '%s%s (%s left)' % (game.prefix, game.guess, irc.reply('%s%s (%s left)' % (game.prefix, game.guess,
game.triesLeft(game.tries)), prefixName=False) game.triesLeft(game.tries)), prefixName=False)
# User input a valid character that has already been tried # User input a valid character that has already been tried
elif letter in self.validLetters: elif letter in self.validLetters:
irc.error(msg, 'That letter has already been tried.') irc.error('That letter has already been tried.')
# User tries to guess the whole word or entered an invalid input # User tries to guess the whole word or entered an invalid input
else: else:
# The length of the word tried by the user and that of the hidden # The length of the word tried by the user and that of the hidden
@ -337,23 +342,23 @@ class Words(callbacks.Privmsg, configurable.Mixin):
if letter == game.hidden: if letter == game.hidden:
game.guessed = True game.guessed = True
else: else:
irc.reply(msg, '%syou did not guess the correct word ' irc.reply('%syou did not guess the correct word '
'and you lose a try' % game.prefix, prefixName=False) 'and you lose a try' % game.prefix, prefixName=False)
game.tries -= 1 game.tries -= 1
else: else:
# User input an invalid character # User input an invalid character
if len(letter) == 1: if len(letter) == 1:
irc.error(msg, 'That is not a valid character.') irc.error('That is not a valid character.')
# User input an invalid word (len(try) != len(hidden)) # User input an invalid word (len(try) != len(hidden))
else: else:
irc.error(msg, 'That is not a valid word guess.') irc.error('That is not a valid word guess.')
# Verify if the user won or lost # Verify if the user won or lost
if game.guessed and game.tries > 0: if game.guessed and game.tries > 0:
irc.reply(msg, '%sYou win! The word was indeed %s' % irc.reply('%sYou win! The word was indeed %s' %
(game.prefix, game.hidden), prefixName=False) (game.prefix, game.hidden), prefixName=False)
self.endGame(channel) self.endGame(channel)
elif not game.guessed and game.tries == 0: elif not game.guessed and game.tries == 0:
irc.reply(msg, '%sYou lose! The word was %s' % irc.reply('%sYou lose! The word was %s' %
(game.prefix, game.hidden), prefixName=False) (game.prefix, game.hidden), prefixName=False)
self.endGame(channel) self.endGame(channel)
### ###