Пример #1
0
        public static Game Map(this GameExtendedDTO gameDTO, int chapterId, string userId)
        {
            Game game = new Game()
            {
                BookId      = gameDTO.BookId,
                CreatedDate = DateTime.UtcNow,
                Name        = gameDTO.Name,
                UserId      = userId.ToString()
            };

            AdventureSheet adventureSheet = new AdventureSheet()
            {
                Luck    = gameDTO.Luck,
                Skill   = gameDTO.Skill,
                Stamina = gameDTO.Stamina
            };

            game.GameHistories.Add(new GameHistory()
            {
                Game           = game,
                ChapterId      = chapterId,
                CreatedDate    = DateTime.UtcNow,
                AdventureSheet = adventureSheet
            });

            return(game);
        }
        public ActionResult <GameExtendedDTO> SetGame(GameExtendedDTO gameDTO)
        {
            var gamehistory = _context.GameHistories
                              .Where(x => x.GameId == gameDTO.Id)
                              .OrderByDescending(y => y.CreatedDate)
                              .Include(x => x.AdventureSheet)
                              .FirstOrDefault();

            AdventureSheet adventureSheet;

            if (gamehistory?.AdventureSheet != null)
            {
                if (gamehistory.AdventureSheet.Luck != gameDTO.Luck ||
                    gamehistory.AdventureSheet.Skill != gameDTO.Skill ||
                    gamehistory.AdventureSheet.Stamina != gameDTO.Stamina)
                {
                    adventureSheet = new AdventureSheet()
                    {
                        Skill   = gameDTO.Skill,
                        Stamina = gameDTO.Stamina,
                        Luck    = gameDTO.Luck
                    };
                }
                else
                {
                    adventureSheet = gamehistory.AdventureSheet;
                }
            }
            else
            {
                throw new Exception("Gamehistory missing");
            }

            var gameHistory = new GameHistory()
            {
                AdventureSheetId = adventureSheet.Id,
                AdventureSheet   = adventureSheet.Id == 0 ? adventureSheet : null,
                ChapterId        = gameDTO.ChapterId,
                CreatedDate      = DateTime.UtcNow,
                GameId           = gameDTO.Id
            };

            _context.Add(gameHistory);

            _context.SaveChanges();

            return(Get(gameDTO.Id));
        }