move high score pickle to tetrislogic

This commit is contained in:
Adrien MALINGREY 2019-10-01 12:36:00 +02:00
parent 11c6321f17
commit c2ec677f3f
2 changed files with 12 additions and 9 deletions

View File

@ -392,7 +392,7 @@ class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
def load_high_score(self): def load_high_score(self):
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 = f.read()
super().load_high_score(crypted_high_score) super().load_high_score(crypted_high_score)
except: except:
self.high_score = 0 self.high_score = 0
@ -403,13 +403,12 @@ class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
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 = super().save_high_score() crypted_high_score = super().save_high_score()
pickle.dump(crypted_high_score, f, pickle.HIGHEST_PROTOCOL) f.write(crypted_high_score)
except Exception as e: except Exception as e:
sys.exit( sys.exit(
"""High score: {:n} """High score: {:n}
High score could not be saved: High score could not be saved:
""".format(self.high_score) """.format(self.high_score) + str(e)
+ str(e)
) )
def start(self, task, period): def start(self, task, period):

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import random import random
import pickle
from .utils import Coord, Movement, Rotation, T_Spin, Line from .utils import Coord, Movement, Rotation, T_Spin, Line
from .tetromino import Tetromino, T, I from .tetromino import Tetromino, T, I
@ -398,17 +399,20 @@ class TetrisLogic():
raise Warning("TetrisLogic.show_text not implemented.") raise Warning("TetrisLogic.show_text not implemented.")
def load_high_score(self, crypted_high_score=None): def load_high_score(self, crypted_high_score=None):
if crypted_high_score is None: if crypted_high_score:
self.high_score = 0 crypted_high_score = int(pickle.loads(crypted_high_score))
self.high_score = crypted_high_score ^ CRYPT_KEY
else:
raise Warning( raise Warning(
"""TetrisLogic.load_high_score not implemented. """TetrisLogic.load_high_score not implemented.
High score is set to 0""" High score is set to 0"""
) )
else: self.high_score = 0
self.high_score = crypted_high_score ^ CRYPT_KEY
def save_high_score(self): def save_high_score(self):
return self.high_score ^ CRYPT_KEY crypted_high_score = self.high_score ^ CRYPT_KEY
crypted_high_score = pickle.dumps(crypted_high_score)
return crypted_high_score
def start(task, period): def start(task, period):
raise Warning("TetrisLogic.start is not implemented.") raise Warning("TetrisLogic.start is not implemented.")