Exemplo n.º 1
0
        public async Task <GameActionViewModel[]> Handle(StartGameCommand request)
        {
            var game = _context.Games
                       .Include(x => x.Players.Select(p => p.User))
                       .FirstOrDefault(x => x.GameId == request.GameId);

            if (game.HostId != request.UserId)
            {
                throw new ValidationException(new[] { new ValidationFailure("HostId", $"User with Id of \"{request.UserId}\" is not the host.") });
            }

            // Start game
            var gameActionList = new List <GameAction>();
            var gameAction     = new GameAction
            {
                GameId      = request.GameId,
                UserId      = request.UserId,
                DateTime    = DateTime.Now,
                Action      = (int)Actions.StartedGame,
                ActionValue = "has started the game."
            };

            _context.GameActions.Add(gameAction);
            gameActionList.Add(gameAction);

            // set next player
            GamePlayer nextPlayer;
            GameAction nextPlayerAction;

            GameHelpers.GetNexPlayer(request, game, gameActionList, out nextPlayer, out nextPlayerAction, _context);
            await _context.SaveChangesAsync();

            return(GameHelpers.ProcessNextPlayerResults(gameActionList, nextPlayer, nextPlayerAction, _context));
        }
Exemplo n.º 2
0
        public async Task <GameActionViewModel[]> Handle(PlayerRolledDieCommand request)
        {
            var game = _context.Games
                       .Include(x => x.Players.Select(p => p.User))
                       .FirstOrDefault(x => x.GameId == request.GameId);

            // wait 5 seconds so you can see a nice spinning animation in UI...
            await Task.Delay(5000);

            // calculate roll
            List <Die> dieList  = request.Die.ToObject <List <Die> >();
            var        dieCount = dieList.Count;

            foreach (var die in dieList)
            {
                die.DieValue = _random.Next(1, die.DieSideCount);
            }
            // build game action
            var gameActionList = new List <GameAction>();
            var gameAction     = new GameAction
            {
                GameId      = request.GameId,
                UserId      = request.UserId,
                DateTime    = DateTime.Now,
                Action      = (int)Actions.Moved,
                ActionValue = $"has rolled {dieCount} die and got the following values: {string.Join(", ", dieList.Select(x => $"D{x.DieSideCount}:{x.DieValue}"))}"
            };

            _context.GameActions.Add(gameAction);
            gameActionList.Add(gameAction);

            // set next player
            GamePlayer nextPlayer;
            GameAction nextPlayerAction;

            GameHelpers.GetNexPlayer(request, game, gameActionList, out nextPlayer, out nextPlayerAction, _context);

            await _context.SaveChangesAsync();

            return(GameHelpers.ProcessNextPlayerResults(gameActionList, nextPlayer, nextPlayerAction, _context));
        }