SES-106 changed SignalR static field to be able to share them in other classes
This commit is contained in:
parent
c384b279cb
commit
f75e9d96f9
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Lista zalogowanych graczy i identyfikator wybranej postaci
|
||||
/// Klasa zawierająca wszystkie dane potrzebne w SignalR odnośnie aktualnej sesji
|
||||
/// </summary>
|
||||
private static Dictionary<string, int> ConnectedCharacters = new Dictionary<string, int>();
|
||||
|
||||
/// <summary>
|
||||
/// Status, czy GM został już zalogowany
|
||||
/// </summary>
|
||||
private static bool GameMasterConnected = new bool();
|
||||
private SessionHubData sessionHubData;
|
||||
|
||||
public SessionHub()
|
||||
{
|
||||
this.sessionHubData = new SessionHubData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -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
|
||||
/// <returns>Zwraca true - jeśli udało się zalogować, false - jesli ktoś zalogował się już jako GM</returns>
|
||||
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
|
||||
/// <returns>Wysyła wiadomość "Welcome" do wszystkich zalogowanych użytkoników</returns>
|
||||
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");
|
||||
|
52
SessionCompanion/SessionCompanion/Hubs/SessionHubData.cs
Normal file
52
SessionCompanion/SessionCompanion/Hubs/SessionHubData.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SessionCompanion.Hubs
|
||||
{
|
||||
/// <summary>
|
||||
/// Statyczna klasa przechowujaca informacje dotyczące SignalR dla SessionHub
|
||||
/// </summary>
|
||||
public class SessionHubData
|
||||
{
|
||||
/// <summary>
|
||||
/// Lista zalogowanych graczy i identyfikator wybranej postaci
|
||||
/// </summary>
|
||||
public static Dictionary<string, int> ConnectedCharacters = new Dictionary<string, int>();
|
||||
|
||||
/// <summary>
|
||||
/// Status, czy GM został już zalogowany
|
||||
/// </summary>
|
||||
public static bool GameMasterConnected;
|
||||
|
||||
/// <summary>
|
||||
/// Zwraca lub ustawia status zalogowania GM
|
||||
/// </summary>
|
||||
public bool GameMasterConnected_Prop
|
||||
{
|
||||
get
|
||||
{
|
||||
return GameMasterConnected;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
GameMasterConnected = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Zwraca lub ustawia listę zalogowanych graczy
|
||||
/// </summary>
|
||||
public Dictionary<string, int> ConnectedCharacters_Prop
|
||||
{
|
||||
get
|
||||
{
|
||||
return ConnectedCharacters;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
ConnectedCharacters = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user