game_api.py 4.3 KB

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