Exemple #1
0
        protected override void DoMatch(GatewayInternalService.GatewayInternalServiceClient gatewayClient,
                                        DeploymentServiceClient deploymentServiceClient)
        {
            Thread.Sleep(TickMs);

            try
            {
                Console.WriteLine("Fetching 1...");
                var resp = gatewayClient.PopWaitingParties(new PopWaitingPartiesRequest
                {
                    Type       = "match1",
                    NumParties = 1
                });
                Console.WriteLine($"Fetched {resp.Parties.Count} parties");
                gatewayClient.AssignDeployments(ConstructAssignRequest(resp.Parties, "1"));
            }
            catch (Exception e)
            {
                Console.WriteLine($"Got exception: {e.Message}\n{e.StackTrace}");
            }

            try
            {
                Console.WriteLine("Fetching 3...");
                var resp = gatewayClient.PopWaitingParties(new PopWaitingPartiesRequest
                {
                    Type       = "match3",
                    NumParties = 3
                });
                Console.WriteLine($"Fetched {resp.Parties.Count} parties");
                gatewayClient.AssignDeployments(ConstructAssignRequest(resp.Parties, "3"));
            }
            catch (Exception e)
            {
                Console.WriteLine($"Got exception: {e.Message}\n{e.StackTrace}");
            }

            try
            {
                Console.WriteLine("Fetching requeueable...");
                var resp = gatewayClient.PopWaitingParties(new PopWaitingPartiesRequest
                {
                    Type       = "to_requeue",
                    NumParties = 1
                });
                Console.WriteLine($"Fetched {resp.Parties.Count} parties");
                gatewayClient.AssignDeployments(ConstructAssignRequest(resp.Parties, "requeue"));
            }
            catch (Exception e)
            {
                Console.WriteLine($"Got exception: {e.Message}\n{e.StackTrace}");
            }
        }
        private void AssignPartyAsRequeued(GatewayInternalService.GatewayInternalServiceClient gatewayClient,
                                           WaitingParty party)
        {
            var assignRequest = new AssignDeploymentsRequest();

            assignRequest.Assignments.Add(new Assignment
            {
                Result = Assignment.Types.Result.Requeued,
                Party  = party.Party
            });
            gatewayClient.AssignDeployments(assignRequest);
        }
        protected override void DoMatch(GatewayInternalService.GatewayInternalServiceClient gatewayClient,
                                        DeploymentServiceClient deploymentServiceClient)
        {
            try
            {
                var resp = gatewayClient.PopWaitingParties(new PopWaitingPartiesRequest
                {
                    Type       = _tag,
                    NumParties = 1
                });
                Console.WriteLine($"Fetched {resp.Parties.Count} from gateway");

                foreach (var party in resp.Parties)
                {
                    Console.WriteLine("Attempting to match a retrieved party.");
                    var deployment = GetDeploymentWithTag(deploymentServiceClient, _tag);
                    if (deployment != null)
                    {
                        var assignRequest = new AssignDeploymentsRequest();
                        Console.WriteLine("Found a deployment, assigning it to the party.");
                        assignRequest.Assignments.Add(new Assignment
                        {
                            DeploymentId   = deployment.Id,
                            DeploymentName = deployment.Name,
                            Result         = Assignment.Types.Result.Matched,
                            Party          = party.Party
                        });
                        MarkDeploymentAsInUse(deploymentServiceClient, deployment);
                        gatewayClient.AssignDeployments(assignRequest);
                    }
                    else
                    {
                        Console.WriteLine(
                            $"Unable to find a deployment with tag {_tag} in project {_project}");
                        Console.WriteLine("Requeueing the party");
                        AssignPartyAsRequeued(gatewayClient, party);
                    }
                }
            }
            catch (RpcException e)
            {
                if (e.StatusCode != StatusCode.ResourceExhausted && e.StatusCode != StatusCode.Unavailable)
                {
                    throw;
                }

                /* Unable to get the requested number of parties - ignore. */
                Console.WriteLine("No parties available.");
                Thread.Sleep(TickMs);
            }
        }
Exemple #4
0
        protected Matcher()
        {
            var spatialRefreshToken = Environment.GetEnvironmentVariable(SpatialRefreshTokenEnvironmentVariable) ??
                                      throw new Exception(
                                                $"{SpatialRefreshTokenEnvironmentVariable} environment variable is required.");

            _spatialDeploymentClient =
                DeploymentServiceClient.Create(credentials: new PlatformRefreshTokenCredential(spatialRefreshToken));
            _gatewayClient =
                new GatewayInternalService.GatewayInternalServiceClient(new Channel(
                                                                            Environment.GetEnvironmentVariable(GatewayServiceTargetEnvironmentVariable)
                                                                            ?? throw new Exception(
                                                                                $"Environment variable {GatewayServiceTargetEnvironmentVariable} is required."),
                                                                            ChannelCredentials.Insecure));
        }
Exemple #5
0
 protected override void DoShutdown(GatewayInternalService.GatewayInternalServiceClient gatewayClient,
                                    DeploymentServiceClient deploymentServiceClient)
 {
 }
 protected override void DoShutdown(GatewayInternalService.GatewayInternalServiceClient gatewayClient,
                                    DeploymentServiceClient deploymentServiceClient)
 {
     // If a matcher maintains state, here is where you hand it back to the Gateway if necessary.
 }
Exemple #7
0
        protected override void DoMatch(GatewayInternalService.GatewayInternalServiceClient gatewayClient,
                                        DeploymentServiceClient deploymentServiceClient)
        {
            try
            {
                var resp = gatewayClient.PopWaitingParties(new PopWaitingPartiesRequest
                {
                    Type       = _tag,
                    NumParties = 1
                });
                Console.WriteLine($"Fetched {resp.Parties.Count} from gateway");

                foreach (var party in resp.Parties)
                {
                    Console.WriteLine("Attempting to match a retrieved party.");
                    _analytics.Send("match", "party_matching", new Dictionary <string, string>
                    {
                        { "partyId", party.Party.Id },
                        { "queueType", _tag },
                        { "partyPhase", party.Party.CurrentPhase.ToString() },
                        { "matchRequestId", party.MatchRequestId }
                    }, party.Party.LeaderPlayerId);

                    foreach (var memberId in party.Party.MemberIds)
                    {
                        _analytics.Send("match", "player_matching", new Dictionary <string, string>
                        {
                            { "partyId", party.Party.Id },
                            { "queueType", _tag },
                            { "playerJoinRequestState", "Matching" },
                            { "matchRequestId", party.MatchRequestId }
                        }, memberId);
                    }

                    var deployment = GetDeploymentWithTag(deploymentServiceClient, _tag);
                    if (deployment != null)
                    {
                        var assignRequest = new AssignDeploymentsRequest();
                        Console.WriteLine("Found a deployment, assigning it to the party.");
                        assignRequest.Assignments.Add(new Assignment
                        {
                            DeploymentId   = deployment.Id,
                            DeploymentName = deployment.Name,
                            Result         = Assignment.Types.Result.Matched,
                            Party          = party.Party
                        });
                        MarkDeploymentAsInUse(deploymentServiceClient, deployment);
                        gatewayClient.AssignDeployments(assignRequest);
                        _analytics.Send("deployment", "deployment_in_use", new Dictionary <string, string>
                        {
                            { "spatialProjectId", _project },
                            { "deploymentName", deployment.Name },
                            { "deploymentId", deployment.Id }
                        });
                    }
                    else
                    {
                        Console.WriteLine(
                            $"Unable to find a deployment with tag {_tag} in project {_project}");
                        Console.WriteLine("Requeueing the party");
                        AssignPartyAsRequeued(gatewayClient, party);
                    }
                }
            }
            catch (RpcException e)
            {
                if (e.StatusCode != StatusCode.ResourceExhausted && e.StatusCode != StatusCode.Unavailable)
                {
                    throw;
                }

                /* Unable to get the requested number of parties - ignore. */
                Console.WriteLine("No parties available.");
                Thread.Sleep(TickMs);
            }
        }
        protected override void DoMatch(GatewayInternalService.GatewayInternalServiceClient gatewayClient,
                                        DeploymentServiceClient deploymentServiceClient)
        {
            try
            {
                var resp = gatewayClient.PopWaitingParties(new PopWaitingPartiesRequest
                {
                    Type       = _tag,
                    NumParties = 1
                });
                Console.WriteLine($"Fetched {resp.Parties.Count} from gateway");

                foreach (var party in resp.Parties)
                {
                    Console.WriteLine($"Attempting to match a retrieved party of {party.Party.MemberIds.Count} players.");

                    bool requeue    = false;
                    var  deployment = GetDeploymentWithTag(deploymentServiceClient, _tag);
                    if (deployment != null)
                    {
                        var assignRequest = new AssignDeploymentsRequest();
                        Console.WriteLine("Found a deployment.");
                        int numPlayers = 0;
                        foreach (string tag in deployment.Tag)
                        {
                            if (tag.StartsWith(PlayersTag))
                            {
                                int.TryParse(tag.Split("_")[1], out numPlayers);
                                Console.WriteLine($"Deployment already has {numPlayers} in.");
                                break;
                            }
                        }

                        int totalPlayers = numPlayers + party.Party.MemberIds.Count;
                        if (totalPlayers <= PlayersPerDeployment)
                        {
                            if (numPlayers > 0)
                            {
                                Console.WriteLine($"Removing tag {PlayersTag}_{numPlayers}");
                                deployment.Tag.Remove($"{PlayersTag}_{numPlayers}");
                            }

                            if (totalPlayers == PlayersPerDeployment)
                            {
                                Console.WriteLine("Deployment is full, marking as in_use");
                                MarkDeploymentAsInUse(deploymentServiceClient, deployment);
                            }
                            else
                            {
                                Console.WriteLine($"Deployment isn't full, adding tag {PlayersTag}_{totalPlayers}");
                                deployment.Tag.Add($"{PlayersTag}_{totalPlayers}");
                                var req = new UpdateDeploymentRequest {
                                    Deployment = deployment
                                };
                                deploymentServiceClient.UpdateDeployment(req);
                            }

                            assignRequest.Assignments.Add(new Assignment
                            {
                                DeploymentId   = deployment.Id,
                                DeploymentName = deployment.Name,
                                Result         = Assignment.Types.Result.Matched,
                                Party          = party.Party
                            });
                            gatewayClient.AssignDeployments(assignRequest);
                        }
                        else
                        {
                            Console.WriteLine("Deployment does not have space for a party of this size.");
                            requeue = true;
                        }
                    }
                    else
                    {
                        Console.WriteLine($"Unable to find a deployment with tag {_tag} in project {_project}");
                        requeue = true;
                    }

                    if (requeue)
                    {
                        Console.WriteLine("Requeueing the party");
                        AssignPartyAsRequeued(gatewayClient, party);
                    }
                }
            }
            catch (RpcException e)
            {
                if (e.StatusCode != StatusCode.ResourceExhausted && e.StatusCode != StatusCode.Unavailable)
                {
                    throw;
                }

                /* Unable to get the requested number of parties - ignore. */
                Thread.Sleep(TickMs);
            }
        }
Exemple #9
0
 protected abstract void DoShutdown(GatewayInternalService.GatewayInternalServiceClient gatewayClient,
                                    DeploymentServiceClient deploymentServiceClient);