move pseudo crypt to tetrislogic

This commit is contained in:
Adrien MALINGREY 2019-10-01 11:55:08 +02:00
parent 58a7736d53
commit 11c6321f17
2 changed files with 12 additions and 13 deletions

View File

@ -54,7 +54,6 @@ else:
USER_PROFILE_DIR = os.environ.get("XDG_DATA_HOME", os.path.expanduser("~/.local/share")) USER_PROFILE_DIR = os.environ.get("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))
USER_PROFILE_DIR = os.path.join(USER_PROFILE_DIR, "TetrArcade") USER_PROFILE_DIR = os.path.join(USER_PROFILE_DIR, "TetrArcade")
HIGH_SCORE_PATH = os.path.join(USER_PROFILE_DIR, ".high_score") HIGH_SCORE_PATH = os.path.join(USER_PROFILE_DIR, ".high_score")
CRYPT_KEY = 987943759387540938469837689379857347598347598379584857934579343
# Text # Text
TEXT_COLOR = arcade.color.BUBBLES TEXT_COLOR = arcade.color.BUBBLES
@ -394,7 +393,7 @@ class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
try: try:
with open(HIGH_SCORE_PATH, "rb") as f: with open(HIGH_SCORE_PATH, "rb") as f:
crypted_high_score = pickle.load(f) crypted_high_score = pickle.load(f)
self.high_score = crypted_high_score ^ CRYPT_KEY super().load_high_score(crypted_high_score)
except: except:
self.high_score = 0 self.high_score = 0
@ -403,7 +402,7 @@ class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
if not os.path.exists(USER_PROFILE_DIR): if not os.path.exists(USER_PROFILE_DIR):
os.makedirs(USER_PROFILE_DIR) os.makedirs(USER_PROFILE_DIR)
with open(HIGH_SCORE_PATH, mode='wb') as f: with open(HIGH_SCORE_PATH, mode='wb') as f:
crypted_high_score = self.high_score ^ CRYPT_KEY crypted_high_score = super().save_high_score()
pickle.dump(crypted_high_score, f, pickle.HIGHEST_PROTOCOL) pickle.dump(crypted_high_score, f, pickle.HIGHEST_PROTOCOL)
except Exception as e: except Exception as e:
sys.exit( sys.exit(

View File

@ -12,6 +12,7 @@ from .consts import (
LINES_CLEAR_NAME = "LINES_CLEAR_NAME" LINES_CLEAR_NAME = "LINES_CLEAR_NAME"
CRYPT_KEY = 987943759387540938469837689379857347598347598379584857934579343
class State: class State:
@ -396,19 +397,18 @@ class TetrisLogic():
print(text) print(text)
raise Warning("TetrisLogic.show_text not implemented.") raise Warning("TetrisLogic.show_text not implemented.")
def load_high_score(self): def load_high_score(self, crypted_high_score=None):
self.high_score = 0 if crypted_high_score is None:
raise Warning( self.high_score = 0
"""TetrisLogic.load_high_score not implemented. raise Warning(
"""TetrisLogic.load_high_score not implemented.
High score is set to 0""" High score is set to 0"""
) )
else:
self.high_score = crypted_high_score ^ CRYPT_KEY
def save_high_score(self): def save_high_score(self):
print("High score: {:n}".format(self.high_score)) return self.high_score ^ CRYPT_KEY
raise Warning(
"""TetrisLogic.save_high_score not implemented.
High score is not saved"""
)
def start(task, period): def start(task, period):
raise Warning("TetrisLogic.start is not implemented.") raise Warning("TetrisLogic.start is not implemented.")