public
Last active

Character import for shadowcraft engine

  • Download Gist
import_character.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
# -*- coding: utf-8 -*-
from os import path
import sys
import pprint
import shelve
 
sys.path.append(path.abspath(path.join(path.dirname(__file__), '..')))
 
from wowapi.api import WoWApi
 
wowapi = WoWApi()
pp = pprint.PrettyPrinter(indent=4)
 
 
class ItemDB:
def __init__(self):
pass
 
def get_item(self, id):
return None
 
def add_item(self, id, item):
pass
 
def close(self):
pass
 
 
 
class SimpleItemDB(ItemDB):
def __init__(self, path):
self.path = path
self.db = shelve.open(path, writeback = True)
 
def get_item(self, id):
str_id = str(id)
if str_id in self.db:
return self.db[str_id]
else:
return None
 
def add_item(self, id, item):
str_id = str(id)
self.db[str_id] = item
self.sync() # FIXME it's best to call it once, at the end of script with close()
 
def close(self):
self.db.close()
 
def sync(self):
self.db.sync()
 
 
item_db = SimpleItemDB('item_db')
 
 
def get_item_cached(region, id):
cached_item = item_db.get_item(id)
if cached_item:
return cached_item
else:
item = wowapi.get_item(region, id)
item_db.add_item(id, item)
return item
 
class CharacterData:
races = {1 : 'human',
2 : 'orc',
3 : 'dwarf',
4 : 'night_elf',
5 : 'undead',
6 : 'tauren',
7 : 'gnome',
8 : 'troll',
9 : 'goblin',
10 : 'blood_elf',
11 : 'draenie',
22 : 'worgen',
24 : 'pandaren'}
 
enchants = {4441 : 'windsong'}
 
def __init__(self, region, realm, name):
self.region = region
self.realm = realm
self.name = name
self.raw_data = None
 
def do_import(self):
self.raw_data = wowapi.get_character(self.region , self.realm, self.name, ['talents', 'items', 'stats'])
 
def get_race(self):
return CharacterData.races[self.raw_data[u'data'][u'race']]
 
def get_weapon(self, weapon_data, item_data):
weapon_info = weapon_data['data'][u'weaponInfo']
damage_info = weapon_info[u'damage']
damage = (damage_info[u'max'] + damage_info[u'min']) / 2
speed = weapon_info[u'weaponSpeed']
# FIXME: need better weapon type recognition here, don't know how to tell weapon type using api
if speed <= 1.8:
type = 'dagger'
else:
type = 'axe'
enchant = CharacterData.enchants[item_data[u'tooltipParams'][u'enchant']]
return [damage, speed, type, enchant]
 
def get_mh(self):
item_data = self.raw_data['data'][u'items'][u'mainHand']
weapon_data = get_item_cached(self.region, item_data[u'id'])
# weapon_data = get_item_cached(self.region, 85924)
ret = self.get_weapon(weapon_data, item_data)
return ret
 
def get_oh(self):
item_data = self.raw_data['data'][u'items'][u'offHand']
weapon_data = get_item_cached(self.region, item_data[u'id'])
ret = self.get_weapon(weapon_data, item_data)
return ret
 
def get_procs(self):
# TODO
return ['windswept_pages', 'searing_words']
#return ['relic_of_xuen', 'searing_words']
# return ['relic_of_xuen', 'windswept_pages']
 
def get_stats(self):
stats_data = self.raw_data['data'][u'stats']
agi = stats_data[u'agi']
str = stats_data[u'str']
ap = stats_data[u'attackPower']
crit = stats_data[u'critRating']
hit = stats_data[u'hitRating']
exp = stats_data[u'expertiseRating']
haste = stats_data[u'hasteRating']
mast = stats_data[u'masteryRating']
ret = [str, agi, ap - 2 * agi, crit, hit, exp, haste, mast]
# ret = [str, agi + 956, 250, crit, hit, exp, haste, mast]
# pp.pprint(ret)
return ret

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.