master.py: update trophies in a lossless fashion (#82)
This commit is contained in:
parent
c4c9421beb
commit
59236cac70
49
master.py
49
master.py
|
@ -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():
|
||||
|
|
Loading…
Reference in New Issue