aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/data/audit_npc_names.py (2060B)


      1 import collections
      2 import json
      3 import pathlib
      4 
      5 import ruamel.yaml
      6 
      7 
      8 def audit(zones):
      9     # Rank unresolved names by affected instances, retaining their district context.
     10     unresolved = {}
     11     instances = collections.Counter()
     12     for zone in zones:
     13         for district in zone['districts']:
     14             for group in district['groups']:
     15                 instances['total'] += len(group['positions'])
     16                 if not group['name'].startswith('Placeholder '):
     17                     continue
     18                 instances['unresolved'] += len(group['positions'])
     19                 entry = unresolved.setdefault(group['template_code'], {
     20                     'template_code': group['template_code'], 'instances': 0, 'locations': [],
     21                 })
     22                 entry['instances'] += len(group['positions'])
     23                 location = {'playfield': zone['id'], 'name': zone['name'], 'district': district['name']}
     24                 if location not in entry['locations']:
     25                     entry['locations'].append(location)
     26     return {
     27         'total_instances': instances['total'],
     28         'unresolved_name_instances': instances['unresolved'],
     29         'unresolved_codes': sorted(unresolved.values(), key=lambda entry: (-entry['instances'], entry['template_code'])),
     30     }
     31 
     32 
     33 def main():
     34     yaml = ruamel.yaml.YAML(typ='safe')
     35     paths = sorted(pathlib.Path('server/gamedata/zones').glob('zone_*.yaml'),
     36                    key=lambda path: int(path.stem.removeprefix('zone_')))
     37     report = audit(yaml.load(path.read_text()) for path in paths)
     38 
     39     # This inventory reads maintained definitions, not a regenerated candidate.
     40     pathlib.Path('local/reports').mkdir(parents=True, exist_ok=True)
     41     output = pathlib.Path('local/reports/npc-name-audit.json')
     42     output.write_text(json.dumps(report, indent=2) + '\n')
     43     print('Unresolved names:', report['unresolved_name_instances'], 'of', report['total_instances'],
     44           'instances across', len(report['unresolved_codes']), 'codes')
     45     print('Wrote', output)
     46 
     47 
     48 if __name__ == '__main__':
     49     main()