from noita.models import LogfileSubmission, Objectiv from noita.services.decode import parse_log, resolve from collections import Counter def parse_objectives_from_logfile(logfile: LogfileSubmission) -> Counter: """Parse a log file, and output a count for each ID.""" file_data = logfile.file.read().decode() ids = [] for entry in parse_log(file_data): idx, _seed = resolve(entry["hash"], entry["ts"]) if idx: ids.append(idx) return Counter(ids) def parse_objectives_and_store(logfile: LogfileSubmission) -> None: """Parse a logfile and store output.""" if not logfile.user: return counter = parse_objectives_from_logfile(logfile) for idx, count in counter.items(): if idx in {"-", "DEBUG", "polylan-mod"}: continue obj, created = Objectiv.objects.get_or_create( objectiv_id=idx, user=logfile.user, ) obj.count += count obj.save(update_fields=["count"])