Exemplo n.º 1
0
class MMGameClient:
    # Thread ID
    tid = -1
    # Player details
    elo = 0

    # Game Counters
    #  -3 - +3
    gameScoreLimit = 3
    gameScore = 0

    gameRound = 0

    # General Alive
    alive = False
    # Ready to start next round
    ready = False
    roundEvaluation = False
    # Answer will update
    answerCorrect = False

    # Detect round end
    myTimeStart = 0
    myTimeEnd = 0

    # MM
    mmEnemy = None

    # Determine leading node
    leadingNode = False
    questionsGenerated = False
    answeringQuestions = False

    # Question details
    qid = -1
    qMLink = ""
    qALink = ""
    qBLink = ""
    qCLink = ""
    qDLink = ""
    qELink = ""
    qFLink = ""
    qCorrect = -1

    def __init__(self, pelo, tid):
        self.tid = tid
        self.elo = pelo
        self.alive = True
        self.dbG = ServiceDBGame()
        self.gameID = str(random.getrandbits(120))
        self.qA = ""
        self.qB = ""
        self.qC = ""
        self.qD = ""
        self.qE = ""
        self.qF = ""
        self.qM = ""
        self.UserSelection = -1

    def setUserID(self, i):
        self.userID = i

    def startRound(self):
        self.single = False
        if (
            self.checkIfBothReadyForRound()
            and self.alive
            and self.ready
            and self.mmEnemy.alive
            and self.mmEnemy.ready
            and abs(self.gameScore) < self.gameScoreLimit
        ):
            if self.leadingNode:
                self.roundEvaluation = False
            else:
                self.roundEvaluation = False

    def startSingleRound(self, cat):
        self.alive = True
        self.ready = True
        self.single = True
        self.leadingNode = True
        self.category = cat
        # Generate links
        self.dbQ = ServiceDBQuestions()
        self.generateLinks()

    def generateLinks(self):
        # Generate questions
        self.questionsGenerated = True
        (
            self.qid,
            self.qMLink,
            self.qALink,
            self.qBLink,
            self.qCLink,
            self.qDLink,
            self.qELink,
            self.qFLink,
            self.qCorrect,
        ) = self.dbQ.retrieveQuestionMath()
        self.qA = urllib2.urlopen("http://127.0.0.1/generateLink.php?file=" + self.qALink).read().strip()
        self.qB = urllib2.urlopen("http://127.0.0.1/generateLink.php?file=" + self.qBLink).read().strip()
        self.qC = urllib2.urlopen("http://127.0.0.1/generateLink.php?file=" + self.qCLink).read().strip()
        self.qD = urllib2.urlopen("http://127.0.0.1/generateLink.php?file=" + self.qDLink).read().strip()
        self.qE = urllib2.urlopen("http://127.0.0.1/generateLink.php?file=" + self.qELink).read().strip()
        self.qF = urllib2.urlopen("http://127.0.0.1/generateLink.php?file=" + self.qFLink).read().strip()
        self.qM = urllib2.urlopen("http://127.0.0.1/generateLink.php?file=" + self.qMLink).read().strip()
        print self.qA

    def logTimeLastLinkPosted(self):
        self.myTimeStart = time.time() * 1000

    def getLink(self, i):
        if (self.single or self.checkIfBothReadyForRound()) and self.questionsGenerated:
            if i == "A":
                return self.qA
            elif i == "B":
                return self.qB
            elif i == "C":
                return self.qC
            elif i == "D":
                return self.qD
            elif i == "E":
                return self.qE
            elif i == "F":
                return self.qF
            elif i == "G":
                self.questionsGenerated = False
                self.answeringQuestions = True
                return self.qM

    def endRound(self, correctAnswer):
        if self.alive and self.ready:
            # Stop answer received
            self.ready = False
            self.gameRound = self.gameRound + 1
            self.myTimeEnd = (time.time() * 1000) - self.myTimeStart
            self.answeringQuestions = False
            if self.single:
                if int(correctAnswer) == int(self.qCorrect):
                    self.gameScore = self.gameScore + 1
                else:
                    self.gameScore = self.gameScore - 1
                # If happy -> continue
                self.generateLinks()
                self.ready = True
                self.answeringQuestions = True
            else:
                if self.leadingNode:
                    if int(correctAnswer) == int(self.qCorrect):
                        self.answerCorrect = True
                    else:
                        self.answerCorrect = False
                else:
                    if int(correctAnswer) == int(self.mmEnemy.qCorrect):
                        self.answerCorrect = True
                    else:
                        self.answerCorrect = False

    # Call to check if enemy is done too, initiate a break, determine if game is over
    def update(self):
        if self.isEnemyOn() and self.alive and self.leadingNode:
            if self.mmEnemy.roundEvaluation and self.roundEvaluation:
                self.roundEvaluation = False
                self.mmEnemy.roundEvaluation = False
                if self.answerCorrect and self.mmEnemy.answerCorrect:
                    # Determine shortest time if both correct
                    if self.myTimeEnd < self.mmEnemy.myTimeEnd:
                        self.gameScore = self.gameScore + 1
                    else:
                        self.gameScore = self.gameScore - 1
                elif self.answerCorrect:
                    # im correct
                    self.gameScore = self.gameScore + 1
                elif self.mmEnemy.answerCorrect:
                    self.gameScore = self.gameScore - 1

                self.mmEnemy.gameScore = -self.gameScore
                # Increase game round
                # Check if game over
                if abs(self.getScore()) >= self.gameScoreLimit:
                    # Destroy game object
                    # Update states
                    self.alive = False
                    self.mmEnemy.alive = False
                    # Purge downloads table
                    self.dbQ.purgeDownloads()
                    return "Terminate"
                else:
                    self.dbG.logRound(
                        self.gameID,
                        self.gameRound,
                        self.userID,
                        self.UserSelection,
                        self.answerCorrect,
                        self.myTimeEnd,
                        self.qid,
                    )
            return "Pong"
        elif not self.alive:
            self.alive = False
            return "Terminate"

    def finalOutcomeUpdate(self, wins, games, knockouts, gamesRemaining):
        if self.getScore() > 0:
            # Rank gain
            self.elo = self.elo + 5
            wins = wins + 1
            if self.gameRound <= 3:
                knockouts = knockouts + 1
        games = games + 1
        gamesRemaining = gamesRemaining - 1
        self.dbM.updateUserGames(self.userID, wins, games, knockouts, self.elo, gamesRemaining)

    def prepareForGame(self, enemy):
        self.mmEnemy = enemy
        self.gameRound = 0
        if self.leadingNode:
            self.gameScore = 0

    def getScore(self):
        # Update game score if all finished
        return self.gameScore

    def checkIfEnemyLive(self):
        if self.isEnemyOn() and self.mmEnemy.ready:
            return True
        else:
            return False

    def checkIfBothReadyForRound(self):
        # Return if both ready for next round
        if (
            self.isEnemyOn()
            and self.mmEnemy.ready
            and self.ready
            and self.alive
            and self.gameRound == self.mmEnemy.gameRound
            and self.questionsGenerated
        ):
            return True
        else:
            return False

    def markAsReady(self):
        self.ready = True
        if (
            self.leadingNode
            and not self.questionsGenerated
            and not self.answeringQuestions
            and not self.mmEnemy.answeringQuestions
        ):
            self.questionsGenerated = True
            self.mmEnemy.questionsGenerated = True
            # Generate links
            self.dbQ = ServiceDBQuestions()
            (
                self.qid,
                self.qMLink,
                self.qALink,
                self.qBLink,
                self.qCLink,
                self.qDLink,
                self.qELink,
                self.qFLink,
                self.qCorrect,
            ) = self.dbQ.retrieveQuestionMath()
            # Set temporary links
            self.qA = urllib2.urlopen("http://127.0.0.1/generateLink.php?file=" + self.qALink).read().strip()
            self.qB = urllib2.urlopen("http://127.0.0.1/generateLink.php?file=" + self.qBLink).read().strip()
            self.qC = urllib2.urlopen("http://127.0.0.1/generateLink.php?file=" + self.qCLink).read().strip()
            self.qD = urllib2.urlopen("http://127.0.0.1/generateLink.php?file=" + self.qDLink).read().strip()
            self.qE = urllib2.urlopen("http://127.0.0.1/generateLink.php?file=" + self.qELink).read().strip()
            self.qF = urllib2.urlopen("http://127.0.0.1/generateLink.php?file=" + self.qFLink).read().strip()
            self.qM = urllib2.urlopen("http://127.0.0.1/generateLink.php?file=" + self.qMLink).read().strip()
            # Enemey
            self.mmEnemy.qA = self.qA
            self.mmEnemy.qB = self.qB
            self.mmEnemy.qC = self.qC
            self.mmEnemy.qD = self.qD
            self.mmEnemy.qE = self.qE
            self.mmEnemy.qF = self.qF
            self.mmEnemy.qM = self.qM

    def clientDC(self):
        self.alive = False
        if self.mmEnemy is not None:
            self.mmEnemy.alive = False

    def isEnemyOn(self):
        if self.mmEnemy is not None and self.mmEnemy.alive:
            return True
        else:
            return False

    def __del__(self):
        self.dbQ.purgeUnusedDownloads(self.qA)
        self.dbQ.purgeUnusedDownloads(self.qB)
        self.dbQ.purgeUnusedDownloads(self.qC)
        self.dbQ.purgeUnusedDownloads(self.qD)
        self.dbQ.purgeUnusedDownloads(self.qE)
        self.dbQ.purgeUnusedDownloads(self.qF)
        self.dbQ.purgeUnusedDownloads(self.qM)
        # Purge expired
        self.dbQ.purgeExpiredRecords()
 (elo,knockouts,games,wins,gamesRemaining,supporterIs,supporterIcon,specialIcon,levelsMath,levelsLogic,levelsDifferences) = dbM.getUserDetails(uidS)
 print elo
 print knockouts 
 print games   
 print wins 
 print gamesRemaining
 print supporterIs
 print supporterIcon
 print specialIcon
 print levelsMath
 print levelsLogic
 print levelsDifferences
 
 
 dbQ = ServiceDBQuestions()
 (questionID,questionLink,AnswerALink,AnswerBLink,AnswerCLink,AnswerDLink,AnswerELink,AnswerFLink,CorrectAnswer) = dbQ.retrieveQuestionMath()
 print questionID
 print questionLink
 print AnswerALink
 print AnswerBLink
 print AnswerCLink
 print AnswerDLink
 print AnswerELink
 print AnswerFLink
 print CorrectAnswer
 
 
 #after game update
 wins = 11
 games = 12
 knockouts = 13