Browse Source

trivia now has reactions to join and leave a trivia game at the starting wait.

tags/v2.2.0
Roxie Gibson 5 years ago
parent
commit
2e9866af1b
1 changed files with 44 additions and 19 deletions
  1. +44
    -19
      roxbot/cogs/trivia.py

+ 44
- 19
roxbot/cogs/trivia.py View File

self.diffs[player_id] = score_to_add self.diffs[player_id] = score_to_add


def sort_leaderboard(self): def sort_leaderboard(self):
return OrderedDict(sorted(self.scores.items(), key=lambda x:x[1], reverse=True))
return OrderedDict(sorted(self.scores.items(), key=lambda x: x[1], reverse=True))


def flush_diffs(self): def flush_diffs(self):
for player in self.diffs: for player in self.diffs:
self.current_question = None self.current_question = None
self.current_question_message = None self.current_question_message = None
self.question_in_progress = False self.question_in_progress = False
self.start_message = None


a_emoji = bot.get_emoji(419572828854026252) or "🇦" a_emoji = bot.get_emoji(419572828854026252) or "🇦"
b_emoji = bot.get_emoji(419572828925329429) or "🇧" b_emoji = bot.get_emoji(419572828925329429) or "🇧"
async def start(self): async def start(self):
self.questions = await self.get_questions(self.length) self.questions = await self.get_questions(self.length)
# TODO: Add a list that shows the current players in the game, then remove messages to join the game as players join to have like a growning list # TODO: Add a list that shows the current players in the game, then remove messages to join the game as players join to have like a growning list
output = "Starting Roxbot Trivia!"
sleep = 0


if not self.solo:
output += " Starting in 20 seconds..."
if self.solo:
embed = discord.Embed(description="Starting Roxbot Trivia!", colour=self.trivia_colour)
sleep = 0
else:
embed = discord.Embed(description="Starting Roxbot Trivia! Starting in 20 seconds...", colour=self.trivia_colour)
embed.description += "\nPress the {} to join, the {} to leave.".format(self.correct_emoji, self.incorrect_emoji)
sleep = 20 sleep = 20
await self.ctx.send(embed=discord.Embed(description=output, colour=self.trivia_colour))

self.start_message = await self.ctx.send(embed=embed)
await self.start_message.add_reaction(self.correct_emoji)
await self.start_message.add_reaction(self.incorrect_emoji)
await asyncio.sleep(sleep) await asyncio.sleep(sleep)
await self.start_message.clear_reactions()


# Checks if there is any players to play the game still # Checks if there is any players to play the game still
if not self.leaderboard.players: if not self.leaderboard.players:


# End of loop, show final leaderboard and winner # End of loop, show final leaderboard and winner


for x, question in enumerate(self.questions):
for index, question in enumerate(self.questions):
self.current_question = question self.current_question = question
timer = 0 timer = 0
message = await self.ctx.send(**question.payload) message = await self.ctx.send(**question.payload)
await message.clear_reactions() await message.clear_reactions()


# Display Correct answer and calculate and display scores. # Display Correct answer and calculate and display scores.
index = question.correct_answer_index
embed = discord.Embed( embed = discord.Embed(
colour=roxbot.EmbedColours.triv_green, colour=roxbot.EmbedColours.triv_green,
description="Correct answer is {} **{}**".format(self.emojis[index], question.correct_answer)
description="Correct answer is {} **{}**".format(self.emojis[question.correct_answer_index], question.correct_answer)
) )
await self.ctx.send(embed=embed) await self.ctx.send(embed=embed)




# Wait for next question, if not last question # Wait for next question, if not last question
wait_time = 3 wait_time = 3
if (x + 1) < len(self.questions):
if (index + 1) < len(self.questions):
embed = discord.Embed(description="Next question in: 3", colour=roxbot.EmbedColours.blue) embed = discord.Embed(description="Next question in: 3", colour=roxbot.EmbedColours.blue)
message = await self.ctx.send(embed=embed) message = await self.ctx.send(embed=embed)
await asyncio.sleep(0.8) await asyncio.sleep(0.8)
embed = discord.Embed(description="Player {} joined the game".format(player.mention), colour=self.trivia_colour) embed = discord.Embed(description="Player {} joined the game".format(player.mention), colour=self.trivia_colour)
return await self.ctx.send(embed=embed) return await self.ctx.send(embed=embed)
else: else:
embed = discord.Embed(description="You have already joined the game. If you want to leave, do `{}trivia leave`".format(self.bot.command_prefix), colour=self.error_colour)
embed = discord.Embed(description="{}. you have already joined the game. If you want to leave, type `{}trivia leave`".format(player, self.bot.command_prefix), colour=self.error_colour)
return await self.ctx.send(embed=embed) return await self.ctx.send(embed=embed)
else: else:
return await self.ctx.send(embed=discord.Embed(description="Game is already in progress.", colour=self.error_colour)) return await self.ctx.send(embed=discord.Embed(description="Game is already in progress.", colour=self.error_colour))
self.games = {} self.games = {}
self.error_colour = roxbot.EmbedColours.dark_red self.error_colour = roxbot.EmbedColours.dark_red
self.trivia_colour = roxbot.EmbedColours.blue self.trivia_colour = roxbot.EmbedColours.blue
self.bot.add_listener(self.game_reaction, "on_reaction_add")


# Discord Events # Discord Events


async def game_reaction(self, reaction, user):
@commands.Cog.listener()
async def on_reaction_add(self, reaction, user):
"""Logic for answering a question""" """Logic for answering a question"""
time = datetime.datetime.now()
channel = reaction.message.channel channel = reaction.message.channel
message = reaction.message


if user == self.bot.user: return if user == self.bot.user: return
if channel.id not in self.games: if channel.id not in self.games:
else: else:
game = self.games[channel.id] game = self.games[channel.id]


accepting_answers = game.question_in_progress and game.current_question_message.id == message.id
if not game.active:
return await self.reaction_game_join_leave(reaction, user)
else:
return await self.reaction_to_question(reaction, user)

async def reaction_game_join_leave(self, reaction, user):
game = self.games[reaction.message.channel.id]
if reaction.emoji == game.correct_emoji:
await game.add_player(user)
elif reaction.emoji == game.incorrect_emoji:
await game.remove_player(user)
await reaction.message.remove_reaction(reaction, user)

async def reaction_to_question(self, reaction, user):
time = datetime.datetime.now()
game = self.games[reaction.message.channel.id]
accepting_answers = game.question_in_progress and game.current_question_message.id == reaction.message.id
user_in_game = bool(user.id in game.leaderboard.players) user_in_game = bool(user.id in game.leaderboard.players)


if user_in_game and accepting_answers: if user_in_game and accepting_answers:
if reaction.emoji in game.emojis: if reaction.emoji in game.emojis:
game.player_answer(user.id, reaction.emoji, time) game.player_answer(user.id, reaction.emoji, time)
else: else:
return await message.remove_reaction(reaction, user)
return await reaction.message.remove_reaction(reaction, user)
else: else:
return await message.remove_reaction(reaction, user)
return await reaction.message.remove_reaction(reaction, user)




# Commands # Commands
@trivia.command() @trivia.command()
async def about(self, ctx): async def about(self, ctx):
"""Displays help in playing Roxbot Trivia. If nothing/an incorrect subcommand is passed to the trivia command, this command is invoked instead.""" """Displays help in playing Roxbot Trivia. If nothing/an incorrect subcommand is passed to the trivia command, this command is invoked instead."""
# TODO: Edit about to include info about the new method of joining
# TODO: Remove mobile stuff as its deprecated
embed = discord.Embed( embed = discord.Embed(
title="About Roxbot Trivia", title="About Roxbot Trivia",
description="Roxbot Trivia is a trivia game in *your* discord server. It's heavily inspired by Tower Unite's Trivia mini-game. Uses the [Open Trivia Database](https://opentdb.com) made by PixelTail Games. To start, just type `{}trivia start`.".format(self.bot.command_prefix), description="Roxbot Trivia is a trivia game in *your* discord server. It's heavily inspired by Tower Unite's Trivia mini-game. Uses the [Open Trivia Database](https://opentdb.com) made by PixelTail Games. To start, just type `{}trivia start`.".format(self.bot.command_prefix),
# Checks if game is in this channel. Then if one isn't active, then if the player has already joined. # Checks if game is in this channel. Then if one isn't active, then if the player has already joined.
if channel.id in self.games: if channel.id in self.games:
await self.games[channel.id].add_player(ctx.author) await self.games[channel.id].add_player(ctx.author)
await ctx.message.delete()
else: else:
return await ctx.send(embed=discord.Embed(description="Game isn't being played here.", colour=self.error_colour))
await ctx.send(embed=discord.Embed(description="Game isn't being played here.", colour=self.error_colour))


@commands.guild_only() @commands.guild_only()
@trivia.command() @trivia.command()
# CANT LEAVE: Game is not active or not in the game # CANT LEAVE: Game is not active or not in the game
if channel.id in self.games: if channel.id in self.games:
await self.games[channel.id].remove_player(player) await self.games[channel.id].remove_player(player)
await ctx.message.delete()
else: else:
await ctx.send(embed=discord.Embed(description="Game isn't being played here.", colour=self.error_colour)) await ctx.send(embed=discord.Embed(description="Game isn't being played here.", colour=self.error_colour))



Loading…
Cancel
Save