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_field_from_bytes(field_type, field_bytes): """Constructs and returns the appropriate cc field Args: field_type (int) : what type of field to construct field_bytes (bytes) : the binary data to be used to create the field """ if field_type == 3: return cc_classes.CCMapTitleField(get_string_from_bytes(field_bytes)) elif field_type == 4: trap_count = int(len(field_bytes) / 10) traps = [] for t_index in range(trap_count): i = t_index * 10 bx = int.from_bytes(field_bytes[i:(i + 2)], byteorder=cc_classes.BYTE_ORDER) by = int.from_bytes(field_bytes[i + 2:(i + 4)], byteorder=cc_classes.BYTE_ORDER) tx = int.from_bytes(field_bytes[i + 4:(i + 6)], byteorder=cc_classes.BYTE_ORDER) ty = int.from_bytes(field_bytes[i + 6:(i + 8)], byteorder=cc_classes.BYTE_ORDER) traps.append(cc_classes.CCTrapControl(bx, by, tx, ty)) return cc_classes.CCTrapControlsField(traps) elif field_type == 5: machine_count = int(len(field_bytes) / 8) machines = [] for m_index in range(machine_count): i = m_index * 8 bx = int.from_bytes(field_bytes[i:(i + 2)], byteorder=cc_classes.BYTE_ORDER) by = int.from_bytes(field_bytes[i + 2:(i + 4)], byteorder=cc_classes.BYTE_ORDER) tx = int.from_bytes(field_bytes[i + 4:(i + 6)], byteorder=cc_classes.BYTE_ORDER) ty = int.from_bytes(field_bytes[i + 6:(i + 8)], byteorder=cc_classes.BYTE_ORDER) machines.append(cc_classes.CCCloningMachineControl(bx, by, tx, ty)) return cc_classes.CCCloningMachineControlsField(machines) elif field_type == 6: # passwords are encoded as a list of ints password = [] # A bytes object behaves as a list of integers # password data is terminated with a zero, iterate to one short of the end of the array for b in field_bytes[0:(len(field_bytes)-1)]: password.append(b) return cc_classes.CCEncodedPasswordField(password) elif field_type == 7: return cc_classes.CCMapHintField(get_string_from_bytes(field_bytes)) elif field_type == 8: return cc_classes.CCPasswordField(get_string_from_bytes(field_bytes)) elif field_type == 10: monster_count = int(len(field_bytes) / 2) monsters = [] for m_index in range(monster_count): i = m_index * 2 x = int.from_bytes(field_bytes[i:(i + 1)], byteorder=cc_classes.BYTE_ORDER) y = int.from_bytes(field_bytes[i + 1:(i + 2)], byteorder=cc_classes.BYTE_ORDER) monsters.append(cc_classes.CCCoordinate(x, y)) return cc_classes.CCMonsterMovementField(monsters) else: if __debug__: raise AssertionError("Unsupported field type: " + str(field_type)) return cc_classes.CCField(field_type, field_bytes)
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_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
def password(data): return cc_classes.CCEncodedPasswordField(data)
new_level.time = json_level["time"] new_level.num_chips = json_level["num_chips"] new_level.upper_layer = json_level["upper_layer"] # new_level.lower_layer = json_level["lower_layer"] for json_field in json_level["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 == "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 == "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) elif field_type == "traps": traps = [] for json_trap in json_field["traps"]:
new_level_pack = cc_classes.CCLevelPack() for level in level_pack_data: new_level = cc_classes.CCLevel() new_level.level_number = level["level_number"] new_level.time = level["time"] new_level.num_chips = level["num_chips"] new_level.upper_layer = level["upper_layer"] optional_fields_data = level["optional_fields"] #for field in optional_fields_data: new_map_title_field = cc_classes.CCMapTitleField( optional_fields_data["CCMapTitleField"]) new_level.add_field(new_map_title_field) encoded_password_field = cc_classes.CCEncodedPasswordField( optional_fields_data["CCEncodedPasswordField"]) new_level.add_field(encoded_password_field) map_hint_field = cc_classes.CCMapHintField( optional_fields_data["CCMapHintField"]) new_level.add_field(map_hint_field) monster_movement_field = cc_classes.CCMonsterMovementField( optional_fields_data["CCMonsterMovementField"]) #new_level.add_field(monster_movement_field) # print(new_level) new_level_pack.levels.append(new_level) # print(new_level_pack)