示例#1
0
def token_verify(token):
    try:
        data = jwt.decode(token, app.config["SECRET_KEY"])
        code = data["code"]
        key = "support_jwt_{}".format(code)
        data = json.loads(redis.get(key))
        if not data:
            return "EXPIRED"
        elif data.get("token") != token:
            return "OFFLINE"
        else:
            user = TbUser.query.filter_by(code=code).first()
            if not user:
                return "NOT_EXIST"
            request.token = data
            # 更新 token
            redis.set(key,
                      value=json.dumps({
                          "exp_time": utc_timestamp() + 24 * 60 * 60,
                          "token": token
                      }),
                      ex=24 * 60 * 60)
            return user
    except TypeError:
        # 代表没有从 redis 中取得数据
        return "EXPIRED"
    except Exception as e:
        app.logger.info("obtain data error: {}".format(str(e)))
        return "INVALID"
示例#2
0
文件: util.py 项目: GSIL-Monitor/xxw
def release_lock(req, user):
    key = "edit_{}_config_lock".format(ConfigNameMap.name[req["category"]])
    data = redis.get(key)
    if not data:
        return {}, 200
    elif data and json.loads(data)["user_code"] == user.code:
        redis.delete(key)
        return {}, 200
    else:
        return Msg.RELEASE_LOCK_FAILED, 400
示例#3
0
def on_solve(data):
    user_id = data.get('user_id')
    answer = data.get('answer')
    if not user_id:
        emit("update", {'error': 'no user id provided', 'status': 'error'})
    if not answer:
        emit("update", {'error': 'no answer provided', 'status': 'error'})

    if not redis.exists(user_id):
        emit("update", {
            'error': 'user doesnt have a game_state started',
            'status': 'error'
        })
    game_state = json.loads(redis.get(user_id))

    if datetime.utcnow() - datetime.strptime(game_state['started_on'],
                                             DATE_FORMAT) > THRESHOLD:
        emit("update", {'error': 'game finished', 'status': 'finished'})

    correct_answer = game_state['correct_answer']
    original_sentence = game_state['original_sentence']
    correct_answer_variations = getAnswerVariations(correct_answer)
    is_correct_answer = process.extractOne(
        answer, correct_answer_variations, score_cutoff=91) != None

    game_state['answers'].append({
        'answer':
        answer,
        'sentence':
        reconstruct_sentence(original_sentence, answer, correct_answer),
        'is_correct':
        is_correct_answer
    })
    if is_correct_answer:
        game_state['score'] = game_state['score'] + 1

    next_expression = get_random_expression()
    next_sentence = next_expression.get('text')
    next_random_word = get_random_word(next_sentence)
    next_user_sentence = obfuscate_in_sentence(next_sentence, next_random_word)
    next_correct_answer = next_random_word if next_random_word[-1] not in [
        ',', ':', ';'
    ] else next_random_word[:-1]
    game_state['user_sentence'] = next_user_sentence
    game_state['correct_answer'] = next_correct_answer
    game_state['original_sentence'] = next_sentence
    game_state['last_time_answered'] = datetime.utcnow().strftime(DATE_FORMAT)

    redis.set(user_id, json.dumps(game_state), ex=2 * THRESHOLD)
    user_game_state = generate_user_game_state(game_state)
    emit("update", {'game_state': user_game_state, 'status': 'ok'})
示例#4
0
def on_check(data):
    user_id = data.get('user_id')
    if not user_id:
        emit("update", {'error': 'no user id provided', 'status': 'error'})
    if not redis.exists(user_id):
        emit("update", {
            'error': 'user doesnt have a game_state started',
            'status': 'error'
        })

    game_state = json.loads(redis.get(user_id))
    if datetime.utcnow() - datetime.strptime(game_state['started_on'],
                                             DATE_FORMAT) > THRESHOLD:
        emit("update", {'error': 'game finished', 'status': 'finished'})
    user_game_state = generate_user_game_state(game_state)
    emit("update", {'game_state': user_game_state, 'status': 'ok'})
示例#5
0
 def post(self):
     """
     编辑请求
     """
     req, error = validate_schema(universal_schema, request.json)
     if error:
         return error, 400
     user = request.current_user
     # 执行锁住操作
     key = "edit_{}_config_lock".format(ConfigNameMap.name[req["category"]])
     res = redis.get(key)
     if res and json.loads(res)["user_code"] != user.code:
         return Msg.EDIT_LOCKED, 400
     else:
         data = json.dumps({"user_code": user.code})
         exp = app.config.get("EDIT_EXPIRATION")
         exp = int(exp) if exp else 3600
         redis.set(key, data, ex=exp)
         return {"exp_time": exp}, 200
示例#6
0
文件: util.py 项目: GSIL-Monitor/xxw
 def get(self):
     data = redis.get(self.key)
     return json.loads(data) if data else {}