Jump to content

Simplify scoring for a database where there can be two users

KeyboardShortcuts

So I'm creating a bot in discord.py that sends a trivia question and a user has 15 seconds to respond but it times out. The code for the trivia question is done, however, the scoring is very messy. Is there any way I can clean this up?

def updateTriviaScores(self, ctx, correctOption, guess):
    try:
        orgUser = list(self.cursor.execute(f"SELECT * FROM triviaScores WHERE guildID == {ctx.guild.id} and userID == {ctx.author.id}"))[0]
    except IndexError:
        # User not in database
        orgUser = (ctx.guild.id, ctx.author.id, 0, 0, 0, 0)
        self.cursor.execute(f"INSERT INTO triviaScores values{orgUser}")
    orgUser = list(orgUser)
    if guess is None:
        # No answer
        orgUser[2] -= 2
        orgUser[4] += 2
    else:
        try:
            guessUser = list(self.cursor.execute(f"SELECT * FROM triviaScores WHERE guildID == {ctx.guild.id} and userID == {guess[1].id}"))[0]
        except IndexError:
            # User not in database
            guessUser = (ctx.guild.id, guess[1].id, 0, 0, 0, 0)
            self.cursor.execute(f"INSERT INTO triviaScores values{guessUser}")
        guessUser = list(guessUser)
        originalAuthor = ctx.author.id
        guessAuthor = guess[1].id
        try:
            if self.triviaReactions[str(guess[0])] == correctOption:
                # Question correct
                if originalAuthor == guessAuthor:
                    # No steal
                    orgUser[2] += 2
                    orgUser[3] += 2
                else:
                    # Steal
                    orgUser[2] += 1
                    orgUser[3] += 1
                    guessUser[2] += 1
                    guessUser[3] += 1
            else:
                # Question incorrect
                if originalAuthor == guessAuthor:
                    # No steal
                    orgUser[2] -= 2
                    orgUser[4] += 2
                else:
                    # Steal
                    orgUser[2] -= 1
                    orgUser[4] += 1
                    guessUser[2] -= 1
                    guessUser[4] += 1
        except KeyError:
            # Unknown emoji
            if originalAuthor == guessAuthor:
                # No steal
                orgUser[2] -= 2
                orgUser[4] += 2
            else:
                # Steal
                orgUser[2] -= 1
                orgUser[4] += 1
                guessUser[2] -= 1
                guessUser[4] += 1
        self.cursor.execute(f"UPDATE triviaScores SET score = {guessUser[2]}, pointsGained = {guessUser[3]}, pointsLost = {guessUser[4]} WHERE guildID == {ctx.guild.id} AND userID == {guessAuthor}")
    self.cursor.execute(f"UPDATE triviaScores SET score = {orgUser[2]}, pointsGained = {orgUser[3]}, pointsLost = {orgUser[4]} WHERE guildID == {ctx.guild.id} AND userID == {ctx.author.id}")
    # Update ranks
    self.updateRanks(ctx.guild.id)

So the scoring is as follows:

  • If a user doesn't answer within the timeframe = -2 points for the original author.
  • If a user sends the wrong emoji and that user is the original author = -2 points for the original author.
  • If a user sends the wrong emoji and that user is different from the original author = -1 point for the original author and the stealer.
  • If a user sends the correct response and that user is the original author = +2 points for the original author.
  • If a user sends the correct response and that user is different from the original author = +1 point for the original author and the stealer.
  • If a user sends the incorrect response and that user is the original author = -2 points for the original author.
  • If a user sends the incorrect response and that user is different from the original author = -1 point for the original author and the stealer.

Thanks!

 

Link to comment
Share on other sites

Link to post
Share on other sites

Some of the terms in your question aren't clear to me and the use of arrays rather than objects with properties makes your code hard to follow. For example:

Quote

If a user doesn't answer within the timeframe = -2 points for the original author.

Who does "original author" refer to? The person answering the question (i.e "the user") or the person asking the question? Is losing points a good thing or a bad thing? E.g. does this mean: "If a user answers a question, but the answer was not given within the timeframe, that user loses two points"?

 

Quote

If a user sends the wrong emoji and that user is the original author = -2 points for the original author.

How do emojis work in this context?

 

Quote

If a user sends the wrong emoji and that user is different from the original author = -1 point for the original author and the stealer.

Who does "stealer" refer to in this context? I assume people have the ability to react to answers (marking them as correct/incorrect) and if their reaction is right/wrong they can steal points from the person posting the answer?

 

You're using terms in your question without explaining what they mean/who they refer to which makes it hard to follow what is being asked. In the code itself you're using arrays to refer to different user properties e.g.

orgUser[2] -= 2
orgUser[4] += 2

This makes it hard to infer what is gaining points or losing them. Using objects with properties rather than arrays would make it easier to follow your code. If you could clarify the terms and use more descriptive names for your variables it might help get some answers.

 

The first step to cleaning up the code would be to separate the game logic from the database code. Move that into separate methods. One method to get the user (and create it if needed) and one method to award points. The second step would be to use more descriptive names. The more you have to think about "what was user[3] again?" the less time you have to think about the logic and how to improve it.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×