diff --git a/squirrelbattle/game.py b/squirrelbattle/game.py index 93af352..a64d82e 100644 --- a/squirrelbattle/game.py +++ b/squirrelbattle/game.py @@ -139,7 +139,15 @@ class Game: """ Loads the game from a dictionary """ - self.map.load_state(d) + try: + self.map.load_state(d) + except KeyError: + self.message = "Some keys are missing in your save file.\n" \ + "Your save seems to be corrupt. It got deleted." + os.unlink(ResourceManager.get_config_path("save.json")) + self.display_actions(DisplayActions.UPDATE) + return + players = self.map.find_entities(Player) if not players: self.message = "No player was found on this map!\n" \ diff --git a/squirrelbattle/tests/game_test.py b/squirrelbattle/tests/game_test.py index a466fa9..5081912 100644 --- a/squirrelbattle/tests/game_test.py +++ b/squirrelbattle/tests/game_test.py @@ -4,6 +4,8 @@ import os import unittest +from squirrelbattle.resources import ResourceManager + from squirrelbattle.enums import DisplayActions from squirrelbattle.bootstrap import Bootstrap @@ -43,6 +45,27 @@ class TestGame(unittest.TestCase): new_state = self.game.save_state() self.assertEqual(old_state, new_state) + # Error on loading save + with open(ResourceManager.get_config_path("save.json"), "w") as f: + f.write("I am not a JSON file") + self.assertIsNone(self.game.message) + self.game.load_game() + self.assertIsNotNone(self.game.message) + self.game.message = None + + with open(ResourceManager.get_config_path("save.json"), "w") as f: + f.write("{}") + self.assertIsNone(self.game.message) + self.game.load_game() + self.assertIsNotNone(self.game.message) + self.game.message = None + + # Load game with a dead player + self.game.map.remove_entity(self.game.player) + self.game.save_game() + self.game.load_game() + self.assertIsNotNone(self.game.message) + def test_bootstrap_fail(self) -> None: """ Ensure that the test can't play the game,