def get_characters(*cats): cats = set(cats) chars = set() pos, neg = partition(lambda c: not c.startswith('-'), cats) chars.update(*[characters_by_category[c] for c in pos]) chars.difference_update(*[characters_by_category['-' + c] for c in pos]) chars.difference_update(*[characters_by_category[c.strip('-')] for c in neg]) chars = list(sorted(chars, key=lambda i: i.__name__)) return chars
def apply_action(self): g = Game.getgame() params = self.params from thb.cards import Deck g.stats = [] g.deck = Deck() g.ehclasses = [] if params['random_force']: seed = get_seed_for(g.players) random.Random(seed).shuffle(g.players) g.draw_extra_card = params['draw_extra_card'] f1 = BatchList() f2 = BatchList() g.forces = BatchList([f1, f2]) H, M = Identity.TYPE.HAKUREI, Identity.TYPE.MORIYA for p, id, f in zip(g.players, [H, H, M, M], [f1, f1, f2, f2]): p.identity = Identity() p.identity.type = id p.force = f f.append(p) pl = g.players for p in pl: g.process_action(RevealIdentity(p, pl)) roll_rst = roll(g, self.items) f1, f2 = partition(lambda p: p.force is roll_rst[0].force, roll_rst) final_order = [f1[0], f2[0], f2[1], f1[1]] g.players[:] = final_order g.emit_event('reseat', None) # ban / choose girls --> from . import characters chars = characters.get_characters('common', '2v2') seed = get_seed_for(g.players) random.Random(seed).shuffle(chars) # ANCHOR(test) testing = list(settings.TESTING_CHARACTERS) testing, chars = partition(lambda c: c.__name__ in testing, chars) chars.extend(testing) chars = chars[-20:] choices = [CharChoice(cls) for cls in chars] banned = set() mapping = {p: choices for p in g.players} with InputTransaction('BanGirl', g.players, mapping=mapping) as trans: for p in g.players: c = user_input([p], ChooseGirlInputlet(g, mapping), timeout=30, trans=trans) c = c or [_c for _c in choices if not _c.chosen][0] c.chosen = p banned.add(c.char_cls) trans.notify('girl_chosen', (p, c)) assert len(banned) == 4 g.stats.extend([{ 'event': 'ban', 'attributes': { 'gamemode': g.__class__.__name__, 'character': i.__name__ } } for i in banned]) chars = [_c for _c in chars if _c not in banned] g.random.shuffle(chars) if Game.CLIENT_SIDE: chars = [None] * len(chars) for p in g.players: p.choices = [CharChoice(cls) for cls in chars[-4:]] p.choices[-1].as_akari = True del chars[-4:] p.reveal(p.choices) g.pause(1) mapping = {p: p.choices for p in g.players} with InputTransaction('ChooseGirl', g.players, mapping=mapping) as trans: ilet = ChooseGirlInputlet(g, mapping) @ilet.with_post_process def process(p, c): c = c or p.choices[0] trans.notify('girl_chosen', (p, c)) return c rst = user_input(g.players, ilet, timeout=30, type='all', trans=trans) # reveal for p, c in rst.items(): c.as_akari = False g.players.reveal(c) g.set_character(p, c.char_cls) # ------- for p in g.players: log.info( u'>> Player: %s:%s %s', p.__class__.__name__, Identity.TYPE.rlookup(p.identity.type), p.account.username, ) # ------- g.emit_event('game_begin', g) for p in g.players: g.process_action(DistributeCards(p, amount=4)) for i, p in enumerate(cycle(pl)): if i >= 6000: break if not p.dead: g.emit_event('player_turn', p) try: g.process_action(PlayerTurn(p)) except InterruptActionFlow: pass return True
def apply_action(self): g = Game.getgame() params = self.params from thb.cards import Deck g.stats = [] g.deck = Deck() g.ehclasses = [] if params['random_force']: seed = get_seed_for(g.players) random.Random(seed).shuffle(g.players) g.draw_extra_card = params['draw_extra_card'] f1 = BatchList() f2 = BatchList() g.forces = BatchList([f1, f2]) H, M = Identity.TYPE.HAKUREI, Identity.TYPE.MORIYA for p, id, f in zip(g.players, [H, H, M, M], [f1, f1, f2, f2]): p.identity = Identity() p.identity.type = id p.force = f f.append(p) pl = g.players for p in pl: g.process_action(RevealIdentity(p, pl)) roll_rst = roll(g, self.items) f1, f2 = partition(lambda p: p.force is roll_rst[0].force, roll_rst) final_order = [f1[0], f2[0], f2[1], f1[1]] g.players[:] = final_order g.emit_event('reseat', None) # ban / choose girls --> from . import characters chars = characters.get_characters('common', '2v2') seed = get_seed_for(g.players) random.Random(seed).shuffle(chars) testing = list(settings.TESTING_CHARACTERS) testing, chars = partition(lambda c: c.__name__ in testing, chars) chars.extend(testing) chars = chars[-20:] choices = [CharChoice(cls) for cls in chars] banned = set() mapping = {p: choices for p in g.players} with InputTransaction('BanGirl', g.players, mapping=mapping) as trans: for p in g.players: c = user_input([p], ChooseGirlInputlet(g, mapping), timeout=30, trans=trans) c = c or [_c for _c in choices if not _c.chosen][0] c.chosen = p banned.add(c.char_cls) trans.notify('girl_chosen', (p, c)) assert len(banned) == 4 g.stats.extend([ {'event': 'ban', 'attributes': { 'gamemode': g.__class__.__name__, 'character': i.__name__ }} for i in banned ]) chars = [_c for _c in chars if _c not in banned] g.random.shuffle(chars) if Game.CLIENT_SIDE: chars = [None] * len(chars) for p in g.players: p.choices = [CharChoice(cls) for cls in chars[-4:]] p.choices[-1].akari = True del chars[-4:] p.reveal(p.choices) g.pause(1) mapping = {p: p.choices for p in g.players} with InputTransaction('ChooseGirl', g.players, mapping=mapping) as trans: ilet = ChooseGirlInputlet(g, mapping) @ilet.with_post_process def process(p, c): c = c or p.choices[0] trans.notify('girl_chosen', (p, c)) return c rst = user_input(g.players, ilet, timeout=30, type='all', trans=trans) # reveal for p, c in rst.items(): c.akari = False g.players.reveal(c) g.set_character(p, c.char_cls) # ------- for p in g.players: log.info( u'>> Player: %s:%s %s', p.__class__.__name__, Identity.TYPE.rlookup(p.identity.type), p.account.username, ) # ------- g.emit_event('game_begin', g) for p in g.players: g.process_action(DistributeCards(p, amount=4)) for i, p in enumerate(cycle(pl)): if i >= 6000: break if not p.dead: g.emit_event('player_turn', p) try: g.process_action(PlayerTurn(p)) except InterruptActionFlow: pass return True
def GetHoldingsContext(userprofile, as_of_date=None): as_of_date = as_of_date or datetime.date.today() holdingdetails = userprofile.GetHoldingDetails().between( as_of_date - datetime.timedelta(days=1), as_of_date).select_related('security', 'account').order_by('security', 'account', '-day') account_data = [] for (account, security), holdings in groupby(holdingdetails, lambda h: (h.account, h.security)): holdings = list(holdings) if len(holdings) == 2: account_data.append(holdings[0] - holdings[1]) elif holdings[0].day == as_of_date: account_data.append(holdings[0] - 0) elif holdings[0].day == as_of_date - datetime.timedelta(days=1): account_data.append(0 - holdings[0]) else: raise HoldingDetail.MultipleObjectsReturned #messages.error('Database corruption error code 126.') #messages.debug('Failed to calculate a holding change for {} {}'.format(account, security)) book_value_list = userprofile.get_book_value_by_account_security( as_of_date) holding_data = [] for security, holdings in groupby(account_data, lambda h: h.security): h = sum(holdings) h.account_data = [d for d in account_data if d.security == security] h.book_value = 0 for account_datum in h.account_data: try: account_datum.book_value = next( -val for a, s, val in book_value_list if account_datum.account.id == a and account_datum.security.symbol == s) account_datum.book_value *= account_datum.account.joint_share account_datum.total_value_gain = account_datum.value - account_datum.book_value h.book_value += account_datum.book_value except StopIteration: continue h.account_data.sort(key=lambda d: d.value, reverse=True) if h.book_value: h.total_value_gain = h.value - h.book_value holding_data.append(h) # Sort by currency first, then symbol. Keep cash at the end (currency = '') holding_data = sorted(holding_data, key=lambda h: (h.security.currency, h.value), reverse=True) cash_types = Security.cash.values_list('symbol', flat=True) holding_data, cash_data = partition( lambda h: h.security.symbol in cash_types, holding_data) total_holdings = sum(account_data) total_holdings.book_value = sum( getattr(a, 'book_value', 0) for a in account_data) total_holdings.total_value_gain = total_holdings.value - total_holdings.book_value context = { 'holding_data': holding_data, 'cash_data': cash_data, 'total': total_holdings } accounts = {h.account for h in holdingdetails} accounts = { a.display_name: { 'id': a.id, 'cur_balance': sum(h.value for h in holdingdetails if h.account == a and h.day == as_of_date), 'cur_cash_balance': sum(h.value for h in holdingdetails if h.account == a and h.day == as_of_date and h.security.symbol in cash_types), 'today_balance_change': sum(h.value for h in holdingdetails if h.account == a and h.day == as_of_date) - \ sum(h.value for h in holdingdetails if h.account == a and h.day < as_of_date) } for a in accounts } context['accounts'] = accounts from collections import defaultdict total = defaultdict(int) for _, d in accounts.items(): for key in ['cur_balance', 'cur_cash_balance', 'today_balance_change']: total[key] += d[key] context['account_total'] = total exchange_live, exchange_delta = Security.objects.get( symbol='USD').GetTodaysChange() context.update({ 'exchange_live': exchange_live, 'exchange_delta': exchange_delta }) return context