def load_game_info(G, seed=None, path='config/game_info.yml'): info = load(path) game = tdict() game.seed = seed G.random = random.Random(seed) # G.random = TestRandom(seed) game.year = info.first_year - 1 # zero based game.last_year = info.last_year num_rounds = game.last_year - game.year game.turn_order_options = info.turn_order_options game.sequence = ['Setup'] + num_rounds * info.year_order + ['Scoring'] game.index = 0 # start below 0, so after increment in next_phase() it starts at 0 #game.action_phases = tset(x for x in info.phases if info.phases[x]) # no need for action phases anymore (all action phases have a pre phase) game.peace_dividends = tlist( sum([[v] * n for v, n in info.peace_dividends.items()], [])) G.random.shuffle(game.peace_dividends) game.victory = info.victory G.game = game G.objects = tdict() G.objects.table = tdict()
def convert_from_saveable(data): if data is None: return None if isinstance(data, (str, int, float)): try: return int(data) except: pass return data if isinstance(data, dict): if len(data) == 1: key, val = next(iter(data.items())) if 'set' == key: return tset(convert_from_saveable(el) for el in val) if 'xset' == key: return xset(convert_from_saveable(el) for el in val) if 'tuple' == key: return tuple(convert_from_saveable(el) for el in val) if '_object_' == key[:8]: typ = eval(key[8:]) obj = typ() obj.load_state(val) return obj if '_id' in data: return idict({convert_from_saveable(k): convert_from_saveable(v) for k, v in data.items()}) return tdict({convert_from_saveable(k): convert_from_saveable(v) for k, v in data.items()}) if isinstance(data, list): return tlist(convert_from_saveable(el) for el in data) try: return data.save_state() except AttributeError: raise Exception('Cannot save data of type: {}'.format(type(data)))
def load_card_decks(G, action_path='config/cards/action_cards.yml', investment_path='config/cards/investment_cards.yml', info_path='config/cards/card_info.yml'): cinfo = load(info_path) caction = load(action_path) cinvest = load(investment_path) action_cards = tdict() action_cards.deck = tlist() for ID, card in caction.items(): card = idict(card) card.obj_type = 'action_card' card.visible = tset() card.__dict__['_id'] = 'action_{}'.format(ID) action_cards.deck.append(card._id) G.objects.table[card._id] = card investment_cards = tdict() investment_cards.deck = tlist() for ID, card in cinvest.items(): card = idict(card) card.obj_type = 'investment_card' card.visible = tset() card.__dict__['_id'] = 'invest_{}'.format(ID) investment_cards.deck.append(card._id) G.objects.table[card._id] = card G.cards = tdict() G.cards.action = action_cards G.cards.action.discard_pile = tlist() G.cards.investment = investment_cards G.cards.investment.discard_pile = tlist() G.cards.info = cinfo shuffle(G.random, G.cards.investment) shuffle(G.random, G.cards.action)
def get_cards(G, stack, N=1): cards = tlist() assert N <= len(stack.deck) + len( stack.discard_pile ), 'Cannot draw {} cards from a total of {} cards'.format( N, len(stack.deck) + len(stack.discard_pile)) for _ in range(N): if len(stack.deck) == 0: shuffle(G.random, stack) cards.append(stack.deck.pop()) return cards
def pre_command_phase(G): if 'temp' in G: del G.temp G.temp = tdict() G.temp.season = G.game.sequence[G.game.index] G.temp.active_idx = 0 G.temp.active_players = G.game.turn_order.copy() if G.temp.season == 'Winter': G.temp.active_players = tlist(p for p in G.game.turn_order if G.players[p].stats.enable_Winter) G.temp.decision = tdict() G.temp.passes = 0 G.temp.borders = tdict({p: tdict() for p in G.players}) G.temp.battles = tdict() return encode_command_card_phase(G)
def load_players_and_minors(G): player_setup = load('config/faction_setup.yml') capitals = load('config/capitals.yml') G.nations = tdict() territories = tdict() designations = tdict() minor_designation = 'Minor' for tile in G.tiles.values(): if 'alligence' in tile: designations[tile.alligence] = minor_designation if tile.alligence not in territories: territories[tile.alligence] = tset() territories[tile.alligence].add(tile._id) designations['USA'] = 'Major' G.nations.designations = designations G.nations.territories = territories G.nations.capitals = capitals for nation, tilename in capitals.items(): G.tiles[tilename].capital = True G.nations.groups = tdict() # load factions/players players = tdict() groups = tset(player_setup.keys()) rivals = tdict() for g in groups: gps = groups.copy() gps.remove(g) rivals[g] = tset(gps) for name, config in player_setup.items(): faction = tdict() faction.stats = tdict() faction.stats.handlimit = config.Handlimit faction.stats.factory_all_costs = config.FactoryCost faction.stats.factory_idx = 0 faction.stats.factory_cost = faction.stats.factory_all_costs[ faction.stats.factory_idx] faction.stats.emergency_command = config.EmergencyCommand faction.stats.rivals = rivals[name] faction.stats.DoW = tdict({r: False for r in rivals[name]}) faction.stats.at_war_with = tdict({r: False for r in rivals[name]}) faction.stats.at_war = False faction.stats.aggressed = False faction.stats.peace_dividends = tlist() faction.stats.enable_USA = 'enable_USA' in config faction.stats.enable_Winter = 'enable_Winter' in config faction.cities = tdict() faction.cities.MainCapital = config.MainCapital faction.cities.SubCapitals = config.SubCapitals faction.members = tdict() for nation, info in config.members.items(): faction.members[nation] = tset([nation]) if info.type == 'Great_Power': faction.stats.great_power = nation if 'Colonies' in info: faction.members[nation].update(info.Colonies) full_cast = tset() for members in faction.members.values(): full_cast.update(members) for member in full_cast: G.nations.designations[member] = name faction.homeland = tdict( {member: tset() for member in faction.members.keys()}) faction.territory = tset() for tile_name, tile in G.tiles.items(): if 'alligence' not in tile: continue if tile.alligence in faction.members: # homeland faction.homeland[tile.alligence].add(tile_name) if tile.alligence in full_cast: faction.territory.add(tile_name) tile.owner = name faction.tracks = tdict() pop, res = compute_tracks(faction.territory, G.tiles) faction.tracks.POP = pop faction.tracks.RES = res faction.tracks.IND = config.initial_ind faction.units = tdict() faction.hand = tset() # for cards faction.technologies = tset() faction.secret_vault = tset() faction.influence = tset() faction.diplomacy = tdict() faction.diplomacy.associates = tset() faction.diplomacy.protectorates = tset() faction.diplomacy.satellites = tset() faction.diplomacy.violations = tset() players[name] = faction G.players = players # load minors/diplomacy minors = tdict() majors = tdict() status = tdict() for name, team in G.nations.designations.items(): if team not in G.nations.groups: G.nations.groups[team] = tset() G.nations.groups[team].add(name) if team in {minor_designation, 'Major'}: status[name] = tdict() status[name].is_armed = False status[name].units = tdict() if team == minor_designation: # only minors minor = tdict() minor.faction = None minor.value = 0 minors[name] = minor if team == 'Major': # only includes neutral majors major = tdict() major.faction = None major.value = 0 majors[name] = major G.diplomacy = tdict() G.diplomacy.minors = minors G.diplomacy.majors = majors G.diplomacy.neutrals = minors.copy() G.diplomacy.neutrals.update(majors) G.diplomacy.influence = tdict() G.nations.status = status
def planning_phase(G, player, action): if action is None: if 'temp' in G: del G.temp G.temp = tdict() G.temp.season = G.game.sequence[G.game.index] G.temp.active_idx = 0 G.temp.active_players = G.game.turn_order.copy() if G.temp.season == 'Winter': G.temp.active_players = tlist(p for p in G.game.turn_order if G.players[p].stats.enable_Winter) G.temp.decision = tdict() G.temp.passes = 0 G.temp.borders = tdict({p: tdict() for p in G.players}) return encode_command_card_phase(G) faction = G.players[player] head, *tail = action if head == 'pass': G.temp.passes += 1 G.temp.active_idx += 1 G.temp.active_idx %= len(G.temp.active_players) G.logger.write('{} passes'.format(player)) elif head in faction.hand: G.temp.passes = 0 card = G.objects.table[head] del card.owner G.objects.updated[head] = card G.temp.decision[player] = card faction.hand.remove(head) G.logger.write('{} plays a card'.format(player)) G.temp.active_players.remove(player) if len(G.temp.active_players): G.temp.active_idx %= len(G.temp.active_players) if len(G.temp.active_players) > G.temp.passes: return encode_command_card_phase(G) # evaluate card choices G.temp.commands = tdict() for p, card in G.temp.decision.items( ): # RULE OVERRULED: emergency priority tie breaks are automatic if 'season' in card: cmd = tdict() cmd.priority = card.priority cmd.moved = tset() if card.season == G.temp.season: val = card.value msg = ' {} command: {} {}'.format(card.season, card.priority, val) else: cmd.emergency = True val = G.players[p].stats.emergency_command msg = 'n emergency command: {} {}'.format(card.priority, val) cmd.value = val G.temp.commands[p] = cmd else: msg = ' bluff (investment card)' G.logger.write('{} has played a{}'.format(p, msg)) discard_cards(G, card._id) if len(G.temp.commands): ##someone played cmd card ##players put in order of cards: G.temp.order contains players G.temp.order = tlist( k for k, v in sorted([(k, v.priority + ('e' if 'emergency' in v else '')) for k, v in G.temp.commands.items()], key=lambda x: x[1])) G.logger.write('Play order is: {}'.format(', '.join(G.temp.order))) G.temp.active_idx = 0 add_next_phase(G, 'Movement') else: G.logger.write('No player played a command card during {}'.format( G.temp.season)) raise PhaseComplete
def make_disputed(G, tile, aggressor): tile.disputed = True tile.aggressors = tlist() tile.aggressors.append(aggressor) G.objects.updated[tile._id] = tile
def planning_phase(G, player, action): faction = G.players[player] head, *tail = action if head == 'pass': G.temp.passes += 1 G.temp.active_idx += 1 G.temp.active_idx %= len(G.temp.active_players) G.logger.write('{} passes'.format(player)) elif head in faction.hand: G.temp.passes = 0 card = G.objects.table[head] # if 'owner' del card.owner G.objects.updated[head] = card G.temp.decision[player] = card faction.hand.remove(head) G.logger.write('{} plays a card'.format(player)) G.temp.active_players.remove(player) if len(G.temp.active_players): G.temp.active_idx %= len(G.temp.active_players) if len(G.temp.active_players) > G.temp.passes: return encode_command_card_phase(G) # evaluate card choices if len(G.temp.decision) == 0: return #raise PhaseComplete G.temp.commands = tdict() for p, card in G.temp.decision.items( ): # RULE OVERRULED: emergency priority tie breaks are automatic if 'season' in card: cmd = tdict() cmd.priority = card.priority cmd.moved = tset() cmd.declarations = tset() if card.season == G.temp.season: val = card.value msg = ' {} command: {} {}'.format(card.season, card.priority, val) else: cmd.emergency = True val = G.players[p].stats.emergency_command msg = 'n emergency command: {} {}'.format(card.priority, val) cmd.value = val G.temp.commands[p] = cmd else: msg = ' bluff (investment card)' G.logger.write('{} has played a{}'.format(p, msg)) discard_cards(G, card._id) if len(G.temp.commands): G.temp.order = tlist( k for k, v in sorted([(k, v.priority + ('e' if 'emergency' in v else '')) for k, v in G.temp.commands.items()], key=lambda x: x[1])) G.logger.write('Play order is: {}'.format(', '.join(G.temp.order))) G.temp.active_idx = 0 else: G.logger.write('No player played a command card during {}'.format( G.temp.season))