import random import locale import db import items import dice class GemArt(items.Item): def __init__(self, name, count, plural, costs, type): items.Item.__init__(self, name, count, plural=plural, disparate_costs=costs, display_cost=True) self.type = type @staticmethod def strsum(total): return locale.format('%d', total, 1) @staticmethod def multival(sums): outvals = dict((s, sums.count(s)) for s in set(sums)) outstrs = ('%d x %s gp' % (outvals[o], GemArt.strsum(o)) if outvals[o] > 1 else '%s gp' % GemArt.strsum(o) for o in sorted(outvals.keys())) return ', '.join(outstrs) @staticmethod def generate(type, realm=None): typevals = db.valueqry(type) def types(count): items = [] for i in range(count): tvroll = dice.tableroll(typevals) total = dice.rolldbdice(tvroll) typens = (db.namerealmqry(type, tvroll['id'], realm) if realm else db.nameqry(type, tvroll['id'])) tnroll = random.choice(typens) name, plural = tnroll['name'], tnroll['plural'] enames = tuple(i[0] for i in items) if name not in enames: items.append([name, plural, [total]]) else: items[enames.index(name)][2].append(total) newitems = (GemArt(i[0], len(i[2]), i[1], i[2], type) for i in items) return newitems return types def __repr__(self): if len(self.disparate_costs) == 1: cost = GemArt.strsum(self.disparate_costs[0]) return '%s (%s gp)' % (self.name, cost) else: costs = GemArt.multival(self.disparate_costs) count = len(self.disparate_costs) return '%d %s (%s)' % (count, self.plural, costs) class Gem(GemArt): def __init__(self, name, count, plural, costs): GemArt.__init__(self, name, count, plural, costs, 'gem') @staticmethod def generate(realm): return GemArt.generate('gem', realm) class Art(GemArt): def __init__(self, name, count, plural, costs): GemArt.__init__(self, name, count, plural, costs, 'art') @staticmethod def generate(): return GemArt.generate('art', None)