master.py: update trophies in a lossless fashion (#82)

This commit is contained in:
Lephe 2021-07-08 17:42:08 +02:00
parent c4c9421beb
commit 59236cac70
Signed by untrusted user: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
1 changed files with 34 additions and 15 deletions

View File

@ -156,28 +156,47 @@ def update_forums():
def update_trophies():
raise Exception("Not implemented as data-lossless for now")
existing = Trophy.query.all()
# Clean up trophies
trophies("clear")
# Create base trophies
# Get the list of what we want to obtain
tr = []
with open(os.path.join(app.root_path, "data", "trophies.yaml")) as fp:
tr = yaml.safe_load(fp.read())
tr = { t["name"]: t for t in tr }
for t in tr:
description = t.get("description", "")
if t["is_title"]:
trophy = Title(t["name"], description, t["hidden"],
t.get("css", ""))
else:
trophy = Trophy(t["name"], description, t["hidden"])
db.session.add(trophy)
# Remove trophies that we don't want or that we want as a different type
for t in existing:
if t.name not in tr or isinstance(t, Title) != tr[t.name]["is_title"]:
kind = "title" if isinstance(t, Title) else "trophy"
print(f"[trophies] Deleted '{t.name}' ({kind})")
db.session.delete(t)
db.session.commit()
print(f"Created {len(tr)} trophies.")
# Add missing trophies
for name, t in tr.items():
description = t.get("description", "")
trophy = Trophy.query.filter_by(name=name).first()
# Updating existing trophies
if trophy is not None:
changes = (trophy.description != description) or \
(trophy.hidden != t["hidden"])
trophy.description = description
trophy.hidden = t["hidden"]
if changes:
print(f"[trophies] Updated '{name}'")
# Add missing ones
elif t["is_title"]:
trophy = Title(name, description, t["hidden"], t.get("css", ""))
print(f"[trophies] Created '{name}' (title)")
else:
trophy = Trophy(name, description, t["hidden"])
print(f"[trophies] Created '{name}' (trophy)")
db.session.add(trophy)
db.session.commit()
def generate_trophy_icons():