forked from c0de/BaseballBot
Fix circular import issue
This commit is contained in:
parent
03fcc5ef52
commit
16a419f558
40
GhostBallBot/game/base.py
Normal file
40
GhostBallBot/game/base.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# Copyright 2022 - c0de <c0de@c0de.dev>
|
||||||
|
# Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
# pylint: disable=missing-module-docstring
|
||||||
|
|
||||||
|
from database.models import database, GameModel as Game
|
||||||
|
|
||||||
|
class BaseGameManager:
|
||||||
|
"""Base Game Manager for each Game Manager class to inherit"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
# Only one game should run at at time
|
||||||
|
self.is_running = False
|
||||||
|
|
||||||
|
self.commands = []
|
||||||
|
|
||||||
|
self.game = Game
|
||||||
|
|
||||||
|
# Discord message
|
||||||
|
self.message = None
|
||||||
|
|
||||||
|
# Discord client instance
|
||||||
|
self.discord = None
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
"""
|
||||||
|
Allows use of `with Game() as game` for try/except statements
|
||||||
|
(https://peps.python.org/pep-0343/)
|
||||||
|
"""
|
||||||
|
|
||||||
|
database.connect()
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exception_type, exception_value, exception_traceback):
|
||||||
|
"""
|
||||||
|
Automagically close the database
|
||||||
|
when this class has ended execution
|
||||||
|
"""
|
||||||
|
database.close()
|
@ -7,7 +7,7 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from database.models import GameModel as Game, GuessModel as Guess
|
from database.models import GameModel as Game, GuessModel as Guess
|
||||||
from game.manager import BaseGameManager
|
from game.base import BaseGameManager
|
||||||
from game.process_guess import ProcessGuess
|
from game.process_guess import ProcessGuess
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from database.models import PlayerModel as Player, GuessModel as Guess
|
from database.models import PlayerModel as Player, GuessModel as Guess
|
||||||
from game.manager import BaseGameManager
|
from game.base import BaseGameManager
|
||||||
|
|
||||||
|
|
||||||
class GuessManager(BaseGameManager):
|
class GuessManager(BaseGameManager):
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
# pylint: disable=missing-module-docstring
|
# pylint: disable=missing-module-docstring
|
||||||
|
|
||||||
from game.manager import BaseGameManager
|
from game.base import BaseGameManager
|
||||||
|
|
||||||
|
|
||||||
class HelpManager(BaseGameManager):
|
class HelpManager(BaseGameManager):
|
||||||
|
@ -10,8 +10,6 @@
|
|||||||
Discord channel
|
Discord channel
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from database.models import database, GameModel as Game
|
|
||||||
|
|
||||||
from game.new_game import NewGameManager
|
from game.new_game import NewGameManager
|
||||||
from game.end_game import EndGameManager
|
from game.end_game import EndGameManager
|
||||||
from game.guess import GuessManager
|
from game.guess import GuessManager
|
||||||
@ -19,40 +17,6 @@ from game.points import PointsManager
|
|||||||
from game.help import HelpManager
|
from game.help import HelpManager
|
||||||
|
|
||||||
|
|
||||||
class BaseGameManager:
|
|
||||||
"""Base Game Manager for each Game Manager class to inherit"""
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
# Only one game should run at at time
|
|
||||||
self.is_running = False
|
|
||||||
|
|
||||||
self.commands = []
|
|
||||||
|
|
||||||
self.game = Game
|
|
||||||
|
|
||||||
# Discord message
|
|
||||||
self.message = None
|
|
||||||
|
|
||||||
# Discord client instance
|
|
||||||
self.discord = None
|
|
||||||
|
|
||||||
def __enter__(self):
|
|
||||||
"""
|
|
||||||
Allows use of `with Game() as game` for try/except statements
|
|
||||||
(https://peps.python.org/pep-0343/)
|
|
||||||
"""
|
|
||||||
|
|
||||||
database.connect()
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __exit__(self, exception_type, exception_value, exception_traceback):
|
|
||||||
"""
|
|
||||||
Automagically close the database
|
|
||||||
when this class has ended execution
|
|
||||||
"""
|
|
||||||
database.close()
|
|
||||||
|
|
||||||
|
|
||||||
class GameManager(
|
class GameManager(
|
||||||
NewGameManager, EndGameManager, GuessManager, PointsManager, HelpManager
|
NewGameManager, EndGameManager, GuessManager, PointsManager, HelpManager
|
||||||
):
|
):
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from database.models import GameModel as Game
|
from database.models import GameModel as Game
|
||||||
from game.manager import BaseGameManager
|
from game.base import BaseGameManager
|
||||||
|
|
||||||
|
|
||||||
class NewGameManager(BaseGameManager):
|
class NewGameManager(BaseGameManager):
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
# pylint: disable=not-an-iterable,missing-module-docstring
|
# pylint: disable=not-an-iterable,missing-module-docstring
|
||||||
|
|
||||||
from database.models import PlayerModel as Player
|
from database.models import PlayerModel as Player
|
||||||
from game.manager import BaseGameManager
|
from game.base import BaseGameManager
|
||||||
|
|
||||||
|
|
||||||
class PointsManager(BaseGameManager):
|
class PointsManager(BaseGameManager):
|
||||||
|
Loading…
Reference in New Issue
Block a user