Merge pull request 'SES-77' (#12) from SES-77 into master

Reviewed-on: #12
This commit is contained in:
Karol Górzyński 2020-12-05 21:32:04 +01:00
commit 6ed0708e49
2 changed files with 61 additions and 1 deletions

View File

@ -0,0 +1,51 @@
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SessionCompanion.Hubs
{
public class SessionHub : Hub
{
private static Dictionary<string, int> ConnectedCharacters = new Dictionary<string, int>();
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
}
}

View File

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.SpaServices.AngularCli; using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -8,6 +9,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using SessionCompanion.Configurations; using SessionCompanion.Configurations;
using SessionCompanion.Database; using SessionCompanion.Database;
using SessionCompanion.Hubs;
namespace SessionCompanion namespace SessionCompanion
{ {
@ -29,7 +31,7 @@ namespace SessionCompanion
options.UseSqlServer( options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection"))); Configuration.GetConnectionString("DefaultConnection")));
services.AddRepositories(); services.AddRepositories();
services.AddSignalR();
// In production, the Angular files will be served from this directory // In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration => services.AddSpaStaticFiles(configuration =>
{ {
@ -70,6 +72,13 @@ namespace SessionCompanion
endpoints.MapControllerRoute( endpoints.MapControllerRoute(
name: "default", name: "default",
pattern: "{controller}/{action=Index}/{id?}"); pattern: "{controller}/{action=Index}/{id?}");
endpoints.MapHub<SessionHub>("/sessionhub", options =>
{
options.Transports =
HttpTransportType.WebSockets |
HttpTransportType.LongPolling;
});
}); });
app.UseSpa(spa => app.UseSpa(spa =>