diff options
Major Business Logic Implementation
* Added GameHub
* Added Back-End Game Loop and Game Mechanics
* Added End Game and Result message
6 files changed, 302 insertions, 0 deletions
| -rw-r--r-- | src/Web/PUSG.Web/Services/GameLoopService.cs | 38 | ||||
| -rw-r--r-- | src/Web/PUSG.Web/Services/GameService.cs | 156 | ||||
| -rw-r--r-- | src/Web/PUSG.Web/Services/IGameLoopService.cs | 11 | ||||
| -rw-r--r-- | src/Web/PUSG.Web/Services/IGameService.cs | 38 | ||||
| -rw-r--r-- | src/Web/PUSG.Web/Services/ISessionService.cs | 19 | ||||
| -rw-r--r-- | src/Web/PUSG.Web/Services/SessionService.cs | 40 |
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<string, GameSession> sessions; + + private ConcurrentDictionary<string, GameLoopService> loops; + + private ConcurrentDictionary<string, CancellationToken> loopCancellationTokens; + + private IServiceProvider serviceProvider; + + public GameService(IServiceProvider serviceProvider) + { + // Key - Socket Id (client), Value - Game Session + this.sessions = new ConcurrentDictionary<string, GameSession>(); + this.loops = new ConcurrentDictionary<string, GameLoopService>(); + this.loopCancellationTokens = new ConcurrentDictionary<string, CancellationToken>(); + this.Waiting = new List<string>(); + this.PlayerUsernames = new ConcurrentDictionary<string, string>(); + this.OtherPlayer = new ConcurrentDictionary<string, string>(); + this.PlayerColor = new ConcurrentDictionary<string, string>(); + this.serviceProvider = serviceProvider; + } + + public List<string> Waiting { get; } + + public ConcurrentDictionary<string, string> PlayerUsernames { get; } + + public ConcurrentDictionary<string, string> OtherPlayer { get; } + + public ConcurrentDictionary<string, string> 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<GameLoopService>(); + + 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<string> Waiting { get; } + + ConcurrentDictionary<string, string> PlayerUsernames { get; } + + ConcurrentDictionary<string, string> OtherPlayer { get; } + + ConcurrentDictionary<string, string> 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<string, string> Users { get; } + + List<string> LiveConnections { get; } + + KeyValuePair<string, string> 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<string, string> users; + + public SessionService() + { + this.users = new Dictionary<string, string>(); + this.LiveConnections = new List<string>(); + } + + public Dictionary<string, string> Users => this.users; + + public List<string> LiveConnections { get; } + + public void AddUser(string connectionId, string username) + { + this.users[connectionId] = username; + } + + public KeyValuePair<string, string> 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); + } + } +} |