Пример #1
0
def create_item(db_session, record):
    print("Loading item types...")
    item_types = {}
    with open(os.path.join(app_root, "app/database/data/item_types.json"),
              "r") as file:
        data = json.load(file)
        for item_type_record in data:
            item_type = (db_session.query(ModelItemTypeTranslation).filter_by(
                locale="en", name=item_type_record["en"]).one().item_type)
            item_types[item_type_record["en"]] = item_type
    item = ModelItem(
        dofus_db_id=record["dofusID"],
        item_type_id=item_types[record["itemType"]].uuid,
        level=record["level"],
        image_url=record["imageUrl"],
    )
    if "conditions" in record:
        conditions = {
            "conditions": record["conditions"].get("conditions", {}),
            "customConditions":
            record["conditions"].get("customConditions", {}),
        }
        item.conditions = conditions
    db_session.add(item)
    db_session.flush()

    for locale in record["name"]:
        item_translations = ModelItemTranslation(
            item_id=item.uuid,
            locale=locale,
            name=record["name"][locale],
        )
        db_session.add(item_translations)
    create_item_stats(db_session, record, item)
    create_weapon_stat(db_session, record, item)
Пример #2
0
def create_item_translations(db_session, record, item):
    db_session.query(ModelItemTranslation).filter_by(
        item_id=item.uuid).delete()
    for locale in languages:
        if record["name"][locale]:
            item_translation = ModelItemTranslation(
                item_id=item.uuid, locale=locale, name=record["name"][locale])
            db_session.add(item_translation)
Пример #3
0
def create_item(db_session, record):
    item_types = {}
    with open(os.path.join(app_root, "app/database/data/item_types.json"),
              "r") as file:
        data = json.load(file)
        for item_type_record in data:
            item_type = (db_session.query(ModelItemTypeTranslation).filter_by(
                locale="en", name=item_type_record["en"]).one().item_type)
            item_types[item_type_record["en"]] = item_type
    if record["itemType"] == "Living object":
        return False
    item = ModelItem(
        dofus_db_id=record["dofusID"],
        item_type_id=item_types[record["itemType"]].uuid,
        level=record["level"],
        image_url=record["imageUrl"],
    )

    if record.get("setID", None):
        set = (db_session.query(ModelSet).filter(
            ModelSet.dofus_db_id == record["setID"]).one())
        set.items.append(item)

    if "conditions" in record:
        conditions = {
            "conditions": record["conditions"].get("conditions", {}),
            "customConditions":
            record["conditions"].get("customConditions", {}),
        }
        item.conditions = conditions
    db_session.add(item)
    db_session.flush()

    for locale in record["name"]:
        if record["name"][locale] == None:
            continue
        item_translations = ModelItemTranslation(
            item_id=item.uuid,
            locale=locale,
            name=record["name"][locale],
        )
        db_session.add(item_translations)
    create_item_stats(db_session, record, item)
    create_weapon_stat(db_session, record, item)
    return True
def add_prysmaradite_translations():
    print("Loading item types...")
    item_types = {}
    with open(os.path.join(app_root, "app/database/data/prysmaradites.json"),
              "r") as file:
        data = json.load(file)

        with session_scope() as db_session:
            for r in data:
                en_translation = db_session.query(
                    ModelItemTranslation).filter_by(locale="en",
                                                    name=r["name"]["en"])
                item = en_translation.one().item
                for locale in locales:
                    if (not db_session.query(ModelItemTranslation).filter_by(
                            locale, item_id=item.id).exists()):
                        new_translation = ModelItemTranslation(
                            item_id=item.id,
                            locale=locale,
                            name=r["name"][locale])
                        db_session.add(new_translation)
Пример #5
0
def update_name(db_session, file_name):
    with open(
            os.path.join(app_root,
                         "app/database/data/{}.json".format(file_name)),
            "r") as file:
        data = json.load(file)
        name_to_record_map = {}

        if file_name in item_file_names:
            for r in data:
                name_to_record_map[r["name"]["en"]] = r
        elif file_name == "spells":
            for r in data:
                for pair in r["spells"]:
                    for spell in pair:
                        name_to_record_map[spell["name"]["en"]] = spell
        else:
            raise ValueError("Invalid file name")

        while True:
            old_name = input(
                "Enter the old name of the record (as it exists in the db) that you would like to update or 'q' to quit: "
            )
            if old_name == "q":
                return
            new_name = input(
                "Enter the new name of the record (as it should appear) that you would like to update: "
            )

            translations = None
            if file_name in item_file_names:
                translations = (db_session.query(ModelItemTranslation).filter(
                    ModelItemTranslation.locale == "en",
                    ModelItemTranslation.name == old_name,
                ).all())
            elif file_name == "spells":
                translations = (db_session.query(ModelSpellTranslation).filter(
                    ModelSpellTranslation.locale == "en",
                    ModelSpellTranslation.name == old_name,
                ).all())
            else:
                raise ValueError("Invalid file name")

            if new_name not in name_to_record_map:
                print(
                    "Error: The new name does not exist within the data files."
                )
            elif len(translations) > 1:
                print(
                    "Error: Multiple records with the old name exist in the db."
                )
            elif len(translations) == 0:
                print(
                    "Error: Record with the provided old name does not exist in the db"
                )
            else:
                print("Updating names for {}".format(new_name))
                if file_name in item_file_names:
                    item = translations[0].item
                    record = name_to_record_map[new_name]
                    db_session.query(ModelItemTranslation).filter_by(
                        item_id=item.uuid).delete()
                    for locale in name_to_record_map[new_name]["name"]:
                        if record["name"].get(locale):
                            item_translation = ModelItemTranslation(
                                item_id=item.uuid,
                                locale=locale,
                                name=record["name"][locale],
                            )
                            db_session.add(item_translation)
                elif file_name == "spells":
                    spell = translations[0].spell
                    record = name_to_record_map[new_name]
                    db_session.query(ModelSpellTranslation).filter_by(
                        spell_id=spell.uuid).delete()
                    for locale in name_to_record_map[new_name]["name"]:
                        if record["name"][locale]:
                            spell_translation = ModelSpellTranslation(
                                spell_id=spell.uuid,
                                locale=locale,
                                name=record["name"][locale],
                                description=record["description"][locale],
                            )
                            db_session.add(spell_translation)
                else:
                    raise ValueError("Invalid file name")
Пример #6
0
            )
            db.session.add(item)

            conditions = {
                "conditions":
                record["conditions"].get("conditions", None),
                "customConditions":
                record["conditions"].get("customConditions", None),
            }
            item.conditions = conditions

            for locale in record["name"]:
                if record["name"][locale] == None:
                    continue
                item_translations = ModelItemTranslation(
                    locale=locale,
                    name=record["name"][locale],
                )
                db.session.add(item_translations)
                item.item_translations.append(item_translations)

            try:
                i = 0
                for stat in record.get("stats", []):
                    item_stat = ModelItemStat(
                        stat=to_stat_enum[stat["stat"]],
                        min_value=stat["minStat"],
                        max_value=stat["maxStat"],
                        order=i,
                    )
                    db.session.add(item_stat)
                    item.stats.append(item_stat)
Пример #7
0
def add_mounts():
    print("Adding mounts to database")
    with open(os.path.join(app_root, "app/database/data/mounts.json"), "r") as file:
        data = json.load(file)
        for record in data:
            item = ModelItem(
                dofus_db_id=record["dofusID"],
                item_type=item_types[record["itemType"]],
                level=record["level"],
                image_url=record["imageUrl"],
            )

            for locale in record["name"]:
                item_translations = ModelItemTranslation(
                    item_id=item.uuid, locale=locale, name=record["name"][locale],
                )
                db.session.add(item_translations)
                item.item_translations.append(item_translations)

            try:
                i = 0
                for stat in record["stats"]:
                    item_stat = ModelItemStat(
                        stat=to_stat_enum[stat["stat"]],
                        min_value=stat["minStat"],
                        max_value=stat["maxStat"],
                        order=i,
                    )
                    db.session.add(item_stat)
                    item.stats.append(item_stat)
                    i = i + 1

                db.session.add(item)

            except KeyError as err:
                print("KeyError occurred:", err)

        db.session.commit()

    with open(os.path.join(app_root, "app/database/data/rhineetles.json"), "r") as file:
        data = json.load(file)
        for record in data:
            item = ModelItem(
                dofus_db_id=record["dofusID"],
                item_type=item_types[record["itemType"]],
                level=record["level"],
                image_url=record["imageUrl"],
            )

            for locale in record["name"]:
                item_translations = ModelItemTranslation(
                    item_id=item.uuid, locale=locale, name=record["name"][locale],
                )
                db.session.add(item_translations)
                item.item_translations.append(item_translations)

            try:
                i = 0
                for stat in record["stats"]:
                    item_stat = ModelItemStat(
                        stat=to_stat_enum[stat["stat"]],
                        min_value=stat["minStat"],
                        max_value=stat["maxStat"],
                        order=i,
                    )
                    db.session.add(item_stat)
                    item.stats.append(item_stat)
                    i = i + 1

                db.session.add(item)

            except KeyError as err:
                print("KeyError occurred:", err)

        db.session.commit()
Пример #8
0
def add_pets():
    print("Adding pets to database")
    with open(os.path.join(app_root, "app/database/data/pets.json"), "r") as file:
        data = json.load(file)
        for record in data:
            item = ModelItem(
                dofus_db_id=record["dofusID"],
                item_type=item_types[record["itemType"]],
                level=record["level"],
                image_url=record["imageUrl"],
            )
            db.session.add(item)

            conditions = {
                "conditions": record["conditions"]["conditions"],
                "customConditions": record["conditions"]["customConditions"],
            }
            item.conditions = conditions

            for locale in record["name"]:
                item_translations = ModelItemTranslation(
                    item_id=item.uuid, locale=locale, name=record["name"][locale],
                )
                db.session.add(item_translations)
                item.item_translations.append(item_translations)

            try:
                i = 0
                for stat in record["stats"]:
                    item_stat = ModelItemStat(
                        stat=to_stat_enum[stat["stat"]],
                        min_value=stat["minStat"],
                        max_value=stat["maxStat"],
                        order=i,
                    )
                    db.session.add(item_stat)
                    item.stats.append(item_stat)
                    i = i + 1
                if record["customStats"] != {} and record["customStats"] != []:
                    num_of_stats = len(record["customStats"]["en"])

                    for j in range(num_of_stats):
                        item_stat = ModelItemStat(order=i)
                        for locale in record["customStats"]:
                            custom_stat = record["customStats"][locale][j]
                            stat_translation = ModelItemStatTranslation(
                                item_stat_id=item_stat.uuid,
                                locale=locale,
                                custom_stat=custom_stat,
                            )
                            db.session.add(stat_translation)
                            item_stat.item_stat_translation.append(stat_translation)

                        db.session.add(item_stat)
                        item.stats.append(item_stat)
                        i = i + 1

                # If this item belongs in a set, query the set and add the relationship to the record
                if record["setID"]:
                    set = record["setID"]
                    set_record = (
                        db.session.query(ModelSet)
                        .filter(ModelSet.dofus_db_id == set)
                        .first()
                    )
                    set_record.items.append(item)
                    db.session.merge(set_record)
            except KeyError as err:
                print("KeyError occurred:", err)

        db.session.commit()