Ejemplo n.º 1
0
def next_battle(battle_id):
    battle = BattleFactory.load_battle(battle_id, db)

    if isinstance(battle, str):
        return failure(battle)

    if battle_id not in next_battle_cache:
        battle_data = battle.get_state()
        next_battle = BattleFactory.clone_battle(current_time(), battle_data,
                                                 db)
        next_battle_cache[battle_id] = next_battle.id

    return success({"next_battle": next_battle_cache[battle_id]})
Ejemplo n.º 2
0
def battle(battle_id):
    battle = BattleFactory.load_battle(battle_id, db)
    user_id = current_user.user_id

    if isinstance(battle, str):
        return failure(battle)

    if request.method == 'GET':
        require = require_format(request.args)
        return success(battle.get_state(current_time(), user_id, require))

    elif request.method == 'POST':
        #todo check user_id match player_id
        request_json = request.get_json(force=True)

        check_res = field_checker(request_json,
                                  ['player_id', 'piece_id', 'position'])
        if check_res is not None:
            return failure(check_res)

        battle.try_drop_piece(current_time(), request_json['player_id'],
                              current_user.user_id, request_json['piece_id'],
                              request_json['position'])
        return success(
            battle.get_state(current_time(), request_json['player_id']))
Ejemplo n.º 3
0
def players(battle_id, player_id):
    succ, battle = BattleFactory.load_battle(battle_id, db)
    if not succ:
        return jsonify({"message": battle})
    request_json = request.get_json(force=True)
    user_id = request_json['user_id']
    result = battle.try_join_player(int(time.time()), player_id, user_id)
    return jsonify(result)
Ejemplo n.º 4
0
def battles():
    if request.method == 'GET':
        try:
            query = json.loads(request.args.get("query", "{}"))
            sort = json.loads(request.args.get("sort", "[]"))
            start = json.loads(request.args.get('start',
                                                "{'start': 0}"))['start']
            limit = json.loads(request.args.get('limit',
                                                "{'limit': 30}"))['limit']
        except:
            return failure("request syntax error! need json string!")

        if "username" in query and not username_checker(db, query['username']):
            return failure("user not exist")

        mongo_query = filter_condition_generate(query)
        if isinstance(query, str):
            return failure(query)

        mongo_sort = sort_condition_generate(sort)
        if isinstance(sort, str):
            return failure(sort)

        current_user.update_perference("condition", {
            "query": query,
            "sort": sort
        })

        return success({
            "start":
            start,
            "battle_list":
            history_clear(
                db.battles.find(filter=mongo_query,
                                sort=mongo_sort)[start:start + limit])
        })

    elif request.method == 'POST':
        if current_user.user_id == -1:
            return failure("need login first!")

        request_json = request.get_json(force=True)
        check_res = field_checker(
            request_json,
            ['battle_name', 'accuracy_time', 'additional_time', 'board_type'])
        if check_res is not None:
            return failure(check_res)

        battle = BattleFactory.create_battle(current_time(), request_json,
                                             request_json['board_type'], db)

        if isinstance(battle, str):
            return failure(battle)

        current_user.update_perference("create", request_json)

        return success({"id": battle.id})
Ejemplo n.º 5
0
def battle_page():
    try:
        battle_id = int(request.args.get('battle_id'))
    except Exception as e:
        return render_template("error.html", message=repr(e))
    
    battle = BattleFactory.load_battle(battle_id, db)

    if isinstance(battle, str):
        return render_template("error.html", message=battle)

    return render_template("battle.html", battle=battle.get_state(current_time(), current_user.user_id))
Ejemplo n.º 6
0
def battles():
    if request.method == 'GET':
        #need config (condition, sort)
        return jsonify(id_clear(db.battles.find()))
    elif request.method == 'POST':
        request_json = request.get_json(force=True)
        succ, battle = BattleFactory.create_battle(int(time.time()),
                                                   request_json['battle_info'],
                                                   request_json['board_type'],
                                                   db)
        if not succ:
            return jsonify({"message": battle})
        return jsonify({"id": battle.id})
Ejemplo n.º 7
0
def battle(battle_id):
    if request.method == 'GET':
        succ, battle = BattleFactory.load_battle(battle_id, db)
        user_id = int(request.args.get('user_id'))
        if not succ:
            return jsonify({"message": battle})
        print(jsonify(battle.get_state(int(time.time()), user_id)))
        return jsonify(battle.get_state(int(time.time()), user_id))
    elif request.method == 'POST':
        #todo check user_id match player_id
        request_json = request.get_json(force=True)
        battle.try_drop_piece(int(time.time()), request_json['player_id'],
                              request_json['piece_id'],
                              request_json['position'])
        return jsonify(
            battle.get_state(int(time.time()), request_json['player_id']))
Ejemplo n.º 8
0
def chat_logs(battle_id):
    if current_user.user_id == -1:
        return failure("need login first!")

    request_json = request.get_json(force=True)

    battle = BattleFactory.load_battle(battle_id, db)
    if isinstance(battle, str):
        return failure(battle)

    require = request_json.get("require", {})
    result = battle.append_chat_log(current_time(),
                                    current_user.username,
                                    request_json['content'],
                                    require=require)
    if isinstance(result, str):
        return failure(result)

    return success(result)
Ejemplo n.º 9
0
def players(battle_id, player_id):
    if current_user.user_id == -1:
        return failure("need login first!")

    battle = BattleFactory.load_battle(battle_id, db)
    if isinstance(battle, str):
        return failure(battle)

    if request.method == 'POST':
        result = battle.try_join_player(current_time(), player_id, current_user.user_id, current_user.dump())
        if (isinstance(result, str)):
            return failure(result)
        
        return success(result)
    
    elif request.method == 'DELETE':
        result = battle.try_remove_player(current_time(), player_id, current_user.user_id)
        if (isinstance(result, str)):
            return failure(result)
        
        return success(result)
Ejemplo n.º 10
0
def hosting(battle_id, player_id):
    battle = BattleFactory.load_battle(battle_id, db)
    if isinstance(battle, str):
        return failure(battle)

    user_id = current_user.user_id
    if user_id == -1:
        return failure("请先登录!")

    if request.method == 'POST':
        result = battle.add_hosting(current_time(), player_id, current_user.user_id)
        if (isinstance(result, str)):
            return failure(result)
        
        return success(result)

    elif request.method == 'DELETE':
        result = battle.remove_hosting(current_time(), player_id, current_user.user_id)
        if (isinstance(result, str)):
            return failure(result)
        
        return success(result)