Skip to content

Instantly share code, notes, and snippets.

@wokka1
Last active September 14, 2024 05:40
Show Gist options
  • Save wokka1/8a53f9603fe06dae86588ac19ee6f148 to your computer and use it in GitHub Desktop.
Save wokka1/8a53f9603fe06dae86588ac19ee6f148 to your computer and use it in GitHub Desktop.
import json
from argparse import ArgumentParser
import shutil
parser = ArgumentParser()
parser.add_argument("-f", "--file", dest="filename",
help="Read in existing ore pool FILE, makes a backup .bak copy, modifies each ore value upward, based on multiplier. Writes back the changes to the original file", metavar="FILE")
parser.add_argument("-m", dest="multi",
help="Multiplier value, integers only")
parser.add_argument("-p", dest="place",
help="Placeholder for zero values. Many ores can be 0, but if you want to put something in it's place, say 10, and that gets multiplied, this will fix you up. Integers only.")
args = parser.parse_args()
# checking for bad input
if args.multi == "0" or args.multi == None:
# nothing to do, they don't want to increase the value
print("You entered 0 for the multiplier, nothing to do, exiting, please play again.")
exit()
# if values are 0, then set a base value of placeholder
if args.place == "0" or args.place == None:
# no place holder, leaving 0's
placeholder = 0
multi = int(args.multi)
else:
multi = int(args.multi)
place = int(args.place)
placeholder = place * multi
# create a backup of the original, name it as .bak
backup = args.filename + ".bak"
try:
shutil.copyfile(args.filename,backup)
except:
print("File : ",args.filename," not found, check syntax")
exit()
# open up the ore file
with open(args.filename) as f:
json_data = json.load(f)
# Iterate through each top-level key in the JSON
for ore in json_data:
# Check if the value associated with the key is a dictionary
if isinstance(json_data[ore], dict):
# Iterate through the inner dictionary
for tile in json_data[ore]:
# Example modification: Increment each value by 1
if isinstance(json_data[ore][tile], (int, float)):
if json_data[ore][tile] == 0:
json_data[ore][tile] = placeholder
else:
json_data[ore][tile] *= multi
# Save the modified data back to a file
print ("Writing output to",args.filename)
with open(args.filename, 'w') as file:
json.dump(json_data, file, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment