def loadFromDict(fields): countries = [createCountry(countryInfo) for countryInfo in fields["countries"]] territories = [createTerritory(tInfo, countries) for tInfo in fields["territories"]] for info in fields["territories"]: territory = Util.getByName(territories, info["name"]) territory.connections = [Util.getByName(territories, neighbour) for neighbour in info["connections"]] unitInfo = {unitType: UnitInfo(unitType, unitInfo) for unitType, unitInfo in fields["unitCatalogue"].items()} units = [] for unitDef in fields["units"]: newUnit = Unit.createUnitFromDict(unitDef, unitInfo, countries, territories) units.append(newUnit) boughtUnits = [] for unitDef in fields["buyList"]: newUnit = Unit.createBoughtUnitFromDict(unitDef, territories) boughtUnits.append(newUnit) board = Board(unitInfo, territories, units, countries, fields["currentPhase"], boughtUnits) for conflictInfo in fields["pastConflicts"]: conflict = Conflict.fromDict(conflictInfo, board) board.resolvedConflicts.append(conflict) board.currentCountry = Util.getByName(countries, fields["currentCountry"]) return board
def loadFromModuleName(moduleName): # load json from module with open(Util.countryFileName(moduleName)) as countryInfo: countries = [createCountry(cInfo) for cInfo in json.load(countryInfo)] with open(Util.unitFileName(moduleName)) as unitInfo: unitCatalogue = json.load(unitInfo) unitInfoDict = {unitType: UnitInfo(unitType, jsonInfo) for unitType, jsonInfo in unitCatalogue.items()} with open(Util.territoryFileName(moduleName)) as territoryInfo: territoryInfo = json.load(territoryInfo) territories = [createTerritory(info, countries) for info in territoryInfo] with open(Util.connectionFileName(moduleName)) as connections: connections = json.load(connections) for c in connections: first = None second = None for t in territories: if t.name == c[0]: first = t elif t.name == c[1]: second = t if first and second: first.connections.append(second) second.connections.append(first) else: raise Exception("Could not find territories for connection: " + json.dumps(c)) units = [] # add units with open(Util.filePath(moduleName, "unitSetup.json")) as unitFile: unitSetup = json.load(unitFile) for tName, unitTypes in unitSetup.items(): territory = Util.getByName(territories, tName) assert territory is not None, "Invalid territory name %r" % tName for unitType in unitTypes: units.append(Unit.Unit(unitInfoDict[unitType], territory.country, territory)) return Board(unitInfoDict, territories, units, countries)