tools/data/npc_assignments.py (4685B)
1 import io 2 import json 3 import pathlib 4 import re 5 6 import ruamel.yaml 7 8 9 def load_assignments(): 10 return json.loads(pathlib.Path(__file__).with_suffix('.json').read_text()) 11 12 13 def group_assignments(catalog): 14 # Context variants, rather than the catalog's most common selection, own groups. 15 result = {} 16 for code, entry in catalog.items(): 17 for variant in entry['variants']: 18 status = variant['confidence'] 19 if status in ['medium', 'low', 'fallback']: 20 status = 'best_effort' 21 fields = {'name': variant['name'], 'monster_data': 0, 'appearance': variant['appearance'], 22 'appearance_status': status} 23 if variant['asset']['kind'] == 'monster_data': 24 fields['monster_data'] = variant['asset']['id'] 25 else: 26 assert variant['asset']['kind'] == 'static_mesh' 27 fields['static_mesh'] = variant['asset']['id'] 28 for identity in variant['groups']: 29 assert identity not in result, identity 30 result[identity] = {'code': code, 'fields': fields, 'evidence': variant} 31 return result 32 33 34 def load_reviewed_groups(): 35 # Maintained comments retain the finer per-group evidence behind catalog variants. 36 result = {} 37 yaml = ruamel.yaml.YAML(typ='safe') 38 for path in sorted(pathlib.Path('server/gamedata/zones').glob('zone_*.yaml')): 39 for district in yaml.load(path.read_text())['districts']: 40 for group in district['groups']: 41 fields = {field: group[field] for field in ['name', 'monster_data', 'appearance', 'appearance_status', 'comment']} 42 for field in ['static_mesh', 'visual', 'shop_template']: 43 if field in group: 44 fields[field] = group[field] 45 result[group['id']] = {'code': group['template_code'], 'fields': fields} 46 return result 47 48 49 def apply_to_population(population, assignments): 50 for playfield in population['playfields']: 51 for district in playfield['districts']: 52 for group in district['groups']: 53 choice = assignments[group['id']] 54 assert group['template_code'] == choice['code'], group['id'] 55 group.update(choice['fields']) 56 57 58 def merge_zone(text, assignments): 59 # Replace only reviewed scalar fields; preserve all other source text and edits. 60 yaml = ruamel.yaml.YAML() 61 yaml.allow_unicode = False 62 yaml.width = 100 63 current = ruamel.yaml.YAML(typ='safe').load(text) 64 groups = {group['id']: group for district in current['districts'] for group in district['groups']} 65 boundaries = list(re.finditer(r'^ - template_code:|^ source_index:|^- name:|^source:', text, re.MULTILINE)) 66 for index in range(len(boundaries)-1, -1, -1): 67 start = boundaries[index] 68 if not start.group().startswith(' - template_code:'): 69 continue 70 end = boundaries[index+1].start() if index+1 < len(boundaries) else len(text) 71 block = text[start.start():end] 72 identity = yaml.load(re.search(r'^ id: (.+)$', block, re.MULTILINE).group(1)) 73 code = yaml.load(block.splitlines()[0].split(':', 1)[1]) 74 choice = assignments[identity] 75 assert code == choice['code'], identity 76 for field, value in choice['fields'].items(): 77 if groups[identity].get(field) == value: 78 continue 79 output = io.StringIO() 80 yaml.dump({field: value}, output) 81 rendered = ''.join(' ' + line + '\n' for line in output.getvalue().splitlines()) 82 pattern = re.compile(r'^ '+field+r':.*?(?=^ [a-z_]+:|\Z)', re.MULTILINE|re.DOTALL) 83 if pattern.search(block): 84 block = pattern.sub(lambda match: rendered, block, count=1) 85 else: 86 assert field == 'static_mesh', (identity, field) 87 block = block.replace(' appearance:', rendered+' appearance:', 1) 88 text = text[:start.start()] + block + text[end:] 89 return text 90 91 92 def main(): 93 expected = group_assignments(load_assignments()) 94 reviewed = load_reviewed_groups() 95 assert expected.keys() == reviewed.keys() 96 for identity, choice in expected.items(): 97 assert reviewed[identity]['code'] == choice['code'], identity 98 for field, value in choice['fields'].items(): 99 assert reviewed[identity]['fields'].get(field) == value, (identity, field) 100 assert reviewed[identity]['fields']['comment'], identity 101 print('Verified', len(reviewed), 'context assignments against the complete handoff catalog') 102 103 104 if __name__ == '__main__': 105 main()