From a88dfaa54fffbcd5bff4efb5ac510129fd8f75cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20G=C3=B3reczny?= Date: Sat, 5 Dec 2020 19:18:45 +0100 Subject: [PATCH] SES-77 Aded singlaR methods for register and override default disconnect --- .../SessionCompanion/Hubs/SessionHub.cs | 39 +++++++++++++++++++ SessionCompanion/SessionCompanion/Startup.cs | 1 + 2 files changed, 40 insertions(+) diff --git a/SessionCompanion/SessionCompanion/Hubs/SessionHub.cs b/SessionCompanion/SessionCompanion/Hubs/SessionHub.cs index 78ed2b8..9d8c7d6 100644 --- a/SessionCompanion/SessionCompanion/Hubs/SessionHub.cs +++ b/SessionCompanion/SessionCompanion/Hubs/SessionHub.cs @@ -8,5 +8,44 @@ namespace SessionCompanion.Hubs { public class SessionHub : Hub { + private static Dictionary ConnectedCharacters = new Dictionary(); + + public SessionHub() + { + } + + public override Task OnDisconnectedAsync(Exception exception) + { + // if true then it is character, if false it is GM + if (ConnectedCharacters.ContainsKey(Context.ConnectionId)) + { + Groups.RemoveFromGroupAsync(Context.ConnectionId, "Players"); + ConnectedCharacters.Remove(Context.ConnectionId); + } + else + Groups.RemoveFromGroupAsync(Context.ConnectionId, "GameMaster"); + Clients.All.SendAsync("GoodBye", "Player has left the game"); + return base.OnDisconnectedAsync(exception); + } + + #region GameMaster + public Task GameMasterLogin() + { + Groups.AddToGroupAsync(Context.ConnectionId, "GameMaster"); + + return Clients.All.SendAsync("Welcome", "Welcome new Game Master"); + } + #endregion + + #region Player + public Task PlayerCharacterLogin(int characterId) + { + ConnectedCharacters.Add(Context.ConnectionId, characterId); + Groups.AddToGroupAsync(Context.ConnectionId, "Players"); + + return Clients.All.SendAsync("Welcome", "Welcome new Player"); + } + #endregion + } } diff --git a/SessionCompanion/SessionCompanion/Startup.cs b/SessionCompanion/SessionCompanion/Startup.cs index b83a9be..da72a46 100644 --- a/SessionCompanion/SessionCompanion/Startup.cs +++ b/SessionCompanion/SessionCompanion/Startup.cs @@ -72,6 +72,7 @@ namespace SessionCompanion endpoints.MapControllerRoute( name: "default", pattern: "{controller}/{action=Index}/{id?}"); + endpoints.MapHub("/sessionhub", options => { options.Transports =