public ActionResult <NewGameResponse> NewGame([FromBody] NewPostBody value) { //validate input if (!NewGameValidation.IsBodyPostValid(value)) { return(BadRequest()); } //create token NewGameResponse response = new NewGameResponse(); response.Token = Guid.NewGuid().ToString(); //generate structure to save (map with mines) MineMatrix mineMatrix = new MineMatrix(value.Height, value.Width, value.Bombs, response.Token); //save structure in repository //it could be an interface for db, file or memcached MineRepository repository = new MineRepository(); repository.Save(mineMatrix); return(response); }
public int Save(MineMatrix matrix) { try { var jsonToSave = JsonConvert.SerializeObject(matrix); File.WriteAllText($"{matrix.Token}.json", jsonToSave); }catch (Exception ex) { Console.WriteLine(ex.Message); return(-1); } return(1); }
public ActionResult <PickResponse> Pick([FromBody] CellCoordinates value, string token) { //middleware checks token if (string.IsNullOrEmpty(token)) { return(BadRequest()); } // validate coordinates if (!PickValidation.IsPickValid(value)) { return(BadRequest()); } MineRepository repository = new MineRepository(); MineMatrix mineMatrix = repository.Load(token); if (mineMatrix == null) { return(StatusCode(500)); } // evaluate if bomb, number or zero MineSweeper core = new MineSweeper(mineMatrix); Cell cell = core.ProcessPoint(value.X, value.Y); PickResponse pr = new PickResponse(); if (core.IsGameOver()) { pr.GameStatus = GameStatus.GAME_OVER.ToString(); } else { pr.GameStatus = GameStatus.PLAYING.ToString(); } pr.TimeLapsed = DateTime.Now.Subtract(mineMatrix.StartedTime).Minutes; return(pr); }
public MineSweeper(MineMatrix mineMatrix) { _mineMatrix = mineMatrix; }