From a02872352fa23cd0b4c6e224d8bdef436025768f Mon Sep 17 00:00:00 2001 From: gsverhoeven Date: Sat, 10 Oct 2020 16:47:54 +0200 Subject: [PATCH 1/7] add a pause/resume option in the web client --- examples/server_example.py | 2 +- ffai/core/game.py | 15 +++++++++++---- ffai/data/saves/.keep | 0 ffai/web/api.py | 8 +++++++- ffai/web/server.py | 6 ++++++ ffai/web/static/dist/js/pybowl.js | 13 +++++++++++++ ffai/web/static/js/controllers.js | 11 ++++++++++- ffai/web/static/js/services.js | 4 ++++ ffai/web/static/partials/game.play.html | 7 ++++--- 9 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 ffai/data/saves/.keep diff --git a/examples/server_example.py b/examples/server_example.py index 9192ca61..eb98f528 100755 --- a/examples/server_example.py +++ b/examples/server_example.py @@ -5,4 +5,4 @@ import ffai.web.server as server if __name__ == "__main__": - server.start_server(debug=True, use_reloader=False, port=1234) \ No newline at end of file + server.start_server(debug=True, use_reloader=False, port=1234) diff --git a/ffai/core/game.py b/ffai/core/game.py index 88a3ba9c..fb253d9a 100755 --- a/ffai/core/game.py +++ b/ffai/core/game.py @@ -187,9 +187,6 @@ def _check_clocks(self): self.action = action break - if not clock.is_running(): - clock.resume() - def _end_game(self): ''' End the game @@ -472,6 +469,16 @@ def pause_clocks(self): for clock in self.state.clocks: if clock.is_running(): clock.pause() + + def pause_resume_clocks(self): + ''' + Pauses all running clocks and resumes all paused clocks. + ''' + for clock in self.state.clocks: + if clock.is_running(): + clock.pause() + else: + clock.resume() def add_secondary_clock(self, team): ''' @@ -2100,4 +2107,4 @@ def get_hypno_modifier(self, player): return 1 - self.num_tackle_zones_in(player) - \ No newline at end of file + diff --git a/ffai/data/saves/.keep b/ffai/data/saves/.keep new file mode 100644 index 00000000..e69de29b diff --git a/ffai/web/api.py b/ffai/web/api.py index 9cfe5585..ad668c2e 100755 --- a/ffai/web/api.py +++ b/ffai/web/api.py @@ -60,6 +60,12 @@ def save_game(game_id, name, team_id): host.save_game(game_id, name, team_id) +def pause_resume_game(game_id): + game = host.get_game(game_id) + if game is not None and game.actor is not None and game.actor.human: + game.pause_resume_clocks() + + def get_game(game_id): game = host.get_game(game_id) if game is not None and game.actor is not None and game.actor.human: @@ -125,4 +131,4 @@ def get_teams(ruleset, board_size=11): def get_bots(): bots = list_bots() - return bots \ No newline at end of file + return bots diff --git a/ffai/web/server.py b/ffai/web/server.py index a6c05ac4..7a7c47ec 100755 --- a/ffai/web/server.py +++ b/ffai/web/server.py @@ -60,6 +60,12 @@ def save(): raise Exception("Cannot save this game") +@app.route('/games//pause', methods=['POST']) +def pause(game_id): + api.pause_resume_game(game_id) + return json.dumps("Game clocks succesfully turned on/off") + + @app.route('/games/', methods=['GET']) def get_all_games(): games = api.get_games() diff --git a/ffai/web/static/dist/js/pybowl.js b/ffai/web/static/dist/js/pybowl.js index d0819469..a0328fd5 100755 --- a/ffai/web/static/dist/js/pybowl.js +++ b/ffai/web/static/dist/js/pybowl.js @@ -930,6 +930,15 @@ appControllers.controller('GamePlayCtrl', ['$scope', '$routeParams', '$location' } }; + $scope.pauseResumeClocks = function pauseResumeClocks(){ + // do pause + GameService.pause($scope.game.game_id).success(function(data){ + console.log("Clocks paused button clicked"); + }); + // do a refresh of the game data + $scope.reload(); + }; + $scope.checkForReload = function checkForReload(time){ if ($scope.available_positions.length === 0 && !$scope.game.state.game_over){ setTimeout(function(){ @@ -1572,6 +1581,10 @@ appServices.factory('GameService', function($http) { return $http.post(options.api.base_url + '/games/' + id + '/act', {'action': action}); }, + pause: function(id) { + return $http.post(options.api.base_url + '/games/' + id + '/pause'); + }, + delete: function(id) { return $http.delete(options.api.base_url + '/games/' + id + "/delete"); }, diff --git a/ffai/web/static/js/controllers.js b/ffai/web/static/js/controllers.js index c4cb1786..ea5fb54f 100755 --- a/ffai/web/static/js/controllers.js +++ b/ffai/web/static/js/controllers.js @@ -878,6 +878,15 @@ appControllers.controller('GamePlayCtrl', ['$scope', '$routeParams', '$location' } }; + $scope.pauseResumeClocks = function pauseResumeClocks(){ + // do pause + GameService.pause($scope.game.game_id).success(function(data){ + console.log("Clocks paused button clicked"); + }); + // do a refresh of the game data + $scope.reload(); + }; + $scope.checkForReload = function checkForReload(time){ if ($scope.available_positions.length === 0 && !$scope.game.state.game_over){ setTimeout(function(){ @@ -1463,4 +1472,4 @@ appControllers.controller('GamePlayCtrl', ['$scope', '$routeParams', '$location' }); } -]); \ No newline at end of file +]); diff --git a/ffai/web/static/js/services.js b/ffai/web/static/js/services.js index 7132497e..c01bb66d 100755 --- a/ffai/web/static/js/services.js +++ b/ffai/web/static/js/services.js @@ -13,6 +13,10 @@ appServices.factory('GameService', function($http) { return $http.post(options.api.base_url + '/games/' + id + '/act', {'action': action}); }, + pause: function(id) { + return $http.post(options.api.base_url + '/games/' + id + '/pause'); + }, + delete: function(id) { return $http.delete(options.api.base_url + '/games/' + id + "/delete"); }, diff --git a/ffai/web/static/partials/game.play.html b/ffai/web/static/partials/game.play.html index 3e1efe00..ffd89743 100755 --- a/ffai/web/static/partials/game.play.html +++ b/ffai/web/static/partials/game.play.html @@ -5,6 +5,9 @@
  • Games
  • +
  • + Pause +
  • - @@ -38,7 +39,7 @@