public async Task SubmitTeam(GameAndPlayerIdDto model)
        {
            Game g = GameStore.GetGame(model.GameId);

            g.CurrentRound.Status = RoundStatus.VotingForTeam;
            GameStore.AddOrUpdateGame(g);
            await _gameHub.Clients.Group(model.GameId.ToString()).SendAsync("UpdateAll");
        }
        public async Task <GameDto> CreateAsync()
        {
            Game g = new Game();

            g.Id = Guid.NewGuid();
            GameStore.AddOrUpdateGame(g);
            return(ObjectMapper.Map <GameDto>(g));
        }
        public async Task RemovePlayer(Guid GameId, Guid PlayerId)
        {
            Game g = GameStore.GetGame(GameId);

            g.Players.RemoveAll(p => p.Id == PlayerId);
            GameStore.AddOrUpdateGame(g);
            await _gameHub.Clients.Group(GameId.ToString()).SendAsync("UpdateAll");
        }
        public async Task SetRoundStatus(SetRoundStatusDto model)
        {
            Game g = GameStore.GetGame(model.GameId);

            g.CurrentRound.Status = model.Status;
            if (model.Status == RoundStatus.SelectingTeam)
            {
                g.CurrentRound.VotesForTeam     = 0;
                g.CurrentRound.VotesAgainstTeam = 0;
            }
            GameStore.AddOrUpdateGame(g);
            await _gameHub.Clients.Group(model.GameId.ToString()).SendAsync("UpdateAll");
        }
        public async Task Assassinate(GameAndPlayerIdDto model)
        {
            Game   g = GameStore.GetGame(model.GameId);
            Player p = g.GetPlayer(model.PlayerId);

            if (p.RoleId == GameRole.Merlin)
            {
                g.PointsEvil += 100;
            }
            g.Status = GameStatus.Ended;
            GameStore.AddOrUpdateGame(g);
            await _gameHub.Clients.Group(g.Id.ToString()).SendAsync("UpdateAll");
        }
        public async Task SetTeam(SetTeamDto model)
        {
            Game          g    = GameStore.GetGame(model.GameId);
            List <Player> team = new List <Player>();

            g.CurrentRound.CurrentTeam.Clear();
            foreach (Guid playerId in model.CurrentTeam)
            {
                team.Add(g.GetPlayer(playerId));
            }
            g.CurrentRound.CurrentTeam = team;
            GameStore.AddOrUpdateGame(g);
            await _gameHub.Clients.Group(model.GameId.ToString()).SendAsync("UpdateHost");
        }
 private async Task ExpeditionResults(Game g)
 {
     if (g.CurrentRound.MissionVoteBad > 0)
     {
         g.CurrentRound.Status = RoundStatus.MissionFailed;
         g.PointsEvil++;
     }
     else
     {
         g.CurrentRound.Status = RoundStatus.MissionSuccess;
         g.PointsInnocent++;
     }
     GameStore.AddOrUpdateGame(g);
     await _gameHub.Clients.Group(g.Id.ToString()).SendAsync("UpdateAll");
 }
        public async Task <Round> ExpeditonVote(VoteDto model)
        {
            Game g = GameStore.GetGame(model.GameId);

            g.CurrentRound.ExpeditionVote(model.Vote);
            if (g.CurrentRound.TotalMissionVotes >= g.CurrentRound.CurrentTeam.Count)
            {
                await ExpeditionResults(g);
            }
            else
            {
                GameStore.AddOrUpdateGame(g);
                await _gameHub.Clients.Group(model.GameId.ToString()).SendAsync("UpdateHost");
            }
            return(g.CurrentRound);
        }
        public async Task <Round> VoteForTeam(VoteDto model)
        {
            Game g = GameStore.GetGame(model.GameId);

            g.CurrentRound.TeamVote(model.Vote);
            if (g.CurrentRound.TotalTeamVotes >= g.Players.Count)
            {
                await VoteForTeamResults(g);
            }
            else
            {
                GameStore.AddOrUpdateGame(g);
                await _gameHub.Clients.Group(model.GameId.ToString()).SendAsync("UpdateHost");
            }
            return(g.CurrentRound);
        }
        public async Task NextRound(GameAndPlayerIdDto model)
        {
            Game g = GameStore.GetGame(model.GameId);

            if (g.PointsEvil >= 3 || g.PointsInnocent >= 3)
            {
                await GameEnd(model.GameId);
            }
            else
            {
                g.CurrentRound.TeamString = String.Join(' ', g.CurrentRound.CurrentTeam.Select(p => p.Name));
                g.PreviousRounds.Add(g.CurrentRound);
                g.CurrentRound = new Round(g.PreviousRounds.Count, g.Players.Count);
            }
            GameStore.AddOrUpdateGame(g);
            await _gameHub.Clients.Group(g.Id.ToString()).SendAsync("UpdateAll");
        }
        private async Task VoteForTeamResults(Game g)
        {
            var Votesfor = g.CurrentRound.VotesForTeam;

            if (Votesfor <= g.Players.Count() / 2)
            {
                g.CurrentRound.Status = RoundStatus.TeamDenied;
                g.CurrentRound.FailedTeams++;
            }
            else
            {
                g.CurrentRound.Status = RoundStatus.TeamApproved;
            }
            g.Counter++;
            g.CurrentPlayer = g.Players[g.Counter % g.Players.Count()];
            GameStore.AddOrUpdateGame(g);
            await _gameHub.Clients.Group(g.Id.ToString()).SendAsync("UpdateAll");
        }
        public async Task <GameDto> StartGame(StartGameDto model)
        {
            Game g = GameStore.GetGame(model.Id);

            for (int i = 0; i < g.Players.Count; i++)
            {
                g.Players[i].Order = i;
            }
            g.CurrentRound  = new Round(1, g.Players.Count);
            g.CurrentPlayer = g.Players[0];
            g.Status        = GameStatus.Playing;
            await AssertRoles(model.Id, model.Roles);

            GameStore.AddOrUpdateGame(g);
            await _gameHub.Clients.Group(g.Id.ToString()).SendAsync("UpdateAll");

            return(ObjectMapper.Map <GameDto>(g));
        }
        public async Task <GameDto> AssertRoles(Guid id, List <string> rollene)
        {
            Game g     = GameStore.GetGame(id);
            var  roles = rollene.Select(int.Parse).ToList();

            roles.ForEach(role =>
            {
                var random = new Random();
                // Random player that is not yet assigned a role
                var notYetChosenPlayers = g.Players.FindAll(p => p.RoleId == GameRole.NotYetChosen);
                var randomPlayer        = notYetChosenPlayers[random.Next(notYetChosenPlayers.Count)];
                randomPlayer.RoleId     = (GameRole)role;
                if (role >= 4)
                {
                    randomPlayer.IsEvil = true;
                }
                g.Players[g.Players.FindIndex(p => p.Id == randomPlayer.Id)] = randomPlayer;
            });
            var numberOfGood    = roles.FindAll(r => r <= 3).Count;
            var numberOfEvil    = roles.Count - numberOfGood;
            var numberOfMinions = GetHowManyEvils(g.Players.Count) - numberOfEvil;

            for (int i = 0; i < numberOfMinions; i++)
            {
                var random = new Random();
                // Random player that is not yet assigned a role
                var notYetChosenPlayers = g.Players.FindAll(p => p.RoleId == GameRole.NotYetChosen);
                var randomPlayer        = notYetChosenPlayers[random.Next(notYetChosenPlayers.Count)];
                randomPlayer.RoleId = GameRole.Minion;
                randomPlayer.IsEvil = true;
                g.Players[g.Players.FindIndex(p => p.Id == randomPlayer.Id)] = randomPlayer;
            }
            g.Players.ForEach(p =>
            {
                if (p.RoleId == GameRole.NotYetChosen)
                {
                    p.RoleId = GameRole.Servant;
                }
                p.RoleName = p.RoleId.ToString();
                p.RoleInfo = ReturnInfo(g, p.RoleId);
            });
            GameStore.AddOrUpdateGame(g);
            return(ObjectMapper.Map <GameDto>(g));
        }
Exemple #14
0
        public async Task <GamePlayerDto> CreateAsync(CreatePlayerDto model)
        {
            Player p = ObjectMapper.Map <Player>(model);

            p.Id = Guid.NewGuid();
            Game g = GameStore.GetGameByJoinCode(model.JoinCode);

            if (g.Players.Select(p => p.Name.ToLower()).Contains(model.Name.ToLower()))
            {
                throw new UserFriendlyException("Name already taken", $"The name '{model.Name}' has already been taken by another player!");
            }
            if (model.Name.Length > 20)
            {
                throw new UserFriendlyException("Name too long", $"Name cannot be longer than 20 characters.");
            }
            g.Players.Add(p);
            GameStore.AddOrUpdateGame(g);
            GamePlayerDto gp = new GamePlayerDto();

            gp.Game   = g;
            gp.Player = p;
            return(gp);
        }
        public async Task GameEnd(Guid id)
        {
            Game g = GameStore.GetGame(id);

            g.Status = GameStatus.Ended;
            if (g.PointsEvil < 3)
            {
                bool AssassinGame = false;
                g.Players.ForEach(x =>
                {
                    if (x.RoleId == GameRole.Assassin)
                    {
                        AssassinGame = true;
                    }
                });
                if (AssassinGame)
                {
                    g.Status = GameStatus.AssassinTurn;
                }
            }
            GameStore.AddOrUpdateGame(g);
            await _gameHub.Clients.Group(g.Id.ToString()).SendAsync("UpdateAll");
        }