From f75e9d96f939e83ded01bd5865f6faf76c81dac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20G=C3=B3reczny?= Date: Fri, 18 Dec 2020 10:52:37 +0100 Subject: [PATCH 1/2] SES-106 changed SignalR static field to be able to share them in other classes --- .../SessionCompanion/Hubs/SessionHub.cs | 25 ++++----- .../SessionCompanion/Hubs/SessionHubData.cs | 52 +++++++++++++++++++ 2 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 SessionCompanion/SessionCompanion/Hubs/SessionHubData.cs diff --git a/SessionCompanion/SessionCompanion/Hubs/SessionHub.cs b/SessionCompanion/SessionCompanion/Hubs/SessionHub.cs index d6c6573..4e59a32 100644 --- a/SessionCompanion/SessionCompanion/Hubs/SessionHub.cs +++ b/SessionCompanion/SessionCompanion/Hubs/SessionHub.cs @@ -1,7 +1,5 @@ using Microsoft.AspNetCore.SignalR; using System; -using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; namespace SessionCompanion.Hubs @@ -9,17 +7,13 @@ namespace SessionCompanion.Hubs public class SessionHub : Hub { /// - /// Lista zalogowanych graczy i identyfikator wybranej postaci + /// Klasa zawierająca wszystkie dane potrzebne w SignalR odnośnie aktualnej sesji /// - private static Dictionary ConnectedCharacters = new Dictionary(); - - /// - /// Status, czy GM został już zalogowany - /// - private static bool GameMasterConnected = new bool(); + private SessionHubData sessionHubData; public SessionHub() { + this.sessionHubData = new SessionHubData(); } /// @@ -31,16 +25,17 @@ namespace SessionCompanion.Hubs public override Task OnDisconnectedAsync(Exception exception) { // if true then it is character, if false it is GM - if (ConnectedCharacters.ContainsKey(Context.ConnectionId)) + if (this.sessionHubData.ConnectedCharacters_Prop.ContainsKey(Context.ConnectionId)) { Groups.RemoveFromGroupAsync(Context.ConnectionId, "Players"); - ConnectedCharacters.Remove(Context.ConnectionId); + this.sessionHubData.ConnectedCharacters_Prop.Remove(Context.ConnectionId); } else { Groups.RemoveFromGroupAsync(Context.ConnectionId, "GameMaster"); - GameMasterConnected = false; + this.sessionHubData.GameMasterConnected_Prop = false; } + Clients.All.SendAsync("GoodBye", "Player has left the game"); return base.OnDisconnectedAsync(exception); } @@ -54,10 +49,10 @@ namespace SessionCompanion.Hubs /// Zwraca true - jeśli udało się zalogować, false - jesli ktoś zalogował się już jako GM public bool GameMasterLogin() { - if (!GameMasterConnected) + if (!this.sessionHubData.GameMasterConnected_Prop) { Groups.AddToGroupAsync(Context.ConnectionId, "GameMaster"); - GameMasterConnected = true; + this.sessionHubData.GameMasterConnected_Prop = true; Clients.All.SendAsync("Welcome", "Welcome new Game Master"); return true; } @@ -77,7 +72,7 @@ namespace SessionCompanion.Hubs /// Wysyła wiadomość "Welcome" do wszystkich zalogowanych użytkoników public Task PlayerCharacterLogin(int characterId) { - ConnectedCharacters.Add(Context.ConnectionId, characterId); + this.sessionHubData.ConnectedCharacters_Prop.Add(Context.ConnectionId, characterId); Groups.AddToGroupAsync(Context.ConnectionId, "Players"); return Clients.All.SendAsync("Welcome", "Welcome new Player"); diff --git a/SessionCompanion/SessionCompanion/Hubs/SessionHubData.cs b/SessionCompanion/SessionCompanion/Hubs/SessionHubData.cs new file mode 100644 index 0000000..d370d20 --- /dev/null +++ b/SessionCompanion/SessionCompanion/Hubs/SessionHubData.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; + +namespace SessionCompanion.Hubs +{ + /// + /// Statyczna klasa przechowujaca informacje dotyczące SignalR dla SessionHub + /// + public class SessionHubData + { + /// + /// Lista zalogowanych graczy i identyfikator wybranej postaci + /// + public static Dictionary ConnectedCharacters = new Dictionary(); + + /// + /// Status, czy GM został już zalogowany + /// + public static bool GameMasterConnected; + + /// + /// Zwraca lub ustawia status zalogowania GM + /// + public bool GameMasterConnected_Prop + { + get + { + return GameMasterConnected; + } + + set + { + GameMasterConnected = value; + } + } + + /// + /// Zwraca lub ustawia listę zalogowanych graczy + /// + public Dictionary ConnectedCharacters_Prop + { + get + { + return ConnectedCharacters; + } + + set + { + ConnectedCharacters = value; + } + } + } +} -- 2.20.1 From eeb9c2ebe0e89bcd82adb83258853ff5fc199cf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20G=C3=B3reczny?= Date: Fri, 18 Dec 2020 10:52:48 +0100 Subject: [PATCH 2/2] SES-106 Added new method in controller --- .../Controllers/UserController.cs | 17 ++++++++++ .../SessionCompanion/SessionCompanion.xml | 31 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/SessionCompanion/SessionCompanion/Controllers/UserController.cs b/SessionCompanion/SessionCompanion/Controllers/UserController.cs index 2e9081b..ec8607a 100644 --- a/SessionCompanion/SessionCompanion/Controllers/UserController.cs +++ b/SessionCompanion/SessionCompanion/Controllers/UserController.cs @@ -6,8 +6,12 @@ using System.Threading.Tasks; namespace SessionCompanion.Controllers { using System; + using System.Security.Policy; + + using Microsoft.AspNetCore.SignalR; using SessionCompanion.Extensions.EitherType; + using SessionCompanion.Hubs; using SessionCompanion.ViewModels.ApiResponses; using SessionCompanion.ViewModels.UserViewModels; @@ -17,9 +21,12 @@ namespace SessionCompanion.Controllers { private readonly IUserService _service; + private SessionHubData _sessionHubData; + public UserController(IUserService service) { this._service = service; + this._sessionHubData = new SessionHubData(); } /// @@ -69,5 +76,15 @@ namespace SessionCompanion.Controllers return new SuccessResponse("User created"); } + + /// + /// Metoda sprawdza czy ktoś jest już zalogowany jako Game Master + /// + /// true jesli ktoś jest już zalogowany i false jeśli nie + [HttpGet("IsGMLogged")] + public Either IsGameMasterAllreadyLogged() + { + return this._sessionHubData.GameMasterConnected_Prop; + } } } \ No newline at end of file diff --git a/SessionCompanion/SessionCompanion/SessionCompanion.xml b/SessionCompanion/SessionCompanion/SessionCompanion.xml index ff31b6a..13641ce 100644 --- a/SessionCompanion/SessionCompanion/SessionCompanion.xml +++ b/SessionCompanion/SessionCompanion/SessionCompanion.xml @@ -26,6 +26,12 @@ Model uzytkownika do zarejestrowania SuccessResponse/ErrorResponse + + + Metoda sprawdza czy ktoś jest już zalogowany jako Game Master + + true jesli ktoś jest już zalogowany i false jeśli nie + Lista zalogowanych graczy i identyfikator wybranej postaci @@ -58,5 +64,30 @@ Identyfikator zalogowanego bohatera Wysyła wiadomość "Welcome" do wszystkich zalogowanych użytkoników + + + Statyczna klasa przechowujaca informacje dotyczące SignalR dla SessionHub + + + + + Lista zalogowanych graczy i identyfikator wybranej postaci + + + + + Status, czy GM został już zalogowany + + + + + Zwraca lub ustawia status zalogowania GM + + + + + Zwraca lub ustawia listę zalogowanych graczy + + -- 2.20.1