save are now deleted end an history save is created when a game end
This commit is contained in:
parent
808b309c40
commit
4be4cf5a03
4 changed files with 41 additions and 13 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -139,3 +139,4 @@ dmypy.json
|
||||||
/.idea/vcs.xml
|
/.idea/vcs.xml
|
||||||
|
|
||||||
/.save/
|
/.save/
|
||||||
|
/.history/
|
||||||
|
|
5
NOTE.md
5
NOTE.md
|
@ -14,9 +14,12 @@ A faire :
|
||||||
- Changer les images, rajouter les fonds, mettre la musique, ...
|
- Changer les images, rajouter les fonds, mettre la musique, ...
|
||||||
- Voir les TODOs
|
- Voir les TODOs
|
||||||
|
|
||||||
|
3. Autre :
|
||||||
|
- test avec "assert" (cahier des charges)
|
||||||
|
- mode d'emploi (video + pdf) expliquant le fonctionnement
|
||||||
|
|
||||||
Bug :
|
Bug :
|
||||||
- Dans de rare cas (souvent en fermant brutalement la fenêtre) le processus ne s'arrête pas
|
- (incertain) Dans de rare cas (souvent en fermant brutalement la fenêtre) le processus ne s'arrête pas
|
||||||
|
|
||||||
|
|
||||||
Autre :
|
Autre :
|
||||||
|
|
|
@ -2,3 +2,6 @@ from pathlib import Path
|
||||||
|
|
||||||
path_save: Path = Path(".save")
|
path_save: Path = Path(".save")
|
||||||
path_save.mkdir(exist_ok=True)
|
path_save.mkdir(exist_ok=True)
|
||||||
|
|
||||||
|
path_history: Path = Path(".history")
|
||||||
|
path_history.mkdir(exist_ok=True)
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
import json
|
import json
|
||||||
import socket
|
import socket
|
||||||
|
from datetime import datetime
|
||||||
|
from pathlib import Path
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from source import path_save
|
from source import path_save, path_history
|
||||||
from source.core.enums import BombState
|
from source.core.enums import BombState
|
||||||
from source.core.error import InvalidBombPosition, PositionAlreadyShot
|
from source.core.error import InvalidBombPosition, PositionAlreadyShot
|
||||||
from source.gui.scene import GameResult
|
from source.gui.scene import GameResult
|
||||||
|
@ -301,6 +303,29 @@ class Game(Scene):
|
||||||
board_enemy_data=data["grid_enemy"]
|
board_enemy_data=data["grid_enemy"]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def save_path(self) -> Path:
|
||||||
|
ip_address, port = self.connection.getpeername()
|
||||||
|
|
||||||
|
# Le nom du fichier est l'IP de l'opposent, suivi d'un entier indiquant si c'est à notre tour ou non.
|
||||||
|
# Cet entier permet aux localhost de toujours pouvoir sauvegarder et charger sans problème.
|
||||||
|
return path_save / (
|
||||||
|
ip_address +
|
||||||
|
(f"-{int(self.my_turn)}" if ip_address == "127.0.0.1" else "") +
|
||||||
|
".bn-save"
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def history_path(self):
|
||||||
|
return path_history / (
|
||||||
|
datetime.now().strftime("%Y-%m-%d_%H-%M-%S") +
|
||||||
|
".bn-history"
|
||||||
|
)
|
||||||
|
|
||||||
|
def save_to_path(self, path: Path):
|
||||||
|
with open(path, "w", encoding="utf-8") as file:
|
||||||
|
json.dump(self.to_json(), file, ensure_ascii=False)
|
||||||
|
|
||||||
def save(self, value: bool):
|
def save(self, value: bool):
|
||||||
self.chat_new_message(
|
self.chat_new_message(
|
||||||
"System",
|
"System",
|
||||||
|
@ -308,19 +333,15 @@ class Game(Scene):
|
||||||
)
|
)
|
||||||
if not value: return
|
if not value: return
|
||||||
|
|
||||||
ip_address, port = self.connection.getpeername()
|
self.save_to_path(self.save_path)
|
||||||
# Le nom du fichier est l'IP de l'opposent, suivi d'un entier indiquant si c'est à notre tour ou non.
|
|
||||||
# Cet entier permet aux localhost de toujours pouvoir sauvegarder et charger sans problème.
|
|
||||||
filename: str = (
|
|
||||||
ip_address +
|
|
||||||
(f"-{int(self.my_turn)}" if ip_address == "127.0.0.1" else "") +
|
|
||||||
".bn-save"
|
|
||||||
)
|
|
||||||
|
|
||||||
with open(path_save / filename, "w", encoding="utf-8") as file:
|
|
||||||
json.dump(self.to_json(), file, ensure_ascii=False, indent=4)
|
|
||||||
|
|
||||||
def game_end(self, won: bool):
|
def game_end(self, won: bool):
|
||||||
|
# s'il existe une ancienne sauvegarde, efface la
|
||||||
|
self.save_path.unlink(missing_ok=True)
|
||||||
|
|
||||||
|
# sauvegarde cette partie dans l'historique
|
||||||
|
self.save_to_path(self.history_path)
|
||||||
|
|
||||||
self.window.add_scene(GameResult, game_scene=self, won=won) # affiche le résultat
|
self.window.add_scene(GameResult, game_scene=self, won=won) # affiche le résultat
|
||||||
self.thread.stop() # coupe la connexion
|
self.thread.stop() # coupe la connexion
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue