def _getdenomvalues(itemsdict, pricesource): """Return a mapping to convert between denominations""" denomtoidx = tf2api.getalldenoms() denoms = tuple(denomtoidx.keys()) getprice = lambda denom: _correctprice( itemsdict[denomtoidx[denom]]['marketprice'][pricesource]['Unique'] .split()[0], denoms[denoms.index(denom) + 1]) table = {'Earbuds': {'Key': getprice('Earbuds')}, 'Key': {'Refined': getprice('Key')}, 'Refined': {'Reclaimed': 3.0}, 'Reclaimed': {'Scrap': 3.0}, 'Scrap': {'Weapon': 2.0}, 'Weapon': {}} def fill(from_, to=None, value=1): if to is None: to = from_ table[from_][to] = value table[to][from_] = 1 / value if denoms.index(to) + 1 < len(denoms): next_ = denoms[denoms.index(to) + 1] fill(from_, next_, value * table[to][next_]) for denom in denoms: fill(denom) return table
def _getdenom(word): """Parse a word and return a price denomination if it matches one""" denomslist = ('bud', 'key', 'ref', 'rec', 'scrap', 'we') denoms = dict(zip(denomslist, tf2api.getalldenoms().keys())) hasdenom = re.search('|'.join(denomslist), word.lower()) if hasdenom: return denoms[hasdenom.group(0)]
def _getpriceasitems(amount, denom, todenom, itemsdict, pricesource): """Return a list of itemdicts that visualize a given price and a dict with the count of each item.""" items = [] amount = _correctprice(amount, denom) denomtoidx = tf2api.getalldenoms() denoms = tuple(denomtoidx.keys()) denomtable = _getdenomvalues(itemsdict, pricesource) if todenom: amount *= denomtable[denom][todenom] else: todenom = denom # Move to the highest possible denomination for d in denoms: value = amount * denomtable[denom][d] if value >= 1: amount = value todenom = d break if amount <= 4000: denomidx = denoms.index(todenom) # Get count of each denomination and add items to results for i, d in enumerate(denoms[denomidx:], denomidx): count = int(round(amount, 10)) if count: items.append({'item': itemsdict[denomtoidx[d]], 'denom': d, 'count': count}) if i + 1 < len(denoms): amount = (amount - count) * denomtable[d][denoms[i + 1]] return items