Example #1
0
        public async Task <OrganizedGameResponse> DeleteGame(string id)
        {
            var game = organizedGamesRepository.Get(id);

            if (game is null)
            {
                throw new NotFoundException("hosted game does not exist");
            }

            var userClaims      = httpContextAccessor.HttpContext.User;
            var authorizeResult = await authorizationService.AuthorizeAsync(null, game, new [] { new HostRequirement() });

            if (!authorizeResult.Succeeded)
            {
                throw new ForbiddenException("User is not host");
            }

            if (game.StartTime.CompareTo(DateTime.UtcNow) <= 0)
            {
                throw new InvalidActionException("Game has already started");
            }

            var deleted = await organizedGamesRepository.Delete(id);

            return(mapper.Map <OrganizedGameResponse>(deleted));
        }
Example #2
0
        public async Task <PlayerResponse> JoinGame(string gameId, string userId)
        {
            var game = organizedGamesRepository.Get(gameId);

            if (game is null)
            {
                throw new NotFoundException("game does not exist");
            }
            if (game.StartTime.CompareTo(DateTime.UtcNow) <= 0)
            {
                throw new InvalidActionException("game has already started");
            }

            var playerFound = gameParticipantsRepository.Filter(x => x.GameId == gameId && x.PlayerId == userId).FirstOrDefault();

            if (!(playerFound is null))
            {
                throw new InvalidActionException("player is already in game");
            }
            var players = gameParticipantsRepository.GetParticipantsByGame(gameId).Count();

            var participant = new GameParticipant
            {
                GameId    = gameId,
                PlayerId  = userId,
                StartTime = game.StartTime.Add(players * game.StartInterval)
            };

            var added = await gameParticipantsRepository.Add(participant);

            return(mapper.Map <PlayerResponse>(added));
        }