Browse Source

Client will poll for match status join the server if a successful match is found

Signed-off-by: AMZN-Gene <[email protected]>
AMZN-Gene 2 years ago
parent
commit
ad66f55ba5

+ 2 - 1
MPSGameLift/Code/Include/MPSGameLift/IMatchmaking.h

@@ -19,7 +19,8 @@ namespace MPSGameLift
 
         // Request a match for the player.
         // @param RegionalLatencies A map of latency times between this client and a regional server endpoint.
-        // Regional latencies help determine the best server to join.
+        //     Regional latencies help determine the best server to join.
+        // @return True if the request was sent; otherwise false.
         virtual bool RequestMatch(const RegionalLatencies& regionalLatencies) = 0;
 
         // Gets the current matchmaking ticket id if any

+ 12 - 9
MPSGameLift/Code/Source/MatchmakingSystemComponent.cpp

@@ -20,7 +20,7 @@
 #include <Multiplayer/Session/ISessionHandlingRequests.h>
 #include <Request/AWSGameLiftRequestBus.h>
 
-#pragma optimize("",off)
+
 namespace MPSGameLift
 {
     namespace ServiceAPI
@@ -312,6 +312,10 @@ namespace MPSGameLift
 
                 // Make a request to check match status every second, until we timeout, or receive a valid match
                 m_requestMatchStatusEvent.Enqueue(AZ::SecondsToTimeMs(1.0));
+
+                // Begin counting a timeout
+                m_matchRequestTimeout = false;
+                m_requestMatchTimeoutEvent.Enqueue(AZ::SecondsToTimeMs(MatchRequestTimeoutSeconds));
             },
             []([[maybe_unused]] ServiceAPI::RequestMatchmakingJob* failJob)
             {
@@ -342,14 +346,15 @@ namespace MPSGameLift
                 if (successJob->result.playerSessionId.empty() || successJob->result.playerSessionId == "NotPlacedYet")
                 {
                     // Make a request to check match status every second, until we timeout, or receive a valid match
-                    AZ_Error("MatchmakingSystemComponent", false, "Match not placed yet. Checking again after 1 second.");
-
-                    m_requestMatchStatusEvent.Enqueue(AZ::SecondsToTimeMs(1.0));
-                    return;
+                    if (!m_matchRequestTimeout)
+                    {
+                        m_requestMatchStatusEvent.Enqueue(AZ::SecondsToTimeMs(1.0));
+                        return;
+                    }
                 }
 
-                // Enable GameLift and connect to host
-                AWSGameLift::AWSGameLiftRequestBus::Broadcast(&AWSGameLift::AWSGameLiftRequestBus::Events::ConfigureGameLiftClient, "us-east-1");
+                // Enable GameLift game client system and connect to the host server
+                AWSGameLift::AWSGameLiftRequestBus::Broadcast(&AWSGameLift::AWSGameLiftRequestBus::Events::ConfigureGameLiftClient, "");
                 Multiplayer::SessionConnectionConfig sessionConnectionConfig {
                     successJob->result.playerSessionId,
                     successJob->result.dnsName,
@@ -370,5 +375,3 @@ namespace MPSGameLift
         requestJob->Start();
     }
 }
-
-#pragma optimize("",on)

+ 20 - 3
MPSGameLift/Code/Source/MatchmakingSystemComponent.h

@@ -8,10 +8,10 @@
 #pragma once
 
 #include <AzCore/Component/Component.h>
+#include <AzCore/Console/ILogger.h>
 #include <AzCore/EBus/ScheduledEvent.h>
 #include <MPSGameLift/IMatchmaking.h>
 
-
 namespace MPSGameLift
 {
     class MatchmakingSystemComponent final
@@ -19,6 +19,8 @@ namespace MPSGameLift
         , public IMatchmaking
     {
     public:
+        static constexpr float MatchRequestTimeoutSeconds = 60.0f;
+
         AZ_COMPONENT(MatchmakingSystemComponent, "{BF5F9343-63B5-4703-89ED-9CDBF4FE6004}");
         static void Reflect(AZ::ReflectContext* context);
         static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
@@ -30,11 +32,26 @@ namespace MPSGameLift
      protected:
         void Activate() override;
         void Deactivate() override;
+
+        // Request matchmaking status.
+        // After requesting a match the client polls every few seconds for a successful match.
+        // The HTTP response contains a PlayerSessionId which is either "NotPlacedYet" or a proper UUID.
+        // A successful response will contain all the information required for the client to join the server;
+        //     Example: the DNS (or IP address), port, and player session id.
         void RequestMatchStatus();
 
     private:
         AZStd::string m_ticketId;
         AZ::ScheduledEvent m_requestMatchStatusEvent = AZ::ScheduledEvent([this] { this->RequestMatchStatus(); }, AZ::Name("MPS Request Match Status"));
-
-     };
+        AZ::ScheduledEvent m_requestMatchTimeoutEvent = AZ::ScheduledEvent([this] 
+            { 
+                AZLOG_ERROR("MatchmakingSystemComponent: Match request timed out! " 
+                    "Matches should start even if only 1 player is found; the backend might not be configured properly.");
+                m_requestMatchStatusEvent.RemoveFromQueue();
+                m_matchRequestTimeout = true;
+            }
+        , AZ::Name("MPS Request Match Timeout"));
+        
+        bool m_matchRequestTimeout = false;
+    };
 }