Example #1
0
def make_level_from_dat(reader):
    """Reads all the data to construct a single level from the active reader
    Note that this assumes the reader is at new level section in the file.
    This code does not error check for invalid data
    Args:
        reader (BufferedReader) : active reader reading a DAT file
    Returns:
        A CCLevel object constructed with the read data
    """
    level = cc_data.CCLevel()
    level.num_bytes = int.from_bytes(do_read(reader, 2),
                                     byteorder=cc_data.BYTE_ORDER)
    level.level_number = int.from_bytes(do_read(reader, 2),
                                        byteorder=cc_data.BYTE_ORDER)
    level.time = int.from_bytes(do_read(reader, 2),
                                byteorder=cc_data.BYTE_ORDER)
    level.num_chips = int.from_bytes(do_read(reader, 2),
                                     byteorder=cc_data.BYTE_ORDER)
    # Note: Map Detail is not used and is expected to always be 1
    map_detail = int.from_bytes(do_read(reader, 2),
                                byteorder=cc_data.BYTE_ORDER)
    upper_layer_byte_count = int.from_bytes(do_read(reader, 2),
                                            byteorder=cc_data.BYTE_ORDER)
    upper_layer_bytes = do_read(reader, upper_layer_byte_count)
    lower_layer_byte_count = int.from_bytes(do_read(reader, 2),
                                            byteorder=cc_data.BYTE_ORDER)
    lower_layer_bytes = do_read(reader, lower_layer_byte_count)
    level.upper_layer = make_layer_from_bytes(upper_layer_bytes)
    level.lower_layer = make_layer_from_bytes(lower_layer_bytes)
    level.optional_fields = make_optional_fields_from_dat(reader)
    return level
Example #2
0
def make_level_library_from_json(json_data):

    level_library = cc_data.CCDataFile()

    for level_data in json_data["levels"]:
        level = cc_data.CCLevel()

        level.level_number = level_data["level"]
        level.time = level_data["time"]
        level.num_chips = level_data["chipNum"]
        level.upper_layer = level_data["Upper_layer"]
        level.lower_layer = level_data["Lower_layer"]

        optional_fields = level_data["optional"]
        hint = cc_data.CCMapHintField(optional_fields["hint"])
        level.add_field(hint)
        title = cc_data.CCMapTitleField(optional_fields["title"])
        level.add_field(title)
        monsters = []
        monster_coords = optional_fields["monsters"]
        for i in monster_coords:
            monster = cc_data.CCCoordinate(i[0], i[1])
            monsters.append(monster)
        monster_field = cc_data.CCMonsterMovementField(monsters)
        level.add_field(monster_field)

        level_library.add_level(level)

    return level_library
Example #3
0
def make_cc_level_from_json(level_data):
    # level_library = cc_data.CCDataFile()
    new_level = cc_data.CCLevel()
    new_level.level_number = level_data["level_number"]
    new_level.num_chips = level_data["num_chips"]
    new_level.upper_layer = level_data["upper_layer"]
    new_level.time = level_data["time"]
    for field_json in level_data["optional_fields"]:
        new_field = None

        print("field_json: " + str(field_json))
        if field_json["id"] == "Hint":
            new_field = cc_data.CCMapHintField(field_json["hint_data"])
            new_level.add_field(new_field)
        if field_json["id"] == "Password":
            new_field = cc_data.CCEncodedPasswordField(
                field_json["Password_data"])
            new_level.add_field(new_field)
        if field_json["id"] == "title":
            new_field = cc_data.CCMapTitleField(field_json["title_data"])
            new_level.add_field(new_field)
        elif field_json["id"] == "monsters":
            monster_coords = []
            for monster_json in field_json["monster_data"]:
                monster_coords.append(
                    cc_data.CCCoordinate(monster_json[0], monster_json[1]))
            new_field = cc_data.CCMonsterMovementField(monster_coords)
            new_level.add_field(new_field)
    return new_level
Example #4
0
def convert_json_to_dat(json_file):

    game_data = cc_data.CCDataFile()
    for level in json_file["levels"]:
        game_level = cc_data.CCLevel()
        game_level.level_number = level["level number"]
        game_level.time = level["time"]
        game_level.num_chips = level["chip number"]
        game_level.upper_layer = level["upper layer"]
        optional_fields_json = level["optional fields"]

        for something in optional_fields_json:
            if something["id"] == "title":
                title = cc_data.CCMapTitleField(something["level title"])
                game_level.add_field(title)
            elif something["id"] == "password":
                password = cc_data.CCEncodedPasswordField(
                    something["encoded password"])
                game_level.add_field(password)
            elif something["id"] == "hint":
                hint = cc_data.CCMapHintField(something["hint text"])
                game_level.add_field(hint)
            elif something["id"] == "monsters":
                coordinates = []
                for coordinate in something["monsters"]:
                    x = coordinate[0]
                    y = coordinate[1]
                    coord = cc_data.CCCoordinate(x, y)
                    coordinates.append(coord)

        game_data.add_level(game_level)
    return game_data
def make_level_from_json(json_data):
    cclevel = cc_data.CCLevel()
    cclevel.level_number = json_data["levelnum"]
    cclevel.time = json_data["time"]
    cclevel.num_chips = json_data["chips"]
    cclevel.upper_layer = json_data["upper"]
    cclevel.lower_layer = json_data["lower"]
    cclevel.optional_fields = make_optional_field_from_json(json_data["optional_fields"])
    return cclevel
Example #6
0
def make_cc_level_from_json(json_data): # makes cc level using json data - includes initialized variables and optional fields
    cc_level = cc_data.CCLevel()  # Create a CCLevel Class from cc_data.py
    cc_level.level_number = json_data["level_number"]
    cc_level.time = json_data["time"]
    cc_level.num_chips = json_data["num_chips"]
    cc_level.upper_layer = json_data["upper_layer"]
    cc_level.lower_layer = json_data["lower_layer"]
    cc_level.optional_fields = make_optional_fields_from_json(json_data["optional_fields"])# make_optional_fields_from_json takes the json_data "optional_fields" and returns list of cc_fields
    return cc_level
Example #7
0
def make_ccfile_from_json(json_data):
    cc_file = cc_data.CCDataFile()
    cc_file.levels = []
    for level in json_data:
        level_object = cc_data.CCLevel()
        level_object.level_number = level["level_number"]
        level_object.time = level["time"]
        level_object.num_chips = level["chip_number"]
        level_object.upper_layer = level["upper_layer"]
        level_object.lower_layer = level["lower_layer"]

        for field in level["optional_fields"]:
            if field["field_num"] == 3:
                obj = cc_data.CCMapTitleField(field["title"])
                level_object.add_field(obj)
            elif field["field_num"] == 6:
                obj = cc_data.CCEncodedPasswordField(field["list"])
                level_object.add_field(obj)
            elif field["field_num"] == 7:
                obj = cc_data.CCMapHintField(field["hint"])
                level_object.add_field(obj)
            elif field["field_num"] == 10:
                a = []
                for i in range(len(field["xcoord"])):
                    mons = cc_data.CCCoordinate(field["xcoord"][i],
                                                field["ycoord"][i])
                    a.append(mons)
                obj = cc_data.CCMonsterMovementField(a)
                level_object.add_field(obj)
            elif field["field_num"] == 4:
                a = []
                for i in range(len(field["trapx"])):
                    #button = cc_data.CCCoordinate(field["buttonx"][i], field["buttony"][i])
                    #trap = cc_data.CCCoordinate(field["trapx"][i], field["trapy"][i])
                    combined = cc_data.CCTrapControl(field["buttonx"][i],
                                                     field["buttony"][i],
                                                     field["trapx"][i],
                                                     field["trapy"][i])
                    a.append(combined)
                obj = cc_data.CCTrapControlsField(a)
                level_object.add_field(obj)
            elif field["field_num"] == 5:
                a = []
                for i in range(len(field["buttonx"])):
                    #button = cc_data.CCCoordinate(field["buttonx"][i], field["buttony"][i])
                    #machine = cc_data.CCCoordinate(field["machinex"][i], field["machiney"][i])
                    combined = cc_data.CCCloningMachineControl(
                        field["buttonx"][i], field["buttony"][i],
                        field["machinex"][i], field["machiney"][i])
                    a.append(combined)
                obj = cc_data.CCCloningMachineControlsField(a)
                level_object.add_field(obj)
            else:
                print("idk")
        cc_file.add_level(level_object)
    return cc_file
Example #8
0
def make_cc_file_from_json(json_data):
    # Initializing CCDataFile
    data_file = cc_data.CCDataFile()

    # Iterating through level dictionaries in JSON file
    for lvl in json_data:
        # Initializing CCLevel
        level = cc_data.CCLevel()

        level.level_number = lvl["level number"]
        level.time = lvl["time"]
        level.num_chips = lvl["num_chips"]
        level.upper_layer = lvl["upper layer"]
        level.lower_layer = lvl["lower layer"]

        # Iterating through optional fields in level
        for fld in lvl["optional_fields"]:
            if fld["id"] == "hint":
                new_field = cc_data.CCMapHintField(fld["hint_text"])
            elif fld["id"] == "title":
                new_field = cc_data.CCMapTitleField(fld["title_text"])
            elif fld["id"] == "password":
                new_field = cc_data.CCEncodedPasswordField(fld["password"])
            elif fld["id"] == "monster":
                coords = []
                # Creating list of CCCoordinate objects
                for c in fld["monster_location"]:
                    new_coord = cc_data.CCCoordinate(c[0], c[1])
                    coords.append(new_coord)
                new_field = cc_data.CCMonsterMovementField(coords)
            elif fld["id"] == "trap":
                traps = []
                b_lst = fld["trap_button"]
                l_lst = fld["trap_location"]

                # bc = cc_data.CCCoordinate(b_lst[0], b_lst[1])
                # tc = cc_data.CCCoordinate(l_lst[0], l_lst[1])

                # Initializing trap information
                traps.append(
                    cc_data.CCTrapControl(b_lst[0], b_lst[1], l_lst[0],
                                          l_lst[1]))
                new_field = cc_data.CCTrapControlsField(traps)
            else:
                print("There was an error... Unexpected field!")

            # Adding newly made field to level
            level.add_field(new_field)

        # Adding level to game data file
        data_file.add_level(level)
    return data_file
Example #9
0
def make_CC_data_from_JSON(json_data):

    CCData = cc_data.CCDataFile()

    for game_level in json_data:

        # Make a new CCLevel
        level = cc_data.CCLevel()

        # Get data from current level
        level.level_number = game_level["level_number"]
        level.time = game_level["time"]
        level.num_chips = game_level["num_chips"]
        level.upper_layer = game_level["upper_layer"]

        # Retrieve data from optional fields
        optional_fields = game_level["optional_fields"]
        for field in optional_fields:
            if field["type_val"] == 3:
                init_field = cc_data.CCMapTitleField(field["title"])
            elif field["type_val"] == 4:
                coordinates = []
                for val in field["traps"]:  #retrieve coordinates from array
                    init_coord = cc_data.CCTrapControl(val[0], val[1], val[2],
                                                       val[3])
                    coordinates.append(init_coord)
                init_field = cc_data.CCTrapControlsField(coordinates)
            elif field["type_val"] == 5:
                coordinates = []
                for val in field["clones"]:  #retrieve coordinates from array
                    init_coord = cc_data.CCCloningMachineControl(
                        val[0], val[1], val[2], val[3])
                    coordinates.append(init_coord)
                init_field = cc_data.CCCloningMachineControlsField(coordinates)
            elif field["type_val"] == 6:
                init_field = cc_data.CCEncodedPasswordField(field["pass"])
            elif field["type_val"] == 7:
                init_field = cc_data.CCMapHintField(field["hint"])
            elif field["type_val"] == 10:
                coordinates = []
                for val in field["monsters"]:  #retrieve coordinates fom array
                    init_coord = cc_data.CCCoordinate(val[0], val[1])
                    coordinates.append(init_coord)
                init_field = cc_data.CCMonsterMovementField(coordinates)
            else:
                return ("Error: field unrecognized")
            level.add_field(init_field)
        CCData.add_level(level)
    print(CCData)
    return CCData
Example #10
0
def make_datafile_from_json(json_data):
    # Initialize a new level set
    datafile = cc_data.CCDataFile()

    # Loop through the json_data
    for level in json_data["levels"]:

        # initiate a temp level
        new_level = cc_data.CCLevel()

        # general info
        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"]
        new_level.lower_layer = level["lower_layer"]

        # go through optional fields
        fields = level["optional_fields"]
        for field in fields:
            type_val = field["type_val"]
            if type_val == 3:
                title = cc_data.CCMapTitleField(field["title"])
                new_level.add_field(title)
            elif type_val == 6:
                password = cc_data.CCEncodedPasswordField(field["password"])
                new_level.add_field(password)
            elif type_val == 7:
                hint = cc_data.CCMapHintField(field["hint"])
                new_level.add_field(hint)
            elif type_val == 10:
                monster_coordinates = []
                for coordinate in field["monsters"]:
                    monster_coordinates.append(cc_data.CCCoordinate(coordinate[0], coordinate[1]))
                monsters = cc_data.CCMonsterMovementField(monster_coordinates)
                new_level.add_field(monsters)
            elif type_val == 4:
                a = 0
                # cc_password = cc_data.CCEncodedPasswordField(field["password"])
                #level.add_field(cc_password)
            elif type_val == 5:
                b = 0
                #c c_password = cc_data.CCEncodedPasswordField(field["password"])
                #level.add_field(cc_password)

        # Add temp level to the level set
        datafile.add_level(new_level)

    return datafile
def make_level_from_dat(example_data):
    #Initialize a new GameLibrary
    with open(example_data) as reader:

        #data = json.load(reader)
        test = open(example_data)

        json_library = json.load(test)
    game_library = cc_data.CCDataFile()
    for game_json in json_library:
        new_game = cc_data.CCLevel()
        new_game.level_number = game_json["level_number"]
        new_game.time = game_json["time"]
        new_game.num_chips = game_json["num_chips"]
        new_game.upper_layer = game_json["upper_layer"]
        new_game.lower_layer = game_json["lower_layer"]
        result = game_json["optional_fields"]
        result = []
        for optional_fields_json in game_json["optional_fields"]:
            new_field = None
            if optional_fields_json["id"] == 1:
                new_field = cc_data.CCMapHintField(
                    optional_fields_json["hint_text"])

            if optional_fields_json["id"] == 2:
                new_field = cc_data.CCEncodedPasswordField(
                    optional_fields_json["password_data"])

            if optional_fields_json["id"] == 3:
                new_field = cc_data.CCMapTitleField(
                    optional_fields_json["title"])

            if optional_fields_json["id"] == 6:

                monster_cords = []
                for cords in optional_fields_json["monsters"]:
                    x = cords["x"]
                    y = cords["y"]
                    new_cord = cc_data.CCCoordinate(x, y)
                    monster_cords.append(new_cord)

                new_field = cc_data.CCMonsterMovementField(monster_cords)

            new_game.add_field(new_field)

        game_library.add_level(new_game)

    return game_library
Example #12
0
def make_optional_field_from_json(optional_fields):
    cc_level = cc_data.CCLevel()
    for field in optional_fields:
        if field["type"] == cc_data.CCMapTitleField.TYPE:
            CC_title = cc_data.CCMapTitleField(field["title"])
        elif field["type"] == cc_data.CCTrapControlsField.TYPE:
            TrapFields = []
            for trap in field["traps"]:
                #print(trap)
                bx = trap[0]
                by = trap[1]
                tx = trap[2]
                ty = trap[3]
                trapfield = cc_data.CCTrapControl(bx, by, tx, ty)
                TrapFields.append(trapfield)
            CC_TrapFields = cc_data.CCTrapControlsField(TrapFields)
        elif field["type"] == cc_data.CCCloningMachineControlsField.TYPE:
            CloFields = []
            for clone in field["Cloning"]:
                bx = clone[0]
                by = clone[1]
                tx = clone[2]
                ty = clone[3]
                clonefield = cc_data.CCCloningMachineControl(bx, by, tx, ty)
                CloFields.append(clonefield)
            CC_CloneMachine = cc_data.CCCloningMachineControlsField(CloFields)
        elif field["type"] == cc_data.CCEncodedPasswordField.TYPE:
            passwrod = field["password"]
            CC_password = cc_data.CCEncodedPasswordField(passwrod)
        elif field["type"] == cc_data.CCMapHintField.TYPE:
            hint = field["hint"]
            CC_hint = cc_data.CCMapHintField(hint)
        elif field["type"] == cc_data.CCMonsterMovementField.TYPE:
            MonsterFields = []
            for monster in field["monsters"]:
                x = monster[0]
                y = monster[1]
                monsterfield = cc_data.CCCoordinate(x, y)
                MonsterFields.append(monsterfield)
            CC_Monsterfield = cc_data.CCMonsterMovementField(MonsterFields)
    cc_level.add_field(CC_title)
    cc_level.add_field(CC_TrapFields)
    cc_level.add_field(CC_CloneMachine)
    cc_level.add_field(CC_password)
    cc_level.add_field(CC_hint)
    cc_level.add_field(CC_Monsterfield)
    return cc_level.optional_fields
def json2dat(json_data): 
    dic_data = json.loads(json_data) 
    ccdata = cc_data.CCDataFile() 
    for level_dic in dic_data: 
        cclevel = cc_data.CCLevel() 
        #set non-field value
        cclevel.level_number = level_dic['level_number'] 
        cclevel.time = level_dic['time'] 
        cclevel.num_chips = level_dic['num_chips'] 
        cclevel.upper_layer = level_dic['upper_layer'] 
        cclevel.lower_layer = level_dic['lower_layer']
        
        #set optional field
        if "monsters" in level_dic:
            monsters = []
            for x_y in level_dic["monsters"]:
                coord = cc_data.CCCoordinate(x_y[0], x_y[1])
                monsters.append(coord)
            monsters_field = cc_data.CCMonsterMovementField(monsters)
            cclevel.add_field(monsters_field)
        if "traps" in level_dic:
            trap_controls = []
            for i in range(len(level_dic["traps"])):
                trap_control = cc_data.CCTrapControl(level_dic["controls"][i][0],level_dic["controls"][i][1],level_dic["traps"][i][0],level_dic["traps"][i][1])
                trap_controls.append(trap_control)
            trap_controls_field = cc_data.CCTrapControlsField(trap_controls)
            cclevel.add_field(trap_controls_field)
        if "clone_machine" in level_dic:
            clones = []
            for i in range(len(level_dic["clone_machine"])):
                clone = cc_data.CCCloningMachineControl(level_dic["clone_button"][i][0], level_dic["clone_button"][i][1], level_dic["clone_machine"][i][0], level_dic["clone_machine"][i][1])
                clones.append(clone)
            clones_field = cc_data.CCCloningMachineControlsField(clones)
            cclevel.add_field(clones_field)

        #set necessary field
        map_title_field = cc_data.CCMapTitleField(level_dic['map_title']) 
        map_hint_field = cc_data.CCMapHintField(level_dic['map_hint']) 
        encoded_password_field = cc_data.CCEncodedPasswordField(level_dic['encoded_password'])

        cclevel.add_field(map_title_field)
        cclevel.add_field(map_hint_field)
        cclevel.add_field(encoded_password_field)
        #add level to ccdata
        ccdata.add_level(cclevel)
    return ccdata
Example #14
0
def make_cc_from_json(json_file):
    data = cc_data.CCDataFile()

    for level in json_file["levels"]:
        new_level = cc_data.CCLevel()

        new_level.level_number = level["level number"]
        new_level.time = level["time"]
        new_level.num_chips = level["chip number"]
        new_level.upper_layer = level["upper layer"]
        new_level.lower_layer = level["lower layer"]

        fields = make_field_from_optional(level["optional fields"])

        for field in fields:
            new_level.add_field(field)

        data.add_level(new_level)

    return data
Example #15
0
def make_level_from_json(level_data):
    """
    Reads data from a JSON file and constructs a CCLevel object
    params:
        json_file (string): file name of the JSON file to read
    returns:
        A CCLevel object constructed with the data from the given file
    """
    level = cc_data.CCLevel()
    # level.num_bytes = ?
    level.level_number = level_data["level number"]
    level.time = level_data["time"]
    level.num_chips = level_data["chip count"]
    level.upper_layer = level_data["upper layer"]
    level.lower_layer = level_data["lower layer"]
    # optional fields
    for json_field in level_data["fields"]:
        # determine which field to create based on type number.
        cc_field = make_field_from_json(json_field["type"],
                                        json_field["value"])
        # add the optional field into the field list
        level.add_field(cc_field)
    return level
Example #16
0
def make_levels_from_json(json_data):
    #Initialize a new GameLibrary
    data_file = cc_data.CCDataFile()
    for level in json_data:
        new_level = cc_data.CCLevel()
        new_level.level_number = level["level_num"]
        new_level.time = level["time"]
        new_level.num_chips = level["chip_num"]
        new_level.upper_layer = level["upper_layer"]
        lower = []
        step = 1024
        while (step > 0):
            lower.append(0)
            step -= 1
        new_level.lower_layer = lower

        for option in level["optional_layers"]:
            id = option["id"]
            if (id == 3):
                result = cc_data.CCMapTitleField(option["title"])
            if (id == 6):
                result = cc_data.CCEncodedPasswordField(option["pass"])
            if (id == 7):
                result = cc_data.CCMapHintField(option["hint"])
            if (id == 10):
                monsterList = []

                for monster in option["coord"]:

                    monsterList.append(
                        cc_data.CCCoordinate(monster[0], monster[1]))
                result = cc_data.CCMonsterMovementField(monsterList)
            new_level.optional_fields.append(result)

        data_file.add_level(new_level)

    return data_file
Example #17
0
def make_level_from_json(json_file):

    cc_level = cc_data.CCLevel()
    cc_level.level_number = json_file["level_number"]
    #cc_level.optional_fields = make_optional_fields_from_json(json_datap["optional fields"])
    return cc_level
Example #18
0
def make_ccdat_from_json(json_data):

    cc_dat = cc_data.CCDataFile()

    for level in json_data["levels"]:

        cclevel = cc_data.CCLevel()

        cclevel.level_number = level["level_number"]
        cclevel.time = level["time"]
        cclevel.num_chips = level["chip_number"]
        cclevel.upper_layer = level["upper_layer"]
        cclevel.lower_layer = level["lower_layer"]

        for field in level["option_fields"]:

            if (field["type"] == 3):

                mapfield = cc_data.CCMapTitleField(field["title"])
                cclevel.add_field(mapfield)

            elif (field["type"] == 6):

                passwordfield = cc_data.CCEncodedPasswordField(
                    field["password"])
                cclevel.add_field(passwordfield)

            elif (field["type"] == 7):

                hintfield = cc_data.CCMapHintField(field["hint"])
                cclevel.add_field(hintfield)

            elif (field["type"] == 10):

                monsters = []

                for coord in field["monsters"]:

                    ccCoord = cc_data.CCCoordinate(coord[0], coord[1])
                    monsters.append(ccCoord)

                monsterfield = cc_data.CCMonsterMovementField(monsters)
                cclevel.add_field(monsterfield)

            elif (field["type"] == 4):

                traps = []

                for trap in field["traps"]:

                    coordsbutton = trap["button_coord"]
                    coordstrap = trap["trap_coord"]

                    cctrapcontrol = cc_data.CCTrapControl(
                        coordsbutton[0], coordsbutton[1], coordstrap[0],
                        coordstrap[1])
                    traps.append(cctrapcontrol)

                trapfield = cc_data.CCTrapControlsField(traps)

                cclevel.add_field(trapfield)

            elif (field["type"] == 5):

                machines = []

                for machine in field["machines"]:

                    coordsbutton = machine["button_coord"]
                    coordsmachine = machine["machine_coord"]

                    ccmachinecontrol = cc_data.CCCloningMachineControl(
                        coordsbutton[0], coordsbutton[1], coordsmachine[0],
                        coordsmachine[1])
                    machines.append(ccmachinecontrol)

                machinesfield = cc_data.CCCloningMachineControlsField(machines)

                cclevel.add_field(machinesfield)

            else:

                continue

        cc_dat.add_level(cclevel)

    return cc_dat
Example #19
0
def make_data_file_from_json(json_data):
    data_file = cc_data.CCDataFile()

    # Parse all levels
    for level_data in json_data["levels"]:
        level = cc_data.CCLevel()
        level.level_number = level_data["level_number"]
        level.time = level_data["time"]
        level.num_chips = level_data["num_chips"]
        level.upper_layer = level_data["upper_layer"]
        level.lower_layer = level_data["lower_layer"]

        # Parse all optional fields
        for field_data in level_data["optional_fields"]:
            field = None
            type_val = field_data["type_val"]

            # Parse Map Title
            if (type_val == 3):
                title = field_data["title"]
                field = cc_data.CCMapTitleField(title)

            # Parse Traps
            elif (type_val == 4):
                traps = []
                for trap_data in field_data["traps"]:
                    bx = trap_data["bx"]
                    by = trap_data["by"]
                    tx = trap_data["tx"]
                    ty = trap_data["ty"]
                    trap = cc_data.CCTrapControl(bx, by, tx, ty)
                    traps.append(trap)
                field = cc_data.CCTrapControlsField(traps)

            # Parse Clone Machines
            elif (type_val == 5):
                machines = []
                for machine_data in field_data["machines"]:
                    bx = machine_data["bx"]
                    by = machine_data["by"]
                    tx = machine_data["tx"]
                    ty = machine_data["ty"]
                    machine = cc_data.CCCloningMachineControl(bx, by, tx, ty)
                    machines.append(machine)
                field = cc_data.CCCloningMachineControlsField(machines)

            # Parse Password
            elif (type_val == 6):
                password = field_data["password"]
                field = cc_data.CCEncodedPasswordField(password)

            # Parse Hint
            elif (type_val == 7):
                hint = field_data["hint"]
                field = cc_data.CCMapHintField(hint)

            # Parse Moving Monsters
            elif (type_val == 10):
                monsters = []
                for monster_data in field_data["monsters"]:
                    x = monster_data["x"]
                    y = monster_data["y"]
                    monster = cc_data.CCCoordinate(x, y)
                    monsters.append(monster)
                field = cc_data.CCMonsterMovementField(monsters)

            level.add_field(field)

        data_file.add_level(level)

    return data_file
Example #20
0
def load_json_to_CCDataFile(json_data):
    # ccDataFile is a container to hold all levels
    # analagous to an overall list
    ccDataFile = cc_data.CCDataFile()

    with open(json_data, "r") as reader:
        json_data = json.load(reader)

    for item in json_data:
        # ccLevel is an object that contains all info of a single level
        ccLevel = cc_data.CCLevel()
        ccLevel.level_number = item["level"]["level_number"]
        ccLevel.time = item["level"]["time_limit"]
        ccLevel.num_chips = item["level"]["chip_count"]
        ccLevel.upper_layer = item["level"]["upper_layer"]
        ccLevel.lower_layer = item["level"]["lower_layer"]

        # title, password, and hint are three basic fields that
        ccMapTitleField = cc_data.CCMapTitleField(
            item["level"]["optional_fields"]["field_3"]["title"])
        ccLevel.add_field(ccMapTitleField)
        ccEncodedPasswordField = cc_data.CCEncodedPasswordField(
            item["level"]["optional_fields"]["field_6"]["password"])
        ccLevel.add_field(ccEncodedPasswordField)
        ccMapHintField = cc_data.CCMapHintField(
            item["level"]["optional_fields"]["field_7"]["hint"])
        ccLevel.add_field(ccMapHintField)

        # adding monsters can also be
        ccMonsterMovementField = []
        for monster in item["level"]["optional_fields"]["field_10"][
                "monsters"]:
            ccMonsterMovementField.append(
                cc_data.CCCoordinate(monster[0], monster[1]))
        ccMonsterMovementField = cc_data.CCMonsterMovementField(
            ccMonsterMovementField)
        ccLevel.add_field(ccMonsterMovementField)

        # adding trap sets
        ccTrapCoordinates = []
        for trap in item["level"]["optional_fields"]["field_4"]["traps"]:
            ccTrapCoordinates.append(
                cc_data.CCTrapControl(trap[0], trap[1], trap[2], trap[3]))
        ccTrapControlsField = cc_data.CCTrapControlsField(ccTrapCoordinates)
        ccLevel.add_field(ccTrapControlsField)

        # adding cloning machines
        ccCloningMachines = []
        for machine in item["level"]["optional_fields"]["field_5"][
                "cloning machines"]:
            ccCloningMachines.append(
                cc_data.CCCloningMachineControl(machine[0], machine[1],
                                                machine[2], machine[3]))
        ccCloningMachineField = cc_data.CCCloningMachineControlsField(
            ccCloningMachines)
        ccLevel.add_field(ccCloningMachineField)

        # adding levels into the ccDataFile Object
        ccDataFile.levels.append(ccLevel)

    return ccDataFile
Example #21
0
def make_cc_data_file_from_json(json_data):
    cc_data_file = cc_data.CCDataFile()

    for json_level in json_data:
        cc_level = cc_data.CCLevel()
        cc_level.level_number = json_level["level_number"]
        cc_level.time = json_level["time"]
        cc_level.num_chips = json_level["num_chips"]

        cc_level.upper_layer = json_level["upper_layer"]
        cc_level.lower_layer = json_level["lower_layer"]

        #Handle optional fields
        json_fields = json_level["optional_fields"]
        for json_field in json_fields:
            field_type = json_field["type"]
            if field_type == "title":
                # print("title field!")
                title = json_field["title"]
                cc_title_field = cc_data.CCMapTitleField(title)
                cc_level.add_field(cc_title_field)
            elif field_type == "hint":
                # print("hint field!")
                hint = json_field["hint"]
                cc_hint_field = cc_data.CCMapHintField(hint)
                cc_level.add_field(cc_hint_field)
            elif field_type == "password":
                #  print("password field!")
                password = json_field["password"]
                cc_password_field = cc_data.CCEncodedPasswordField(password)
                cc_level.add_field(cc_password_field)
            elif field_type == "monster":
                #  print("monster field!")
                monsters = json_field["monsters"]
                cc_monster_coordinate = []
                for monster in monsters:
                    cc_monster_coordinate.append(
                        cc_data.CCCoordinate(monster[0], monster[1]))
                cc_monsters_field = cc_data.CCMonsterMovementField(
                    cc_monster_coordinate)
                cc_level.add_field(cc_monsters_field)
            elif field_type == "clone_machines":
                clone_machines = json_field["clone_machines"]
                cc_machine = []
                for clone_machine in clone_machines:
                    bx = clone_machine["button_coord"][0]
                    by = clone_machine["button_coord"][1]
                    tx = clone_machine["machine_coord"][0]
                    ty = clone_machine["machine_coord"][1]
                    cc_machine.append(
                        cc_data.CCCloningMachineControl(bx, by, tx, ty))
                cc_clone_machine_field = cc_data.CCCloningMachineControlsField(
                    cc_machine)
                cc_level.add_field(cc_clone_machine_field)
            elif field_type == "traps":
                traps = json_field["traps"]
                cc_trap = []
                for trap in traps:
                    bx = trap["button_coord"][0]
                    by = trap["button_coord"][1]
                    tx = trap["trap_coord"][0]
                    ty = trap["trap_coord"][1]
                    cc_trap.append(cc_data.CCTrapControl(bx, by, tx, ty))
                cc_trap_field = cc_data.CCTrapControlsField(cc_trap)
                cc_level.add_field(cc_trap_field)
            else:
                print("no other field")

        print(cc_level)
        cc_data_file.add_level(cc_level)

    return cc_data_file