From 8f3e86a0765e4a5de798375c35ded0e3baf4e816 Mon Sep 17 00:00:00 2001 From: Yizhe Shen Date: Sun, 8 Feb 2015 21:09:24 -0500 Subject: [PATCH] More code cleanup. - Changed objects named 'OtherHint' to 'ExtraHint' - Modified code for Game.getHintString() --- plugin.py | 133 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 73 insertions(+), 60 deletions(-) diff --git a/plugin.py b/plugin.py index a41a036..4fb55b7 100644 --- a/plugin.py +++ b/plugin.py @@ -141,7 +141,7 @@ class Game: points /= 2 * (self.hintsCounter - 1) # Handle a correct answer for a KAOS question - if self.questionType = 'kaos': + if self.questionType == 'kaos': if usernameCanonical not in self.correctPlayers: self.correctPlayers[usernameCanonical] = 0 self.correctPlayers[usernameCanonical] += 1 @@ -174,18 +174,6 @@ class Game: self.sendMessage('Total Awarded: \x02%d\x02 Points to \x02%d\x02 Players' % (int(self.totalAmountWon), len(self.correctPlayers))) threadStorage.updateQuestionStats(self.questionID, 1, 0) - self.removeEvent() - - # Check for any pending stops, otherwise queue next question - if self.stopPending == True: - self.stop() - else: - waitTime = self.registryValue('general.waitTime', self.channel) - if waitTime < 2: - waitTime = 2 - log.error('waitTime was set too low (<2 seconds). Setting to 2 seconds') - waitTime = time.time() + waitTime - self.queueEvent(waitTime, self.nextQuestion) # Handle a correct answer for a regular question else: @@ -203,7 +191,7 @@ class Game: else: self.streak += 1 streakBonus = points * .01 * (self.streak-1) - streakBonus = min(steakBonus, points * .5) + streakBonus = min(streakBonus, points * .5) # Update database threadStorage.updateGameStreak(self.channel, self.lastWinner, self.streak) @@ -238,7 +226,8 @@ class Game: recapMessageList.append(' this MONTH: \x02%d\x02' % (monthScore)) recapMessage = ''.join(recapMessageList) self.sendMessage(recapMessage) - + + if self.questionOver: self.removeEvent() # Check for any pending stops, otherwise queue next question @@ -256,58 +245,83 @@ class Game: if hintNum == None: hintNum = self.hintsCounter hintRatio = self.registryValue('hints.hintRatio') # % to show each hint - hints = '' + hint = '' ratio = float(hintRatio * .01) charMask = self.registryValue('hints.charMask', self.channel) # create a string with hints for all of the answers - for ans in self.answers: - if ircutils.toLower(ans) in self.guessedAnswers: - continue - ans = unicode(ans.decode('utf-8')) - if hints != '': - hints += ' ' - if len(self.answers) > 1: - hints += '[' + if self.questionType == 'kaos': + for ans in self.answers: + if ircutils.toLower(ans) not in self.guessedAnswers: + ans = unicode(ans.decode('utf-8')) + hintStr = '' + if hintNum == 0: + for char in ans: + if char in self.unmaskedChars: + hintStr += char + else: + hintStr += charMask + elif hintNum == 1: + divider = int(len(ans) * ratio) + divider = min(divider, 3) + divider = min(divider, len(ans)-1) + hintStr += ans[:divider] + masked = ans[divider:] + for char in masked: + if char in self.unmaskedChars: + hintStr += char + else: + hintStr += charMask + elif hintNum == 2: + divider = int(len(ans) * ratio) + divider = min(divider, 3) + divider = min(divider, len(ans)-1) + lettersInARow = divider-1 + maskedInARow = 0 + hintStr += ans[:divider] + ansend = ans[divider:] + hintsend = '' + unmasked = 0 + if self.registryValue('hints.vowelsHint', self.channel): + hintStr += self.getMaskedVowels(ansend, divider-1) + else: + hintStr += self.getMaskedRandom(ansend, divider-1) + hint += ' [{0}]'.format(hintStr) + else: + ans = unicode(self.answers[0].decode('utf-8')) if hintNum == 0: - masked = ans - for i in range(len(masked)): - if masked[i] in self.unmaskedChars: - hints+= masked[i] + for char in ans: + if char in self.unmaskedChars: + hint += char else: - hints += charMask + hint += charMask elif hintNum == 1: divider = int(len(ans) * ratio) - if divider > 3: - divider = 3 - if divider >= len(ans): - divider = len(ans)-1 - hints += ans[:divider] + divider = min(divider, 3) + divider = min(divider, len(ans)-1) + hint += ans[:divider] masked = ans[divider:] - for i in range(len(masked)): - if masked[i] in self.unmaskedChars: - hints+= masked[i] + for char in masked: + if char in self.unmaskedChars: + hint += char else: - hints += charMask + hint += charMask elif hintNum == 2: divider = int(len(ans) * ratio) - if divider > 3: - divider = 3 - if divider >= len(ans): - divider = len(ans)-1 - lettersInARow=divider-1 - maskedInARow=0 - hints += ans[:divider] + divider = min(divider, 3) + divider = min(divider, len(ans)-1) + lettersInARow = divider-1 + maskedInARow = 0 + hint += ans[:divider] ansend = ans[divider:] hintsend = '' unmasked = 0 if self.registryValue('hints.vowelsHint', self.channel): - hints+= self.getMaskedVowels(ansend, divider-1) + hint += self.getMaskedVowels(ansend, divider-1) else: - hints+= self.getMaskedRandom(ansend, divider-1) - if len(self.answers) > 1: - hints += ']' - return hints.encode('utf-8') + hint += self.getMaskedRandom(ansend, divider-1) + + return hint.strip().encode('utf-8') def getMaskedVowels(self, letters, sizeOfUnmasked): charMask = self.registryValue('hints.charMask', self.channel) @@ -356,9 +370,9 @@ class Game: hints += charMask return hints - def getOtherHintString(self): + def getExtraHintString(self): charMask = self.registryValue('hints.charMask', self.channel) - if len(self.answers) > 1 or len(self.answers) < 1: + if self.questionType == 'kaos': return ans = self.answers[0] @@ -386,18 +400,17 @@ class Game: return hints - def getOtherHint(self): + def getExtraHint(self): if self.questionOver: return if self.shownHint == False: self.shownHint = True - if len(self.answers) == 1: - self.sendMessage(self.getOtherHintString()) + self.sendMessage(self.getExtraHintString()) def getRemainingKAOS(self): if self.questionOver: return - if len(self.answers) > 1: + if self.questionType == 'kaos': if self.shownHint == False: self.shownHint = True self.sendMessage('\x02\x0312%s' % (self.getHintString(self.hintsCounter-1))) @@ -436,7 +449,7 @@ class Game: # Check for any pending stops, otherwise queue next question if self.stopPending == True: self.stop() - else + else: waitTime = self.registryValue('general.waitTime',self.channel) if waitTime < 2: waitTime = 2 @@ -2755,13 +2768,13 @@ class TriviaTime(callbacks.Plugin): return channelCanonical = ircutils.toLower(channel) - otherHintCommand = self.registryValue('commands.extraHint', channel) + extraHintCommand = self.registryValue('commands.extraHint', channel) extraHintTime = self.registryValue('hints.extraHintTime', channel) game = self.getGame(irc, channel) if game is not None: # Check for extra hint command - if msg.args[1] == otherHintCommand: + if msg.args[1] == extraHintCommand: if game.question.find("KAOS:") == 0: game.getRemainingKAOS() else: @@ -2772,7 +2785,7 @@ class TriviaTime(callbacks.Plugin): self.reply(irc, msg, 'You must wait %d seconds to be able to use the extra hint command.' % (game.hintTimeoutList.getTimeLeft(usernameCanonical)), notice=True) else: game.hintTimeoutList.append(usernameCanonical) - game.getOtherHint() + game.getExtraHint() else: # check the answer game.checkAnswer(msg)