def set_ready(self, uid, session, **kwargs): """ User is ready to play the game ? method call: set_ready(session, uid, name, **kwargs) -> where **kwargs : <optional parameters> mandatory parameters: token - auth token uid - user ID session - session name team - team name. Example: Blue Team Returns: SUCCESS: returns STATUS_SUCCESS = 0 ERROR: REST_EXCEPTION = -1 PARAMETER_NOT_FOUND = -4 DATA_NOT_FOUND = -5 SAVE_ERROR = -3 Call example: - Request: { "c":"4534564", "m":"set_ready", "p" :{ "token":"603fccbc9a9ec13532d7f3259d184f5de35360da", "session":"xJbh", "uid":"7999o944" } } - Response: - SUCCESS: STATUS_SUCCESS = 0 {"m": "REST method exception: 'Unable to create RTS session'", "c": "4534564", "e": -1} """ if uid is None: raise ServerParametersNotFound("UID is invalid!") if session is None: raise ServerParametersNotFound("Session name not specified") session = GameSession(uid=uid, session=session) game_session = session.find_session() if game_session is None: raise DataNotFound("Session not found") ''' session is expired ? ''' if int(time.time()) > game_session.expired_at: raise ServerException("Session expired!") if len(game_session.users) < 1: raise ServerException("No users found in current session") ''' user is ready to play the game ?''' try: game_session = session.user_is_ready(game_session) game_session.save() except: traceback.print_exc() raise SaveDataException("Unable to save user data.") ''' create session on RT server and start the game ''' game = PluginHandler.get_plugin(game_session.application_name) if game.can_start_game(game_session): try: game_session = session.create_rt_session(game_session) except Exception as exc: traceback.print_exc() raise ServerException("Unable to create RTS session") ''' save session data ''' try: game_session.save() except: traceback.print_exc() raise SaveDataException("Unable to save session data.") return STATUS_SUCCESS
def set_team(self, session, uid, team, **kwargs): """ Set user team method call: set_team(session, uid, team, **kwargs) -> where **kwargs : <optional parameters> mandatory parameters: token - auth token uid - user ID session - session name team - team name. Example: Blue Team Returns: SUCCESS: returns STATUS_SUCCESS = 0 ERROR: REST_EXCEPTION = -1 PARAMETER_NOT_FOUND = -4 DATA_NOT_FOUND = -5 SAVE_ERROR = -3 Call example: - Request: { "c":"4534564", "m":"set_team", "p" :{ "token":"0c8d4ef958eeb5e793f9b963686595781171b6e2", "session":"ShyU7", "uid":"yoyo", "team":"Blue Team" } } - Response: - SUCCESS: STATUS_SUCCESS = 0 {"m": "REST method exception: 'Session name not specified'", "c": "4534564", "e": -4} """ if uid is None: raise ServerParametersNotFound("UID is invalid!") if session is None: raise ServerParametersNotFound("Session name not specified") if team is None: raise ServerParametersNotFound("Team name not specified") session = GameSession(uid=uid, session=session) game_session = session.find_session() if game_session is None: raise DataNotFound("Session not found") ''' read game (appliction) plugin ''' try: game = PluginHandler.get_plugin(game_session.application_name) user_team_name = game.get_joinable_team(game_session.teams, uid, name=team) if user_team_name: _save = False for team in game_session.teams: if team.name == user_team_name: team.users.append(uid) _save = True break if _save: game_session.save() except: traceback.print_exc() raise SaveDataException("Unable to set user in team") return STATUS_SUCCESS
def get_session(self, **kwargs): ''' Create new game session on controller with status 1. Incomplete if no of users < game available slots 2. Complete (start game) if no of users = game available slots 3. if no of users > game available slots then create new session ''' ''' get session for game ''' session = Session.objects( application_name=self.application_name, status=SESSION_STATUS[0], is_active=False ).first() ''' if session not found then create new session with status INCOMPLETE ''' if session is None: session_owner = self.get_user() session = Session( name=self.__create_session(), application_name = self.application_name, owner = session_owner.uid, users = [session_owner], status = SESSION_STATUS[0], is_active=False, expired_at=self.expire_in() ) else: user_exist = False if self.is_expired(session.expired_at): session.delete(safe=True) return None ''' if session exists then join in session ''' if len(session.users) == 0: session.users = [] else: if any(user.uid == self.uid for user in session.users): user_exist = True if not user_exist: session.users.append(self.get_user()) ''' create teams if not exists''' game = PluginHandler.get_plugin(self.application_name) if len(session.teams) == 0: if self.game_type is not None: teams = game.setup_teams(self.game_type) if not isinstance(teams, list) or len(teams) == 0: raise Exception("Not instance of list or list is empty!") session.teams = [] for team in teams: session.teams.append(Team(name=team)) session.game_type = self.game_type ''' add user in any team is flag is autosetteam''' if self.auto_team: team_name = game.get_joinable_team(session.teams, self.uid, name=None) for team in session.teams: if team.name == team_name: team.users.append(self.uid) break ''' set flag auto_ready = True and start the game ''' if self.auto_ready: for user in session.users: if user.uid == self.uid: user.ready = True break ''' save session ''' session.save() return session