def startgame(self): cookie_id = self.get_secure_cookie('cookie_id').decode() temp_id = self.get_secure_cookie('temp_id').decode() # user_id = self.get_secure_cookie('user_id').decode() user_id = self.get_secure_cookie('user_id') if check_temp_id_session_id_from_cookie_id(cookie_id, temp_id): cards_info = BaseHandler.init_cards() cards_info['player_id'] = user_id self.step = 'STARTGAME' self.Session['step'] = self.step session_id = GenerateId().generate_session_id(cookie_id) RedisPool().hmset(session_id, dict(step=self.step)) for key, value in cards_info.items(): self.Session[key] = value RedisPool().hmset(session_id, {key: value}) status, info = calc_match_result(self.Session) if info == 'BLACKJACK': self.Session['match_status'] = False self.Session['match_status'] = True current_session = self.Session self.render('startgame.html', current_session=current_session, **current_session)
def login(self): user_name = self.get_argument('user_name', '') user_password = self.get_argument('user_password', '') if user_name and user_password and user_name == user_password: if not RedisPool().exists(user_name): RedisPool().insert_user_name(user_name, user_password) user_id = GenerateId().generate_user_id(user_name) self.set_secure_cookie('user_id', user_id) cookie_id = self.get_secure_cookie('cookie_id').decode() temp_id = self.get_secure_cookie('temp_id').decode() if check_temp_id_session_id_from_cookie_id(cookie_id, temp_id): self.Session['user_name'] = user_name self.Session['user_password'] = user_password self.Session['user_id'] = user_id RedisPool().hmset(self.Session['session_id'], self.Session) self.redirect('/startgame') else: logging.info( 'user_name or user_password is invalid, redirect to /login...') self.render('login.html')
def _fetch(self, cookie_id): session_id = GenerateId().generate_session_id(cookie_id) if RedisPool().exists(session_id): session_data = RedisPool().hgetall(session_id) if session_data and isinstance(session_data, dict): return session_data else: return {} else: return {}
def save(self): try: cookie_id = self.request_handler.get_secure_cookie("cookie_id").decode() temp_id = self.request_handler.get_secure_cookie("temp_id").decode() session_id = GenerateId().generate_session_id(cookie_id) if RedisPool().exists(session_id): RedisPool().expire(session_id) else: self.session_manager.set(self.request_handler, self) except: self.session_manager.set(self.request_handler, self)
def stopcard(self): cookie_id = self.get_secure_cookie('cookie_id').decode() temp_id = self.get_secure_cookie('temp_id').decode() if check_temp_id_session_id_from_cookie_id(cookie_id, temp_id): session_id = self.Session['session_id'] status, info = calc_match_result(self.Session) self.Session['match_status'] = False self.Session['match_status_info'] = info RedisPool().hmset(session_id, self.Session) print(self.Session) current_session = self.Session print(current_session) # ######## # for git comment to add this line # ######## self.render('startgame.html', current_session=current_session, **current_session)
def set(self, request_handler, session): request_handler.set_secure_cookie("cookie_id", session.cookie_id) request_handler.set_secure_cookie("temp_id", session.temp_id) session.session_id = session['session_id'] = GenerateId().generate_session_id(session.cookie_id) RedisPool().hmset(session.session_id, session)
def _wrapper(self, *args, **kwargs): cookie_id = self.get_secure_cookie('cookie_id') if not cookie_id: self.redirect('/') return cookie_id = cookie_id.decode() session_id = GenerateId().generate_session_id(cookie_id) if not RedisPool().exists(session_id): logging.info('check_cookie_id wrapper: cookie_id found but session_id lost, redirect to / to init...') self.redirect('/') return return function(self, *args, **kwargs)
def _wrapper(self, *args, **kwargs): cookie_id = self.get_secure_cookie('cookie_id') if not cookie_id: self.redirect('/') return session_id = GenerateId().generate_session_id(cookie_id.decode()) if not RedisPool().hget(session_id, 'user_id'): logging.info('check_login wrapper: redirect to /login to login...') self.redirect('/login') return return function(self, *args, **kwargs)
def check_temp_id_session_id_from_cookie_id(cookie_id, temp_id): """ 根据get_secure_cookie获取到的cookie_id和temp_id来生成session_id,校验cookie_id和temp_id是否与重新生成的一致,校验session_id是否存在redis中(有效性)。 :param cookie_id:str :param temp_id: str :return: True/False """ check_temp_id = GenerateId().generate_temp_id(cookie_id) check_session_id = GenerateId().generate_session_id(cookie_id) if temp_id == check_temp_id and RedisPool().exists(check_session_id): return True else: return False
def getcard(self): cookie_id = self.get_secure_cookie('cookie_id').decode() temp_id = self.get_secure_cookie('temp_id').decode() if check_temp_id_session_id_from_cookie_id(cookie_id, temp_id): session_id = self.Session['session_id'] player_cards = eval(self.Session['player_cards']) left_cards = eval(self.Session['left_cards']) player_cards.append(left_cards.pop()) player_cards_total_count, player_cards_status = get_hand_cards_total_cout_status( player_cards) self.step = 'GETCARD' changes_dict = dict( player_cards=player_cards, player_cards_total_count=player_cards_total_count, player_cards_status=player_cards_status, left_cards=left_cards, step=self.step, ) for key, value in changes_dict.items(): self.Session[key] = value RedisPool().hmset(session_id, {key: value}) status, info = calc_match_result(self.Session) if info == 'BOOM': self.Session['match_status'] = False self.Session['match_status'] = True self.Session['match_status_info'] = info current_session = self.Session self.render('startgame.html', current_session=current_session, **current_session)