Skip to content

Commit a778a22

Browse files
committed
Build data generation script now merges new build data with existing build data correctly
It also now takes the sc2reader project dir as a parameter and writes build data files directly to correct locations in the sc2reader project
1 parent 7291d99 commit a778a22

File tree

1 file changed

+66
-15
lines changed

1 file changed

+66
-15
lines changed

generate_build_data.py

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,47 +155,98 @@ def generate_build_data(balance_data_path):
155155
sorted_units = collections.OrderedDict(sorted(units.items(), key=lambda x: int(x[0])))
156156
sorted_abilities = collections.OrderedDict(sorted(abilities.items(), key=lambda x: int(x[0])))
157157

158-
unit_lookup = [[unit_name, unit_name] for _, unit_name in sorted_units.items()]
158+
unit_lookup = dict((unit_name, unit_name) for _, unit_name in sorted_units.items())
159159

160160
return sorted_units, sorted_abilities, unit_lookup, ability_lookup
161161

162162

163+
def combine_lookups(old_unit_lookup, old_ability_lookup, new_unit_lookup, new_ability_lookup):
164+
unit_lookup = collections.OrderedDict(old_unit_lookup)
165+
ability_lookup = collections.OrderedDict(old_ability_lookup)
166+
167+
# Just straightforwardly add any missing units
168+
unit_lookup.update(new_unit_lookup)
169+
170+
# When merging old and new ability lookups, prefer to overwrite old cell data with new cell data when merging rows
171+
# in the case of a key clash, but preserve old cell data if cell is empty in new ability lookup table
172+
for ability_name, commands in new_ability_lookup.items():
173+
if ability_name not in ability_lookup:
174+
ability_lookup[ability_name] = commands
175+
else:
176+
for i, command in enumerate(commands):
177+
# Pad potential gaps with empty commands
178+
while len(ability_lookup[ability_name]) <= i:
179+
ability_lookup[ability_name].append("")
180+
181+
if command != "":
182+
ability_lookup[ability_name][i] = command
183+
184+
return unit_lookup, ability_lookup
185+
186+
163187
def main():
164-
parser = argparse.ArgumentParser(description='Generate [BUILD_VERSION]_abilities.csv, [BUILD_VERSION]_units.csv,'
165-
' ability_lookup.csv and unit_lookup.csv files from exported balance'
166-
' data.')
188+
parser = argparse.ArgumentParser(description='Generate and install new [BUILD_VERSION]_abilities.csv, '
189+
'[BUILD_VERSION]_units.csv, and update ability_lookup.csv and '
190+
'unit_lookup.csv files with any new units and ability commands.')
191+
parser.add_argument('expansion', metavar='EXPANSION', type=str, choices=['WoL', 'HotS', 'LotV'],
192+
help='the expansion level of the balance data export, one of \'WoL\', \'HotS\', or \'LotV\'')
167193
parser.add_argument('build_version', metavar='BUILD_VERSION', type=int,
168194
help='the build version of the balance data export')
169195
parser.add_argument('balance_data_path', metavar='BALANCE_DATA_PATH', type=str,
170196
help='the path to the balance data export')
197+
parser.add_argument('project_path', metavar='SC2READER_PROJECT_PATH', type=str,
198+
help='the path to the root of the sc2reader project directory')
171199

172200
args = parser.parse_args()
173201

174-
units, abilities, unit_lookup, ability_lookup = generate_build_data(args.balance_data_path)
202+
units, abilities, new_unit_lookup, new_ability_lookup = generate_build_data(args.balance_data_path)
175203

176204
if not units or not abilities:
177205
parser.print_help()
178206
print("\n")
179207

180208
raise ValueError("No balance data found at provided balance data path.")
181209

182-
with open('{}_units.csv'.format(args.build_version), 'w', newline='') as csvfile:
183-
csv_writer = csv.writer(csvfile, delimiter=',', lineterminator=os.linesep)
210+
unit_lookup_path = os.path.join(args.project_path, 'sc2reader', 'data', 'unit_lookup.csv')
211+
with open(unit_lookup_path, 'r') as file:
212+
csv_reader = csv.reader(file, delimiter=',', lineterminator=os.linesep)
213+
old_unit_lookup = collections.OrderedDict([(row[0], row[1]) for row in csv_reader if len(row) > 1])
214+
215+
ability_lookup_path = os.path.join(args.project_path, 'sc2reader', 'data', 'ability_lookup.csv')
216+
with open(ability_lookup_path, 'r') as file:
217+
csv_reader = csv.reader(file, delimiter=',', lineterminator=os.linesep)
218+
old_ability_lookup = collections.OrderedDict([(row[0], row[1:]) for row in csv_reader if len(row) > 0])
219+
220+
if not old_unit_lookup or not old_ability_lookup:
221+
parser.print_help()
222+
print("\n")
223+
224+
raise ValueError("Could not find existing unit or ability lookups. Is the sc2reader project path correct?")
225+
226+
unit_lookup, ability_lookup = combine_lookups(
227+
old_unit_lookup, old_ability_lookup, new_unit_lookup, new_ability_lookup)
228+
229+
units_file_path = os.path.join(
230+
args.project_path, 'sc2reader', 'data', args.expansion, '{}_units.csv'.format(args.build_version))
231+
with open(units_file_path, 'w') as file:
232+
csv_writer = csv.writer(file, delimiter=',', lineterminator=os.linesep)
184233
for unit_index, unit_name in units.items():
185234
csv_writer.writerow([unit_index, unit_name])
186235

187-
with open('{}_abilities.csv'.format(args.build_version), 'w', newline='') as csvfile:
188-
csv_writer = csv.writer(csvfile, delimiter=',', lineterminator=os.linesep)
236+
abilities_file_path = os.path.join(
237+
args.project_path, 'sc2reader', 'data', args.expansion, '{}_abilities.csv'.format(args.build_version))
238+
with open(abilities_file_path, 'w') as file:
239+
csv_writer = csv.writer(file, delimiter=',', lineterminator=os.linesep)
189240
for ability_index, ability_name in abilities.items():
190241
csv_writer.writerow([ability_index, ability_name])
191242

192-
with open('unit_lookup.csv'.format(args.build_version), 'w', newline='') as csvfile:
193-
csv_writer = csv.writer(csvfile, delimiter=',', lineterminator=os.linesep)
194-
for lookup_entry in unit_lookup:
195-
csv_writer.writerow(lookup_entry)
243+
with open(unit_lookup_path, 'w') as file:
244+
csv_writer = csv.writer(file, delimiter=',', lineterminator=os.linesep)
245+
for entry in unit_lookup.items():
246+
csv_writer.writerow(list(entry))
196247

197-
with open('ability_lookup.csv'.format(args.build_version), 'w', newline='') as csvfile:
198-
csv_writer = csv.writer(csvfile, delimiter=',', lineterminator=os.linesep)
248+
with open(ability_lookup_path, 'w') as file:
249+
csv_writer = csv.writer(file, delimiter=',', lineterminator=os.linesep)
199250
for ability_name, commands in ability_lookup.items():
200251
csv_writer.writerow([ability_name] + commands)
201252

0 commit comments

Comments
 (0)