game_api.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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>/config")
  40. def game_config(game_id):
  41. g = require_game(game_id)
  42. return jsonify({
  43. "timer": g.timer,
  44. "rounds": g.rounds,
  45. "onlyAmerica": g.only_america,
  46. })
  47. @game.route("/<game_id>/coords")
  48. def coords(game_id):
  49. g = require_game(game_id)
  50. return jsonify({
  51. str(c.round_number): {
  52. "lat": c.latitude,
  53. "lng": c.longitude,
  54. } for c in g.coordinates
  55. })
  56. @game.route("/<game_id>/players")
  57. def players(game_id):
  58. g = require_game(game_id)
  59. return jsonify({"players": [p.to_dict() for p in g.players]})
  60. @game.route("/<game_id>/linked", methods=["GET", "POST"])
  61. def link_game(game_id):
  62. g = require_game(game_id)
  63. if request.method == "GET":
  64. return jsonify({"linkedGame": g.linked_game})
  65. link_id = request.json.get("linkedGame", None)
  66. if link_id is None or db.Game.query.get(link_id) is None:
  67. abort(401)
  68. g.link(link_id)
  69. return "", 201
  70. @game.route("/<game_id>/join", methods=["POST"])
  71. def join(game_id):
  72. name = request.json.get("playerName", None)
  73. if name is None:
  74. abort(400)
  75. g = require_game(game_id)
  76. if db.Player.query.filter(db.Player.game_id == game_id, db.Player.player_name == name).first() is not None:
  77. abort(409)
  78. p = g.join(name)
  79. return jsonify({
  80. "playerId": str(p.player_id)
  81. }), 201
  82. @game.route("/<game_id>/current")
  83. def current_round(game_id):
  84. g = require_game(game_id)
  85. player = require_player(game_id)
  86. cur_rnd = player.get_current_round()
  87. if cur_rnd is None:
  88. coord = None
  89. else:
  90. lookup = db.Coordinate.query.get((game_id, cur_rnd))
  91. cur_rnd = str(cur_rnd)
  92. if lookup is None:
  93. coord = None
  94. else:
  95. coord = {
  96. "lat": lookup.latitude,
  97. "lng": lookup.longitude,
  98. }
  99. return jsonify({
  100. "currentRound": cur_rnd,
  101. "coord": coord,
  102. "timer": g.timer,
  103. })
  104. @game.route("/<game_id>/guesses/<int:round_num>", methods=["POST"])
  105. def make_guess(game_id, round_num):
  106. player = require_player(game_id)
  107. if round_num != player.get_current_round():
  108. abort(409)
  109. timed_out = request.json.get("timeout", False)
  110. if timed_out:
  111. player.add_timeout(round_num)
  112. db.session.commit()
  113. return jsonify({
  114. "score": 0,
  115. "totalScore": player.get_total_score(),
  116. "distance": None,
  117. }), 201
  118. try:
  119. lat = float(request.json.get("lat", None))
  120. lng = float(request.json.get("lng", None))
  121. remaining = int(request.json.get("timeRemaining", None))
  122. except ValueError:
  123. abort(400)
  124. target = db.Coordinate.query.get((game_id, round_num))
  125. if target is None:
  126. abort(400)
  127. guess_score, distance = lib.score((target.latitude, target.longitude), (lat, lng))
  128. player.add_guess(round_num, lat, lng, guess_score, remaining)
  129. return jsonify({
  130. "score": guess_score,
  131. "totalScore": player.get_total_score(),
  132. "distance": distance,
  133. }), 201