public bool AddMatch(Match match)
 {
     _ctx.Matches.Add(match);
     return true;
 }
Ejemplo n.º 2
0
        public IHttpActionResult PostMatch([FromBody]CreateMatchViewModel data)
        {
            if (!ModelState.IsValid)
                return BadRequest();

            if (data.P1Name == data.P2Name)
                return BadRequest("Two different players are required for this game");

            //this condition verify if is required the creation of some player
            var p1Created = _gameOfDronesRepository.AddPlayer(data.P1Name);
            var p2Created = _gameOfDronesRepository.AddPlayer(data.P2Name);

            if (p1Created || p2Created)
                _gameOfDronesRepository.SaveChanges();

            var currentRule = _gameOfDronesRepository.GetCurrentRule();

            var match = new Match
            {
                Rule = currentRule,
            };

            //initialize default player stats. this data will be filled after the game ends
            var player1Stats = new PlayerStats
            {
                Player = _gameOfDronesRepository.GetPlayerByName(data.P1Name),
                MatchResult = MatchResult.Invalid,
            };
            var player2Stats = new PlayerStats
            {
                Player = _gameOfDronesRepository.GetPlayerByName(data.P2Name),
                MatchResult = MatchResult.Invalid,
            };

            match.AddPlayerStats(player1Stats);
            match.AddPlayerStats(player2Stats);

            _gameOfDronesRepository.AddMatch(match);
            _gameOfDronesRepository.SaveChanges();

            return Ok(match);
        }