def make_levelPack_from_json(json_data): CCLevels = cc_classes.CCLevelPack() for l in json_data: print("KK") myL = cc_classes.CCLevel() myL.level_number = l["level_number"] myL.time = l["time"] myL.num_chips = l["num_chips"] myL.upper_layer = l["upper_layer"] myL.optional_fields = [] opt = l["optional_fields"] for o in opt: if (o["field_type"] == "title"): print(o["title"]) hu = cc_classes.CCMapTitleField(o["title"]) print(hu) myL.add_field(hu) if (o["field_type"] == "passcode"): print("OO") myL.add_field(cc_classes.CCEncodedPasswordField(o["passcode"])) if (o["field_type"] == "hint"): myL.add_field(cc_classes.CCMapHintField(o["hint"])) if (o["field_type"] == "monsters"): cL = [] for c in o["coordinates"]: cL.append(cc_classes.CCCoordinate(c["x"], c["y"])) myL.add_field(cc_classes.CCMonsterMovementField(cL)) CCLevels.add_level(myL) cc_dat_utils.write_cc_level_pack_to_dat(CCLevels, output_dat_file) mycc = cc_dat_utils.make_cc_level_pack_from_dat("data/pjsheeha_cc1.dat") print(mycc)
password = cc_classes.CCEncodedPasswordField(field["password_array"]) cclevel.add_field(password) elif field["field_type"] == "monsters": monster_coords_list = [] for ord_pair in field["monster_coords"]: coords = cc_classes.CCCoordinate(ord_pair["x"],ord_pair["y"]) monster_coords_list.append(coords) monster_list = cc_classes.CCMonsterMovementField(monster_coords_list) cclevel.add_field(monster_list) level_pack.add_level(cclevel) return level_pack #Part 3 #Load your custom JSON file input_json_file = "data/rolandog_cc1.json" file = open(input_json_file, "r") json_file = json.load(file) #Convert JSON data to CCLevelPack level_pack = make_cc_level_pack_from_json(json_file) print(level_pack) #Save converted data to DAT file cc_dat_utils.write_cc_level_pack_to_dat(level_pack, "data/rolandog_cc1.dat")
cc_level.time = level["time"] cc_level.num_chips = level["num_chips"] cc_level.upper_layer = level["upper_layer"] cc_level.lower_layer = level["lower_layer"] for field in level["optional_fields"]: cc_level.add_field(ConvertCCField(field)) return cc_level ###### #Main# ###### #Input input_file = "data/tigerj_cc1.json" output_file = "data/tigerj_cc1.dat" with open(input_file, "r") as reader: json_data = json.load(reader) #Construct CCLevelPack level_pack = cc_classes.CCLevelPack() for level in json_data: level_pack.add_level(MakeCCLevel(level)) #Output cc_dat_utils.write_cc_level_pack_to_dat(level_pack, output_file) #Validate print(cc_dat_utils.make_cc_level_pack_from_dat(output_file))
if field_type == "hint": new_hint = cc_classes.CCMapHintField(json_field["value"]) optional_fields.append(new_hint) elif field_type == "title": new_title = cc_classes.CCMapTitleField(json_field["value"]) optional_fields.append(new_title) elif field_type == "password": new_password = cc_classes.CCEncodedPasswordField( json_field["value"]) optional_fields.append(new_password) elif field_type == "monsters": monsters = [] for json_monster in json_field["monsters"]: x = json_monster["x"] y = json_monster["y"] new_monster_coord = cc_classes.CCCoordinate(x, y) monsters.append(new_monster_coord) new_monster_field = cc_classes.CCMonsterMovementField(monsters) optional_fields.append(new_monster_field) level.optional_fields = optional_fields new_level_pack.add_level(level) return new_level_pack new_level_pack = make_cc_class(data1) #print(levelPack) cc_dat_utils.write_cc_level_pack_to_dat(new_level_pack, "data/bweng_cc1.dat") #write_cc_level_pack_to_date(levelPack, "data/bweng_cc1.dat")
ftype = field["field_type"] #condition on field type, add these values to the level if ftype == "title": title = ccc.CCMapTitleField(field["value"]) level.add_field(title) elif ftype == "hint": hint = ccc.CCMapHintField(field["value"]) level.add_field(hint) elif ftype == "password": password = ccc.CCEncodedPasswordField(field["value"]) level.add_field(password) elif ftype == "monsters": monsterslist = [] for mons in field["value"]: monster = ccc.CCCoordinate(mons["x"], mons["y"]) monsterslist.append(monster) monsters_list = ccc.CCMonsterMovementField(monsterslist) level.add_field(monsters_list) levelpack.add_level(level) return levelpack inputfile = "ecordaro_cc1.json" with open(inputfile, "r") as reader: levelfile = json.load(reader) finalLevel = make_level(levelfile) ccd.write_cc_level_pack_to_dat(finalLevel, "data/ecordaro_cc1.dat")
new_monster_field = cc_classes.CCMonsterMovementField(monsters) new_level.add_field(new_monster_field) elif field_type == "traps": traps = [] for json_trap in json_field["traps"]: # new_button_coord = cc_classes.CCCoordinate(json_trap["x_button"], json_trap["y_button"]) # new_trap_coord = cc_classes.CCCoordinate(json_trap["x_trap"], json_trap["y_trap"]) traps.append(cc_classes.CCTrapControl(json_trap["x_button"], json_trap["y_button"], json_trap["x_trap"], json_trap["y_trap"])) new_level.add_field(cc_classes.CCTrapControlsField(traps)) elif field_type == "cloners": cloners = [] for json_cloner in json_field["cloners"]: # new_button_coord = cc_classes.CCCoordinate(json_trap["x_button"], json_trap["y_button"]) # new_cloner_coord = cc_classes.CCCoordinate(json_trap["x_cloner"], json_trap["y_cloner"]) cloners.append(cc_classes.CCCloningMachineControl(json_cloner["x_button"], json_cloner["y_button"], json_cloner["x_cloner"], json_cloner["y_cloner"])) new_level.add_field(cc_classes.CCCloningMachineControlsField(cloners)) new_level_pack.add_level(new_level) # cc_dat_utils.write_cc_level_pack_to_dat(new_level_pack, "data/rchew_cc1.dat") # level_pack_from_dat = cc_dat_utils.make_cc_level_pack_from_dat("data/rchew_cc1.dat") # print(level_pack_from_dat) cc_dat_utils.write_cc_level_pack_to_dat(new_level_pack, "data/rchew_pack.dat") level_pack_from_dat = cc_dat_utils.make_cc_level_pack_from_dat("data/rchew_pack.dat") print(level_pack_from_dat)
new_level.time = json_level["time"] new_level.upper_layer = json_level["upper_layer"] for json_field in json_level["optional_field"]: field_type = json_field["field_type"] if field_type == "hint": new_hint_field = cc_classes.CCMapHintField(json_field["hint"]) new_level.add_field(new_hint_field) elif field_type == "password": new_password_field = cc_classes.CCEncodedPasswordField( json_field["password"]) new_level.add_field(new_password_field) elif field_type == "title": new_title_field = cc_classes.CCMapTitleField(json_field["title"]) new_level.add_field(new_title_field) elif field_type == "monster": monster = [] for json_monster in json_field["monster"]: x = json_monster["x"] y = json_monster["y"] new_monster_coord = cc_classes.CCCoordinate(x, y) monster.append(new_monster_coord) new_monster_field = cc_classes.CCMonsterMovementField(monster) new_level.add_field(new_monster_field) new_level_pack.add_level(new_level) #Save converted data to DAT file cc_dat_utils.write_cc_level_pack_to_dat(new_level_pack, "data/mingzhic_cc1.dat")
field_type = json_field["field_type"] if field_type == "hint": new_hint_field = cc_classes.CCMapHintField(json_field["hint"]) new_level.add_field(new_hint_field) elif field_type == "title": new_title_field = cc_classes.CCMapTitleField(json_field["title"]) new_level.add_field(new_title_field) elif field_type == "password": new_password_field = cc_classes.CCEncodedPasswordField( json_field["password"]) new_level.add_field(new_password_field) elif field_type == "monster": monsters = [] json_monster_list = json_field["monsters"] for json_monster in json_monster_list: x = json_monster["x"] y = json_monster["y"] new_monster_coord = cc_classes.CCCoordinate(x, y) monsters.append(new_monster_coord) new_monster_field = cc_classes.CCMonsterMovementField(monsters) print("made new monster field") print(new_monster_field) new_level.add_field(new_monster_field) new_level_pack.add_level(new_level) #print(new_level_pack) cc_dat_utils.write_cc_level_pack_to_dat(new_level_pack, "data/esuder_cc1.dat") #test_dat = cc_dat_utils.make_cc_level_pack_from_dat("data/esuder_cc1.dat") #print(test_dat)
new_level.upper_layer = new_level_next["upper_layer"] #looping through json data for optional fields for json_field in new_level_next["optional_fields"]: field_type = json_field["field_type"] if field_type == "hint": new_hint_field = cc_classes.CCMapHintField(json_field["hint"]) new_level.add_field(new_hint_field) elif field_type == "password": new_password_field = cc_classes.CCEncodedPasswordField(json_field["password"]) new_level.add_field(new_password_field) elif field_type == "title": new_title_field = cc_classes.CCMapTitleField(json_field["title"]) new_level.add_field(new_title_field) elif field_type == "monsters": monsters = [] for json_monster in json_field["monsters"]: x = json_monster["x"] y = json_monster["y"] new_monster_coord = cc_classes.CCCoordinate(x, y) monsters.append(new_monster_coord) new_monster_field = cc_classes.CCMonsterMovementField(monsters) new_level.add_field(new_monster_field) new_level_pack.add_level(new_level) #Save converted data to DAT file cc_dat_utils.write_cc_level_pack_to_dat(new_level_pack, "data/tharkare_cc1.dat") test_dat = cc_dat_utils.make_cc_level_pack_from_dat("data/tharkare_cc1.dat") print(test_dat)