From 1ef728dc50b8456cce7c0bf39b2b70997a8dfd55 Mon Sep 17 00:00:00 2001 From: Shuozhe Date: Sat, 26 Jun 2021 00:25:59 +0200 Subject: [PATCH] got c# backend working (more or less..) --- Controllers/TicTacTocController.cs | 20 ++--- Data/DataAccessContext.TicTacToc.cs | 2 +- Data/ITicTacToctRepository.cs | 2 +- Logic/TicTacTocBoard.cs | 92 +++++++++++++++++++++++ Logic/TicTacTocManager.cs | 31 +++++++- Models/TicTacTocBoard.cs | 13 ---- Models/TicTacTocMove.cs | 13 ---- Models/TicTacTocRequest.cs | 13 ++++ Models/TicTacTocResponse.cs | 11 +++ Program.cs | 1 - Startup.cs | 111 +++++++++++++++------------- 11 files changed, 216 insertions(+), 93 deletions(-) create mode 100644 Logic/TicTacTocBoard.cs delete mode 100644 Models/TicTacTocBoard.cs delete mode 100644 Models/TicTacTocMove.cs create mode 100644 Models/TicTacTocRequest.cs create mode 100644 Models/TicTacTocResponse.cs diff --git a/Controllers/TicTacTocController.cs b/Controllers/TicTacTocController.cs index bbb1e95..3652d04 100644 --- a/Controllers/TicTacTocController.cs +++ b/Controllers/TicTacTocController.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using ProjectGrid.Data; +using ProjectGrid.Models; namespace ProjectGrid.Controllers { @@ -16,30 +17,31 @@ namespace ProjectGrid.Controllers private readonly ILogger _logger; - //static readonly Models.IUserRepository repository = new Models.UserRepository(); - private readonly ITicTacToctRepository repository; + private readonly TicTacTocManager _manager; - public TicTacTocController(ILogger logger, DataAccessContext context) + public TicTacTocController(ILogger logger, TicTacTocManager manager) { _logger = logger; - repository = context; + _manager = manager; } [HttpGet] [Route("api/ttt/GetBoard")] - public Models.TicTacTocBoard GetBoard() + public TicTacTocResponse GetBoard() { - return repository.GetBoard(); + _logger.LogTrace("GetBoard called."); + return _manager.GetResponse(); } [HttpPost] [Route("api/ttt/SetPiece")] [Consumes("application/json")] - public Models.UserModel PostMove(Models.UserModel item) + public TicTacTocResponse PostMove(TicTacTocRequest move) { - return repository.Add(item); + _logger.LogTrace($"PostMove called. {move.DebugString()}"); + _manager.NextMove(move); + return _manager.GetResponse(move.Player); } - } } \ No newline at end of file diff --git a/Data/DataAccessContext.TicTacToc.cs b/Data/DataAccessContext.TicTacToc.cs index 2955db6..6afc569 100644 --- a/Data/DataAccessContext.TicTacToc.cs +++ b/Data/DataAccessContext.TicTacToc.cs @@ -10,7 +10,7 @@ namespace ProjectGrid.Data public partial class DataAccessContext : ITicTacToctRepository { - public bool AddPiece(TicTacTocMove user) + public bool AddPiece(TicTacTocRequest request) { throw new System.NotImplementedException(); } diff --git a/Data/ITicTacToctRepository.cs b/Data/ITicTacToctRepository.cs index b358356..86a584a 100644 --- a/Data/ITicTacToctRepository.cs +++ b/Data/ITicTacToctRepository.cs @@ -6,6 +6,6 @@ namespace ProjectGrid.Data { TicTacTocBoard GetBoard(); - bool AddPiece(TicTacTocMove user); + bool AddPiece(TicTacTocRequest user); } } \ No newline at end of file diff --git a/Logic/TicTacTocBoard.cs b/Logic/TicTacTocBoard.cs new file mode 100644 index 0000000..bb04ce4 --- /dev/null +++ b/Logic/TicTacTocBoard.cs @@ -0,0 +1,92 @@ +using System.Collections.Generic; +using System.Diagnostics; + +namespace ProjectGrid.Models +{ + /// + /// Holds data for a TicTacTocBoard + /// 0: empty field, > 0 players + /// + public class TicTacTocBoard + { + private List _field; + public List Field => _field; + + private int _lastSetValue; + public int LastSetValue => _lastSetValue; + + private int PosToIdx(int x, int y) + { + Debug.Assert(x >= 0 && x <= 3 + && y >= 0 && y <= 3, + $"{x}:{y} outside of playarea!"); + return x + y * 3; + } + + private int GetField(int x, int y) => _field[PosToIdx(x, y)]; + private void SetField(int value, int x, int y) + { + _field[PosToIdx(x, y)] = value; + _lastSetValue = value; + } + + // 0 1 2 + // 3 4 5 + // 6 7 8 + private static readonly int[,] _winning = new int[,] { + { 0, 1, 2 }, + { 3, 4, 5 }, + { 6, 7 ,8 }, + { 0, 3 ,6 }, + { 1, 4 ,7 }, + { 2, 5 ,8 }, + { 0, 4 ,8 }, + { 2, 4 ,6 }, + }; + + public TicTacTocBoard() + { + _field = new List(new int[9] { + 0, 0, 0, + 0, 0, 0, + 0, 0, 0, + }); + + _lastSetValue = 0; + } + + public bool SetFieldValue(int value, int x, int y, bool force = false) + { + int field = GetField(x, y); + if (0 == field || force) + { + SetField(value, x, y); + return true; + } + return false; + } + + public bool ValueWon(int value) + { + Debug.Assert(value > 0, "ValueWon called for Empty field 0"); + + for (int x = 0; x < _winning.GetLength(0); x++) + { + bool won = true; + for (int y = 0; y < _winning.GetLength(1); y++) + { + if (_field[_winning[x, y]] != value) + { + won = false; + break; + } + } + + if (won) + return true; + } + + return false; + } + } +} \ No newline at end of file diff --git a/Logic/TicTacTocManager.cs b/Logic/TicTacTocManager.cs index 455e7ca..fdffe6e 100644 --- a/Logic/TicTacTocManager.cs +++ b/Logic/TicTacTocManager.cs @@ -5,16 +5,41 @@ using System; namespace ProjectGrid { - public class TicTacTocManager + public interface ITicTacTocManager + { + public bool NextMove(TicTacTocRequest move); + public TicTacTocResponse GetResponse(int player = 0); + } + + public class TicTacTocManager : ITicTacTocManager { private ITicTacToctRepository _repo; private TicTacTocBoard _board; + public TicTacTocBoard Board => _board; - public TicTacTocManager(ITicTacToctRepository repo) + public TicTacTocManager(/*ITicTacToctRepository repo*/) { - _repo = repo; + //_repo = repo; + _board = new TicTacTocBoard(); + } + + public bool NextMove(TicTacTocRequest move) + { + return _board.SetFieldValue(move.Player, move.PosX, move.PosY); + } + + public TicTacTocResponse GetResponse(int player = 0) + { + TicTacTocResponse response = new TicTacTocResponse(); + if (player > 0 && _board.ValueWon(player)) + response.PlayerWon = player; + + response.Board = _board.Field; + + return response; + } } } \ No newline at end of file diff --git a/Models/TicTacTocBoard.cs b/Models/TicTacTocBoard.cs deleted file mode 100644 index 01bc9ba..0000000 --- a/Models/TicTacTocBoard.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace ProjectGrid.Models -{ - public class TicTacTocBoard - { - public int Id { get; set; } - - public string firstName { get; set; } - - public string lastName { get; set; } - - public string email { get; set; } - } -} \ No newline at end of file diff --git a/Models/TicTacTocMove.cs b/Models/TicTacTocMove.cs deleted file mode 100644 index 3e865ac..0000000 --- a/Models/TicTacTocMove.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace ProjectGrid.Models -{ - public class TicTacTocMove - { - public int Id { get; set; } - - public string firstName { get; set; } - - public string lastName { get; set; } - - public string email { get; set; } - } -} \ No newline at end of file diff --git a/Models/TicTacTocRequest.cs b/Models/TicTacTocRequest.cs new file mode 100644 index 0000000..8711c96 --- /dev/null +++ b/Models/TicTacTocRequest.cs @@ -0,0 +1,13 @@ +namespace ProjectGrid.Models +{ + public class TicTacTocRequest + { + public int PosX { get; set; } + + public int PosY { get; set; } + + public int Player { get; set; } + + public string DebugString() => $"Pos: [{PosX}|{PosY}; Player: {Player}"; + } +} \ No newline at end of file diff --git a/Models/TicTacTocResponse.cs b/Models/TicTacTocResponse.cs new file mode 100644 index 0000000..4d4b4b8 --- /dev/null +++ b/Models/TicTacTocResponse.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace ProjectGrid.Models +{ + public class TicTacTocResponse + { + public List Board { get; set; } + + public int PlayerWon { get; set; } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 405facb..69d9621 100644 --- a/Program.cs +++ b/Program.cs @@ -30,7 +30,6 @@ namespace ProjectGrid } catch (Exception ex) { - _logger.LogError(ex, "An error occurred while seeding the database."); } } diff --git a/Startup.cs b/Startup.cs index cc7aaf9..ae45b86 100644 --- a/Startup.cs +++ b/Startup.cs @@ -15,58 +15,65 @@ using ProjectGrid.Data; namespace ProjectGrid { - public class Startup + public class Startup + { + ILogger _logger; + + public Startup() { - ILogger _logger; - - public Startup() - { - Configuration = new ConfigurationBuilder() - .AddJsonFile("appSettings.json") // Get Connectionstring from appsetting.json - .Build(); - } - - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - var connectionString = Configuration.GetConnectionString("DefaultConnection"); - // Cant log here.. - //_logger.LogInformation($"Try to connect to Database with {connectionString}"); - services.AddDbContext(options => options.UseSqlServer(connectionString)); - //_logger.LogInformation($"DataAccessContext registered with {connectionString}"); - // TODO: for Testing - //services.AddDatabaseDeveloperPageExceptionFilter(); - services.AddControllers(); - services.AddSwaggerGen(c => - { - c.SwaggerDoc("v1", new OpenApiInfo { Title = "ProjectGrid", Version = "v1" }); - }); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger logger) - { - _logger = logger; - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - app.UseSwagger(); - app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ProjectGrid v1")); - } - - app.UseHttpsRedirection(); - - app.UseRouting(); - - app.UseAuthorization(); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - }); - } + Configuration = new ConfigurationBuilder() + .AddJsonFile("appSettings.json") // Get Connectionstring from appsetting.json + .Build(); } + + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + var connectionString = Configuration.GetConnectionString("DefaultConnection"); + // Cant log here.. + //_logger.LogInformation($"Try to connect to Database with {connectionString}"); + services.AddDbContext(options => options.UseSqlServer(connectionString)); + //_logger.LogInformation($"DataAccessContext registered with {connectionString}"); + // TODO: for Testing + //services.AddDatabaseDeveloperPageExceptionFilter(); + services.AddControllers(); + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new OpenApiInfo { Title = "ProjectGrid", Version = "v1" }); + }); + + // Manager + services.AddSingleton(new TicTacTocManager()); + services.AddScoped(); + + // DataAccess + services.AddScoped(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger logger) + { + _logger = logger; + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + app.UseSwagger(); + app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ProjectGrid v1")); + } + + app.UseHttpsRedirection(); + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } } \ No newline at end of file