Example #1
0
        public async Task <MatchRequestResult> RequestMatch(ActiveUser activeUser,
                                                            string oppoId)
        {
            var dUser = await _masterRepo.GetUserByIdAsyc(activeUser.Id);

            if (dUser.Money < Room.MinBet)
            {
                throw new BadUserInputException();
            }

            //BadUserInputException is thrown when something is wrong but should've been
            //validated by the client

            var oppoUser = await _masterRepo.GetUserByIdAsyc(oppoId);

            var friendship = _masterRepo.GetFriendship(activeUser.Id, oppoId);

            if (friendship is FriendShip.None or FriendShip.Follower && !oppoUser.EnableOpenMatches)
            {
                throw new BadUserInputException();
            }

            if (!_sessionRepo.IsUserActive(oppoId))
            {
                return(MatchRequestResult.Offline);
            }

            if (_sessionRepo.DoesRoomUserExist(oppoId))
            {
                return(MatchRequestResult.Playing);
            }

            if (oppoUser.Money < Room.MinBet)
            {
                return(MatchRequestResult.NoMoney);
            }

            //can't call again because this fun domain is lobby.idle only
            activeUser.Domain = typeof(UserDomain.App.Lobby.Pending);

            activeUser.ChallengeRequestTarget = oppoId;

            var oppoAU = _sessionRepo.GetActiveUser(oppoId);
            //oppo is 100% active at this satage

            await _masterHub.SendOrderedAsync(oppoAU, "ChallengeRequest",
                                              Mapper.UserToMinUserInfoFunc(dUser));

            return(MatchRequestResult.Available);
        }
Example #2
0
        public async ValueTask <object> InvokeMethodAsync(HubInvocationContext invocationContext,
                                                          Func <HubInvocationContext, ValueTask <object> > next)
        {
            //HubException from here can terminate the invokation before calling the hub method or any next, and the clietn will recieve an error

            //practical: you can add custom attribute on the method and fetch it's data from here
            //E.G:
            // var languageFilter = (LanguageFilterAttribute)Attribute.GetCustomAttribute(
            //             invocationContext.HubMethod, typeof(LanguageFilterAttribute));
            // if (languageFilter != null &&
            //     invocationContext.HubMethodArguments.Count > languageFilter.FilterArgument &&
            //     invocationContext.HubMethodArguments[languageFilter.FilterArgument] is string str)

            _logger.LogInformation(
                $"Calling hub method '{invocationContext.HubMethodName}'" +
                $" with args {string.Join(", ", invocationContext.HubMethodArguments)}");

            var activeUser = _sessionRepo.GetActiveUser(invocationContext.Context.UserIdentifier);
            var domain     = _methodDomains.GetDomain(invocationContext.HubMethodName);

            if (activeUser.IsDisconnected)
            {
                throw new Exception(
                          "there's something wrong with ur sys, a user is disconnected and calling!");
            }

            if (domain == null)
            {
                throw new BadUserInputException(
                          "the user is invoking a function that doesn't exist or it's not an rpc");
            }
            if (!activeUser.Domain.IsSubclassOf(domain) &&
                !activeUser.Domain.IsEquivalentTo(domain))
            {
                throw new BadUserInputException(
                          $"the called function with domain {domain} is not valid in the current user domain {activeUser.Domain}");
            }

            _requestCache.Init(invocationContext.Context.UserIdentifier);

            try
            {
                return(await next(invocationContext));

                // invokes the next filter. And if it's the last filter, invokes the hub method
            }
            catch (BadUserInputException)
            {
                _logger.LogInformation("BadUserInputException happened");

                throw;
                // return new ValueTask<int>(1);
                // return new ValueTask<User>(new User {Name = "test name on the returned user"});
                // return new ValueTask<object>($"there's a buie exc on the server {e.Message}");
            }
            // catch (Exception ex)
            // {
            //     _logger.LogInformation($"Exception calling '{invocationContext.HubMethodName}': {ex}");
            //     throw;
            // }
            finally
            {
                //check againest the called funtion if it's a trigger
                //is it throw?
                //call the serverloop to create new scope to check for distribute or finalize
                //my issue is the place
                //so create a trigger system
            }
        }