Пример #1
0
        public async Task <string> JoinGameAsync(string accessCode, User user)
        {
            var org = await _OrganizationLogic.GetOrganizationByUserAsync(user.UserId).ConfigureAwait(false);

            if (org == null)
            {
                // Organization unknown
                return(null);
            }

            //Get game
            var game = await _GameDataAccess.GetGameByAccessCodeAsync(accessCode).ConfigureAwait(false);

            if (game == null)
            {
                // Game doesnt exist or code is wrong
                return(null);
            }

            if (!game.OrganizationId.Equals(org.OrganizationId))
            {
                // User not in the organization
                return(null);
            }

            if (game.State != GameStatusEnum.WaitingForPlayers)
            {
                // Game already started or finished
                return(null);
            }

            if (game.PlayerIds.Contains(user.UserId))
            {
                // Player already in game
                return(null);
            }

            // Double check access code
            if (game.AccessCode.Equals(accessCode, StringComparison.OrdinalIgnoreCase))
            {
                var addPlayerResult = await _GameDataAccess.AddPlayerToGameAsync(game.GameId, user.UserId).ConfigureAwait(false);

                if (addPlayerResult)
                {
                    return(game.GameId);
                }
            }

            //Code is wrong
            return(null);
        }