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']))
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)
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))
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]})
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']))
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)
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)
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)