Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Web
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-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
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<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> 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<string, Shell> Shells { get; set; } = new ConcurrentDictionary<string, Shell>();
+
+ 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<Shell> shellsToDelete = new Stack<Shell>();
+
+ 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<Shell> 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<string, string> directionsRight = new Dictionary<string, string>
+ {
+ ["up"] = "right",
+ ["right"] = "down",
+ ["down"] = "left",
+ ["left"] = "up"
+ };
+
+ private Dictionary<string, string> directionsLeft = new Dictionary<string, string>
+ {
+ ["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<Startup>();
+ var port = Environment.GetEnvironmentVariable("PORT");
+
+ webBuilder.UseStartup<Startup>().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<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/Static/ISessionService.cs b/src/Web/PUSG.Web/Services/ISessionService.cs
index 05eae53..05eae53 100644
--- a/src/Web/PUSG.Web/Static/ISessionService.cs
+++ b/src/Web/PUSG.Web/Services/ISessionService.cs
diff --git a/src/Web/PUSG.Web/Static/SessionService.cs b/src/Web/PUSG.Web/Services/SessionService.cs
index 0e8e61a..0e8e61a 100644
--- a/src/Web/PUSG.Web/Static/SessionService.cs
+++ b/src/Web/PUSG.Web/Services/SessionService.cs
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<ISessionService, SessionService>();
+ services.AddSingleton<IGameService, GameService>();
+ services.AddSingleton<GameLoopService>();
+ services.AddHostedService<GameLoopService>(provider => provider.GetService<GameLoopService>());
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<HomeHub>("/home");
+ endpoints.MapHub<GameHub>("/game");
endpoints.MapControllers();
});
}