def load_status(self, name=None, path=None): self.graph = load_yaml(self.graph_path) self._get_base_region = {f'{base}-{coast}':base for base, node in self.graph.items() if 'fleet' in node['edges'] and isinstance(node['edges']['fleet'], dict) for coast in node['edges']['fleet']} self._get_base_region.update({base: base for base in self.graph}) self._get_base_region.update({f'{base}-c': base for base, node in self.graph.items() # if 'fleet' in node['edges'] and 'army' in node['edges']}) if 'fleet' in node['edges'] and node["type"] == "coast"}) self._get_base_region.update({base.lower(): val for base, val in self._get_base_region.items()}) self._get_region = {f'{base}-{coast}': f'{base}-{coast}' for base, node in self.graph.items() if 'fleet' in node['edges'] and isinstance(node['edges']['fleet'], dict) for coast in node['edges']['fleet']} self._get_region.update({base: base for base in self.graph}) self._get_region.update({base.lower(): val for base, val in self._get_region.items()}) self._unit_texts = {'a': 'army', 'f': 'fleet', 'army': 'army', 'fleet': 'fleet'} bad_names = [name for name in self._get_region if any(name.startswith(f'{u} ') for u in self._unit_texts)] if len(bad_names): raise BadNamesError(bad_names) if name is not None: if not name.endswith('.yaml'): name = f'{name}.yaml' path = self.states_root / name if path is not None and path.exists(): self.set_state(load_yaml(path)) else: self.set_state(self._find_latest_state(self.states_root)) print(f'Loaded state: {self.time}') return self.time
def _load_bot_data(self, path): self.persistent = load_yaml(path) if path.exists() else {} if 'players' not in self.persistent: self.persistent['players'] = {} if 'roles' not in self.persistent: self.persistent['roles'] = {} if 'channels' not in self.persistent: self.persistent['channels'] = {} self.player_users = self._find_discord_objects( self.persistent['players'], self.guild.members) if len(self.player_users): print(f'Found {len(self.player_users)} members that are players.') self.player_roles = self._find_discord_objects( self.persistent['roles'], self.guild.roles) if len(self.player_roles): print(f'Found {len(self.player_roles)} roles for players.') self.player_channels = self._find_discord_objects( self.persistent['channels'], self.guild.channels) if len(self.player_channels): print( f'Found {len(self.player_channels)} channels for player orders.' )
def load_raw_info(path): '''Loads the info yaml file''' raw = load_yaml(path) if os.path.isfile(path) else None if raw is None: raw = {} raw['info_path'] = path # automatically set info_path to the path raw['info_dir'] = os.path.dirname(path) return raw
def _find_actions_from_state(self, state, persistent=True): actions_path = self._get_action_path() if actions_path.exists(): actions = load_yaml(actions_path) actions = {player: {action['loc']: action for action in acts} for player, acts in actions.items()} return actions actions = self.generate_actions_from_state(state) if persistent: save_yaml(actions, actions_path) return actions
def _load_map_info(self, nodes_path, edges_path): nodes = load_yaml(nodes_path) coasts = util.separate_coasts(nodes) self.coasts = coasts for ID, coast in coasts.items(): origin = coast['coast-of'] if 'coasts' not in nodes[origin]: nodes[origin]['coasts'] = [] nodes[origin]['coasts'].append(ID) for ID, node in nodes.items(): node['ID'] = ID edges = load_yaml(edges_path) return nodes, edges
def _find_latest_state(self, state_root, persistent=True): state_paths = list(state_root.glob('*')) if not len(state_paths): state = self.generate_initial_state() if persistent: save_yaml(state, state_root / '1-1.yaml') return state times = {} for path in state_paths: year, season, *other = path.stem.split('-') times[int(year), int(season), len(other) and other[0] == 'r'] = path latest = times[max(times.keys())] return load_yaml(latest)
def _load_config_from_path(self, path, process=True): ''' Load the yaml file and transform data to a config object Generally, ``get_config`` should be used instead of this method :param path: must be the full path to a yaml file :param process: if False, the loaded yaml data is passed without converting to a config object :return: loaded data from path (usually as a config object) ''' # path = find_config_path(path) data = load_yaml(path) # TODO setup global enable other file types if data is None: data = {} if process: return configurize(data) return data
def _load_graph_info(self, graph_path): graph = load_yaml(graph_path) if graph_path.endswith('.yaml') else load_json(graph_path) for info in graph.values(): if 'army' in info['edges']: info['army-edges'] = info['edges']['army'] if 'fleet' in info['edges']: info['fleet-edges'] = info['edges']['fleet'] coasts = {} edges = {'army': {name: info['army-edges'] for name, info in graph.items() if 'army-edges' in info}} fleet = {} edges['fleet'] = fleet for name, info in graph.items(): if 'fleet-edges' in info: es = info['fleet-edges'] if isinstance(es, dict): info['coasts'] = [] for coast, ces in es.items(): coast = self.encode_region_name(name=name, coast=coast) coasts[coast] = {'name': coast, 'type': 'coast', 'coast-of': name, 'dir': coast} info['coasts'].append(coast) fleet[coast] = ces else: if info['type'] == 'coast': coast = self.encode_region_name(name=name, node_type='coast', unit_type='fleet') coasts[coast] = {'name': coast, 'type': 'coast', 'coast-of': name} info['coasts'] = [coast] fleet[name] = es nodes = graph self.graph = graph self.coasts = coasts return nodes, edges
def __init__(self, A, **kwargs): super().__init__(A, **kwargs) self.base_path = Path(A.pull('renderbase-path')) assert self.base_path.exists(), 'No render base found"' self.base_root = self.base_path.parents[0] self.overlay_path = self.base_root / 'overlay.png' self.skip_control = A.pull('skip-control', False) self.label_props = A.pull('label-props', {}) if self.label_props.get('bbox', {}).get('facecolor', '') is None: self.label_props['bbox']['facecolor'] = 'none' if self.label_props.get('bbox', {}).get('edgecolor', '') is None: self.label_props['bbox']['edgecolor'] = 'none' self.coast_label_props = A.pull('coast-label-props', {}) if self.coast_label_props.get('bbox', {}).get('facecolor', '') is None: self.coast_label_props['bbox']['facecolor'] = 'none' if self.coast_label_props.get('bbox', {}).get('edgecolor', '') is None: self.coast_label_props['bbox']['edgecolor'] = 'none' self.unit_shapes = A.pull('unit-shapes', {'army': 'o', 'fleet': 'v'}) self.unit_props = A.pull('unit-props', {}) self.sc_props = A.pull('sc-props', {}) self.capital_props = A.pull('capital-props', {}) self.home_props = A.pull('home-props', {}) self.retreat_props = A.pull('retreat-props', {}) if self.retreat_props.get('mfc', '') is None: self.retreat_props['mfc'] = 'none' self.retreat_arrow = A.pull('retreat-arrow', {}) self.disband_props = A.pull('disband-props', {}) self.arrow_ratio = A.pull('arrow-ratio', 0.9) self.build_props = A.pull('build-props', {}) if self.build_props.get('mfc', '') is None: self.build_props['mfc'] = 'none' self.hold_props = A.pull('hold-props', {}) if self.hold_props.get('mfc', '') is None: self.hold_props['mfc'] = 'none' self.move_arrow = A.pull('move-arrow', {}) self.support_props = A.pull('support-props', {}) self.support_arrow = A.pull('support-arrow', {}) self.support_dot = A.pull('support-dot', None) self.support_defend_props = A.pull('support-defend-props', {}) self.convoy_props = A.pull('convoy-props', {}) self.convoy_arrow = A.pull('convoy-arrow', {}) self.convoy_dot = A.pull('convoy-dot', None) self.retreat_action_props = A.pull('retreat-action-arrow', {}) self.graph_path = A.pull('graph-path', None) self.regions_path = A.pull('regions-path', None) self.bg_path = A.pull('bgs-path', None) self.players_path = A.pull('players-path', None) self.players = load_yaml(self.players_path) self.capitals = {info['capital']: name for name, info in self.players.items() if 'capital' in info} self.config = A self._known_action_drawers = { 'build': self._draw_build, 'retreat': self._draw_retreat, 'disband': self._draw_disband, # disband due to retreat 'destroy': self._draw_destroy, # disband during winter 'hold': self._draw_hold, 'move': self._draw_move, 'support': self._draw_support, 'support-defend': self._draw_support_defend, 'convoy-move': self._draw_move, 'convoy-transport': self._draw_convoy, }
def load_checkpoint(self, path, ident='dataset'): path = Path(path) path = path / f'{ident}.yaml' self.info = load_yaml(str(path)) self.epoch_seed = self.info.get('epoch_seed', self.epoch_seed)
def _load_player_info(player_path): return load_yaml(player_path)