diff --git a/README.md b/README.md index 78665a7..7fb2f01 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,9 @@ The puzzle is presented as a cube map in the same manner as in [Old Pochman](htt 1. Open ```Solution/Solution.sln``` in Visual Studio 2. Run **CubeService** to make the cube available via the API. -3. Run **CubeStatePublisher** to make the cube state available to frontends over SignalR. -4. Run front end application(s) of your choosing to view the cube and interact with it: +3. Run **CubeProxy** to make the CubeService API available to other components. +4. Run **CubeStatePublisher** to make the cube state available to frontends over SignalR. +5. Run front end application(s) of your choosing to view the cube and interact with it: - Try **CubeVisualizer** to view the cube in 3D - Try **FaceViewer** instances to view individual faces of the cube - Try **FaceViewerCLI** to view the cube within the command line and input moves diff --git a/Solution/CubeManipulator/CubeManipulatorViewModel.cs b/Solution/CubeManipulator/CubeManipulatorViewModel.cs index 615ea7c..d4b01a8 100644 --- a/Solution/CubeManipulator/CubeManipulatorViewModel.cs +++ b/Solution/CubeManipulator/CubeManipulatorViewModel.cs @@ -5,7 +5,7 @@ namespace CubeManipulator public class CubeManipulatorViewModel { //full path MoveViaApi - LibCubeIntegration.PerformMoveStrategies.MoveViaApiStrategy moveStrategy = new MoveViaApiStrategy(); + LibCubeIntegration.PerformMoveStrategies.MoveViaApiStrategy moveStrategy = new MoveViaApiStrategy("CubeProxy"); private RelayCommand moveCommand; public RelayCommand MoveCommand //Get command result diff --git a/Solution/CubeProxy/Controllers/CubeController.cs b/Solution/CubeProxy/Controllers/CubeController.cs new file mode 100644 index 0000000..4f752c0 --- /dev/null +++ b/Solution/CubeProxy/Controllers/CubeController.cs @@ -0,0 +1,107 @@ +using LibCubeIntegration.Services; +using LibNetCube; +using Microsoft.AspNetCore.Mvc; + +namespace CubeProxy.Controllers +{ + [ApiController] + [Route("api/[controller]")] + + public class CubeController : Controller + { + readonly CubeServiceFacade _cubeService = new CubeServiceFacade("CubeService"); + + [HttpPost("[action]")] + public IActionResult Reset() + { + return Ok(); + } + + [HttpPost("[action]")] + public IActionResult ApplyShuffle([FromQuery] string? shuffle = null) + { + return Ok(); + } + + [HttpPost("[action]")] + public async Task PerformMove([FromQuery] string? move = null) + { + if (move is string) + { + try + { + //attempt to parse move as Enum + CubeMove parsedMove = MoveParser.ParseMove(move)!; + await _cubeService.PerformMoveAsync(move); + + return Ok(); + } + catch + { + //Not a valid move + return BadRequest(); + } + } + + return BadRequest(); + } + + [HttpGet] + [Route("")] //follows base route of api/[controller] + [Route("[action]")] + public async Task State() + { + CubeState? result = await _cubeService.GetStateAsync(); + + if (result is CubeState state) + { + Dictionary faces = CreateDictionaryOfFaces(state); + return Ok(faces); + } + + return NotFound(); + } + + + [HttpGet("[action]")] + public async Task Face([FromQuery] string? face = null) + { + CubeState? result = await _cubeService.GetStateAsync(); + + try + { + if (result is CubeState state) + { + Dictionary faces = CreateDictionaryOfFaces(state); + if (face is string) + { + return Ok(faces[face]); + } + } + else + { + return NotFound(); + } + + return BadRequest(); + } + catch + { + return BadRequest(); + } + } + + private Dictionary CreateDictionaryOfFaces(CubeState state) + { + Dictionary faces = new Dictionary(); + foreach (CubeFace face in CubeState.GetFaceNames()) + { + int[,] ints = state.ReadFace(face); + int[] formattedInts = ints.Cast().ToArray(); + faces.Add(face.ToString(), formattedInts); + } + + return faces; + } + } +} diff --git a/Solution/CubeProxy/CubeProxy.csproj b/Solution/CubeProxy/CubeProxy.csproj new file mode 100644 index 0000000..dac6d42 --- /dev/null +++ b/Solution/CubeProxy/CubeProxy.csproj @@ -0,0 +1,18 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + diff --git a/Solution/CubeProxy/CubeProxy.http b/Solution/CubeProxy/CubeProxy.http new file mode 100644 index 0000000..a99eb0a --- /dev/null +++ b/Solution/CubeProxy/CubeProxy.http @@ -0,0 +1,6 @@ +@CubeProxy_HostAddress = http://localhost:5290 + +GET {{CubeProxy_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/Solution/CubeProxy/Program.cs b/Solution/CubeProxy/Program.cs new file mode 100644 index 0000000..baff427 --- /dev/null +++ b/Solution/CubeProxy/Program.cs @@ -0,0 +1,34 @@ + +namespace CubeProxy +{ + public class Program + { + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + // Add services to the container. + + builder.Services.AddControllers(); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) + { + app.UseSwagger(); + app.UseSwaggerUI(); + } + + app.UseAuthorization(); + + + app.MapControllers(); + + app.Run(); + } + } +} diff --git a/Solution/CubeProxy/Properties/launchSettings.json b/Solution/CubeProxy/Properties/launchSettings.json new file mode 100644 index 0000000..ecd0366 --- /dev/null +++ b/Solution/CubeProxy/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:57278", + "sslPort": 0 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5290", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Solution/CubeProxy/appsettings.Development.json b/Solution/CubeProxy/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Solution/CubeProxy/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Solution/CubeProxy/appsettings.json b/Solution/CubeProxy/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/Solution/CubeProxy/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Solution/CubeStatePublisher/PublishCubeService.cs b/Solution/CubeStatePublisher/PublishCubeService.cs index 4bf9df5..efaa8ca 100644 --- a/Solution/CubeStatePublisher/PublishCubeService.cs +++ b/Solution/CubeStatePublisher/PublishCubeService.cs @@ -20,7 +20,7 @@ public class PublishCubeService : BackgroundService public PublishCubeService(IHubContext hubContext) { _hubContext = hubContext; - _getCubeStrategy = new GetCubeViaApiStrategy("CubeService"); + _getCubeStrategy = new GetCubeViaApiStrategy("CubeProxy"); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) diff --git a/Solution/LibCubeIntegration/NetworkingConfiguration.cs b/Solution/LibCubeIntegration/NetworkingConfiguration.cs index 366d695..41b9fe0 100644 --- a/Solution/LibCubeIntegration/NetworkingConfiguration.cs +++ b/Solution/LibCubeIntegration/NetworkingConfiguration.cs @@ -44,6 +44,8 @@ public static int GetPortForServer(string project) { case "DummyService": return 5000; + case "CubeProxy": + return 5290; case "CubeService": return 5295; case "CubeStatePublisher": diff --git a/Solution/Solution.sln b/Solution/Solution.sln index ecb8e4d..cea53e2 100644 --- a/Solution/Solution.sln +++ b/Solution/Solution.sln @@ -29,6 +29,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CubeVisualizer", "CubeVisua EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CubeStatePublisher", "CubeStatePublisher\CubeStatePublisher.csproj", "{7D02ECFC-C216-4E83-928A-08C7CBE652DA}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CubeProxy", "CubeProxy\CubeProxy.csproj", "{BBC773F5-B0E2-4337-B942-24EDEF6807C7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -79,6 +81,10 @@ Global {7D02ECFC-C216-4E83-928A-08C7CBE652DA}.Debug|Any CPU.Build.0 = Debug|Any CPU {7D02ECFC-C216-4E83-928A-08C7CBE652DA}.Release|Any CPU.ActiveCfg = Release|Any CPU {7D02ECFC-C216-4E83-928A-08C7CBE652DA}.Release|Any CPU.Build.0 = Release|Any CPU + {BBC773F5-B0E2-4337-B942-24EDEF6807C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BBC773F5-B0E2-4337-B942-24EDEF6807C7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BBC773F5-B0E2-4337-B942-24EDEF6807C7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BBC773F5-B0E2-4337-B942-24EDEF6807C7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE