From e8ee7b69663a95d97b226266ebe3ccc95e13c309 Mon Sep 17 00:00:00 2001 From: "ivaylo.zhelev" Date: Sun, 31 Jan 2021 16:42:30 +0200 Subject: Major Business Logic Implementation * Added GameHub * Added Back-End Game Loop and Game Mechanics * Added End Game and Result message --- src/Web/PUSG.Web/Controllers/HomeController.cs | 2 +- src/Web/PUSG.Web/Dockerfile | 22 ++++ src/Web/PUSG.Web/Extensions/UtilityExtensions.cs | 13 ++ src/Web/PUSG.Web/Hubs/GameHub.cs | 152 ++++++++++++++++++++++ src/Web/PUSG.Web/Hubs/HomeHub.cs | 2 +- src/Web/PUSG.Web/Hubs/ISocketService.cs | 6 + src/Web/PUSG.Web/Models/BaseGameObject.cs | 9 ++ src/Web/PUSG.Web/Models/CollisionEngine.cs | 16 +++ src/Web/PUSG.Web/Models/GameSession.cs | 99 ++++++++++++++ src/Web/PUSG.Web/Models/GameState.cs | 13 ++ src/Web/PUSG.Web/Models/Player.cs | 73 +++++++++++ src/Web/PUSG.Web/Models/Shell.cs | 40 ++++++ src/Web/PUSG.Web/Program.cs | 4 +- src/Web/PUSG.Web/Properties/launchSettings.json | 12 +- src/Web/PUSG.Web/Services/GameLoopService.cs | 38 ++++++ src/Web/PUSG.Web/Services/GameService.cs | 156 +++++++++++++++++++++++ src/Web/PUSG.Web/Services/IGameLoopService.cs | 11 ++ src/Web/PUSG.Web/Services/IGameService.cs | 38 ++++++ src/Web/PUSG.Web/Services/ISessionService.cs | 19 +++ src/Web/PUSG.Web/Services/SessionService.cs | 40 ++++++ src/Web/PUSG.Web/Startup.cs | 6 +- src/Web/PUSG.Web/Static/ISessionService.cs | 19 --- src/Web/PUSG.Web/Static/SessionService.cs | 40 ------ 23 files changed, 761 insertions(+), 69 deletions(-) create mode 100644 src/Web/PUSG.Web/Dockerfile create mode 100644 src/Web/PUSG.Web/Extensions/UtilityExtensions.cs create mode 100644 src/Web/PUSG.Web/Hubs/GameHub.cs create mode 100644 src/Web/PUSG.Web/Hubs/ISocketService.cs create mode 100644 src/Web/PUSG.Web/Models/BaseGameObject.cs create mode 100644 src/Web/PUSG.Web/Models/CollisionEngine.cs create mode 100644 src/Web/PUSG.Web/Models/GameSession.cs create mode 100644 src/Web/PUSG.Web/Models/GameState.cs create mode 100644 src/Web/PUSG.Web/Models/Player.cs create mode 100644 src/Web/PUSG.Web/Models/Shell.cs create mode 100644 src/Web/PUSG.Web/Services/GameLoopService.cs create mode 100644 src/Web/PUSG.Web/Services/GameService.cs create mode 100644 src/Web/PUSG.Web/Services/IGameLoopService.cs create mode 100644 src/Web/PUSG.Web/Services/IGameService.cs create mode 100644 src/Web/PUSG.Web/Services/ISessionService.cs create mode 100644 src/Web/PUSG.Web/Services/SessionService.cs delete mode 100644 src/Web/PUSG.Web/Static/ISessionService.cs delete mode 100644 src/Web/PUSG.Web/Static/SessionService.cs (limited to 'src/Web') diff --git a/src/Web/PUSG.Web/Controllers/HomeController.cs b/src/Web/PUSG.Web/Controllers/HomeController.cs index 60cfe2f..724c88b 100644 --- a/src/Web/PUSG.Web/Controllers/HomeController.cs +++ b/src/Web/PUSG.Web/Controllers/HomeController.cs @@ -6,7 +6,7 @@ namespace PUSG.Web.Controllers [ApiController] public class HomeController : ControllerBase { - [Route("/")] + [Route("/index")] public IActionResult Index() { return Ok(); diff --git a/src/Web/PUSG.Web/Dockerfile b/src/Web/PUSG.Web/Dockerfile new file mode 100644 index 0000000..4c19158 --- /dev/null +++ b/src/Web/PUSG.Web/Dockerfile @@ -0,0 +1,22 @@ +FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env +WORKDIR /app + +# Copy csproj and restore as distinct layers +COPY *.csproj ./ +RUN dotnet restore + +# Copy everything else and build +COPY . . +RUN dotnet publish -c Release -o out + +# Build runtime image +FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 +WORKDIR /app +COPY --from=build-env /app/out . + +# Run the app on container startup +# Use your project name for the second parameter +# e.g. MyProject.dll +ENTRYPOINT [ "dotnet", "PUSG.Web.dll" ] + +CMD ASPNETCORE_URLS=http://*:$PORT dotnet PUSG.Web.dll \ No newline at end of file diff --git a/src/Web/PUSG.Web/Extensions/UtilityExtensions.cs b/src/Web/PUSG.Web/Extensions/UtilityExtensions.cs new file mode 100644 index 0000000..fc96486 --- /dev/null +++ b/src/Web/PUSG.Web/Extensions/UtilityExtensions.cs @@ -0,0 +1,13 @@ +using System.Collections.Concurrent; + +namespace PUSG.Web.Extensions +{ + public static class UtilityExtensions + { + public static bool Delete(this ConcurrentDictionary dictionary, TKey key) + { + TValue result = default(TValue); + return dictionary.TryRemove(key, out result); + } + } +} 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 diff --git a/src/Web/PUSG.Web/Models/BaseGameObject.cs b/src/Web/PUSG.Web/Models/BaseGameObject.cs new file mode 100644 index 0000000..2a76f39 --- /dev/null +++ b/src/Web/PUSG.Web/Models/BaseGameObject.cs @@ -0,0 +1,9 @@ +namespace PUSG.Web.Models +{ + public abstract class BaseGameObject + { + public double X { get; set; } + + public double Y { get; set; } + } +} diff --git a/src/Web/PUSG.Web/Models/CollisionEngine.cs b/src/Web/PUSG.Web/Models/CollisionEngine.cs new file mode 100644 index 0000000..4ae2e13 --- /dev/null +++ b/src/Web/PUSG.Web/Models/CollisionEngine.cs @@ -0,0 +1,16 @@ +using System.Drawing; + +namespace PUSG.Web.Models +{ + public class CollisionEngine + { + public static bool CollidesTankWithShell(BaseGameObject tank, BaseGameObject shell) + { + Rectangle targetRectangle = new Rectangle((int)tank.X, (int)tank.Y, 50, 40); + + Rectangle colliderRectangle = new Rectangle((int)shell.X, (int)shell.Y, 25, 25); + + return colliderRectangle.IntersectsWith(targetRectangle); + } + } +} diff --git a/src/Web/PUSG.Web/Models/GameSession.cs b/src/Web/PUSG.Web/Models/GameSession.cs new file mode 100644 index 0000000..21c65d0 --- /dev/null +++ b/src/Web/PUSG.Web/Models/GameSession.cs @@ -0,0 +1,99 @@ +using PUSG.Web.Extensions; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; + +namespace PUSG.Web.Models +{ + public class GameSession + { + public Player BluePlayer { get; set; } + + public Player RedPlayer { get; set; } + + public bool IsDone { get; private set; } = false; + + public string Winner { get; private set; } = null; + + public ConcurrentDictionary Shells { get; set; } = new ConcurrentDictionary(); + + public Player this[string color] + { + get + { + if (color == "blue") return this.BluePlayer; + if (color == "red") return this.RedPlayer; + return null; + } + } + + public void Update() + { + lock (this.Shells) + { + Stack shellsToDelete = new Stack(); + + foreach (var shell in this.Shells) + { + shell.Value.Update(); + + if(shell.Value.IsDestroyable) + { + shellsToDelete.Push(shell.Value); + } + } + + foreach (var item in shellsToDelete) + { + this.Shells.Delete(item.Id); + } + + foreach (var shell in this.Shells.Values.Where(shell => shell.Color == "red")) + { + if (CollisionEngine.CollidesTankWithShell(this.BluePlayer, shell)) + { + this.IsDone = true; // RED WINS + this.Winner = "red"; + } + } + + foreach (var shell in this.Shells.Values.Where(shell => shell.Color == "blue")) + { + if (CollisionEngine.CollidesTankWithShell(this.RedPlayer, shell)) + { + this.IsDone = true; // BLUE WINS + this.Winner = "blue"; + } + } + } + } + + public void Shoot(string playerColor) + { + if(playerColor == "blue") + { + Shell shell = new Shell + { + Direction = this[playerColor].Direction, + Color = playerColor, + X = this[playerColor].X, + Y = this[playerColor].Y + }; + + this.Shells[shell.Id] = shell; + } + else if(playerColor == "red") + { + Shell shell = new Shell + { + Direction = this[playerColor].Direction, + Color = playerColor, + X = this[playerColor].X, + Y = this[playerColor].Y + }; + + this.Shells[shell.Id] = shell; + } + } + } +} diff --git a/src/Web/PUSG.Web/Models/GameState.cs b/src/Web/PUSG.Web/Models/GameState.cs new file mode 100644 index 0000000..aeea038 --- /dev/null +++ b/src/Web/PUSG.Web/Models/GameState.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace PUSG.Web.Models +{ + public class GameState + { + public Player Blue { get; set; } + + public Player Red { get; set; } + + public List Shells { get; set; } + } +} diff --git a/src/Web/PUSG.Web/Models/Player.cs b/src/Web/PUSG.Web/Models/Player.cs new file mode 100644 index 0000000..cb0cc5a --- /dev/null +++ b/src/Web/PUSG.Web/Models/Player.cs @@ -0,0 +1,73 @@ +using System.Collections.Generic; + +namespace PUSG.Web.Models +{ + public class Player : BaseGameObject + { + private Dictionary directionsRight = new Dictionary + { + ["up"] = "right", + ["right"] = "down", + ["down"] = "left", + ["left"] = "up" + }; + + private Dictionary directionsLeft = new Dictionary + { + ["up"] = "left", + ["left"] = "down", + ["down"] = "right", + ["right"] = "up" + }; + + public string Color { get; set; } + + public string Direction { get; set; } + + public string Type { get; set; } + + public bool IsDead { get; set; } = false; + + public void MoveUp() + { + if (this.Y - 20 >= 0) + { + this.Y -= 20; + } + } + + public void MoveRight() + { + if (this.X + 20 <= 1366) + { + this.X += 20; + } + } + + public void MoveDown() + { + if (this.Y + 20 <= 1366) + { + this.Y += 20; + } + } + + public void MoveLeft() + { + if (this.X - 20 >= 0) + { + this.X -= 20; + } + } + + public void RotateLeft() + { + this.Direction = this.directionsLeft[this.Direction]; + } + + public void RotateRight() + { + this.Direction = this.directionsRight[this.Direction]; + } + } +} diff --git a/src/Web/PUSG.Web/Models/Shell.cs b/src/Web/PUSG.Web/Models/Shell.cs new file mode 100644 index 0000000..f005423 --- /dev/null +++ b/src/Web/PUSG.Web/Models/Shell.cs @@ -0,0 +1,40 @@ +using System; + +namespace PUSG.Web.Models +{ + public class Shell : BaseGameObject + { + public string Id { get; set; } = Guid.NewGuid().ToString(); + + public string Direction { get; set; } + + public string Color { get; set; } + + public bool IsDestroyable { get; set; } = false; + + public void Update() + { + if(this.Direction == "up") + { + this.Y -= 7; + } + else if(this.Direction == "right") + { + this.X += 7; + } + else if(this.Direction == "down") + { + this.Y += 7; + } + else if(this.Direction == "left") + { + this.X -= 7; + } + + if(this.Y < 0 || this.Y > 1366 || this.X < 0 || this.X > 1366) + { + this.IsDestroyable = true; + } + } + } +} diff --git a/src/Web/PUSG.Web/Program.cs b/src/Web/PUSG.Web/Program.cs index 0bf82e6..cd804d3 100644 --- a/src/Web/PUSG.Web/Program.cs +++ b/src/Web/PUSG.Web/Program.cs @@ -20,7 +20,9 @@ namespace PUSG.Web Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { - webBuilder.UseStartup(); + var port = Environment.GetEnvironmentVariable("PORT"); + + webBuilder.UseStartup().UseUrls("http://*:" + port); }); } } diff --git a/src/Web/PUSG.Web/Properties/launchSettings.json b/src/Web/PUSG.Web/Properties/launchSettings.json index ea77118..5e6cbb1 100644 --- a/src/Web/PUSG.Web/Properties/launchSettings.json +++ b/src/Web/PUSG.Web/Properties/launchSettings.json @@ -1,13 +1,13 @@ -{ - "$schema": "http://json.schemastore.org/launchsettings.json", +{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:60787", + "applicationUrl": "http://localhost:3000", "sslPort": 44385 } }, + "$schema": "http://json.schemastore.org/launchsettings.json", "profiles": { "IIS Express": { "commandName": "IISExpress", @@ -21,10 +21,10 @@ "commandName": "Project", "launchBrowser": true, "launchUrl": "weatherforecast", - "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "applicationUrl": "https://localhost:5001;http://localhost:5000" } } -} +} \ No newline at end of file diff --git a/src/Web/PUSG.Web/Services/GameLoopService.cs b/src/Web/PUSG.Web/Services/GameLoopService.cs new file mode 100644 index 0000000..9ddbb20 --- /dev/null +++ b/src/Web/PUSG.Web/Services/GameLoopService.cs @@ -0,0 +1,38 @@ +using PUSG.Web.Models; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace PUSG.Web.Static +{ + public class GameLoopService : IGameLoopService + { + private Timer timer; + + public GameSession GameSession { get; set; } + + private void Step(object state) + { + this.GameSession?.Update(); + } + + public Task StartAsync(CancellationToken cancellationToken) + { + this.timer = new Timer(Step, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(17)); + + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) + { + this.timer?.Change(Timeout.Infinite, 0); + + return Task.CompletedTask; + } + + public void Dispose() + { + this.timer?.Dispose(); + } + } +} diff --git a/src/Web/PUSG.Web/Services/GameService.cs b/src/Web/PUSG.Web/Services/GameService.cs new file mode 100644 index 0000000..2108d64 --- /dev/null +++ b/src/Web/PUSG.Web/Services/GameService.cs @@ -0,0 +1,156 @@ +using Microsoft.Extensions.DependencyInjection; +using PUSG.Web.Extensions; +using PUSG.Web.Models; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace PUSG.Web.Static +{ + public class GameService : IGameService + { + private ConcurrentDictionary sessions; + + private ConcurrentDictionary loops; + + private ConcurrentDictionary loopCancellationTokens; + + private IServiceProvider serviceProvider; + + public GameService(IServiceProvider serviceProvider) + { + // Key - Socket Id (client), Value - Game Session + this.sessions = new ConcurrentDictionary(); + this.loops = new ConcurrentDictionary(); + this.loopCancellationTokens = new ConcurrentDictionary(); + this.Waiting = new List(); + this.PlayerUsernames = new ConcurrentDictionary(); + this.OtherPlayer = new ConcurrentDictionary(); + this.PlayerColor = new ConcurrentDictionary(); + this.serviceProvider = serviceProvider; + } + + public List Waiting { get; } + + public ConcurrentDictionary PlayerUsernames { get; } + + public ConcurrentDictionary OtherPlayer { get; } + + public ConcurrentDictionary PlayerColor { get; } + + public async Task CreateGameSession(string blueClientId, string redClientId) + { + GameSession gameSession = new GameSession(); + + gameSession.BluePlayer = new Player + { + X = 50, + Y = 384, + Direction = "right", + Color = "blue", + Type = "basic" + }; + + gameSession.RedPlayer = new Player + { + X = 1266, + Y = 384, + Direction = "left", + Color = "red", + Type = "basic" + }; + + this.sessions[blueClientId] = gameSession; + this.sessions[redClientId] = gameSession; + this.OtherPlayer[blueClientId] = redClientId; + this.OtherPlayer[redClientId] = blueClientId; + this.PlayerColor[blueClientId] = "blue"; + this.PlayerColor[redClientId] = "red"; + + CancellationTokenSource tokenSource = new CancellationTokenSource(); + CancellationToken token = tokenSource.Token; + GameLoopService gameLoop = this.serviceProvider.CreateScope().ServiceProvider.GetRequiredService(); + + this.loops[blueClientId] = gameLoop; + this.loops[redClientId] = gameLoop; + this.loopCancellationTokens[blueClientId] = token; + this.loopCancellationTokens[redClientId] = token; + + gameLoop.GameSession = gameSession; + await gameLoop.StartAsync(token); + } + + public async Task DestroyGameSession(string clientId) + { + string otherClientId = this.OtherPlayer.ContainsKey(clientId) ? this.OtherPlayer[clientId] : null; + + await this.loops[clientId].StopAsync(this.loopCancellationTokens[clientId]); + this.loops[clientId].Dispose(); + + this.loops.Delete(clientId); + this.sessions.Delete(clientId); + this.PlayerUsernames.Delete(clientId); + this.PlayerColor.Delete(clientId); + this.loopCancellationTokens.Delete(clientId); + this.OtherPlayer.Delete(clientId); + + if (otherClientId != null) + { + this.loops.Delete(otherClientId); + this.sessions.Delete(otherClientId); + this.PlayerUsernames.Delete(otherClientId); + this.PlayerColor.Delete(otherClientId); + this.loopCancellationTokens.Delete(otherClientId); + this.OtherPlayer.Delete(otherClientId); + } + } + + public GameSession this[string clientId] + { + get + { + return this.sessions.ContainsKey(clientId) ? this.sessions[clientId] : null; + } + set + { + this.sessions[clientId] = value; + } + } + + public void Shoot(string clientId) + { + this.sessions[clientId].Shoot(this.PlayerColor[clientId]); + } + public void MoveUp(string clientId) + { + this.sessions[clientId][this.PlayerColor[clientId]].MoveUp(); + } + + public void MoveRight(string clientId) + { + this.sessions[clientId][this.PlayerColor[clientId]].MoveRight(); + } + + public void MoveDown(string clientId) + { + this.sessions[clientId][this.PlayerColor[clientId]].MoveDown(); + } + + public void MoveLeft(string clientId) + { + this.sessions[clientId][this.PlayerColor[clientId]].MoveLeft(); + } + + public void RotateLeft(string clientId) + { + this.sessions[clientId][this.PlayerColor[clientId]].RotateLeft(); + } + + public void RotateRight(string clientId) + { + this.sessions[clientId][this.PlayerColor[clientId]].RotateRight(); + } + } +} diff --git a/src/Web/PUSG.Web/Services/IGameLoopService.cs b/src/Web/PUSG.Web/Services/IGameLoopService.cs new file mode 100644 index 0000000..e761684 --- /dev/null +++ b/src/Web/PUSG.Web/Services/IGameLoopService.cs @@ -0,0 +1,11 @@ +using Microsoft.Extensions.Hosting; +using PUSG.Web.Models; +using System; + +namespace PUSG.Web.Static +{ + public interface IGameLoopService : IHostedService, IDisposable + { + GameSession GameSession { get; set; } + } +} diff --git a/src/Web/PUSG.Web/Services/IGameService.cs b/src/Web/PUSG.Web/Services/IGameService.cs new file mode 100644 index 0000000..52a4389 --- /dev/null +++ b/src/Web/PUSG.Web/Services/IGameService.cs @@ -0,0 +1,38 @@ +using PUSG.Web.Models; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace PUSG.Web.Static +{ + public interface IGameService + { + Task CreateGameSession(string blueClientId, string redClientId); + + Task DestroyGameSession(string clientId); + + GameSession this[string clientId] { get; set; } + + List Waiting { get; } + + ConcurrentDictionary PlayerUsernames { get; } + + ConcurrentDictionary OtherPlayer { get; } + + ConcurrentDictionary PlayerColor { get; } + + void Shoot(string clientId); + + void MoveUp(string clientId); + + void MoveRight(string clientId); + + void MoveDown(string clientId); + + void MoveLeft(string clientId); + + void RotateLeft(string clientId); + + void RotateRight(string clientId); + } +} diff --git a/src/Web/PUSG.Web/Services/ISessionService.cs b/src/Web/PUSG.Web/Services/ISessionService.cs new file mode 100644 index 0000000..05eae53 --- /dev/null +++ b/src/Web/PUSG.Web/Services/ISessionService.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; + +namespace PUSG.Web.Static +{ + public interface ISessionService + { + bool IsLogged(string username); + + void AddUser(string connectionId, string username); + + void RemoveUser(string connectionId); + + Dictionary Users { get; } + + List LiveConnections { get; } + + KeyValuePair GetUser(string connectionId); + } +} diff --git a/src/Web/PUSG.Web/Services/SessionService.cs b/src/Web/PUSG.Web/Services/SessionService.cs new file mode 100644 index 0000000..0e8e61a --- /dev/null +++ b/src/Web/PUSG.Web/Services/SessionService.cs @@ -0,0 +1,40 @@ +using System.Collections.Generic; +using System.Linq; + +namespace PUSG.Web.Static +{ + public class SessionService : ISessionService + { + private Dictionary users; + + public SessionService() + { + this.users = new Dictionary(); + this.LiveConnections = new List(); + } + + public Dictionary Users => this.users; + + public List LiveConnections { get; } + + public void AddUser(string connectionId, string username) + { + this.users[connectionId] = username; + } + + public KeyValuePair GetUser(string connectionId) + { + return this.users.SingleOrDefault(user => user.Key == connectionId); + } + + public bool IsLogged(string credential) + { + return this.users.ContainsKey(credential) || this.users.ContainsValue(credential); + } + + public void RemoveUser(string connectionId) + { + this.users.Remove(connectionId); + } + } +} diff --git a/src/Web/PUSG.Web/Startup.cs b/src/Web/PUSG.Web/Startup.cs index 82ae7e4..931903c 100644 --- a/src/Web/PUSG.Web/Startup.cs +++ b/src/Web/PUSG.Web/Startup.cs @@ -21,6 +21,9 @@ namespace PUSG.Web public void ConfigureServices(IServiceCollection services) { services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddHostedService(provider => provider.GetService()); services.AddControllers(); services.AddSignalR(); @@ -30,7 +33,7 @@ namespace PUSG.Web options.AddPolicy("CustomPolicy", builder => { builder - .WithOrigins("http://localhost:8001") + .WithOrigins("https://it-careers.github.io", "http://localhost:8001") .AllowCredentials() .AllowAnyHeader() .AllowAnyMethod(); @@ -55,6 +58,7 @@ namespace PUSG.Web app.UseEndpoints(endpoints => { endpoints.MapHub("/home"); + endpoints.MapHub("/game"); endpoints.MapControllers(); }); } diff --git a/src/Web/PUSG.Web/Static/ISessionService.cs b/src/Web/PUSG.Web/Static/ISessionService.cs deleted file mode 100644 index 05eae53..0000000 --- a/src/Web/PUSG.Web/Static/ISessionService.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections.Generic; - -namespace PUSG.Web.Static -{ - public interface ISessionService - { - bool IsLogged(string username); - - void AddUser(string connectionId, string username); - - void RemoveUser(string connectionId); - - Dictionary Users { get; } - - List LiveConnections { get; } - - KeyValuePair GetUser(string connectionId); - } -} diff --git a/src/Web/PUSG.Web/Static/SessionService.cs b/src/Web/PUSG.Web/Static/SessionService.cs deleted file mode 100644 index 0e8e61a..0000000 --- a/src/Web/PUSG.Web/Static/SessionService.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - -namespace PUSG.Web.Static -{ - public class SessionService : ISessionService - { - private Dictionary users; - - public SessionService() - { - this.users = new Dictionary(); - this.LiveConnections = new List(); - } - - public Dictionary Users => this.users; - - public List LiveConnections { get; } - - public void AddUser(string connectionId, string username) - { - this.users[connectionId] = username; - } - - public KeyValuePair GetUser(string connectionId) - { - return this.users.SingleOrDefault(user => user.Key == connectionId); - } - - public bool IsLogged(string credential) - { - return this.users.ContainsKey(credential) || this.users.ContainsValue(credential); - } - - public void RemoveUser(string connectionId) - { - this.users.Remove(connectionId); - } - } -} -- cgit v1.3.1-26-gb76a