Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Web / PUSG.Web / Hubs
diff options
context:
space:
mode:
Commite8ee7b69663a95d97b226266ebe3ccc95e13c309 (patch)
Author
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-01-31 16:42:30 +0200
Committer
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-01-31 16:42:30 +0200
Files9eed7de1b817c812d61514950a9299aa2cb8d887 /src/Web/PUSG.Web/Hubs
pusg-api-e8ee7b69663a95d97b226266ebe3ccc95e13c309.tar
pusg-api-e8ee7b69663a95d97b226266ebe3ccc95e13c309.tar.gz
pusg-api-e8ee7b69663a95d97b226266ebe3ccc95e13c309.zip
Parent6fb05a2a6f94e008dc689291d0522f39f87a6622 (diff)
Major Business Logic Implementation
* Added GameHub * Added Back-End Game Loop and Game Mechanics * Added End Game and Result message
3 files changed, 159 insertions, 1 deletions
-rw-r--r--src/Web/PUSG.Web/Hubs/GameHub.cs152
-rw-r--r--src/Web/PUSG.Web/Hubs/HomeHub.cs2
-rw-r--r--src/Web/PUSG.Web/Hubs/ISocketService.cs6
diff --git a/src/Web/PUSG.Web/Hubs/GameHub.cs b/src/Web/PUSG.Web/Hubs/GameHub.cs
new file mode 100644
index 0000000..d7c587f
--- /dev/null
+++ b/src/Web/PUSG.Web/Hubs/GameHub.cs
@@ -0,0 +1,152 @@
+using Microsoft.AspNetCore.SignalR;
+using PUSG.Web.Models;
+using PUSG.Web.Static;
+using System;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace PUSG.Web.Hubs
+{
+ public class GameHub : Hub
+ {
+ private IGameService gameService;
+
+ public GameHub(IGameService gameService)
+ {
+ this.gameService = gameService;
+ }
+
+ public override async Task OnConnectedAsync()
+ {
+ this.gameService.Waiting.Add(this.Context.ConnectionId);
+
+ if(this.gameService.Waiting.Count >= 2)
+ {
+ await this.gameService.CreateGameSession(
+ this.gameService.Waiting[0],
+ this.gameService.Waiting[1]);
+
+ await this.Clients.Client(
+ this.gameService.Waiting[0]).SendAsync("StartGame", new []{ "blue" });
+ await this.Clients.Client(
+ this.gameService.Waiting[1]).SendAsync("StartGame", new[]{ "red" });
+
+ this.gameService.Waiting.RemoveAt(0);
+ this.gameService.Waiting.RemoveAt(0);
+ }
+
+ await base.OnConnectedAsync();
+ }
+
+ public async Task Login(string username)
+ {
+ this.gameService.PlayerUsernames[this.Context.ConnectionId] = username;
+ }
+
+ public override async Task OnDisconnectedAsync(Exception exception)
+ {
+ string id = this.Context.ConnectionId;
+ string otherId = this.gameService.OtherPlayer.ContainsKey(id)
+ ? this.gameService.OtherPlayer[id]
+ : null;
+
+ await this.gameService.DestroyGameSession(id);
+
+ await this.Clients.Client(otherId).SendAsync("GameEnd");
+
+ await base.OnDisconnectedAsync(exception);
+ }
+
+ public async Task Update(string command)
+ {
+ switch(command)
+ {
+ case "up":
+ {
+ this.gameService.MoveUp(this.Context.ConnectionId);
+ await this.Clients.Client(this.gameService.OtherPlayer[this.Context.ConnectionId])
+ .SendAsync("OtherUp");
+ break;
+ }
+ case "right":
+ {
+ this.gameService.MoveRight(this.Context.ConnectionId);
+ await this.Clients.Client(this.gameService.OtherPlayer[this.Context.ConnectionId])
+ .SendAsync("OtherRight");
+ break;
+ }
+ case "down":
+ {
+ this.gameService.MoveDown(this.Context.ConnectionId);
+ await this.Clients.Client(this.gameService.OtherPlayer[this.Context.ConnectionId])
+ .SendAsync("OtherDown");
+ break;
+ }
+ case "left":
+ {
+ this.gameService.MoveLeft(this.Context.ConnectionId);
+ await this.Clients.Client(this.gameService.OtherPlayer[this.Context.ConnectionId])
+ .SendAsync("OtherLeft");
+ break;
+ }
+ case "rotate-left":
+ {
+ this.gameService.RotateLeft(this.Context.ConnectionId);
+ await this.Clients.Client(this.gameService.OtherPlayer[this.Context.ConnectionId])
+ .SendAsync("OtherRotateLeft");
+ break;
+ }
+ case "rotate-right":
+ {
+ this.gameService.RotateRight(this.Context.ConnectionId);
+ await this.Clients.Client(this.gameService.OtherPlayer[this.Context.ConnectionId])
+ .SendAsync("OtherRotateRight");
+ break;
+ }
+ case "pew":
+ {
+ this.gameService.Shoot(this.Context.ConnectionId);
+ await this.Clients.Client(this.gameService.OtherPlayer[this.Context.ConnectionId])
+ .SendAsync("OtherShoot");
+ break;
+ }
+ }
+ }
+
+ public async Task GetState()
+ {
+ GameSession gameSession = this.gameService[this.Context.ConnectionId];
+
+ if (gameSession.IsDone)
+ {
+ string id = this.Context.ConnectionId;
+ string otherId = this.gameService.OtherPlayer.ContainsKey(id)
+ ? this.gameService.OtherPlayer[id]
+ : null;
+
+ string clientUsername = this.gameService.PlayerUsernames[id];
+ string otherClientUsername = this.gameService.PlayerUsernames[otherId];
+
+ string winningMessage =
+ this.gameService.PlayerColor[id] == gameSession.Winner
+ ? $"{clientUsername} won against {otherClientUsername}!"
+ : $"{otherClientUsername} won against {clientUsername}!";
+
+ await this.Clients.Clients(id, otherId).SendAsync("GameEnd", new { Result = winningMessage });
+ }
+ else
+ {
+ GameState gameState = new GameState
+ {
+ Blue = gameSession.BluePlayer,
+ Red = gameSession.RedPlayer,
+ Shells = gameSession.Shells.Select(shell => shell.Value).ToList(),
+ };
+
+ await this.Clients
+ .Client(this.Context.ConnectionId)
+ .SendAsync("GameState", gameState);
+ }
+ }
+ }
+}
diff --git a/src/Web/PUSG.Web/Hubs/HomeHub.cs b/src/Web/PUSG.Web/Hubs/HomeHub.cs
index b762635..2f9677e 100644
--- a/src/Web/PUSG.Web/Hubs/HomeHub.cs
+++ b/src/Web/PUSG.Web/Hubs/HomeHub.cs
@@ -28,7 +28,7 @@ namespace PUSG.Web.Hubs
public async Task SendMessage(string username, string messageContent)
{
- if (this.sessionService.IsLogged(username))
+ if (this.sessionService.IsLogged(username) || username == "PUSG")
{
await this.BroadcastToLoggedIn("ReceiveMessage", username, messageContent);
}
diff --git a/src/Web/PUSG.Web/Hubs/ISocketService.cs b/src/Web/PUSG.Web/Hubs/ISocketService.cs
new file mode 100644
index 0000000..4e23c62
--- /dev/null
+++ b/src/Web/PUSG.Web/Hubs/ISocketService.cs
@@ -0,0 +1,6 @@
+namespace PUSG.Web.Hubs
+{
+ internal interface ISocketService
+ {
+ }
+} \ No newline at end of file