Mass editing jsons - Python
Posted: Tue Aug 31, 2021 7:47 am
I have opened this topic to "teach"/FYI anyone who is interested the power of python scripts to modify multiple files with complex logics if needed.
so all that you can not do with mass replace feature (written here: viewtopic.php?f=205&t=6120)
Example: Upscaling trigger values in maps
this example upscales all AOF map json triggers in "test" folder so all trigger effect affecting armor/hp/power will be upscaled as we did for units on the end of 2020.
here the short python code
Example: Mass setting unit sight based on speed value
We needed to update all units in AOMW to have sight to be at least as movement value.
i used this script that updates file where sight < movement speed. and it set the sight to movement speed.
so all that you can not do with mass replace feature (written here: viewtopic.php?f=205&t=6120)
Example: Upscaling trigger values in maps
this example upscales all AOF map json triggers in "test" folder so all trigger effect affecting armor/hp/power will be upscaled as we did for units on the end of 2020.
here the short python code
Code: Select all
from os import listdir
from os.path import isfile, join
import json
path_work="test/"
path_workout="output/"
onlyfiles = [f for f in listdir(path_work) if isfile(join(path_work, f))]
for filename in onlyfiles :
with open(path_work+filename) as f:
data = json.load(f)
if 'triggers' in data:
was=False
for p_triggers in data['triggers']:
if 'conditionsOrEffects' in p_triggers:
for p_conditionsOrEffects in p_triggers['conditionsOrEffects']:
if p_conditionsOrEffects['typeID'] == 1020:
p_conditionsOrEffects['int1'] = p_conditionsOrEffects['int1'] * 3;
was=True
if p_conditionsOrEffects['typeID'] == 1019:
p_conditionsOrEffects['int1'] = p_conditionsOrEffects['int1'] * 4;
was=True
if p_conditionsOrEffects['typeID'] == 1031:
p_conditionsOrEffects['int1'] = p_conditionsOrEffects['int1'] * 4;
was=True
if p_conditionsOrEffects['typeID'] == 1028:
p_conditionsOrEffects['int1'] = p_conditionsOrEffects['int1'] * 2;
was=True
if p_conditionsOrEffects['typeID'] == 1029:
p_conditionsOrEffects['int1'] = p_conditionsOrEffects['int1'] * 2;
was=True
if was:
print("was change:" + filename)
f_out = open(path_workout+filename, 'w')
f_out.write(json.dumps(data, separators=(',', ':')))
print("all files written\n")
We needed to update all units in AOMW to have sight to be at least as movement value.
i used this script that updates file where sight < movement speed. and it set the sight to movement speed.
Code: Select all
from os import listdir
from os.path import isfile, join
import json
path_units="units/"
path_units_out="units/out/"
onlyfiles = [f for f in listdir(path_units) if isfile(join(path_units, f))]
for filename in onlyfiles :
with open(path_units+filename) as f:
replace_string = ""
replace_to_string = ""
data = json.load(f)
if 'unitStatSheet' in data:
if 'unit' in data['unitStatSheet']:
if 'rangeWalk' in data['unitStatSheet']['unit'] and 'sight' in data['unitStatSheet']['unit']:
if data['unitStatSheet']['unit']['rangeWalk']>data['unitStatSheet']['unit']['sight']:
replace_string = "\"sight\":"+str(data['unitStatSheet']['unit']['sight'])
replace_to_string = "\"sight\":"+str(data['unitStatSheet']['unit']['rangeWalk'])
if replace_string != "":
fin = open(path_units+filename, "rt")
tempFile = open(path_units_out+filename, "wt")
for line in fin:
tempFile.write( line.replace( replace_string, replace_to_string ) )
tempFile.close()
print("done: " + filename)
print("ENDing\n")