Add ability to save data to file and load it back

This commit is contained in:
Alexandre CATTEAU 2024-05-01 14:46:15 +02:00
parent 737915cb3b
commit c18209402c
3 changed files with 34 additions and 3 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.json

View File

@ -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`

View File

@ -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 <number> 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)