Game displays an error message when a save file could not be loaded.

This commit is contained in:
Yohann D'ANELLO 2020-11-27 18:09:08 +01:00
parent be9c726fa0
commit 25ba94b9ac
1 changed files with 19 additions and 5 deletions

View File

@ -1,6 +1,6 @@
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse # Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from json import JSONDecodeError
from random import randint from random import randint
from typing import Any, Optional from typing import Any, Optional
import json import json
@ -37,7 +37,7 @@ class Game:
self.settings.write_settings() self.settings.write_settings()
self.settings_menu.update_values(self.settings) self.settings_menu.update_values(self.settings)
self.logs = Logs() self.logs = Logs()
self.message = "Vive les écureuils" self.message = None
def new_game(self) -> None: def new_game(self) -> None:
""" """
@ -139,8 +139,15 @@ class Game:
Loads the game from a dictionary Loads the game from a dictionary
""" """
self.map.load_state(d) self.map.load_state(d)
# noinspection PyTypeChecker players = self.map.find_entities(Player)
self.player = self.map.find_entities(Player)[0] if not players:
self.message = "No player was found on this map!\n" \
"Maybe you died?"
self.player.health = 0
self.display_actions(DisplayActions.UPDATE)
return
self.player = players[0]
self.display_actions(DisplayActions.UPDATE) self.display_actions(DisplayActions.UPDATE)
def load_game(self) -> None: def load_game(self) -> None:
@ -150,7 +157,14 @@ class Game:
file_path = ResourceManager.get_config_path("save.json") file_path = ResourceManager.get_config_path("save.json")
if os.path.isfile(file_path): if os.path.isfile(file_path):
with open(file_path, "r") as f: with open(file_path, "r") as f:
self.load_state(json.loads(f.read())) try:
state = json.loads(f.read())
self.load_state(state)
except JSONDecodeError:
self.message = "The JSON file is not correct.\n" \
"Your save seems corrupted. It got deleted."
os.unlink(file_path)
self.display_actions(DisplayActions.UPDATE)
def save_game(self) -> None: def save_game(self) -> None:
""" """