From 7d35dd8ae981fc1b4eb2065fb31818e330e2735d Mon Sep 17 00:00:00 2001 From: anima Date: Sat, 21 Sep 2024 22:39:28 +0200 Subject: [PATCH] add rename server --- SatiAPI.py | 95 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 59 insertions(+), 36 deletions(-) diff --git a/SatiAPI.py b/SatiAPI.py index de948d7..6e48beb 100644 --- a/SatiAPI.py +++ b/SatiAPI.py @@ -16,7 +16,7 @@ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) class SatiAPI: """A API wrapper for Satisfactory dedicated server""" __AUTHOR__ = 'anima' - __VERSION__ = '0.9.0' + __VERSION__ = '0.10.1' def __init__(self, host: str = None, port: int = 7777, token: str = None, conffile: str = 'conf.yml', logfile: str = 'SatiAPI.log', loglevel: int = 20) -> None: """create a wrapper for satisfactory dedicated server @@ -197,6 +197,10 @@ class SatiAPI: # self.get_token() self._log.error(f'wrong response data {json_response=}') + elif response.status_code == 204: + # response from rename server + return True + else: self._log.error(f'non successfull response {response.content} [{response.status_code}]') self._log.debug(f'{response.request.url=}') @@ -263,6 +267,36 @@ class SatiAPI: self._log.error('server can not be clamed because no authentication possable') return False + def get_token(self, password: str = None, privilegeLevel: str = 'Administrator') -> bool: + """get token from satisfacory dedicated server, password is needed! + Attempts to log in to the Dedicated Server as a player using either Admin Password or Client Protection Password. This function requires no Authentication. + + :param password: password of admin level + :type password: string + :param privilegeLevel: level of authentication + :type privilegeLevel: string + :return: true if successfull get a token + :rtype: bool + """ + if self.token is not None: + self._log.warning('you have already a token') + if isinstance(password, str): + self._log.debug('password used from parameter') + else: + password = self._get_config_password() + if password is not None: + self._log.debug('password used from conf file') + else: + self._log.error(f'no password for auth given!') + return False + + data = dict() + data['password'] = password + data['MinimumPrivilegeLevel'] = privilegeLevel + + response = self.__query('PasswordLogin', data) + return self._save_token(response) + def set_admin_password(self, password: str = None): """Updates the currently set Admin Password. This will invalidate all previously issued Client and Admin authentication tokens. Requires Admin privileges. Function does not return any data on success. @@ -303,36 +337,24 @@ class SatiAPI: response = self.__query('SetClientPassword', data, True) return True - def get_token(self, password: str = None, privilegeLevel: str = 'Administrator') -> bool: - """get token from satisfacory dedicated server, password is needed! - Attempts to log in to the Dedicated Server as a player using either Admin Password or Client Protection Password. This function requires no Authentication. + def set_servername(self, servername: str) -> bool: + """Renames the Dedicated Server once it has been claimed. Requires Admin privileges. Function does not return any data on success. - :param password: password of admin level - :type password: string - :param privilegeLevel: level of authentication - :type privilegeLevel: string - :return: true if successfull get a token + :param servername: New name of the Dedicated Server + :type servername: string + :return: true if set new name successfull :rtype: bool """ - if self.token is not None: - self._log.warning('you have already a token') - if isinstance(password, str): - self._log.debug('password used from parameter') + if isinstance(servername, str): + # TODO: add check if contain non allowed characters + data = dict() + data['ServerName'] = servername + + response = self.__query('RenameServer', data, True) + return response else: - password = self._get_config_password() - if password is not None: - self._log.debug('password used from conf file') - else: - self._log.error(f'no password for auth given!') - return False - - data = dict() - data['password'] = password - data['MinimumPrivilegeLevel'] = privilegeLevel - - response = self.__query('PasswordLogin', data) - return self._save_token(response) - + self._log.error(f'{servername=} is not a valid name') + return False def get_status(self) -> dict: """Retrieves the current state of the Dedicated Server. Does not require any input parameters. @@ -374,25 +396,26 @@ class SatiAPI: ## todo - #RenameServer - #SetClientPassword + #RunCommand + #Shutdown + #SetAutoLoadSessionName #ApplyServerOptions #ApplyAdvancedGameSettings - #RunCommand - #Shutdown + #CreateNewGame #SaveGame + #LoadGame + #DownloadSaveGame + #UploadSaveGame + #DeleteSaveFile #DeleteSaveSession #EnumerateSessions - #LoadGame - #UploadSaveGame - #DownloadSaveGame + if __name__ == "__main__": sati = SatiAPI(loglevel=10) # print('Verbundene Spieler: ', sati.get_status()['serverGameState']['numConnectedPlayers']) print(sati.get_status()) - print(sati.token) - print(sati.set_client_password()) \ No newline at end of file + print(sati.set_servername('OtherName')) \ No newline at end of file