I have made a game in python which generates quite a large number of highscores which I want to store, with preference for security, storage efficiency and ease of use. At the moment I am storing them in a dictionary, with keys that refer to the different level types, which is then written to a .txt
file and retrieved using eval
.
EDIT: I am now aware this is a bad idea and am using json instead. However the file is still legible, and any encryption that could be added would be beneficial.
import os
#Reading from file
try:
with open(os.path.join(root_directory, 'highscores.txt'), 'r') as f:
highscores = eval(f.read())
except IOError:
highscores = dict()
#Writing to file
with open(os.path.join(root_directory, 'highscores.txt'), 'w') as f:
f.write(highscores)
However, there a few issues I have with this method that I would like to get some advice on.
- I want to reduce the file size. Compressing the file does make a big difference, but any more efficient formats would be preferable.
- I would like to make it a bit more secure. Ideally I would want it to be hard for a player who is familiar with python to be able to work out how to edit the highscores.
- I would like to make it easier to update a master copy of the highscores with new additions to distributed copies, in particular the use of tuple keys in the dictionary is rather clumsy.
Would it be worthwhile making a Highscore
class which would make use of serialize
and deserialize
methods, and is there any additional security which could/should be added to this?