From e90ad53ee6af6587a9d7f8b3dbef84ad0950af4a Mon Sep 17 00:00:00 2001 From: scdhh Date: Wed, 15 Nov 2023 11:37:12 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=E5=9C=A8?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E5=93=8D=E5=BA=94=E5=99=A8=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E9=80=80=E5=87=BA=E5=90=8E=20Recorder=20=E7=BB=A7=E7=BB=AD?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../game_data_processor/__init__.py | 3 +++ nonebot_plugin_tetris_stats/utils/recorder.py | 27 ++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/nonebot_plugin_tetris_stats/game_data_processor/__init__.py b/nonebot_plugin_tetris_stats/game_data_processor/__init__.py index 9305699..cae4ff2 100644 --- a/nonebot_plugin_tetris_stats/game_data_processor/__init__.py +++ b/nonebot_plugin_tetris_stats/game_data_processor/__init__.py @@ -70,6 +70,9 @@ class Processor(ABC): def __del__(self) -> None: finish_time = datetime.now(tz=UTC) + if Recorder.is_error_event(self.event_id): + Recorder.del_error_event(self.event_id) + return historical_data = Recorder.get_historical_data(self.event_id) historical_data.game_platform = self.game_platform historical_data.command_type = self.command_type diff --git a/nonebot_plugin_tetris_stats/utils/recorder.py b/nonebot_plugin_tetris_stats/utils/recorder.py index c991d40..512b8ac 100644 --- a/nonebot_plugin_tetris_stats/utils/recorder.py +++ b/nonebot_plugin_tetris_stats/utils/recorder.py @@ -15,6 +15,7 @@ driver = get_driver() class Recorder: matchers: ClassVar[set[type[Matcher]]] = set() historical_data: ClassVar[dict[int, tuple[HistoricalData, bool]]] = {} + error_event: ClassVar[set[int]] = set() @classmethod def create_historical_data(cls, event_id: int, historical_data: HistoricalData) -> None: @@ -32,17 +33,27 @@ class Recorder: @classmethod async def save_historical_data(cls, event_id: int) -> None: - if event_id not in cls.historical_data: - raise KeyError - historical_data, completed = cls.historical_data.pop(event_id) + historical_data, completed = cls.del_historical_data(event_id) if completed: async with get_session() as session: session.add(historical_data) await session.commit() @classmethod - def del_historical_data(cls, event_id: int) -> None: - cls.historical_data.pop(event_id) + def del_historical_data(cls, event_id: int) -> tuple[HistoricalData, bool]: + return cls.historical_data.pop(event_id) + + @classmethod + def add_error_event(cls, event_id: int) -> None: + cls.error_event.add(event_id) + + @classmethod + def del_error_event(cls, event_id: int) -> None: + cls.error_event.remove(event_id) + + @classmethod + def is_error_event(cls, event_id: int) -> bool: + return event_id in cls.error_event @driver.on_startup @@ -73,7 +84,9 @@ def _(bot: Bot, event: Event, matcher: Matcher): @run_postprocessor async def _(event: Event, matcher: Matcher, exception: Exception | None): if isinstance(matcher, tuple(Recorder.matchers)): + event_id = id(event) if exception is not None: - Recorder.del_historical_data(id(event)) + Recorder.add_error_event(event_id) + Recorder.del_historical_data(event_id) else: - await Recorder.save_historical_data(id(event)) + await Recorder.save_historical_data(event_id)