Website Summary Files Commits Branches @commit @diff @stats
path: root / src
diff options
context:
space:
mode:
Commit1489a24d74985ef9e51fd9976fc580857062011c (patch)
Author
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-02-28 14:22:18 +0200
Committer
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-02-28 14:22:18 +0200
Filesd98fea7203c6dfabad2c692b1c9848e94fd69e76 /src
pusg-api-main.tar
pusg-api-main.tar.gz
pusg-api-main.zip
Parent5bd0777227894df6204cd8e52d19f019f296e910 (diff)
Added Event Service HEAD main
* Major Refactoring to Services (moved to correct namespace) * Fixed bug on Disconnect for HomeHub * Fixed bug with multiple notifications of end game (introduced new bug to fix later)
diff --git a/src/Web/PUSG.Web/Events/Api/IEventHandler.cs b/src/Web/PUSG.Web/Events/Api/IEventHandler.cs
new file mode 100644
index 0000000..5398f44
--- /dev/null
+++ b/src/Web/PUSG.Web/Events/Api/IEventHandler.cs
@@ -0,0 +1,11 @@
+using System.Threading.Tasks;
+
+namespace PUSG.Web.Events.Api
+{
+ public interface IEventHandler
+ {
+ Task HandleEvent(string eventName, object eventArgs);
+
+ bool IsAvailable();
+ }
+}
diff --git a/src/Web/PUSG.Web/Events/EventService.cs b/src/Web/PUSG.Web/Events/EventService.cs
new file mode 100644
index 0000000..8ab1b68
--- /dev/null
+++ b/src/Web/PUSG.Web/Events/EventService.cs
@@ -0,0 +1,41 @@
+using PUSG.Web.Events.Api;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace PUSG.Web.Events
+{
+ public class EventService : IEventService
+ {
+ private List<IEventHandler> eventHandlers;
+
+ public EventService()
+ {
+ this.eventHandlers = new List<IEventHandler>();
+ }
+
+ private void DisposeUnavailable()
+ {
+ this.eventHandlers.RemoveAll(eventHandler => !eventHandler.IsAvailable());
+ }
+
+ public IEnumerable<IEventHandler> EventHandlers => this.eventHandlers;
+
+ public async Task Publish(string eventName, params object[] args)
+ {
+ List<IEventHandler> availableHandlers = this.EventHandlers.Where(eventHandler => eventHandler.IsAvailable()).ToList();
+
+ foreach (var eventHandler in availableHandlers)
+ {
+ await eventHandler.HandleEvent(eventName, args);
+ }
+
+ this.DisposeUnavailable();
+ }
+
+ public void Subscribe(IEventHandler eventHandler)
+ {
+ this.eventHandlers.Add(eventHandler);
+ }
+ }
+}
diff --git a/src/Web/PUSG.Web/Events/IEventService.cs b/src/Web/PUSG.Web/Events/IEventService.cs
new file mode 100644
index 0000000..11e2c86
--- /dev/null
+++ b/src/Web/PUSG.Web/Events/IEventService.cs
@@ -0,0 +1,15 @@
+using PUSG.Web.Events.Api;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+
+namespace PUSG.Web.Events
+{
+ public interface IEventService
+ {
+ IEnumerable<IEventHandler> EventHandlers { get; }
+
+ void Subscribe(IEventHandler eventHandler);
+
+ Task Publish(string eventName, params object[] args);
+ }
+}
diff --git a/src/Web/PUSG.Web/Hubs/GameHub.cs b/src/Web/PUSG.Web/Hubs/GameHub.cs
index d7c587f..1e0db0a 100644
--- a/src/Web/PUSG.Web/Hubs/GameHub.cs
+++ b/src/Web/PUSG.Web/Hubs/GameHub.cs
@@ -1,7 +1,8 @@
using Microsoft.AspNetCore.SignalR;
using PUSG.Web.Models;
-using PUSG.Web.Static;
+using PUSG.Web.Services;
using System;
+using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@@ -9,11 +10,19 @@ namespace PUSG.Web.Hubs
{
public class GameHub : Hub
{
+ private const string ServerMessageUser = "PUSG";
+
+ private ISessionService sessionService;
+
private IGameService gameService;
- public GameHub(IGameService gameService)
+ private IHubContext<HomeHub> homeHubContext;
+
+ public GameHub(ISessionService sessionService, IGameService gameService, IHubContext<HomeHub> homeHubContext)
{
+ this.sessionService = sessionService;
this.gameService = gameService;
+ this.homeHubContext = homeHubContext;
}
public override async Task OnConnectedAsync()
@@ -127,11 +136,18 @@ namespace PUSG.Web.Hubs
string clientUsername = this.gameService.PlayerUsernames[id];
string otherClientUsername = this.gameService.PlayerUsernames[otherId];
+ List<string> clientSessionIds = this.sessionService.Users.Where(user => user.Value == clientUsername).Select(user => user.Key).ToList();
+
+ List<string> otherClientSessionIds = this.sessionService.Users.Where(user => user.Value == otherClientUsername).Select(user => user.Key).ToList();
+
+ List<string> totalExcludedIds = clientSessionIds.Concat(otherClientSessionIds).ToList();
+
string winningMessage =
this.gameService.PlayerColor[id] == gameSession.Winner
? $"{clientUsername} won against {otherClientUsername}!"
: $"{otherClientUsername} won against {clientUsername}!";
+ await this.homeHubContext.Clients.AllExcept(totalExcludedIds).SendAsync("ReceiveMessage", new[] { ServerMessageUser, winningMessage });
await this.Clients.Clients(id, otherId).SendAsync("GameEnd", new { Result = winningMessage });
}
else
diff --git a/src/Web/PUSG.Web/Hubs/HomeHub.cs b/src/Web/PUSG.Web/Hubs/HomeHub.cs
index 06a8d8e..c5e520f 100644
--- a/src/Web/PUSG.Web/Hubs/HomeHub.cs
+++ b/src/Web/PUSG.Web/Hubs/HomeHub.cs
@@ -1,5 +1,5 @@
using Microsoft.AspNetCore.SignalR;
-using PUSG.Web.Static;
+using PUSG.Web.Services;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -9,6 +9,8 @@ namespace PUSG.Web.Hubs
{
public class HomeHub : Hub
{
+ private const string ServerMessageUser = "PUSG";
+
private ISessionService sessionService;
public HomeHub(ISessionService sessionService)
@@ -55,10 +57,14 @@ namespace PUSG.Web.Hubs
public override async Task OnDisconnectedAsync(Exception exception)
{
- string username = this.sessionService.Users[this.Context.ConnectionId];
- this.sessionService.LiveConnections.Remove(this.Context.ConnectionId);
- this.sessionService.RemoveUser(this.Context.ConnectionId);
- await this.BroadcastToLoggedIn("OnLoggedOut", username);
+ if (this.sessionService.IsLogged(this.Context.ConnectionId))
+ {
+ string username = this.sessionService.Users[this.Context.ConnectionId];
+ this.sessionService.LiveConnections.Remove(this.Context.ConnectionId);
+ this.sessionService.RemoveUser(this.Context.ConnectionId);
+ await this.BroadcastToLoggedIn("OnLoggedOut", username);
+ }
+
await base.OnDisconnectedAsync(exception);
}
}
diff --git a/src/Web/PUSG.Web/Services/GameLoopService.cs b/src/Web/PUSG.Web/Services/GameLoopService.cs
index 9ddbb20..83029c2 100644
--- a/src/Web/PUSG.Web/Services/GameLoopService.cs
+++ b/src/Web/PUSG.Web/Services/GameLoopService.cs
@@ -3,7 +3,7 @@ using System;
using System.Threading;
using System.Threading.Tasks;
-namespace PUSG.Web.Static
+namespace PUSG.Web.Services
{
public class GameLoopService : IGameLoopService
{
diff --git a/src/Web/PUSG.Web/Services/GameService.cs b/src/Web/PUSG.Web/Services/GameService.cs
index 2108d64..ea8f902 100644
--- a/src/Web/PUSG.Web/Services/GameService.cs
+++ b/src/Web/PUSG.Web/Services/GameService.cs
@@ -7,7 +7,7 @@ using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
-namespace PUSG.Web.Static
+namespace PUSG.Web.Services
{
public class GameService : IGameService
{
diff --git a/src/Web/PUSG.Web/Services/IGameLoopService.cs b/src/Web/PUSG.Web/Services/IGameLoopService.cs
index e761684..09c1024 100644
--- a/src/Web/PUSG.Web/Services/IGameLoopService.cs
+++ b/src/Web/PUSG.Web/Services/IGameLoopService.cs
@@ -2,7 +2,7 @@
using PUSG.Web.Models;
using System;
-namespace PUSG.Web.Static
+namespace PUSG.Web.Services
{
public interface IGameLoopService : IHostedService, IDisposable
{
diff --git a/src/Web/PUSG.Web/Services/IGameService.cs b/src/Web/PUSG.Web/Services/IGameService.cs
index 52a4389..0e87af6 100644
--- a/src/Web/PUSG.Web/Services/IGameService.cs
+++ b/src/Web/PUSG.Web/Services/IGameService.cs
@@ -3,7 +3,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
-namespace PUSG.Web.Static
+namespace PUSG.Web.Services
{
public interface IGameService
{
diff --git a/src/Web/PUSG.Web/Services/ISessionService.cs b/src/Web/PUSG.Web/Services/ISessionService.cs
index 05eae53..9560fae 100644
--- a/src/Web/PUSG.Web/Services/ISessionService.cs
+++ b/src/Web/PUSG.Web/Services/ISessionService.cs
@@ -1,6 +1,6 @@
using System.Collections.Generic;
-namespace PUSG.Web.Static
+namespace PUSG.Web.Services
{
public interface ISessionService
{
diff --git a/src/Web/PUSG.Web/Services/SessionService.cs b/src/Web/PUSG.Web/Services/SessionService.cs
index 0e8e61a..d26e624 100644
--- a/src/Web/PUSG.Web/Services/SessionService.cs
+++ b/src/Web/PUSG.Web/Services/SessionService.cs
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Linq;
-namespace PUSG.Web.Static
+namespace PUSG.Web.Services
{
public class SessionService : ISessionService
{
diff --git a/src/Web/PUSG.Web/Startup.cs b/src/Web/PUSG.Web/Startup.cs
index 931903c..acb6cc9 100644
--- a/src/Web/PUSG.Web/Startup.cs
+++ b/src/Web/PUSG.Web/Startup.cs
@@ -4,7 +4,8 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using PUSG.Web.Hubs;
-using PUSG.Web.Static;
+using PUSG.Web.Services;
+using PUSG.Web.Events;
namespace PUSG.Web
{
@@ -20,6 +21,7 @@ namespace PUSG.Web
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
+ services.AddSingleton<IEventService, EventService>();
services.AddSingleton<ISessionService, SessionService>();
services.AddSingleton<IGameService, GameService>();
services.AddSingleton<GameLoopService>();