def test_special_character(self): """Make paths have special characters and check some basic operations""" outer = tempfile.mkdtemp(self.__class__.odd_characters) inner = str(os.path.join(outer, self.__class__.odd_characters)) inner2 = str(os.path.join(outer, self.__class__.odd_characters + "2")) PATHS.USER_DATA_DIR = inner create_user_dirs() scenario_file = os.listdir(SavegameManager.scenarios_dir)[0] shutil.copy(os.path.join(SavegameManager.scenarios_dir, scenario_file), inner) SavegameManager.scenarios_dir = inner SavegameManager.autosave_dir = inner2 SavegameManager.init() # try to read scenario files SavegameManager.get_available_scenarios() os.remove(os.path.join(inner, scenario_file)) SavegameManager.create_autosave_filename() os.rmdir(inner) os.rmdir(inner2) os.rmdir(outer)
def test_special_character(self): """Make paths have special characters and check some basic operations""" outer = tempfile.mkdtemp(self.__class__.odd_characters) inner = str(os.path.join(outer, self.__class__.odd_characters)) inner2 = str(os.path.join(outer, self.__class__.odd_characters + "2")) PATHS.USER_DATA_DIR = inner PATHS.LOG_DIR = os.path.join(inner, "log") PATHS.USER_MAPS_DIR = os.path.join(inner, "maps") PATHS.SCREENSHOT_DIR = os.path.join(inner, "screenshots") PATHS.SAVEGAME_DIR = os.path.join(inner, "save") create_user_dirs(migrate=False) scenario_file = os.listdir(SavegameManager.scenarios_dir)[0] shutil.copy(os.path.join(SavegameManager.scenarios_dir, scenario_file), inner) SavegameManager.scenarios_dir = inner SavegameManager.autosave_dir = inner2 SavegameManager.init() # try to read scenario files SavegameManager.get_available_scenarios() os.remove(os.path.join(inner, scenario_file)) SavegameManager.create_autosave_filename() for dirpath, _dirnames, _filenames in os.walk(inner, topdown=False): os.rmdir(dirpath) os.rmdir(inner2) os.rmdir(outer)
def test_special_character(self): """Make paths have special characters and check some basic operations""" outer = tempfile.mkdtemp( self.__class__.odd_characters ) inner = unicode(os.path.join(outer, self.__class__.odd_characters)) inner2 = unicode(os.path.join(outer, self.__class__.odd_characters+u"2")) PATHS.USER_DIR = inner create_user_dirs() scenario_file = os.listdir(SavegameManager.scenarios_dir)[0] shutil.copy(os.path.join(SavegameManager.scenarios_dir, scenario_file), inner) SavegameManager.scenarios_dir = inner SavegameManager.autosave_dir = inner2 SavegameManager.init() # try to read scenario files SavegameManager.get_available_scenarios() os.remove(os.path.join(inner, scenario_file)) SavegameManager.create_autosave_filename() os.rmdir(inner) os.rmdir(inner2) os.rmdir(outer)
def _start_map(map_name, is_scenario=False, campaign={}): """Start a map specified by user @return: bool, whether loading succeded""" maps = SavegameManager.get_available_scenarios( ) if is_scenario else SavegameManager.get_maps() map_file = None for i in xrange(0, len(maps[1])): # exact match if maps[1][i] == map_name: map_file = maps[0][i] break # check for partial match if maps[1][i].startswith(map_name): if map_file is not None: # multiple matches, collect all for output map_file += u'\n' + maps[0][i] else: map_file = maps[0][i] if map_file is None: print _("Error: Cannot find map \"%s\".") % map_name return False if len(map_file.splitlines()) > 1: print _("Error: Found multiple matches: ") for match in map_file.splitlines(): print os.path.basename(match) return False load_game(map_file, is_scenario, campaign=campaign) return True
def _start_map( map_name, ai_players=0, is_scenario=False, pirate_enabled=True, trader_enabled=True, force_player_id=None, is_map=False, ): """Start a map specified by user @param map_name: name of map or path to map @return: bool, whether loading succeeded""" if is_scenario: savegames = SavegameManager.get_available_scenarios(locales=True) else: savegames = SavegameManager.get_maps() map_file = _find_matching_map(map_name, savegames) if not map_file: return False options = StartGameOptions.create_start_singleplayer( map_file, is_scenario, ai_players, trader_enabled, pirate_enabled, force_player_id, is_map ) start_singleplayer(options) return True
def show(self): # Campaign and scenarios data campaign_info = SavegameManager.get_campaign_info(name = self.session.campaign['campaign_name']) available_scenarios = SavegameManager.get_available_scenarios()[1] # [0] is the list of xml files, we don't need it scenarios = [s for s in campaign_info.get('scenario_names', []) if s in available_scenarios] self._gui.distributeInitialData({'scenario_list' : scenarios}) # select the first one self._gui.distributeData({ 'scenario_list' : 0, }) def _update_infos(): self.selected_scenario = scenarios[self._gui.collectData("scenario_list")] data = SavegameManager.get_scenario_info(name = self.selected_scenario) #xgettext:python-format text = [_("Difficulty: {difficulty}").format(difficulty=data.get('difficulty', '')), _("Author: {author}").format(author=data.get('author', '')), _("Description: {desc}").format(desc=data.get('description', '')), ] self._gui.findChild(name="scenario_details").text = u"\n".join(text) self._gui.findChild(name="scenario_list").mapEvents({ 'scenario_list/action': _update_infos, 'scenario_list/mouseWheelMovedUp' : _update_infos, 'scenario_list/mouseWheelMovedDown' : _update_infos }) _update_infos() self._gui.show()
def _start_map(map_name, is_scenario = False, campaign = {}): """Start a map specified by user @return: bool, whether loading succeded""" maps = SavegameManager.get_available_scenarios() if is_scenario else SavegameManager.get_maps() map_file = None for i in xrange(0, len(maps[1])): # exact match if maps[1][i] == map_name: map_file = maps[0][i] break # check for partial match if maps[1][i].startswith(map_name): if map_file is not None: # multiple matches, collect all for output map_file += u'\n' + maps[0][i] else: map_file = maps[0][i] if map_file is None: print _("Error: Cannot find map \"%s\".") % map_name return False if len(map_file.splitlines()) > 1: print _("Error: Found multiple matches: ") for match in map_file.splitlines(): print os.path.basename(match) return False load_game(map_file, is_scenario, campaign = campaign) return True
def show(self): self._aidata.hide() self._files, maps_display = SavegameManager.get_available_scenarios(hide_test_scenarios=True) # get the map files and their display names. display tutorials on top. prefer_tutorial = lambda x: ('tutorial' not in x, x) maps_display.sort(key=prefer_tutorial) self._files.sort(key=prefer_tutorial) self._gui.distributeInitialData({'maplist' : maps_display}) if maps_display: self._gui.distributeData({'maplist': 0}) # add all locales to lang list, select current locale as default and sort lang_list = self._gui.findChild(name="uni_langlist") lang_list.items = self._get_available_languages() cur_locale = horizons.globals.fife.get_locale() if LANGUAGENAMES[cur_locale] in lang_list.items: lang_list.selected = lang_list.items.index(LANGUAGENAMES[cur_locale]) else: lang_list.selected = 0 self._gui.mapEvents({ 'maplist/action': self._update_infos, 'uni_langlist/action': self._update_infos, }) self._update_infos()
def show(self): # Campaign and scenarios data scenario_list = self._gui.findChild(name="scenario_list") campaign_info = SavegameManager.get_campaign_info(name=self.session.campaign["campaign_name"]) available_scenarios = SavegameManager.get_available_scenarios()[ 1 ] # [0] is the list of xml files, we don't need it scenarios = [s for s in campaign_info.get("scenario_names", []) if s in available_scenarios] self._gui.distributeInitialData({"scenario_list": scenarios}) # select the first one self._gui.distributeData({"scenario_list": 0}) def _update_infos(): self.selected_scenario = scenarios[self._gui.collectData("scenario_list")] data = SavegameManager.get_scenario_info(name=self.selected_scenario) text = [ _("Difficulty: ") + unicode(data.get("difficulty", "")), _("Author: ") + unicode(data.get("author", "")), _("Description: ") + unicode(data.get("description", "")), ] self._gui.findChild(name="scenario_details").text = u"\n".join(text) self._gui.findChild(name="scenario_list").capture(_update_infos) _update_infos() self._gui.show()
def show(self): # Campaign and scenarios data scenario_list = self._gui.findChild(name="scenario_list") campaign_info = SavegameManager.get_campaign_info( name=self.session.campaign['campaign_name']) available_scenarios = SavegameManager.get_available_scenarios()[ 1] # [0] is the list of xml files, we don't need it scenarios = [ s for s in campaign_info.get('scenario_names', []) if s in available_scenarios ] self._gui.distributeInitialData({'scenario_list': scenarios}) # select the first one self._gui.distributeData({ 'scenario_list': 0, }) def _update_infos(): self.selected_scenario = scenarios[self._gui.collectData( "scenario_list")] data = SavegameManager.get_scenario_info( name=self.selected_scenario) text = [ _("Difficulty: ") + unicode(data.get('difficulty', '')), _("Author: ") + unicode(data.get('author', '')), _("Description: ") + unicode(data.get('description', '')), ] self._gui.findChild( name="scenario_details").text = u"\n".join(text) self._gui.findChild(name="scenario_list").capture(_update_infos) _update_infos() self._gui.show()
def show(self): self._aidata.hide() self._scenarios = SavegameManager.get_available_scenarios() # get the map files and their display names. display tutorials on top. self.maps_display = self._scenarios.keys() if not self.maps_display: return prefer_tutorial = lambda x: ('tutorial' not in x, x) self.maps_display.sort(key=prefer_tutorial) self._gui.distributeInitialData({'maplist': self.maps_display}) self._gui.distributeData({'maplist': 0}) # add all locales to lang list, select current locale as default and sort scenario_langs = list( set(l for s in self._scenarios.values() for l, filename in s)) lang_list = self._gui.findChild(name="uni_langlist") lang_list.items = sorted([LANGUAGENAMES[l] for l in scenario_langs]) cur_locale = horizons.globals.fife.get_locale() if LANGUAGENAMES[cur_locale] in lang_list.items: lang_list.selected = lang_list.items.index( LANGUAGENAMES[cur_locale]) else: lang_list.selected = 0 self._gui.mapEvents({ 'maplist/action': self._update_infos, 'uni_langlist/action': self._update_infos, }) self._update_infos()
def _start_map(map_name, ai_players=0, human_ai=False, is_scenario=False, campaign=None, pirate_enabled=True, trader_enabled=True, force_player_id=None): """Start a map specified by user @param map_name: name of map or path to map @return: bool, whether loading succeded""" # check for exact/partial matches in map list first maps = SavegameManager.get_available_scenarios() if is_scenario else SavegameManager.get_maps() map_file = None for i in xrange(0, len(maps[1])): # exact match if maps[1][i] == map_name: map_file = maps[0][i] break # check for partial match if maps[1][i].startswith(map_name): if map_file is not None: # multiple matches, collect all for output map_file += u'\n' + maps[0][i] else: map_file = maps[0][i] if map_file is None: # not a map name, check for path to file or fail if os.path.exists(map_name): map_file = map_name else: #xgettext:python-format print u"Error: Cannot find map '{name}'.".format(name=map_name) return False if len(map_file.splitlines()) > 1: print "Error: Found multiple matches:" for match in map_file.splitlines(): print os.path.basename(match) return False load_game(ai_players, human_ai, map_file, is_scenario, campaign=campaign, trader_enabled=trader_enabled, pirate_enabled=pirate_enabled, force_player_id=force_player_id) return True
def show(self): self._aidata.hide() self._scenarios = SavegameManager.get_available_scenarios() # get the map files and their display names. display tutorials on top. self.maps_display = self._scenarios.keys() if not self.maps_display: return prefer_tutorial = lambda x: ('tutorial' not in x, x) self.maps_display.sort(key=prefer_tutorial) self._gui.distributeInitialData({'maplist' : self.maps_display}) self._gui.distributeData({'maplist': 0}) # add all locales to lang list, select current locale as default and sort scenario_langs = list(set(l for s in self._scenarios.values() for l, filename in s)) lang_list = self._gui.findChild(name="uni_langlist") lang_list.items = sorted([LANGUAGENAMES[l] for l in scenario_langs]) cur_locale = horizons.globals.fife.get_locale() if LANGUAGENAMES[cur_locale] in lang_list.items: lang_list.selected = lang_list.items.index(LANGUAGENAMES[cur_locale]) else: lang_list.selected = 0 self._gui.mapEvents({ 'maplist/action': self._update_infos, 'uni_langlist/action': self._update_infos, }) self._update_infos()
def _start_map(map_name, ai_players=0, human_ai=False, is_scenario=False, campaign=None, pirate_enabled=True, trader_enabled=True, force_player_id=None): """Start a map specified by user @param map_name: name of map or path to map @return: bool, whether loading succeded""" if is_scenario: savegames = SavegameManager.get_available_scenarios(locales=True) else: savegames = SavegameManager.get_maps() map_file = _find_matching_map(map_name, savegames) if not map_file: return False load_game(ai_players, human_ai, map_file, is_scenario, campaign=campaign, trader_enabled=trader_enabled, pirate_enabled=pirate_enabled, force_player_id=force_player_id) return True
def _start_map(map_name, ai_players=0, human_ai=False, is_scenario=False, campaign=None, pirate_enabled=True, trader_enabled=True, force_player_id=None): """Start a map specified by user @param map_name: name of map or path to map @return: bool, whether loading succeded""" maps = SavegameManager.get_available_scenarios(locales=True) if is_scenario else SavegameManager.get_maps() map_file = None # get system's language game_language = fife.get_locale() # now we have "_en.yaml" which is set to language_extension variable language_extension = '_' + game_language + '.' + SavegameManager.scenario_extension # check for exact/partial matches in map list first for i in xrange(0, len(maps[1])): # exact match if maps[1][i] == map_name: map_file = maps[0][i] break # we want to match when map_name is like "tutorial" not "tutorial_en" if maps[1][i] == map_name + language_extension: map_file = maps[0][i] break # check for partial match if maps[1][i].startswith(map_name): if map_file is not None: # multiple matches, collect all for output map_file += u'\n' + maps[0][i] else: map_file = maps[0][i] if map_file is None: # not a map name, check for path to file or fail if os.path.exists(map_name): map_file = map_name else: #xgettext:python-format print u"Error: Cannot find map '{name}'.".format(name=map_name) return False if len(map_file.splitlines()) > 1: print "Error: Found multiple matches:" for match in map_file.splitlines(): print os.path.basename(match) return False load_game(ai_players, human_ai, map_file, is_scenario, campaign=campaign, trader_enabled=trader_enabled, pirate_enabled=pirate_enabled, force_player_id=force_player_id) return True
def _start_map(map_name, ai_players=0, is_scenario=False, pirate_enabled=True, trader_enabled=True, force_player_id=None, is_map=False): """Start a map specified by user @param map_name: name of map or path to map @return: bool, whether loading succeeded""" if is_scenario: map_file = _find_scenario(map_name, SavegameManager.get_available_scenarios(locales=True)) else: map_file = _find_map(map_name, SavegameManager.get_maps()) if not map_file: return False options = StartGameOptions.create_start_singleplayer(map_file, is_scenario, ai_players, trader_enabled, pirate_enabled, force_player_id, is_map) start_singleplayer(options) return True
def show(self): self._aidata.hide() self._scenarios = SavegameManager.get_available_scenarios() # get the map files and their display names. display tutorials on top. self.maps_display = list(self._scenarios.keys()) if not self.maps_display: return prefer_tutorial = lambda x: ('tutorial' not in x, x) self.maps_display.sort(key=prefer_tutorial) self._gui.distributeInitialData({'maplist': self.maps_display}) self._gui.distributeData({'maplist': 0}) self._gui.mapEvents({ 'maplist/action': self._on_map_change, 'uni_langlist/action': self._update_infos, }) self._on_map_change()
def show_single(self, show = 'scenario'): # tutorial """ @param show: string, which type of games to show """ assert show in ('random', 'scenario', 'campaign', 'free_maps') self.hide() # reload since the gui is changed at runtime self.widgets.reload('singleplayermenu') self._switch_current_widget('singleplayermenu', center=True) eventMap = { 'cancel' : self.show_main, 'okay' : self.start_single, 'scenario' : Callback(self.show_single, show='scenario'), 'campaign' : Callback(self.show_single, show='campaign'), 'random' : Callback(self.show_single, show='random'), 'free_maps' : Callback(self.show_single, show='free_maps') } # init gui for subcategory show_ai_options = False del eventMap[show] self.current.findChild(name=show).marked = True right_side = self.widgets['sp_%s' % show] self.current.findChild(name="right_side_box").addChild(right_side) if show == 'random': game_settings = self.widgets['game_settings'] if self.current.findChild(name="game_settings") is None: self.current.findChild(name="game_settings_box").addChild(game_settings) show_ai_options = True elif show == 'free_maps': self.current.files, maps_display = SavegameManager.get_maps() game_settings = self.widgets['game_settings'] if self.current.findChild(name="game_settings") is None: self.current.findChild(name="game_settings_box").addChild(game_settings) self.current.distributeInitialData({ 'maplist' : maps_display, }) if len(maps_display) > 0: # select first entry self.current.distributeData({ 'maplist' : 0, }) show_ai_options = True else: choosable_locales = ['en', horizons.main.fife.get_locale()] if show == 'campaign': self.current.files, maps_display = SavegameManager.get_campaigns() # tell people that we don't have any content text = u"We currently don't have any campaigns available for you. " + \ u"If you are interested in adding campaigns to Unknown Horizons, " + \ u"please contact us via our website (http://www.unknown-horizons.org)!" self.show_popup("No campaigns available yet", text) elif show == 'scenario': self.current.files, maps_display = SavegameManager.get_available_scenarios(locales = choosable_locales) # get the map files and their display names self.current.distributeInitialData({ 'maplist' : maps_display, }) if len(maps_display) > 0: # select first entry self.current.distributeData({ 'maplist' : 0, }) if show == 'scenario': # update infos for scenario from horizons.scenario import ScenarioEventHandler, InvalidScenarioFileFormat def _update_infos(): """Fill in infos of selected scenario to label""" try: difficulty = ScenarioEventHandler.get_difficulty_from_file( self.__get_selected_map() ) desc = ScenarioEventHandler.get_description_from_file( self.__get_selected_map() ) author = ScenarioEventHandler.get_author_from_file( self.__get_selected_map() ) except InvalidScenarioFileFormat, e: self.__show_invalid_scenario_file_popup(e) return self.current.findChild(name="map_difficulty").text = _("Difficulty: ") + unicode( difficulty ) self.current.findChild(name="map_author").text = _("Author: ") + unicode( author ) self.current.findChild(name="map_desc").text = _("Description: ") + unicode( desc ) #self.current.findChild(name="map_desc").parent.adaptLayout() elif show == 'campaign': # update infos for campaign def _update_infos(): """Fill in infos of selected campaign to label""" campaign_info = SavegameManager.get_campaign_info(filename = self.__get_selected_map()) if not campaign_info: # TODO : an "invalid campaign popup" self.__show_invalid_scenario_file_popup(e) return self.current.findChild(name="map_difficulty").text = _("Difficulty: ") + unicode(campaign_info.get('difficulty', '')) self.current.findChild(name="map_author").text = _("Author: ") + unicode(campaign_info.get('author', '')) self.current.findChild(name="map_desc").text = _("Description: ") + unicode(campaign_info.get('description', '')) self.current.findChild(name="maplist").capture(_update_infos) _update_infos()
def show_single(self, show = 'scenario'): # tutorial """ @param show: string, which type of games to show """ assert show in ('random', 'scenario', 'campaign', 'free_maps') self.hide() # reload since the gui is changed at runtime self.widgets.reload('singleplayermenu') self._switch_current_widget('singleplayermenu', center=True) eventMap = { 'cancel' : self.show_main, 'okay' : self.start_single, 'showScenario' : Callback(self.show_single, show='scenario'), 'showCampaign' : Callback(self.show_single, show='campaign'), 'showRandom' : Callback(self.show_single, show='random'), 'showMaps' : Callback(self.show_single, show='free_maps') } adjust_widget_black_background(self.widgets['singleplayermenu']) # init gui for subcategory if show == 'random': del eventMap['showRandom'] self.current.findChild(name="showRandom").marked = True to_remove = self.current.findChild(name="map_list_area") to_remove.parent.removeChild(to_remove) to_remove = self.current.findChild(name="choose_map_lbl") to_remove.parent.removeChild(to_remove) # need to add some options here (generation algo, size, ... ) else: if show == 'free_maps': del eventMap['showMaps'] self.current.findChild(name="showMaps").marked = True self.current.files, maps_display = SavegameManager.get_maps() elif show == 'campaign': del eventMap['showCampaign'] self.current.findChild(name="showCampaign").marked = True self.current.files, maps_display = SavegameManager.get_campaigns() else: # scenario del eventMap['showScenario'] self.current.findChild(name="showScenario").marked = True choosable_locales = ['en',horizons.main.fife.get_locale()] self.current.files, maps_display = SavegameManager.get_available_scenarios(locales = choosable_locales) # get the map files and their display names self.current.distributeInitialData({ 'maplist' : maps_display, }) if len(maps_display) > 0: # select first entry self.current.distributeData({ 'maplist' : 0, }) if show == 'scenario': # update infos for scenario from horizons.scenario import ScenarioEventHandler, InvalidScenarioFileFormat def _update_infos(): """Fill in infos of selected scenario to label""" try: difficulty = ScenarioEventHandler.get_difficulty_from_file( self.__get_selected_map() ) desc = ScenarioEventHandler.get_description_from_file( self.__get_selected_map() ) author = ScenarioEventHandler.get_author_from_file( self.__get_selected_map() ) except InvalidScenarioFileFormat, e: self.__show_invalid_scenario_file_popup(e) return self.current.findChild(name="map_difficulty").text = _("Difficulty: ") + unicode( difficulty ) self.current.findChild(name="map_author").text = _("Author: ") + unicode( author ) self.current.findChild(name="map_desc").text = _("Description: ") + unicode( desc ) #self.current.findChild(name="map_desc").parent.adaptLayout() elif show == 'campaign': # update infos for campaign def _update_infos(): """Fill in infos of selected campaign to label""" campaign_info = SavegameManager.get_campaign_info(file = self.__get_selected_map()) if not campaign_info: # TODO : an "invalid campaign popup" self.__show_invalid_scenario_file_popup(e) return self.current.findChild(name="map_difficulty").text = _("Difficulty: ") + unicode(campaign_info.get('difficulty', '')) self.current.findChild(name="map_author").text = _("Author: ") + unicode(campaign_info.get('author', '')) self.current.findChild(name="map_desc").text = _("Description: ") + unicode(campaign_info.get('description', '')) if show in ('scenario', 'campaign'): self.current.findChild(name="maplist").capture(_update_infos) _update_infos()
def _select_single(self, show): assert show in ('random', 'scenario', 'campaign', 'free_maps') self.hide() event_map = { 'cancel' : Callback.ChainedCallbacks(self._save_player_name, self.show_main), 'okay' : self.start_single, 'scenario' : Callback(self._select_single, show='scenario'), 'campaign' : Callback(self._select_single, show='campaign'), 'random' : Callback(self._select_single, show='random'), 'free_maps' : Callback(self._select_single, show='free_maps') } # init gui for subcategory del event_map[show] right_side = self.widgets['sp_%s' % show] show_ai_options = False self.current.findChild(name=show).marked = True self.current.aidata.hide() # hide previous widget, unhide new right side widget if self.active_right_side is not None: self.active_right_side.parent.hideChild(self.active_right_side) right_side.parent.showChild(right_side) self.active_right_side = right_side self._current_mode = show if show == 'random': show_ai_options = True self._setup_random_map_selection(right_side) self._setup_game_settings_selection() self._on_random_map_parameter_changed() elif show == 'free_maps': self.current.files, maps_display = SavegameManager.get_maps() self.active_right_side.distributeInitialData({ 'maplist' : maps_display, }) def _update_infos(): number_of_players = SavegameManager.get_recommended_number_of_players( self._get_selected_map() ) #xgettext:python-format self.current.findChild(name="recommended_number_of_players_lbl").text = \ _("Recommended number of players: {number}").format(number=number_of_players) self.map_preview.update_map(self._get_selected_map()) if len(maps_display) > 0: # select first entry self.active_right_side.distributeData({ 'maplist' : 0, }) _update_infos() # update preview whenever somehting is selected in the list self.active_right_side.findChild(name="maplist").mapEvents({ 'maplist/action' : _update_infos, 'maplist/mouseWheelMovedUp' : _update_infos, 'maplist/mouseWheelMovedDown' : _update_infos }) self.active_right_side.findChild(name="maplist").capture(_update_infos, event_name="keyPressed") show_ai_options = True self._setup_game_settings_selection() else: choosable_locales = ['en', horizons.main.fife.get_locale()] if show == 'campaign': self.current.files, maps_display = SavegameManager.get_campaigns() # tell people that we don't have any content text = u"We currently don't have any campaigns available for you. " + \ u"If you are interested in adding campaigns to Unknown Horizons, " + \ u"please contact us via our website (http://www.unknown-horizons.org)!" self.show_popup("No campaigns available yet", text) elif show == 'scenario': self.current.files, maps_display = SavegameManager.get_available_scenarios(locales = choosable_locales) # get the map files and their display names self.active_right_side.distributeInitialData({ 'maplist' : maps_display, }) if len(maps_display) > 0: # select first entry self.active_right_side.distributeData({ 'maplist' : 0, }) if show == 'scenario': # update infos for scenario from horizons.scenario import ScenarioEventHandler, InvalidScenarioFileFormat def _update_infos(): """Fill in infos of selected scenario to label""" try: difficulty = ScenarioEventHandler.get_difficulty_from_file( self._get_selected_map() ) desc = ScenarioEventHandler.get_description_from_file( self._get_selected_map() ) author = ScenarioEventHandler.get_author_from_file( self._get_selected_map() ) except InvalidScenarioFileFormat as e: self._show_invalid_scenario_file_popup(e) return self.current.findChild(name="map_difficulty").text = \ _("Difficulty: {difficulty}").format(difficulty=difficulty) #xgettext:python-format self.current.findChild(name="map_author").text = \ _("Author: {author}").format(author=author) #xgettext:python-format self.current.findChild(name="map_desc").text = \ _("Description: {desc}").format(desc=desc) #xgettext:python-format #self.current.findChild(name="map_desc").parent.adaptLayout() elif show == 'campaign': # update infos for campaign def _update_infos(): """Fill in infos of selected campaign to label""" campaign_info = SavegameManager.get_campaign_info(filename = self._get_selected_map()) if not campaign_info: self._show_invalid_scenario_file_popup("Unknown error") return self.current.findChild(name="map_difficulty").text = \ _("Difficulty: {difficulty}").format(difficulty=campaign_info.get('difficulty', '')) #xgettext:python-format self.current.findChild(name="map_author").text = \ _("Author: {author}").format(author=campaign_info.get('author', '')) #xgettext:python-format self.current.findChild(name="map_desc").text = \ _("Description: {desc}").format(desc=campaign_info.get('description', '')) #xgettext:python-format self.active_right_side.findChild(name="maplist").capture(_update_infos) _update_infos() self.current.mapEvents(event_map) if show_ai_options: self.current.aidata.show() self.current.show() self.on_escape = self.show_main
def show_single(self, show='scenario'): # tutorial """ @param show: string, which type of games to show """ assert show in ('random', 'scenario', 'campaign', 'free_maps') self.hide() # reload since the gui is changed at runtime self.widgets.reload('singleplayermenu') self._switch_current_widget('singleplayermenu', center=True) eventMap = { 'cancel': self.show_main, 'okay': self.start_single, 'showScenario': Callback(self.show_single, show='scenario'), 'showCampaign': Callback(self.show_single, show='campaign'), 'showRandom': Callback(self.show_single, show='random'), 'showMaps': Callback(self.show_single, show='free_maps') } adjust_widget_black_background(self.widgets['singleplayermenu']) # init gui for subcategory if show == 'random': del eventMap['showRandom'] self.current.findChild(name="showRandom").marked = True to_remove = self.current.findChild(name="map_list_area") to_remove.parent.removeChild(to_remove) to_remove = self.current.findChild(name="choose_map_lbl") to_remove.parent.removeChild(to_remove) # need to add some options here (generation algo, size, ... ) else: if show == 'free_maps': del eventMap['showMaps'] self.current.findChild(name="showMaps").marked = True self.current.files, maps_display = SavegameManager.get_maps() elif show == 'campaign': del eventMap['showCampaign'] self.current.findChild(name="showCampaign").marked = True self.current.files, maps_display = SavegameManager.get_campaigns( ) else: # scenario del eventMap['showScenario'] self.current.findChild(name="showScenario").marked = True choosable_locales = ['en', horizons.main.fife.get_locale()] self.current.files, maps_display = SavegameManager.get_available_scenarios( locales=choosable_locales) # get the map files and their display names self.current.distributeInitialData({ 'maplist': maps_display, }) if len(maps_display) > 0: # select first entry self.current.distributeData({ 'maplist': 0, }) if show == 'scenario': # update infos for scenario from horizons.scenario import ScenarioEventHandler, InvalidScenarioFileFormat def _update_infos(): """Fill in infos of selected scenario to label""" try: difficulty = ScenarioEventHandler.get_difficulty_from_file( self.__get_selected_map()) desc = ScenarioEventHandler.get_description_from_file( self.__get_selected_map()) author = ScenarioEventHandler.get_author_from_file( self.__get_selected_map()) except InvalidScenarioFileFormat, e: self.__show_invalid_scenario_file_popup(e) return self.current.findChild( name="map_difficulty" ).text = _("Difficulty: ") + unicode(difficulty) self.current.findChild( name="map_author" ).text = _("Author: ") + unicode(author) self.current.findChild( name="map_desc" ).text = _("Description: ") + unicode(desc) #self.current.findChild(name="map_desc").parent.adaptLayout() elif show == 'campaign': # update infos for campaign def _update_infos(): """Fill in infos of selected campaign to label""" campaign_info = SavegameManager.get_campaign_info( file=self.__get_selected_map()) if not campaign_info: # TODO : an "invalid campaign popup" self.__show_invalid_scenario_file_popup(e) return self.current.findChild( name="map_difficulty" ).text = _("Difficulty: ") + unicode( campaign_info.get('difficulty', '')) self.current.findChild( name="map_author").text = _("Author: ") + unicode( campaign_info.get('author', '')) self.current.findChild( name="map_desc" ).text = _("Description: ") + unicode( campaign_info.get('description', '')) if show in ('scenario', 'campaign'): self.current.findChild( name="maplist").capture(_update_infos) _update_infos()
def _select_single(self, show): assert show in ('random', 'scenario', 'campaign', 'free_maps') self.hide() event_map = { 'cancel' : Callback.ChainedCallbacks(self._save_player_name, self.show_main), 'okay' : self.start_single, 'scenario' : Callback(self._select_single, show='scenario'), 'campaign' : Callback(self._select_single, show='campaign'), 'random' : Callback(self._select_single, show='random'), 'free_maps' : Callback(self._select_single, show='free_maps') } self.current.mapEvents(event_map) # init gui for subcategory right_side = self.widgets['sp_%s' % show] self.current.findChild(name=show).marked = True self.current.aidata.hide() # hide previous widget, unhide new right side widget if self.active_right_side is not None: self.active_right_side.parent.hideChild(self.active_right_side) right_side.parent.showChild(right_side) self.active_right_side = right_side if show == 'random': self._setup_random_map_selection(right_side) self._setup_game_settings_selection() self._on_random_map_parameter_changed() self.active_right_side.findChild(name="open_random_map_archive").capture(self._open_random_map_archive) self.current.aidata.show() elif show == 'free_maps': self.current.files, maps_display = SavegameManager.get_maps() self.active_right_side.distributeInitialData({ 'maplist' : maps_display, }) # update preview whenever something is selected in the list self.active_right_side.mapEvents({ 'maplist/action': self._update_free_map_infos, }) self._setup_game_settings_selection() if maps_display: # select first entry self.active_right_side.distributeData({'maplist': 0}) self.current.aidata.show() self._update_free_map_infos() elif show == 'campaign': # tell people that we don't have any content text = u"We currently don't have any campaigns available for you. " + \ u"If you are interested in adding campaigns to Unknown Horizons, " + \ u"please contact us via our website (http://www.unknown-horizons.org)!" self.show_popup("No campaigns available yet", text) self.current.files, maps_display = SavegameManager.get_campaigns() self.active_right_side.distributeInitialData({ 'maplist' : maps_display, }) self.active_right_side.mapEvents({ 'maplist/action': self._update_campaign_infos }) if maps_display: # select first entry self.active_right_side.distributeData({'maplist': 0}) self._update_campaign_infos() elif show == 'scenario': self.current.files, maps_display = SavegameManager.get_available_scenarios() # get the map files and their display names. display tutorials on top. prefer_tutorial = lambda x : ('tutorial' not in x, x) maps_display.sort(key=prefer_tutorial) self.current.files.sort(key=prefer_tutorial) #add all locales to lang list, select current locale as default and sort lang_list = self.current.findChild(name="uni_langlist") self.active_right_side.distributeInitialData({ 'maplist' : maps_display, }) if maps_display: # select first entry self.active_right_side.distributeData({'maplist': 0}) lang_list.items = self._get_available_languages() cur_locale = horizons.main.fife.get_locale() if LANGUAGENAMES[cur_locale] in lang_list.items: lang_list.selected = lang_list.items.index(LANGUAGENAMES[cur_locale]) else: lang_list.selected = 0 self.active_right_side.mapEvents({ 'maplist/action': self._update_scenario_infos, 'uni_langlist/action': self._update_scenario_infos, }) self._update_scenario_infos() self.current.show() self.on_escape = self.show_main
def _select_single(self, show): assert show in ('random', 'scenario', 'free_maps') self.hide() event_map = { 'cancel': Callback.ChainedCallbacks(self._save_player_name, self.show_main), 'okay': self.start_single, 'scenario': Callback(self._select_single, show='scenario'), 'random': Callback(self._select_single, show='random'), 'free_maps': Callback(self._select_single, show='free_maps') } self.current.mapEvents(event_map) # init gui for subcategory right_side = self.widgets['sp_%s' % show] self.current.findChild(name=show).marked = True self.current.aidata.hide() # hide previous widget, unhide new right side widget if self.active_right_side is not None: self.active_right_side.parent.hideChild(self.active_right_side) right_side.parent.showChild(right_side) self.active_right_side = right_side if show == 'random': self._setup_random_map_selection(right_side) self._setup_game_settings_selection() self._on_random_map_parameter_changed() self.active_right_side.findChild( name="open_random_map_archive").capture( self._open_random_map_archive) self.current.aidata.show() elif show == 'free_maps': self.current.files, maps_display = SavegameManager.get_maps() self.active_right_side.distributeInitialData({ 'maplist': maps_display, }) # update preview whenever something is selected in the list self.active_right_side.mapEvents({ 'maplist/action': self._update_free_map_infos, }) self._setup_game_settings_selection() if maps_display: # select first entry self.active_right_side.distributeData({'maplist': 0}) self.current.aidata.show() self._update_free_map_infos() elif show == 'scenario': self.current.files, maps_display = SavegameManager.get_available_scenarios( hide_test_scenarios=True) # get the map files and their display names. display tutorials on top. prefer_tutorial = lambda x: ('tutorial' not in x, x) maps_display.sort(key=prefer_tutorial) self.current.files.sort(key=prefer_tutorial) #add all locales to lang list, select current locale as default and sort lang_list = self.current.findChild(name="uni_langlist") self.active_right_side.distributeInitialData({ 'maplist': maps_display, }) if maps_display: # select first entry self.active_right_side.distributeData({'maplist': 0}) lang_list.items = self._get_available_languages() cur_locale = horizons.globals.fife.get_locale() if LANGUAGENAMES[cur_locale] in lang_list.items: lang_list.selected = lang_list.items.index( LANGUAGENAMES[cur_locale]) else: lang_list.selected = 0 self.active_right_side.mapEvents({ 'maplist/action': self._update_scenario_infos, 'uni_langlist/action': self._update_scenario_infos, }) self._update_scenario_infos() self.current.show() self.on_escape = self.show_main
def _select_single(self, show): assert show in ('random', 'scenario', 'campaign', 'free_maps') self.hide() event_map = { 'cancel' : Callback.ChainedCallbacks(self._save_player_name, self.show_main), 'okay' : self.start_single, 'scenario' : Callback(self._select_single, show='scenario'), 'campaign' : Callback(self._select_single, show='campaign'), 'random' : Callback(self._select_single, show='random'), 'free_maps' : Callback(self._select_single, show='free_maps') } # init gui for subcategory del event_map[show] right_side = self.widgets['sp_%s' % show] show_ai_options = False self.current.findChild(name=show).marked = True self.current.aidata.hide() # hide previous widget, unhide new right side widget if self.active_right_side is not None: self.active_right_side.parent.hideChild(self.active_right_side) right_side.parent.showChild(right_side) self.active_right_side = right_side self._current_mode = show if show == 'random': show_ai_options = True self._setup_random_map_selection(right_side) self._setup_game_settings_selection() self._on_random_map_parameter_changed() self.active_right_side.findChild(name="open_random_map_archive").capture(self._open_random_map_archive) elif show == 'free_maps': self.current.files, maps_display = SavegameManager.get_maps() self.active_right_side.distributeInitialData({ 'maplist' : maps_display, }) def _update_infos(): number_of_players = SavegameManager.get_recommended_number_of_players( self._get_selected_map() ) #xgettext:python-format self.current.findChild(name="recommended_number_of_players_lbl").text = \ _("Recommended number of players: {number}").format(number=number_of_players) self.map_preview.update_map(self._get_selected_map()) if len(maps_display) > 0: # select first entry self.active_right_side.distributeData({ 'maplist' : 0, }) _update_infos() # update preview whenever something is selected in the list self.active_right_side.findChild(name="maplist").mapEvents({ 'maplist/action' : _update_infos }) self.active_right_side.findChild(name="maplist").capture(_update_infos, event_name="keyPressed") show_ai_options = True self._setup_game_settings_selection() else: choosable_locales = ['en', horizons.main.fife.get_locale()] if show == 'campaign': self.current.files, maps_display = SavegameManager.get_campaigns() # tell people that we don't have any content text = u"We currently don't have any campaigns available for you. " + \ u"If you are interested in adding campaigns to Unknown Horizons, " + \ u"please contact us via our website (http://www.unknown-horizons.org)!" self.show_popup("No campaigns available yet", text) elif show == 'scenario': self.current.files, maps_display = SavegameManager.get_available_scenarios() # get the map files and their display names. display tutorials on top. prefer_tutorial = lambda x : ('tutorial' not in x, x) maps_display.sort(key=prefer_tutorial) self.current.files.sort(key=prefer_tutorial) #add all locales to lang list, select current locale as default and sort lang_list = self.current.findChild(name="uni_langlist") self.active_right_side.distributeInitialData({ 'maplist' : maps_display, }) # select first entry self.active_right_side.distributeData({ 'maplist' : 0, }) selectable_languages = [] #show only selectable languages for i in find_available_languages().keys(): if os.path.exists(self._get_selected_map() + '_' + i + '.' + SavegameManager.scenario_extension): selectable_languages.append(LANGUAGENAMES[i]) selectable_languages.sort() lang_list.items = selectable_languages cur_locale = horizons.main.fife.get_locale() if LANGUAGENAMES[cur_locale] in lang_list.items: lang_list.selected = lang_list.items.index(LANGUAGENAMES[cur_locale]) else: lang_list.selected = 0 self.active_right_side.distributeInitialData({ 'maplist' : maps_display, }) if len(maps_display) > 0: # select first entry self.active_right_side.distributeData({ 'maplist' : 0, }) if show == 'scenario': # update infos for scenario from horizons.scenario import ScenarioEventHandler, InvalidScenarioFileFormat def _update_infos(): """Fill in infos of selected scenario to label""" def _find_map_filename(locale = None): """Finds the selected map's filename with its locale.""" this_locale = "" new_map_name = "" if locale is None: this_locale = LANGUAGENAMES.get_by_value(lang_list.selected_item) else: this_locale = locale #check if selected map's file ends with .yaml if self._get_selected_map().find('.yaml') == -1: new_map_name = self._get_selected_map() + '_' + \ this_locale + '.' + \ SavegameManager.scenario_extension #if selected map's file ends with .yaml then get current locale #to remove locale postfix from selected_map's name else: #get current locale to split current map file name current_locale = yamlcache.YamlCache.get_file(self._get_selected_map(), \ game_data=True)['locale'] new_map_name = self._get_selected_map()[:self._get_selected_map().\ find('_' + current_locale)] + '_' + \ this_locale + '.' + \ SavegameManager.scenario_extension return new_map_name cur_selected_language = lang_list.selected_item selectable_languages = [] #show only selectable languages for i in find_available_languages().keys(): if os.path.exists(_find_map_filename(i)): selectable_languages.append(LANGUAGENAMES[i]) selectable_languages.sort() lang_list.items = selectable_languages if cur_selected_language in lang_list.items: lang_list.selected = lang_list.items.index(cur_selected_language) else: lang_list.selected = 0 def _update_translation_infos(new_map_name): """Fill in translation infos of selected scenario to translation label. It gets translation_status from new_map_file. If there is no attribute like translation_status then selected locale is the original locale of the selected scenario. In this case, hide translation_status_label. If there are fuzzy translations, show them as untranslated. This function also sets scenario map name using locale. (e.g. tutorial -> tutorial_en.yaml)""" translation_status_label = self.current.findChild(name="translation_status") try: #get translation status translation_status_message = yamlcache.YamlCache.get_file(new_map_name, \ game_data=True)['translation_status'] #find integers in translation_levels string translation_levels = [int(x) for x in re.findall(r'\d+', translation_status_message)] #if translation_levels' len is 3 it shows us there are fuzzy ones #show them as untranslated if len(translation_levels) == 3: translation_levels[2] += translation_levels[1] #if everything is translated then set untranslated count as 0 if len(translation_levels) == 1: translation_levels.append(0) translation_status_label.text = _("Translation status:") + '\n' + \ _("{translated} translated messages, {untranslated} untranslated messages")\ .format(translated=translation_levels[0], \ untranslated=translation_levels[-1]) #if selected language is english then don't show translation status translation_status_label.show() #if there is no translation_status then hide it except KeyError: translation_status_label.hide() self.current.files[ self.active_right_side.collectData('maplist') ] = new_map_name #Add locale postfix to fix scenario file try: _update_translation_infos(_find_map_filename()) #if there is no scenario with selected locale then select system's default except IOError: default_locale = "" _default_locale, default_encoding = locale.getdefaultlocale() try: default_locale = _default_locale.split('_')[0] except: # If default locale could not be detected use 'EN' as fallback default_locale = "en" #check if default_locale is in list if LANGUAGENAMES[default_locale] in lang_list.items: lang_list.selected = lang_list.items.index(LANGUAGENAMES[default_locale]) #if default locale is not in list then select first one else: lang_list.selected = 0 _update_infos() try: difficulty = ScenarioEventHandler.get_difficulty_from_file( self._get_selected_map() ) desc = ScenarioEventHandler.get_description_from_file( self._get_selected_map() ) author = ScenarioEventHandler.get_author_from_file( self._get_selected_map() ) except InvalidScenarioFileFormat as e: self._show_invalid_scenario_file_popup(e) return self.current.findChild(name="uni_map_difficulty").text = \ _("Difficulty: {difficulty}").format(difficulty=difficulty) #xgettext:python-format self.current.findChild(name="uni_map_author").text = \ _("Author: {author}").format(author=author) #xgettext:python-format self.current.findChild(name="uni_map_desc").text = \ _("Description: {desc}").format(desc=desc) #xgettext:python-format self.active_right_side.findChild(name="uni_langlist").mapEvents({ 'uni_langlist/action': _update_infos }) self.active_right_side.findChild(name="uni_langlist").capture(_update_infos, event_name="keyPressed") _update_infos() #hide and show current window to keep bugs away from us #if we don't do this, translation_label doesn't hide even if #selected language is english or doesn't show if selected #language has translation_status attribute self.current.hide() self.current.show() elif show == 'campaign': # update infos for campaign def _update_infos(): """Fill in infos of selected campaign to label""" campaign_info = SavegameManager.get_campaign_info(filename = self._get_selected_map()) if not campaign_info: self._show_invalid_scenario_file_popup("Unknown error") return self.current.findChild(name="map_difficulty").text = \ _("Difficulty: {difficulty}").format(difficulty=campaign_info.get('difficulty', '')) #xgettext:python-format self.current.findChild(name="map_author").text = \ _("Author: {author}").format(author=campaign_info.get('author', '')) #xgettext:python-format self.current.findChild(name="map_desc").text = \ _("Description: {desc}").format(desc=campaign_info.get('description', '')) #xgettext:python-format self.active_right_side.findChild(name="maplist").mapEvents({ 'maplist/action': _update_infos }) self.active_right_side.findChild(name="maplist").capture(_update_infos, event_name="keyPressed") _update_infos() self.current.mapEvents(event_map) if show_ai_options: self.current.aidata.show() self.current.show() self.on_escape = self.show_main
def _select_single(self, show): assert show in ('random', 'scenario', 'free_maps') self.hide() event_map = { 'cancel' : Callback.ChainedCallbacks(self._save_player_name, self.mainmenu.show_main), 'okay' : self.start_single, 'scenario' : Callback(self._select_single, show='scenario'), 'random' : Callback(self._select_single, show='random'), 'free_maps' : Callback(self._select_single, show='free_maps') } self.current.mapEvents(event_map) # init gui for subcategory right_side = self.widgets['sp_%s' % show] self.current.findChild(name=show).marked = True self.current.aidata.hide() # hide previous widget, unhide new right side widget if self.active_right_side is not None: self.active_right_side.parent.hideChild(self.active_right_side) right_side.parent.showChild(right_side) self.active_right_side = right_side if show == 'random': self._setup_random_map_selection(right_side) self._setup_game_settings_selection() self._on_random_map_parameter_changed() self.current.aidata.show() elif show == 'free_maps': self.current.files, maps_display = SavegameManager.get_maps() self.active_right_side.distributeInitialData({ 'maplist' : maps_display, }) # update preview whenever something is selected in the list self.active_right_side.mapEvents({ 'maplist/action': self._update_free_map_infos, }) self._setup_game_settings_selection() if maps_display: # select first entry self.active_right_side.distributeData({'maplist': 0}) self.current.aidata.show() self._update_free_map_infos() elif show == 'scenario': self.current.files, maps_display = SavegameManager.get_available_scenarios(hide_test_scenarios=True) # get the map files and their display names. display tutorials on top. prefer_tutorial = lambda x : ('tutorial' not in x, x) maps_display.sort(key=prefer_tutorial) self.current.files.sort(key=prefer_tutorial) #add all locales to lang list, select current locale as default and sort lang_list = self.current.findChild(name="uni_langlist") self.active_right_side.distributeInitialData({ 'maplist' : maps_display, }) if maps_display: # select first entry self.active_right_side.distributeData({'maplist': 0}) lang_list.items = self._get_available_languages() cur_locale = horizons.globals.fife.get_locale() if LANGUAGENAMES[cur_locale] in lang_list.items: lang_list.selected = lang_list.items.index(LANGUAGENAMES[cur_locale]) else: lang_list.selected = 0 self.active_right_side.mapEvents({ 'maplist/action': self._update_scenario_infos, 'uni_langlist/action': self._update_scenario_infos, }) self._update_scenario_infos() self.current.show() self.mainmenu.on_escape = self.mainmenu.show_main