web.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from flask import Flask, flash, redirect, render_template, abort, request, session
  4. from tournament import Tournament
  5. app = Flask(__name__)
  6. app.secret_key = "This is an extremely secret key"
  7. T = Tournament(sdump=True)
  8. @app.route("/")
  9. def index():
  10. compact = session.get('compact', False)
  11. games = list(T.running_games.values())
  12. games.sort(key = lambda x: x.table)
  13. return render_template("index.html.j2", tournament=T, compact=compact, sdump=T.sdump, games=games)
  14. @app.route("/compact/<z>")
  15. def compact(z):
  16. c = bool(int(z))
  17. session['compact'] = c
  18. return redirect('/')
  19. @app.route("/sdump/<z>")
  20. def sdump(z):
  21. c = bool(int(z))
  22. T.sdump = c
  23. return redirect('/')
  24. @app.route("/sched", methods=['POST'])
  25. def schedule_default():
  26. try:
  27. T.schedule_round()
  28. except RuntimeError as e:
  29. flash(str(e))
  30. return redirect('/')
  31. @app.route("/finalize/<int:t>", methods=['POST'])
  32. def finalize(t):
  33. if t not in T.running_games:
  34. abort(404)
  35. g = T.running_games[t]
  36. try:
  37. if 's' in request.form:
  38. s1 = int(request.form['s'])
  39. s2 = 4-s1
  40. else:
  41. s1 = int(request.form['s1'])
  42. s2 = int(request.form['s2'])
  43. except (ValueError, KeyError):
  44. abort(400)
  45. g.finalize((s1,s2))
  46. return redirect('/')
  47. @app.route("/cancel", methods=['POST'])
  48. def cancel():
  49. for g in list(T.running_games.values()):
  50. g.finalize((0, 0))
  51. flash("All games have been cancelled")
  52. return redirect('/')
  53. @app.route("/randomize", methods=['POST'])
  54. def randomize():
  55. import random
  56. for g in list(T.running_games.values()):
  57. a = random.randint(0, 4)
  58. g.finalize((a, 4-a))
  59. flash("All games have been randomized")
  60. return redirect('/')
  61. @app.route("/sort", methods=['POST'])
  62. def sort():
  63. T.sort_players()
  64. return redirect('/')
  65. @app.route("/remove", methods=['POST'])
  66. def remove():
  67. try:
  68. n = int(request.form['n'])
  69. except (ValueError, KeyError):
  70. abort(400)
  71. try:
  72. r = T.remove_players(n)
  73. except (ValueError, RuntimeError) as e:
  74. flash(str(e))
  75. return redirect('/')
  76. return render_template("removed.html.j2", rem=r)
  77. @app.route("/load_sdump", methods=['POST'])
  78. def load_sdump():
  79. try:
  80. T.load_sdump(request.form['fname'])
  81. except KeyError:
  82. abort(400)
  83. except (RuntimeError, IOError) as e:
  84. flash(str(e))
  85. return redirect('/')
  86. if __name__ == '__main__':
  87. import IPython
  88. import signal
  89. def debug(sig, frame):
  90. print("Running debug shell.")
  91. IPython.embed()
  92. signal.signal(signal.SIGUSR2, debug)
  93. app.run()