def initialize_galaxy_graph(self): start_time = time.process_time() self.galaxy_graph = nx.Graph() with datamodel.get_db_session(self.game_id) as session: for system in session.query(datamodel.System): assert isinstance(system, datamodel.System) self.galaxy_graph.add_node( system.system_id_in_game, name=system.get_name(), country=GalaxyMapData.UNCLAIMED, system_id=system.system_id, pos=[-system.coordinate_x, -system.coordinate_y], ) for hl in session.query(datamodel.HyperLane).all(): sys_one, sys_two = ( hl.system_one.system_id_in_game, hl.system_two.system_id_in_game, ) self.galaxy_graph.add_edge(sys_one, sys_two, country=self.UNCLAIMED) self._prepare_system_shapes() logger.info( f"Initialized galaxy graph in {time.process_time() - start_time} seconds." )
def history_page(game_id="", version=None): show_old_version_notice = version is not None and utils.is_old_version( version) matches = datamodel.get_known_games(game_id) if not matches: logger.warning(f"Could not find a game matching {game_id}") return render_template("404_page.html", game_not_found=True, game_name=game_id) game_id = matches[0] games_dict = datamodel.get_available_games_dict() country = games_dict[game_id]["country_name"] event_filter = get_event_filter() with datamodel.get_db_session(game_id) as session: dict_builder = EventTemplateDictBuilder(session, game_id, event_filter) events, title, details, links = dict_builder.get_event_and_link_dicts() wars = dict_builder.get_war_list() return render_template( "history_page.html", game_name=game_id, country=country, wars=wars, events=events, details=details, links=links, title=title, is_filtered_page=not event_filter.is_empty_filter, show_old_version_notice=show_old_version_notice, version=utils.VERSION, update_version_id=version, )
def get_current_execution_plot_data( game_name: str, country_perspective: Optional[int] = None ) -> "PlotDataManager": """Update and retrieve the PlotDataManager object stored for the requested game. :param game_name: The exact name of a game for which a database is available :return: """ global _CURRENT_EXECUTION_PLOT_DATA if game_name not in _CURRENT_EXECUTION_PLOT_DATA: with datamodel.get_db_session(game_name) as session: game = session.query(datamodel.Game).filter_by(game_name=game_name).first() if not game: logger.warning(f"Warning: Game {game_name} could not be found in database!") plot_specifications = [ ps for pslist in get_plot_specifications_for_tab_layout().values() for ps in pslist ] _CURRENT_EXECUTION_PLOT_DATA[game_name] = PlotDataManager( game_name, plot_specifications ) _CURRENT_EXECUTION_PLOT_DATA[game_name].initialize() _CURRENT_EXECUTION_PLOT_DATA[game_name].country_perspective = country_perspective _CURRENT_EXECUTION_PLOT_DATA[game_name].update_with_new_gamestate() return _CURRENT_EXECUTION_PLOT_DATA[game_name]
def update_content(tab_value, search, date_fraction, dash_plot_checklist, country_perspective): config.CONFIG.normalize_stacked_plots = ("normalize_stacked_plots" in dash_plot_checklist) game_id, matches = _get_game_ids_matching_url(search) if not matches: logger.warning(f"Could not find a game matching {game_id}") return render_template("404_page.html", game_not_found=True, game_name=game_id) game_id = matches[0] games_dict = datamodel.get_available_games_dict() if game_id not in games_dict: logger.warning(f"Game ID {game_id} does not match any known game!") return [] logger.info( f"dash_server.update_content: Tab is {tab_value}, Game is {game_id}") with datamodel.get_db_session(game_id) as session: current_date = utils.get_most_recent_date(session) children = [] if tab_value in visualization_data.THEMATICALLY_GROUPED_PLOTS: plots = visualization_data.THEMATICALLY_GROUPED_PLOTS[tab_value] plot_data = visualization_data.get_current_execution_plot_data( game_id, country_perspective) for plot_spec in plots: figure_data = get_figure_data(plot_data, plot_spec) if not figure_data: continue figure_layout = get_figure_layout(plot_spec) figure_layout["title"] = plot_spec.title figure = go.Figure(data=figure_data, layout=figure_layout) children.append( html.Div( [ dcc.Graph( id=plot_spec.plot_id, figure=figure, style=dict(textAlign="center"), ) ], style=dict( margin="auto", width=f"{config.CONFIG.plot_width}px", height=f"{config.CONFIG.plot_height}px", ), )) else: slider_date = 0.01 * date_fraction * current_date children.append(get_galaxy(game_id, slider_date)) children.append( html.P( f"Galaxy Map at {datamodel.days_to_date(slider_date)}", style=TEXT_STYLE, )) return children
def adjust_slider_values(tab_value, search): _, matches = _get_game_ids_matching_url(search) with datamodel.get_db_session(matches[0]) as session: max_date = utils.get_most_recent_date(session) if tab_value == config.GALAXY_MAP_TAB: marks = {0: "2200.01.01", 100: datamodel.days_to_date(max_date)} for x in range(20, 100, 20): marks[x] = datamodel.days_to_date(x / 100 * max_date) logger.info(f"Setting marks to {marks}") return marks else: return {}
def _get_system_ids_by_owner(self, time_days) -> Dict[str, Set[int]]: owned_systems = set() systems_by_owner = {GalaxyMapData.UNCLAIMED: set()} with datamodel.get_db_session(self.game_id) as session: for system in session.query(datamodel.System): country = system.get_owner_country_at(time_days) country = self._country_display_name(country) owned_systems.add(system.system_id_in_game) if country not in systems_by_owner: systems_by_owner[country] = set() systems_by_owner[country].add(system.system_id_in_game) systems_by_owner[GalaxyMapData.UNCLAIMED] |= ( set(self.galaxy_graph.nodes) - owned_systems) return systems_by_owner
def update_country_select_options(search): game_id, _ = _get_game_ids_matching_url(search) games_dict = datamodel.get_available_games_dict() if game_id not in games_dict: logger.warning(f"Game ID {game_id} does not match any known game!") return [] options = [{"label": "None", "value": None}] with datamodel.get_db_session(game_id) as session: for c in session.query(datamodel.Country): if ( c.is_real_country() and (c.has_met_player() or config.CONFIG.show_everything) and not c.is_other_player ): options.append({"label": c.country_name, "value": c.country_id_in_game}) return options
def initialize_galaxy_graph(self): start_time = time.clock() self.galaxy_graph = nx.Graph() with datamodel.get_db_session(self.game_id) as session: for system in session.query(datamodel.System): assert isinstance( system, datamodel.System) # to remove pycharm warnings self.galaxy_graph.add_node( system.system_id_in_game, name=system.name, country=GalaxyMapData.UNCLAIMED, pos=[-system.coordinate_x, -system.coordinate_y], ) for hl in session.query(datamodel.HyperLane).all(): sys_one, sys_two = ( hl.system_one.system_id_in_game, hl.system_two.system_id_in_game, ) self.galaxy_graph.add_edge(sys_one, sys_two, country=self.UNCLAIMED) logger.debug( f"Initialized galaxy graph in {time.clock() - start_time} seconds." )
def update_content( tab_value, search, date_fraction, dash_plot_checklist, country_perspective ): config.CONFIG.normalize_stacked_plots = ( "normalize_stacked_plots" in dash_plot_checklist ) game_id, matches = _get_game_ids_matching_url(search) if not matches: logger.warning(f"Could not find a game matching {game_id}") return render_template("404_page.html", game_not_found=True, game_name=game_id) game_id = matches[0] games_dict = datamodel.get_available_games_dict() if game_id not in games_dict: logger.warning( f"Game ID {game_id} does not match any known game! (URL parameter {search})" ) return [] logger.info(f"dash_server.update_content: Tab is {tab_value}, Game is {game_id}") with datamodel.get_db_session(game_id) as session: current_date = utils.get_most_recent_date(session) children = [] if tab_value in config.CONFIG.tab_layout: plots = visualization_data.get_plot_specifications_for_tab_layout().get( tab_value ) plot_data = visualization_data.get_current_execution_plot_data( game_id, country_perspective ) for plot_spec in plots: if not plot_spec: continue # just in case it's possible to sneak in an invalid ID start = time.time() figure_data = get_raw_plot_data_dicts(plot_data, plot_spec) end = time.time() logger.debug( f"Prepared figure {plot_spec.title} in {end - start:5.3f} seconds." ) if not figure_data: continue figure_layout = get_figure_layout(plot_spec) figure_layout["title"] = plot_spec.title figure = go.Figure(data=figure_data, layout=figure_layout) children.append( html.Div( [ dcc.Graph( id=plot_spec.plot_id, figure=figure, style=dict(textAlign="center"), ) ], style=dict(margin="auto", width=f"{config.CONFIG.plot_width}px",), ) ) else: slider_date = 0.01 * date_fraction * current_date children.append( html.Div( [get_galaxy(game_id, slider_date), SELECT_SYSTEM_DEFAULT], style=dict( margin="auto", width=f"{config.CONFIG.plot_width}px", # height=f"{config.CONFIG.plot_height}px", backgroundColor=BACKGROUND_DARK, ), ) ) return children