game_api.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from flask import Blueprint, abort, request, jsonify
  2. import db
  3. import lib
  4. game = Blueprint("game", __name__)
  5. def require_game(game_id):
  6. g = db.Game.query.get(game_id)
  7. if g is None:
  8. abort(404)
  9. return g
  10. def require_player(game_id):
  11. auth = request.headers.get("Authorization", type=str)
  12. if auth is None:
  13. abort(401)
  14. try:
  15. pid = int(auth.split(maxsplit=1)[-1])
  16. except ValueError:
  17. abort(401)
  18. player = db.Player.query.get(pid)
  19. if player is None:
  20. abort(404)
  21. return player
  22. @game.route("", methods=["PUT"])
  23. def create_game():
  24. timer = request.json.get("timer", None)
  25. if not isinstance(timer, int) or timer <= 0:
  26. abort(400)
  27. rounds = request.json.get("rounds", None)
  28. if not isinstance(rounds, int) or rounds <= 0:
  29. abort(400)
  30. only_america = request.json.get("onlyAmerica", False)
  31. if not isinstance(only_america, bool):
  32. abort(400)
  33. new_game = db.Game.create(timer, rounds, only_america=only_america)
  34. return jsonify({"gameId": new_game.game_id})
  35. @game.route("/<game_id>")
  36. def game_settings(game_id):
  37. g = require_game(game_id)
  38. return jsonify(g.to_dict())
  39. @game.route("/<game_id>/linked", methods=["POST"])
  40. def link_game(game_id):
  41. g = require_game(game_id)
  42. link_id = request.json.get("linkedGame", None)
  43. if link_id is None or db.Game.query.get(link_id) is None:
  44. abort(401)
  45. g.link(link_id)
  46. return "", 201
  47. @game.route("/<game_id>/join", methods=["POST"])
  48. def join(game_id):
  49. name = request.json.get("playerName", None)
  50. if name is None:
  51. abort(400)
  52. g = require_game(game_id)
  53. if db.Player.query.filter(db.Player.game_id == game_id, db.Player.player_name == name).first() is not None:
  54. abort(409)
  55. p = g.join(name)
  56. return jsonify({
  57. "playerId": str(p.player_id)
  58. }), 201
  59. @game.route("/<game_id>/current")
  60. def current_round(game_id):
  61. g = require_game(game_id)
  62. player = require_player(game_id)
  63. cur_rnd = player.get_current_round()
  64. if cur_rnd is None:
  65. coord = None
  66. else:
  67. lookup = db.Coordinate.query.get((game_id, cur_rnd))
  68. cur_rnd = str(cur_rnd)
  69. if lookup is None:
  70. coord = None
  71. else:
  72. coord = {
  73. "lat": lookup.latitude,
  74. "lng": lookup.longitude,
  75. }
  76. return jsonify({
  77. "currentRound": cur_rnd,
  78. "coord": coord,
  79. "timer": g.timer,
  80. })
  81. @game.route("/<game_id>/guesses/<int:round_num>", methods=["POST"])
  82. def make_guess(game_id, round_num):
  83. player = require_player(game_id)
  84. if round_num != player.get_current_round():
  85. abort(409)
  86. timed_out = request.json.get("timeout", False)
  87. if timed_out:
  88. player.add_timeout(round_num)
  89. db.session.commit()
  90. return jsonify({
  91. "score": 0,
  92. "totalScore": player.get_total_score(),
  93. "distance": None,
  94. }), 201
  95. try:
  96. lat = float(request.json.get("lat", None))
  97. lng = float(request.json.get("lng", None))
  98. except ValueError:
  99. abort(400)
  100. target = db.Coordinate.query.get((game_id, round_num))
  101. if target is None:
  102. abort(400)
  103. guess_score, distance = lib.score((target.latitude, target.longitude), (lat, lng))
  104. player.add_guess(round_num, lat, lng, guess_score)
  105. return jsonify({
  106. "score": guess_score,
  107. "totalScore": player.get_total_score(),
  108. "distance": distance,
  109. }), 201