Beispiel #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));
        }
Beispiel #3
0
        public static GameExtendedDTO Map(this Game game, GameHistory history, Book book)
        {
            var             adventureSheet = history.AdventureSheet;
            GameExtendedDTO gameDTO        = new GameExtendedDTO()
            {
                Id        = game.Id,
                BookId    = game.BookId,
                ChapterId = history.ChapterId,
                BookName  = book.Name,
                Name      = game.Name,
                Luck      = adventureSheet.Luck,
                Skill     = adventureSheet.Skill,
                Stamina   = adventureSheet.Stamina
            };

            return(gameDTO);
        }
        public ActionResult <GameExtendedDTO> Get(int gameId)
        {
            var game = _context.Games
                       .AsNoTracking()
                       .Include(x => x.Book)
                       .First(x => x.Id == gameId && x.UserId == User.Identity.GetId());

            var gamehistory = _context.GameHistories
                              .AsNoTracking()
                              .Where(x => x.GameId == gameId)
                              .OrderByDescending(y => y.CreatedDate)
                              .Include(x => x.AdventureSheet)
                              .First();

            GameExtendedDTO gameDTO = game.Map(gamehistory, game.Book);

            return(new JsonResult(gameDTO));
        }
        public ActionResult <int> Create(GameExtendedDTO gameDTO)
        {
            if (gameDTO == null)
            {
                throw new ArgumentNullException(nameof(gameDTO));
            }

            var chapterId = _context.Chapters
                            .Where(x => x.Book_Id == gameDTO.BookId && x.Number == 1)
                            .Select(x => x.Id).First();

            var game = gameDTO.Map(chapterId, User.Identity.GetId());

            _context.Add(game);
            _context.SaveChanges();

            //return CreatedAtRoute("GetTodo", new { id = item.Id }, item);
            return(new JsonResult(game.Id));
        }