serialize persistent high score to be less readable

This commit is contained in:
Adrien MALINGREY 2019-10-01 10:20:05 +02:00
parent 578f33ae64
commit 58a7736d53

View File

@ -3,6 +3,7 @@ import sys
import locale import locale
import time import time
import os import os
import pickle
try: try:
import arcade import arcade
@ -391,8 +392,9 @@ class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
def load_high_score(self): def load_high_score(self):
try: try:
with open(HIGH_SCORE_PATH, "r") as f: with open(HIGH_SCORE_PATH, "rb") as f:
self.high_score = int(f.read()) ^ CRYPT_KEY crypted_high_score = pickle.load(f)
self.high_score = crypted_high_score ^ CRYPT_KEY
except: except:
self.high_score = 0 self.high_score = 0
@ -400,8 +402,9 @@ class TetrArcade(tetrislogic.TetrisLogic, arcade.Window):
try: try:
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='w') as f: with open(HIGH_SCORE_PATH, mode='wb') as f:
f.write(str(self.high_score ^ CRYPT_KEY)) crypted_high_score = self.high_score ^ CRYPT_KEY
pickle.dump(crypted_high_score, f, pickle.HIGHEST_PROTOCOL)
except Exception as e: except Exception as e:
sys.exit( sys.exit(
"""High score: {:n} """High score: {:n}