async def add_map(self, filename, insert=True, save_matchsettings=True): """ Add or insert map to current online playlist. :param filename: Load from filename relative to the 'Maps' directory on the dedicated host server. :param insert: Insert after the current map, this will make it play directly after the current map. True by default. :param save_matchsettings: Save match settings as well. :type filename: str :type insert: bool :type save_matchsettings: bool :raise: pyplanet.contrib.map.exceptions.MapIncompatible :raise: pyplanet.contrib.map.exceptions.MapException """ gbx_method = 'InsertMap' if insert else 'AddMap' try: result = await self._instance.gbx(gbx_method, filename) except Fault as e: if 'unknown' in e.faultString: raise MapNotFound('Map is not found on the server.') elif 'already' in e.faultString: raise MapException('Map already added to server.') raise MapException(e.faultString) # Try to save match settings. try: if save_matchsettings: await self.save_matchsettings() except Exception as e: handle_exception(e, __name__, 'add_map', extra_data={'EXTRAHOOK': 'Map Insert bug, see #306'}) return result
async def save_matchsettings(self, filename=None): """ Save the current playlist and configuration to the matchsettings file. :param filename: Give the filename of the matchsettings, Leave empty to use the current loaded and configured one. :type filename: str :raise: pyplanet.contrib.map.exceptions.MapException :raise: pyplanet.core.storage.exceptions.StorageException """ setting = settings.MAP_MATCHSETTINGS if isinstance(setting, dict) and self._instance.process_name in setting: setting = setting[self._instance.process_name] if not isinstance(setting, str): setting = None if not filename and not setting: raise ImproperlyConfigured( 'The setting \'MAP_MATCHSETTINGS\' is not configured for this server! We can\'t save the Match Settings!' ) if not filename: filename = 'MatchSettings/{}'.format( setting.format( server_login=self._instance.game.server_player_login)) try: await self._instance.gbx('SaveMatchSettings', filename) await self._override_timelimit(filename) except Exception as e: logging.exception(e) raise MapException( 'Can\'t save matchsettings to \'{}\'!'.format(filename)) from e
async def upload_map(self, fh, filename, insert=True, overwrite=False): """ Upload and add/insert the map to the current online playlist. :param fh: File handler, bytesio object or any readable context. :param filename: The filename when saving on the server. Must include the map.gbx! Relative to 'Maps' folder. :param insert: Insert after the current map, this will make it play directly after the current map. True by default. :param overwrite: Overwrite current file if exists? Default False. :type filename: str :type insert: bool :type overwrite: bool :raise: pyplanet.contrib.map.exceptions.MapIncompatible :raise: pyplanet.contrib.map.exceptions.MapException :raise: pyplanet.core.storage.exceptions.StorageException """ exists = await self._instance.storage.driver.exists(filename) if exists and not overwrite: raise MapException('Map with filename already located on server!') if not exists: await self._instance.storage.driver.touch('{}{}'.format( self._instance.storage.MAP_FOLDER, filename)) async with self._instance.storage.open_map(filename, 'wb+') as fw: await fw.write(fh.read(-1)) return await self.add_map(filename, insert=insert)
async def remove_map(self, map, delete_file=False): """ Remove and optionally delete file from server and playlist. :param map: Map instance or filename in string. :param delete_file: Boolean to decide if we are going to remove the file from the server too. Defaults to False. :type delete_file: bool :raise: pyplanet.contrib.map.exceptions.MapException :raise: pyplanet.core.storage.exceptions.StorageException """ if isinstance(map, Map): map = map.file if not isinstance(map, str): raise ValueError('Map must be instance or string uid!') try: success = await self._instance.gbx('RemoveMap', map) if success: the_map = None for m in self._maps: if m.file == map: the_map = m break if the_map: self._maps.remove(the_map) except Fault as e: if 'unknown' in e.faultString: raise MapNotFound( 'Dedicated can\'t find map. Already removed?') raise MapException( 'Error when removing map from playlist: {}'.format( e.faultString)) # Try to save match settings. try: await self.save_matchsettings() except: pass # Delete the actual file. if delete_file: try: await self._instance.storage.remove_map(map) except: raise MapException( 'Can\'t delete map file after removing from playlist.')
async def load_matchsettings(self, filename): """ Load Match Settings file and insert it into the current map playlist. :param filename: File to load, relative to Maps folder. :return: Boolean if loaded. """ try: if not await self._instance.storage.driver.exists( os.path.join(self._instance.storage.MAP_FOLDER, filename) ): raise MapException('Can\'t find match settings file. Does it exist?') else: await self._instance.gbx('LoadMatchSettings', filename) except Exception as e: logging.warning('Can\'t load match settings!') raise MapException('Can\'t load matchsettings according the dedicated server, tried loading from \'{}\'!'.format(filename)) from e