def make_level(data): #initialize levelpack levelpack = ccc.CCLevelPack() #initialize crucial aspects of level for each level in input file for l in data: level = ccc.CCLevel() level.level_number = l["level_number"] level.time = l["time"] level.num_chips = l["num_chips"] level.upper_layer = l["upper_layer"] #go through optional fields and add fields to self.optional fields for field in l["optional_fields"]: 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
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)
def make_cc_level_pack_from_json( json_data ): #Initialize a new CCLevelPack level_pack = cc_classes.CCLevelPack() #Loop through the json_data for level in json_data: #Initialize a new CCLevel cclevel = cc_classes.CCLevel() # Level number cclevel.level_number = level["level_number"] print("level number: " + str(cclevel.level_number)) # Time cclevel.time = level["time_limit"] print("time limit: " + str(cclevel.time)) # Number of chips cclevel.num_chips = level["num_chips"] print("# of chips: " + str(cclevel.num_chips)) # Upper layer cclevel.upper_layer = level["upper_layer"] print("upper layer: " + str(cclevel.upper_layer)) for field in level["optional_fields"]: print(field) if field["field_type"] == "title": title = cc_classes.CCMapTitleField(field["map_title"]) cclevel.add_field(title) elif field["field_type"] == "hint": hint = cc_classes.CCMapHintField(field["hint_text"]) cclevel.add_field(hint) elif field["field_type"] == "password": 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
def make_cc_level_pack_from_dat(dat_file): """Reads a DAT file and constructs a CCLevelPack object out of it This code assumes a valid DAT file and does not error check for invalid data Args: dat_file (string) : the filename of the DAT file to read in Returns: A CCLevelPack object constructed with the data from the given file """ data = cc_classes.CCLevelPack() with open(dat_file, 'rb') as reader: header_bytes = do_read(reader, 4) if header_bytes != CC_DAT_HEADER_CODE: print("ERROR: Invalid header found. Expected " + str(CC_DAT_HEADER_CODE) + ", but found " + header_bytes) return num_levels = int.from_bytes(do_read(reader, 2), byteorder=cc_classes.BYTE_ORDER) for i in range(num_levels): level = make_level_from_dat(reader) data.levels.append(level) return data
def make_cc_class(json_data): new_level_pack = cc_classes.CCLevelPack() for value in json_data: level = cc_classes.CCLevel() level.level_number = value["level_number"] level.time = value["time_limit"] level.num_chips = value["num_chips"] level.upper_layer = value["upper_layer"] #level.optional_fields = value["optional_fields"] optional_fields = [] for json_field in value["optional_fields"]: field_type = json_field["field_type"] 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
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))