From 2c4602049739495b4a07c1ab1c21ada3bb22a47a Mon Sep 17 00:00:00 2001 From: Jonas Weinz Date: Sun, 10 Mar 2019 14:06:55 +0100 Subject: [PATCH] notification changes --- game_manager.js | 32 +++++++++++++++++++++++++++++--- sidebar.js | 2 +- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/game_manager.js b/game_manager.js index 158eb39..36342c3 100644 --- a/game_manager.js +++ b/game_manager.js @@ -16,8 +16,6 @@ class GameManager this.grid.player_change_listener(this.dummy_player); - this.received_close_message = false; - // possible modes: // -- none // -- local @@ -194,6 +192,8 @@ class GameManager var status_title = "" + this.remote_player.get_name() + " vs. " + opponent_name; + this.notify("Your game against " + opponent_name + " started"); + if (is_first_move) { this.grid.deactivate_all(); @@ -238,6 +238,8 @@ class GameManager this.status_change_listener("game was closed by server or opponent", "Game Over"); + this.notify("Game is closed"); + this.end_game(); } @@ -257,6 +259,7 @@ class GameManager { this.status_change_listener("your turn"); this.grid.player_change_listener(this.remote_player); + this.notify("it's your turn"); } else { @@ -265,11 +268,15 @@ class GameManager } } - end_game() + end_game(clicked_by_local_user = false) { if (this.game_mode == "remote") { this.game_server_connection.close(); + if (clicked_by_local_user) + { + this.status_change_listener("you closed the game", "Game Over"); + } } else if (this.game_mode == "local") { @@ -284,4 +291,23 @@ class GameManager this.status_change_listener("connection error", "Game Over"); this.end_game(); } + + notify(text) { + if (!("Notification" in window)) { + return; + } + + else if (Notification.permission === "granted") { + var notification = new Notification(text); + } + + else if (Notification.permission !== 'denied') { + Notification.requestPermission(function (permission) { + if (permission === "granted") { + var notification = new Notification(text); + } + }); + } + + } } \ No newline at end of file diff --git a/sidebar.js b/sidebar.js index 7e8bb17..d96cd08 100644 --- a/sidebar.js +++ b/sidebar.js @@ -92,7 +92,7 @@ class Sidebar this.game_manager.register_status_change_listener((c,t=null) => this.status_change_listener(c, t)); this.b_local.addEventListener("click", () => this.game_manager.start_local_game()); - this.b_end_game.addEventListener("click", () => this.game_manager.end_game()); + this.b_end_game.addEventListener("click", () => this.game_manager.end_game(true)); this.b_remote.addEventListener("click", () => this.game_manager.register_remote_game(this.get_player_name())); }