예제 #1
0
        public static async Task MatchFound(
            [QueueTrigger("matchfound")] string request,
            [SignalR(HubName = "PubSub")] IAsyncCollector <SignalRMessage> signalRMessageQueue,
            ILogger log)
        {
            log.LogInformation($"MatchFound: Received match_found event: {request}.");

            // We need to extact the matchId and queueName from the context. These are contained in the PlayStream V2 event payload and will be used to query GetMatch.
            var context            = JsonConvert.DeserializeObject <EntityPlayStreamFunctionExecutionContext <MatchFoundPayload, object> >(request);
            var matchmakingPayload = context.PlayStreamEvent.Payload;

            // Making a call to PlayFab to get the match information from GetMatch. This uses the title auth context, matchId, and queueName provided in the request.
            PlayFabSettings.staticSettings.TitleId = context.TitleAuthenticationContext.Id;
            var getMatchRequest = new GetMatchRequest()
            {
                AuthenticationContext = new PlayFabAuthenticationContext()
                {
                    EntityToken = context.TitleAuthenticationContext.EntityToken
                },
                EscapeObject           = false,
                MatchId                = matchmakingPayload.MatchId,
                QueueName              = matchmakingPayload.QueueName,
                ReturnMemberAttributes = true
            };

            var match = await PlayFabMultiplayerAPI.GetMatchAsync(getMatchRequest);

            if (match.Error != null)
            {
                log.LogError($"GetMatch returned error: {match.Error.ErrorMessage}");
                return;
            }

            log.LogInformation($"GetMatch response: {match}");

            // We extracted the list of players from the GetMatch result. Now we iterate through them and send the match details to each.
            foreach (var player in match.Result.Members)
            {
                await signalRMessageQueue.AddAsync(
                    new SignalRMessage
                {
                    Target    = "matchmakingComplete",
                    Arguments = new[] { JsonConvert.SerializeObject(match.Result) },
                    UserId    = player.Entity.Id
                });
            }
        }