Skip to content

Commit

Permalink
Move game error handling to 'GameError'
Browse files Browse the repository at this point in the history
  • Loading branch information
Parzival-05 committed Aug 20, 2024
1 parent 9049635 commit 2d64781
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
32 changes: 29 additions & 3 deletions AIAgent/ml/game/errors_game.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
import logging
import traceback
from func_timeout import FunctionTimedOut

from config import FeatureConfig
from ml.protocols import Predictor
from ml.training.epochs_statistics import StatisticsCollector
from connection.errors_connection import GameInterruptedError
from connection.errors_connection import GameInterruptedError, ProcessStoppedError
from common.game import GameMap2SVM


class GameError(Exception):
def __init__(self, game_map2svm: GameMap2SVM, error: Exception) -> None:

def __init__(
self, with_predictor: Predictor, game_map2svm: GameMap2SVM, error: Exception
) -> None:
self._map = game_map2svm
self._error = error
super().__init__(GameError, self._error)

name_of_predictor = with_predictor.name()
need_to_save = True

if isinstance(error, FunctionTimedOut):
log_message = f"<{name_of_predictor}> timeouted on map {game_map2svm.GameMap.MapName} with {error.timedOutAfter}s"
elif isinstance(error, ProcessStoppedError):
log_message = f"<{name_of_predictor}> failed on map {game_map2svm.GameMap.MapName}: process suddenly disappeared"
need_to_save = False
else:
log_message = f"<{name_of_predictor}> failed on map {game_map2svm.GameMap.MapName}:\n{traceback.format_exc()}"
logging.warning(log_message)

if need_to_save:
FeatureConfig.SAVE_IF_FAIL_OR_TIMEOUT.save_model(
with_predictor.model(), with_name=name_of_predictor
)
super().__init__(with_predictor, game_map2svm, error)

def handle_error(self, statistics_collector: StatisticsCollector):
if isinstance(self._error, GameInterruptedError):
Expand Down
22 changes: 3 additions & 19 deletions AIAgent/ml/game/play_game.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import logging
from time import perf_counter
import traceback
from typing import TypeAlias

from common.classes import GameResult, Map2Result
from common.game import GameState, GameMap2SVM
from config import FeatureConfig
from connection.broker_conn.socket_manager import game_server_socket_manager
from connection.errors_connection import (
ProcessStoppedError,
)
from connection.game_server_conn.connector import Connector
from func_timeout import FunctionTimedOut, func_set_timeout
from ml.protocols import Predictor
Expand Down Expand Up @@ -168,19 +164,7 @@ def play_game(
)
map2result = Map2Result(game_map2svm, game_result)
except (FunctionTimedOut, Exception) as error:
need_to_save = True
if isinstance(error, FunctionTimedOut):
log_message = f"<{with_predictor.name()}> timeouted on map {game_map2svm.GameMap.MapName} with {error.timedOutAfter}s"
elif isinstance(error, ProcessStoppedError):
log_message = f"<{with_predictor.name()}> failed on map {game_map2svm.GameMap.MapName}: process suddenly disappeared"
need_to_save = False
else:
log_message = f"<{with_predictor.name()}> failed on map {game_map2svm.GameMap.MapName}:\n{traceback.format_exc()}"
logging.warning(log_message)
if need_to_save:
FeatureConfig.SAVE_IF_FAIL_OR_TIMEOUT.save_model(
with_predictor.model(), with_name=f"{with_predictor.name()}"
)
raise GameError(game_map2svm, error)

raise GameError(
with_predictor=with_predictor, game_map2svm=game_map2svm, error=error
)
return map2result

0 comments on commit 2d64781

Please sign in to comment.