Skip to content

Commit

Permalink
Appeased pylint and cleaned up naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Amdrel committed Sep 21, 2019
1 parent 7b288e3 commit 9fb7286
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 165 deletions.
25 changes: 13 additions & 12 deletions cho_commands.py → commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

import logging
import re

import discord
import sql.guild

from cho_utils import cho_command
from utils import cho_command

CMD_START = "start"
CMD_STOP = "stop"
Expand All @@ -36,11 +37,11 @@
LOGGER = logging.getLogger("cho")


class ChoCommandsMixin():
class CommandsMixin():
"""Contains command handler functions for ChoClient."""

@cho_command(CMD_HELP)
async def _handle_help(self, message, args, config):
async def handle_help(self, message, args, config):
"""Responds with help to teach users about the bot's functions.
:param m message:
Expand Down Expand Up @@ -92,7 +93,7 @@ async def _handle_help(self, message, args, config):
await message.channel.send(embed=embed)

@cho_command(CMD_START, kind="channel")
async def _handle_start_command(self, message, args, config):
async def handle_start_command(self, message, args, config):
"""Starts a new game at the request of a user.
:param m message:
Expand All @@ -101,7 +102,7 @@ async def _handle_start_command(self, message, args, config):
:type m: discord.message.Message
"""

if self._is_game_in_progress(message.guild.id):
if self.is_game_in_progress(message.guild.id):
await message.channel.send(
"A game is already active in the trivia channel. If you "
"want to participate please go in there."
Expand All @@ -115,10 +116,10 @@ async def _handle_start_command(self, message, args, config):
await message.channel.send(
"Okay I'm starting a game. Don't expect me to go easy."
)
await self._start_game(message.guild, message.channel)
await self.start_game(message.guild, message.channel)

@cho_command(CMD_STOP, kind="channel")
async def _handle_stop_command(self, message, args, config):
async def handle_stop_command(self, message, args, config):
"""Stops the current game at the request of the user.
:param m message:
Expand All @@ -129,12 +130,12 @@ async def _handle_stop_command(self, message, args, config):

guild_id = message.guild.id

if self._is_game_in_progress(guild_id):
if self.is_game_in_progress(guild_id):
LOGGER.info(
"Stopping game in guild %s, requested by %s",
guild_id, message.author
)
await self._stop_game(guild_id)
await self.stop_game(guild_id)

await message.channel.send(
"I'm stopping the game for now. Maybe we can play another time?"
Expand All @@ -147,7 +148,7 @@ async def _handle_stop_command(self, message, args, config):
)

@cho_command(CMD_SCOREBOARD, kind="channel")
async def _handle_scoreboard_command(self, message, args, config):
async def handle_scoreboard_command(self, message, args, config):
"""Displays a scoreboard at the request of the user.
:param m message:
Expand Down Expand Up @@ -190,7 +191,7 @@ async def _handle_scoreboard_command(self, message, args, config):
"get some scores in the scoreboard.")

@cho_command(CMD_SET_CHANNEL, admin_only=True)
async def _handle_set_channel(self, message, args, config):
async def handle_set_channel(self, message, args, config):
"""Updates the trivia channel configuration for the guild.
:param m message:
Expand Down Expand Up @@ -225,7 +226,7 @@ async def _handle_set_channel(self, message, args, config):
)

@cho_command(CMD_SET_PREFIX, admin_only=True)
async def _handle_set_prefix(self, message, args, config):
async def handle_set_prefix(self, message, args, config):
"""Updates the prefix used for the guild.
:param m message:
Expand Down
3 changes: 2 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

"""Contains helper functions to get configuration data for the worker."""

import logging
import os

import logging

from logging.handlers import RotatingFileHandler


Expand Down
87 changes: 34 additions & 53 deletions cho_game.py → game.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@

from discord.channel import TextChannel
from discord.guild import Guild
from discord.message import Message

import cho_utils
import sql.guild
import sql.scoreboard

Expand All @@ -35,26 +33,16 @@
LOGGER = logging.getLogger("cho")


class ChoGameMixin():
class GameMixin():
"""Adds trivia game logic to ChoClient. Gotta love compartmentalization."""

async def handle_message_response(self, message: Message):
"""Processes a non-command message received during an active game.
def __cleanup_game(self, guild_id: int):
"""Removes the game state of a guild from memory.
:param m message:
:type m: discord.message.Message
:param int guild_id:
"""

guild_id = guild_id = message.guild.id

guild_query_results = sql.guild.get_guild(self.engine, guild_id)
if guild_query_results:
_, config = guild_query_results
else:
config = {}

if cho_utils.is_message_from_trivia_channel(message, config):
await self._process_answer(message)
del self.active_games[guild_id]

async def resume_incomplete_games(self):
"""Resumes all inactive games, usually caused by the bot going down."""
Expand All @@ -65,22 +53,21 @@ async def resume_incomplete_games(self):
"Found %d incomplete games that need to be resumed",
len(incomplete_games))

for guild_id, active_game_dict in incomplete_games:
for guild_id, existing_game in incomplete_games:
existing_game_state = GameState(
self.engine,
guild_id,
active_game_dict=active_game_dict,
existing_game=existing_game,
save_to_db=True)
self.active_games[guild_id] = existing_game_state

guild = self.get_guild(guild_id)

if guild:
channel = guild.get_channel(existing_game_state.channel_id)
asyncio.ensure_future(
self._ask_question(channel, existing_game_state))
self.ask_question(channel, existing_game_state))

async def _start_game(self, guild: Guild, channel: TextChannel):
async def start_game(self, guild: Guild, channel: TextChannel):
"""Starts a new trivia game.
:param g guild:
Expand All @@ -89,30 +76,30 @@ async def _start_game(self, guild: Guild, channel: TextChannel):
:type c: discord.channel.TextChannel
"""

new_game = self._create_game(guild.id, channel.id)
new_game = self.create_game(guild.id, channel.id)

await asyncio.sleep(SHORT_WAIT_SECS)
await self._ask_question(channel, new_game)
await self.ask_question(channel, new_game)

async def _stop_game(self, guild_id: int):
async def stop_game(self, guild_id: int):
"""Stops a game in progress for a guild.
:param int guild_id:
"""

game_state = self._get_game(guild_id)
game_state = self.get_game(guild_id)
game_state.stop_game()

self._cleanup_game(guild_id)
self.__cleanup_game(guild_id)

async def _process_answer(self, message):
async def process_answer(self, message):
"""Called when an answer is received from a user.
:param m message:
:type m: discord.message.Message
"""

game_state = self._get_game(message.guild.id)
game_state = self.get_game(message.guild.id)

# Don't process the answer if the bot is currently in-between asking
# questions. Without this multiple people can get the answer right
Expand All @@ -138,11 +125,11 @@ async def _process_answer(self, message):
),
)
await asyncio.sleep(SHORT_WAIT_SECS)
await self._ask_question(message.channel, game_state)
await self.ask_question(message.channel, game_state)
else:
LOGGER.debug("Incorrect answer received: %s", message.content)

async def _ask_question(self, channel, game_state):
async def ask_question(self, channel, game_state):
"""Asks a trivia question in a Discord channel.
:param c channel:
Expand All @@ -158,22 +145,22 @@ async def _ask_question(self, channel, game_state):
# This check also covers the rare edge-case where a game is stopped and
# started again within the 10 second window between questions so that
# the trivia game doesn't duplicate itself.
if not self._is_same_game_in_progress(guild_id, game_state):
if not self.is_same_game_in_progress(guild_id, game_state):
return

if game_state.complete:
await self._finish_game(channel, game_state)
await self.complete_game(channel, game_state)
return

question = game_state.get_question()
last_correct_answers_total = game_state.correct_answers_total

game_state.waiting = True

await channel.send(question["text"])
await asyncio.sleep(LONG_WAIT_SECS)

# Check again as it can happen here too.
if not self._is_same_game_in_progress(guild_id, game_state):
if not self.is_same_game_in_progress(guild_id, game_state):
return

# If the correct answer total was not incrememnted, that means that no
Expand All @@ -188,9 +175,9 @@ async def _ask_question(self, channel, game_state):
),
)
await asyncio.sleep(SHORT_WAIT_SECS)
await self._ask_question(channel, game_state)
await self.ask_question(channel, game_state)

async def _finish_game(self, channel, game_state):
async def complete_game(self, channel, game_state):
"""Outputs the scoreboard and announces the winner of a game.
:param c channel:
Expand All @@ -199,14 +186,14 @@ async def _finish_game(self, channel, game_state):
"""

guild_id = channel.guild.id
self._cleanup_game(guild_id)
self.__cleanup_game(guild_id)

score_fmt = "{emoji} <@!{user_id}> - {score} point{suffix}\n"
scores = list(game_state.scores.items())

# Don't bother making a scoreboard if it's going to be empty. It's
# better to make fun of everyone for being so bad at the game instead!
if len(scores) == 0:
if not scores:
await channel.send(
"Well it appears no one won because no one answered a "
"*single* question right. You people really don't know much "
Expand Down Expand Up @@ -260,15 +247,7 @@ async def _finish_game(self, channel, game_state):
.format(str(ties + 1), scoreboard)
)

def _cleanup_game(self, guild_id: int):
"""Removes the game state of a guild from memory.
:param int guild_id:
"""

del self.active_games[guild_id]

def _get_game(self, guild_id: int) -> GameState:
def get_game(self, guild_id: int) -> GameState:
"""Retrieves a guild's game state from memory.
:param int guild_id:
Expand All @@ -278,7 +257,7 @@ def _get_game(self, guild_id: int) -> GameState:

return self.active_games[guild_id]

def _create_game(self, guild_id: int, channel_id: int) -> GameState:
def create_game(self, guild_id: int, channel_id: int) -> GameState:
"""Creates a new game state.
:param int guild_id:
Expand All @@ -292,10 +271,12 @@ def _create_game(self, guild_id: int, channel_id: int) -> GameState:
guild_id,
channel_id=channel_id,
save_to_db=True)

self.active_games[guild_id] = new_game

return new_game

def _is_game_in_progress(self, guild_id: int) -> bool:
def is_game_in_progress(self, guild_id: int) -> bool:
"""Checks if a game is in progress for a guild.
:param int guild_id:
Expand All @@ -305,7 +286,7 @@ def _is_game_in_progress(self, guild_id: int) -> bool:

return guild_id in self.active_games

def _is_same_game_in_progress(
def is_same_game_in_progress(
self,
guild_id: int,
game_state: GameState) -> bool:
Expand All @@ -318,6 +299,6 @@ def _is_same_game_in_progress(
"""

return (
self._is_game_in_progress(guild_id)
and self._get_game(guild_id).uuid == game_state.uuid
self.is_game_in_progress(guild_id)
and self.get_game(guild_id).uuid == game_state.uuid
)
Loading

0 comments on commit 9fb7286

Please sign in to comment.