Last active
April 19, 2020 05:50
-
-
Save rxw1/41aed7247b9ac1fa0cf6a335cc2b8f7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3.7 | |
import argparse | |
import json | |
import re | |
import vdf | |
import sys | |
from os.path import expanduser | |
# ./dota.py -f AttackRate | sort -k2 -g | |
# ./dota.py -f HasAggressiveStance | sort -gk2 | |
# ./dota.py -f ProjectileSpeed | sort -gk2 | grep -C10 viper | |
# for i in $(./dota.py -l | grep -i speed); do echo \# $i; ./dota.py -f $i maiden meepo viper riki | sort -k2 -g; echo; done | |
# ./dota.py -f StatusHealthRegen | sort -gk2 | |
def getopt(opt=None): | |
parser = argparse.ArgumentParser(description="Dota Heroes") | |
_add_arguments(parser) | |
args = parser.parse_args() | |
if opt: | |
if opt not in args: | |
raise ValueError("Opt {} not found in args {}.".format(opt, args)) | |
return vars(args)[opt] | |
return parser.parse_args() | |
def _add_arguments(parser): | |
parser.add_argument('-f', nargs='?', dest="fields", | |
help='compare field') | |
parser.add_argument("-s", action="store_true", default=False, dest="show_stats", | |
help="show stats") | |
parser.add_argument("-l", action="store_true", default=False, dest="list", | |
help="list fields") | |
parser.add_argument("-r", action="store_true", default=False, dest="show_role", | |
help="compare roles") | |
parser.add_argument("-j", action="store_true", default=False, dest="json", | |
help="output json") | |
parser.add_argument("heroes", nargs=argparse.REMAINDER) | |
def get_heroes(): | |
u = "/.local/share/Steam/steamapps/common/dota 2 " + "beta/game/dota/scripts/npc/npc_heroes.txt" | |
vdfsrc = vdf.load(open(expanduser("~") + u)) | |
m = re.compile("^npc_dota_hero_.*(" + "|".join(getopt("heroes")) + ").*$", re.IGNORECASE) | |
return {k: v for k, v in vdfsrc["DOTAHeroes"].items() if m.match(k)} | |
heroes = get_heroes(); | |
if getopt("fields"): | |
field = getopt("fields") | |
for h in heroes.items(): | |
try: | |
name = h[0][14:] | |
value = h[1][field] | |
print("{:20} {:8.2f}".format(name, float(value))) | |
except KeyError: | |
#print("field {} not found on {}".format(field, name), file=sys.stderr) | |
value = 0.0 | |
pass | |
except ValueError: | |
pass | |
except NameError: | |
pass | |
except TypeError: | |
pass | |
elif getopt("list"): | |
all_fields = {} | |
for h in heroes.items(): | |
all_fields = all_fields | h[1].keys() | |
for i in sorted(all_fields): | |
print(i) | |
elif getopt("show_stats"): | |
for h in heroes.items(): | |
name = h[0][14:] | |
print("{:-<76}".format("\n--- " + name + " ")) | |
for f in h[1].keys(): | |
try: | |
print("{:30}: {}".format(f, h[1][f])) | |
except KeyError: | |
pass | |
elif getopt("show_role"): | |
for h in heroes.items(): | |
name = h[0][14:] | |
roles = zip(h[1]["Role"].split(","), h[1]["Rolelevels"].split(",")) | |
print("\n{}".format(name)) | |
for role, level in roles: | |
print("\t\t{:10} {}".format(role, level)) | |
#print("{:-<76}".format("\n--- " + name + " ")) | |
#print("{:30}: {}".format(f, h[1][f])) | |
#for f in fields: | |
# try: | |
# except KeyError: | |
# pass | |
elif getopt("json"): | |
print(json.dumps(heroes, ensure_ascii=False)) | |
else: | |
print(getopt()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment