UiGameLiftFlexMatchConnect.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <Authorization/AWSCognitoAuthorizationBus.h>
  10. #include <AzCore/Component/Component.h>
  11. #include <MPSGameLift/IRegionalLatencyFinder.h>
  12. #include <Multiplayer/IMultiplayer.h>
  13. #include <Multiplayer/Session/ISessionHandlingRequests.h>
  14. namespace MPSGameLift
  15. {
  16. /*!
  17. * \class UiGameLiftFlexMatchConnect
  18. * \brief An example ui component used for joining multiplayer matches via the IMatchmaking system.
  19. */
  20. class UiGameLiftFlexMatchConnect
  21. : public AZ::Component
  22. , AWSClientAuth::AWSCognitoAuthorizationNotificationBus::Handler
  23. {
  24. static constexpr char StatusPlayerAuthInitSuccess[] = "Authorization initialized.";
  25. static constexpr char StatusPlayerAuthInitFailed[] = "Failed to initialized authorization service.";
  26. static constexpr char StatusAnonPlayerCredentialsReceived[] = "Anonymous player credentials received.";
  27. static constexpr char StatusAnonPlayerCredentialsFailed[] = "Failed to receive anonymous player credentials.";
  28. static constexpr char StatusRequestingServerRegionLatencies[] = "Finding server region latencies...";
  29. static constexpr char StatusLatencyRequestFailed[] = "Failed to reach any server endpoints.\nMake sure you are connected to the internet and try again.";
  30. static constexpr char StatusLatencyRequestSuccess[] = "Server endpoint latencies:\n%s.";
  31. static constexpr char StatusMatchmakingTicketReceived[] = "Matchmaking Ticket: %s.";
  32. static constexpr char StatusMatchmakingMatchFound[] = "Match found!";
  33. static constexpr char StatusMatchmakingFailedToReceiveTicket[] = "Failed to receive matchmaking ticket.\nAre server fleets running?";
  34. static constexpr char StatusMatchmakingFailedToReceiveStatusUpdate[] = "Failed to receive status update.\nMake sure you are connected to the internet and try again.";
  35. static constexpr char StatusMatchmakingTimedOut[] = "Matchmaking timed out!\nReport ticket id to a developer.";
  36. static constexpr char StatusMatchmakingFailedReasonUnknown[] = "Matchmaking failed.\nReason unknown.";
  37. public:
  38. AZ_COMPONENT(MPSGameLift::UiGameLiftFlexMatchConnect, "{EFB9D394-8134-400F-B751-42BA81CD08A7}");
  39. /*
  40. * Reflects component data into the reflection contexts, including the serialization, edit, and behavior contexts.
  41. */
  42. static void Reflect(AZ::ReflectContext* context);
  43. UiGameLiftFlexMatchConnect();
  44. protected:
  45. void Activate() override;
  46. void Deactivate() override;
  47. private:
  48. // AWSClientAuth::AWSCognitoAuthorizationNotificationBus::Handler overrides...
  49. void OnRequestAWSCredentialsSuccess(const AWSClientAuth::ClientAuthAWSCredentials& awsCredentials) override;
  50. void OnRequestAWSCredentialsFail(const AZStd::string& error) override;
  51. // RequestLatenciesCompleteEventHandler...
  52. void OnRequestLatenciesComplete(const RegionalLatencies& regionLatencies);
  53. RequestLatenciesCompleteEvent::Handler m_requestLatenciesComplete{ [this](const RegionalLatencies& regionLatencies) { OnRequestLatenciesComplete(regionLatencies); }};
  54. // Listen for matchmaking events...
  55. MatchmakingSuccessEvent::Handler m_onMatchmakingSuccess{ [this]()
  56. {
  57. PushStatusUpdate(StatusMatchmakingMatchFound);
  58. } };
  59. MatchmakingFailedEvent::Handler m_onMatchmakingFailed;
  60. MatchmakingTicketReceivedEvent::Handler m_onMatchmakingTicketReceived{ [this](AZStd::string ticketId)
  61. {
  62. PushStatusUpdate(AZStd::string::format(StatusMatchmakingTicketReceived, ticketId.c_str()));
  63. } };
  64. // Listen for disconnect events to know if connecting to the host server failed
  65. void OnConnectToHostFailed();
  66. Multiplayer::EndpointDisconnectedEvent::Handler m_onConnectToHostFailed{ [this]([[maybe_unused]] Multiplayer::MultiplayerAgentType agent) { OnConnectToHostFailed(); } };
  67. void OnQuitClicked();
  68. void OnConnectClicked();
  69. void OnConnectionFailedAcknowledged();
  70. void PushStatusUpdate(const AZStd::string& statusUpdate);
  71. void ReplaceStatusUpdate(const AZStd::string& statusUpdate);
  72. void PushStatusFail(const AZStd::string& reason);
  73. void RenderStatusText();
  74. AZ::EntityId m_connectButtonUi;
  75. AZ::EntityId m_matchmakingStatusTextUi;
  76. AZ::EntityId m_quitButtonUi;
  77. AZ::EntityId m_attemptConnectionBlockerUi;
  78. AZ::EntityId m_connectToHostFailedUi;
  79. Multiplayer::SessionConnectionConfig m_sessionConnectionConfig;
  80. AZStd::string m_region;
  81. AZStd::vector<AZStd::string> m_statusUpdates;
  82. };
  83. } // namespace MultiplayerSample