Use abstract base classes in backend base class and factory.
This commit is contained in:
parent
50216b1d45
commit
73f340947b
@ -1,6 +1,7 @@
|
|||||||
from abc import ABC
|
from abc import ABC, abstractmethod
|
||||||
import traceback
|
import traceback
|
||||||
from typing import Any, Dict, Iterator, List, Optional, Set, Tuple, Type
|
from typing import Any, Dict, Iterator, List, Optional, Set, Tuple, Type
|
||||||
|
from typing_extensions import Final
|
||||||
|
|
||||||
from bemani.common import Model, ValidatedDict, Profile, PlayStatistics, GameConstants, Time
|
from bemani.common import Model, ValidatedDict, Profile, PlayStatistics, GameConstants, Time
|
||||||
from bemani.data import Config, Data, Machine, UserID, RemoteUser
|
from bemani.data import Config, Data, Machine, UserID, RemoteUser
|
||||||
@ -14,14 +15,14 @@ class Status:
|
|||||||
"""
|
"""
|
||||||
List of statuses we return to the game for various reasons.
|
List of statuses we return to the game for various reasons.
|
||||||
"""
|
"""
|
||||||
SUCCESS = 0
|
SUCCESS: Final[int] = 0
|
||||||
NO_PROFILE = 109
|
NO_PROFILE: Final[int] = 109
|
||||||
NOT_ALLOWED = 110
|
NOT_ALLOWED: Final[int] = 110
|
||||||
NOT_REGISTERED = 112
|
NOT_REGISTERED: Final[int] = 112
|
||||||
INVALID_PIN = 116
|
INVALID_PIN: Final[int] = 116
|
||||||
|
|
||||||
|
|
||||||
class Factory:
|
class Factory(ABC):
|
||||||
"""
|
"""
|
||||||
The base class every game factory inherits from. Defines a create method
|
The base class every game factory inherits from. Defines a create method
|
||||||
which should return some game class which can handle packets. Game classes
|
which should return some game class which can handle packets. Game classes
|
||||||
@ -29,16 +30,17 @@ class Factory:
|
|||||||
Dispatch will look up in order to handle calls.
|
Dispatch will look up in order to handle calls.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
MANAGED_CLASSES: List[Type["Base"]] = []
|
MANAGED_CLASSES: List[Type["Base"]]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@abstractmethod
|
||||||
def register_all(cls) -> None:
|
def register_all(cls) -> None:
|
||||||
"""
|
"""
|
||||||
Subclasses of this class should use this function to register themselves
|
Subclasses of this class should use this function to register themselves
|
||||||
with Base, using Base.register(). Factories specify the game code that
|
with Base, using Base.register(). Factories specify the game code that
|
||||||
they support, which Base will use when routing requests.
|
they support, which Base will use when routing requests.
|
||||||
"""
|
"""
|
||||||
raise Exception('Override this in subclass!')
|
raise NotImplementedError('Override this in subclass!')
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def run_scheduled_work(cls, data: Data, config: Config) -> None:
|
def run_scheduled_work(cls, data: Data, config: Config) -> None:
|
||||||
@ -84,6 +86,7 @@ class Factory:
|
|||||||
yield (game.game, game.version, game.get_settings())
|
yield (game.game, game.version, game.get_settings())
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@abstractmethod
|
||||||
def create(cls, data: Data, config: Config, model: Model, parentmodel: Optional[Model]=None) -> Optional['Base']:
|
def create(cls, data: Data, config: Config, model: Model, parentmodel: Optional[Model]=None) -> Optional['Base']:
|
||||||
"""
|
"""
|
||||||
Given a modelstring and an optional parent model, return an instantiated game class that can handle a packet.
|
Given a modelstring and an optional parent model, return an instantiated game class that can handle a packet.
|
||||||
@ -102,7 +105,7 @@ class Factory:
|
|||||||
A subclass of Base that hopefully has a handle_<call>_request method on it, for the particular
|
A subclass of Base that hopefully has a handle_<call>_request method on it, for the particular
|
||||||
call that Dispatch wants to resolve, or None if we can't look up a game.
|
call that Dispatch wants to resolve, or None if we can't look up a game.
|
||||||
"""
|
"""
|
||||||
raise Exception('Override this in subclass!')
|
raise NotImplementedError('Override this in subclass!')
|
||||||
|
|
||||||
|
|
||||||
class Base(ABC):
|
class Base(ABC):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user