Exemplo n.º 1
0
    def get(self, request_handler=None):
        if request_handler:
            try:
                cookie_id = request_handler.get_secure_cookie("cookie_id").decode()
                temp_id = request_handler.get_secure_cookie("temp_id").decode()
            except Exception as e:
                logging.info('cookie_id or temp_id not in request_handler: {0}'.format(request_handler))
        else:
            cookie_id = None
            temp_id = None

        if not cookie_id:
            session_exists = False
            cookie_id = GenerateId().generate_cookie_id()
            temp_id = GenerateId().generate_temp_id(cookie_id)
        elif cookie_id:
            session_exists = True

        check_temp_id = GenerateId().generate_temp_id(cookie_id)
        if check_temp_id == temp_id:
            session = SessionData(cookie_id, temp_id)
        else:
            raise InvalidSessionException()

        if session_exists:
            session_data = self._fetch(cookie_id)
            for key, value in session_data.items():
                session[key] = value

        return session
Exemplo n.º 2
0
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
Exemplo n.º 3
0
    def init_cards():
        try:
            dealer_name = BaseHandler.online_user_list.popleft()
        except Exception as e:
            dealer_name = str(uuid.uuid4())

        dealer_id = GenerateId().generate_user_id(dealer_name)
        dealer_cards = []
        player_cards = []
        desk_id = GenerateId().generate_desk_id()
        left_cards = [
            'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'
        ] * 4
        random.shuffle(left_cards)
        logging.info(
            'BaseHandler: Generate desk_id: {0}, left_cards info: {1}'.format(
                desk_id, left_cards))
        dealer_cards.append(left_cards.pop())
        dealer_cards.append(left_cards.pop())
        player_cards.append(left_cards.pop())
        player_cards.append(left_cards.pop())
        logging.info(
            'BaseHandler: Init dealer_cards: {0}, player_cards: {1}, left_cards: {2}'
            .format(dealer_cards, player_cards, left_cards))

        # dealer_cards_total_count = BaseHandler.calc_card_total_number(dealer_cards)
        # player_cards_total_count = BaseHandler.calc_card_total_number(player_cards)
        #
        # dealer_is_blackjack = BaseHandler.check_balckjack(dealer_cards_total_count)
        # player_is_blackjack = BaseHandler.check_balckjack(player_cards_total_count)

        player_cards_total_count, player_cards_status = get_hand_cards_total_cout_status(
            player_cards)
        dealer_cards_total_count, dealer_cards_status = get_hand_cards_total_cout_status(
            dealer_cards)

        init_cards_info = dict(
            desk_id=desk_id,
            player_cards=player_cards,
            player_cards_total_count=player_cards_total_count,
            player_cards_status=player_cards_status,
            dealer_id=dealer_id,
            dealer_cards=dealer_cards,
            dealer_cards_total_count=dealer_cards_total_count,
            dealer_cards_status=dealer_cards_status,
            left_cards=left_cards,
        )

        return init_cards_info
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
    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')
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    def __init__(self, cookie_id, temp_id):
        self.cookie_id = cookie_id
        self.temp_id = temp_id
        self.session_id =  GenerateId().generate_session_id(self.cookie_id)

        self['cookie_id'] = cookie_id
        self['temp_id'] = temp_id
        self['session_id'] = self.session_id
Exemplo n.º 8
0
 def insert_user_session_id(self, user_name):
     user_id = self.hget(user_name, 'user_id')
     user_session_id = GenerateId().generate_session_id2(user_name)
     user_session_id_dict = dict(
         user_session_id=user_session_id,
         user_id=user_id,
     )
     self.redis_pool.hmset(user_session_id, user_session_id_dict)
     self.redis_pool.expire(user_session_id, self.redis_timeout)
Exemplo n.º 9
0
 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 {}
Exemplo n.º 10
0
 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)
Exemplo n.º 11
0
 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)
Exemplo n.º 12
0
    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)
Exemplo n.º 13
0
 def insert_user_name(self,
                      user_name,
                      user_password,
                      user_gender=None,
                      user_avatar=None):
     """
     check passed user info to redis
     :param user_name:
     :param user_password:
     :param user_gender:
     :param user_avatar:
     :return:
     """
     user_id = GenerateId().generate_user_id(user_name)
     user_dict = dict(
         user_name=user_name,
         user_password=user_password,
         user_id=user_id,
         user_gender=user_gender,
         user_avatar=user_avatar,
     )
     self.redis_pool.hmset(user_name, user_dict)
     self.redis_pool.expire(user_name, self.redis_timeout)
Exemplo n.º 14
0
 def get_user_session_from_user_name(self, user_name):
     user_session_id = GenerateId().generate_session_id2(user_name)
     user_session = self.hgetall(user_session_id)
     user_session = eval(user_session)
     return user_session