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); }
async private void CheckFinish() { bool hasFinished = false; if (this.wrongFlags == 0 && this.FlaggedMines == this.Mines) { hasFinished = true; foreach (Plate item in this.plates) { if (!item.IsRevealed && !item.IsMined) { hasFinished = false; break; } } } if (hasFinished) { score = TimeElapsed; gameTimer.Stop(); MessageBox.Show(score.ToString()); //score for KOSTY MineRepository mr = MineRepository.Initialize(); await System.Threading.Tasks.Task.Run(() => { mr.WriteResult(score); }); } }
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); }