From c18209402c8d941a3ed9d2f78acf5484a0efdd61 Mon Sep 17 00:00:00 2001 From: Alexandre CATTEAU Date: Wed, 1 May 2024 14:46:15 +0200 Subject: [PATCH] Add ability to save data to file and load it back --- .gitignore | 1 + README.md | 3 +++ data-generator.py | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a6c57f5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.json diff --git a/README.md b/README.md index b5be393..e4c6045 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,6 @@ Tooling for running a campaign of Ticket to Ride Legacy: Legends of the West wit ### Components * `data-generator.py` contains models to generate data for a campaign * currently, only generates a Circus instance + +### Requirements +* Python library `jsonpickle` diff --git a/data-generator.py b/data-generator.py index ba985b9..93ade79 100644 --- a/data-generator.py +++ b/data-generator.py @@ -1,7 +1,9 @@ # Generation of data for a campaign of Ticket to Ride Legacy Legends of the West # Imports +import os import random +import jsonpickle # Class for Circus: list of wagons, of the 5 colors class Circus: @@ -25,8 +27,33 @@ class Circus: self.wagons.append(color) remaining[color] = remaining[color] - 1 - #def next(): # returns next color (i.e., color that the player will get it they use circus) +class Game: -myCircus = Circus(3) + # initGame() is used to generate all data for a campaign and initialize status variables + def initGame(self, playersNb): + self.circusList = Circus(playersNb) + # Mama O'Connell + # Treasures + # Concessions + self.circusState = 0 # 0 means Circus is not used in game yet, 1 means next wagon in list is #1, and so on + self.year = 1858 -print(myCircus.wagons) + def saveData(self): + savefile = open('./savefile.json', 'w+') + savefile.write(jsonpickle.encode(self)) + savefile.close() + +def loadData(): + with open('./savefile.json', 'r') as savefile: + content = savefile.read() + data = jsonpickle.decode(content) + return data + +# Test of data generation +playersNb = 3 +# myGame = Game() +# myGame.initGame(playersNb) +# myGame.saveData() +myGame = loadData() +print(myGame.year) +print(myGame.circusList.wagons)