UiGameLiftConnectWithPlayerSessionData.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #include <Components/UI/UiGameLiftConnectWithPlayerSessionData.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <LyShine/Bus/UiButtonBus.h>
  12. #include <LyShine/Bus/UiCursorBus.h>
  13. #include <LyShine/Bus/UiElementBus.h>
  14. #include <LyShine/Bus/UiTextBus.h>
  15. #include <LyShine/Bus/UiTextInputBus.h>
  16. #include <LyShine/Bus/UiInteractableBus.h>
  17. #include <Multiplayer/Session/SessionRequests.h>
  18. #include <Request/AWSGameLiftRequestBus.h>
  19. #include <Request/AWSGameLiftSessionRequestBus.h>
  20. #include <AzCore/Jobs/JobContext.h>
  21. #include <AzCore/Jobs/JobFunction.h>
  22. #include <Multiplayer/Session/ISessionHandlingRequests.h>
  23. #include <Framework/Util.h>
  24. namespace MPSGameLift
  25. {
  26. void UiGameLiftConnectWithPlayerSessionData::Reflect(AZ::ReflectContext* context)
  27. {
  28. if (const auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  29. {
  30. serializeContext->Class<UiGameLiftConnectWithPlayerSessionData, AZ::Component>()
  31. ->Version(1)
  32. ->Field("ConnectButton", &UiGameLiftConnectWithPlayerSessionData::m_connectButtonUi)
  33. ->Field("ExitButton", &UiGameLiftConnectWithPlayerSessionData::m_quitButtonUi)
  34. ->Field("PlayerSessionDataInputUi", &UiGameLiftConnectWithPlayerSessionData::m_playerSessionDataJsonInputUi)
  35. ->Field("AttemptConnectionBlockerUi", &UiGameLiftConnectWithPlayerSessionData::m_attemptConnectionBlockerUi)
  36. ->Field("ConnectToHostFailedUi", &UiGameLiftConnectWithPlayerSessionData::m_connectToHostFailedUi)
  37. ->Field("JsonParseFailTextUi", &UiGameLiftConnectWithPlayerSessionData::m_jsonParseFailTextUi)
  38. ;
  39. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  40. {
  41. editContext->Class<UiGameLiftConnectWithPlayerSessionData>("UiGameLiftConnectWithPlayerSessionData", "Component to setup the start menu")
  42. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  43. ->Attribute(AZ::Edit::Attributes::Category, "Multiplayer Sample UI")
  44. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Component_Placeholder.svg")
  45. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("CanvasUI"))
  46. ->DataElement(AZ::Edit::UIHandlers::Default, &UiGameLiftConnectWithPlayerSessionData::m_connectButtonUi, "Connect Button", "The UI button hosting a game (only available for unified launchers which can run as a client-host).")
  47. ->DataElement(AZ::Edit::UIHandlers::Default, &UiGameLiftConnectWithPlayerSessionData::m_quitButtonUi, "Quit Button", "The UI button to quit the app.")
  48. ->DataElement(AZ::Edit::UIHandlers::Default, &UiGameLiftConnectWithPlayerSessionData::m_playerSessionDataJsonInputUi, "GameLift Player Session Text Input", "The UI text input providing the game session and player session id.")
  49. ->DataElement(AZ::Edit::UIHandlers::Default, &UiGameLiftConnectWithPlayerSessionData::m_attemptConnectionBlockerUi, "Attempt Connection Blocker", "Fullscreen UI for blocking user input while the client tries to connect.")
  50. ->DataElement(AZ::Edit::UIHandlers::Default, &UiGameLiftConnectWithPlayerSessionData::m_connectToHostFailedUi, "Connection To Host Failed", "UI to inform the user that connecting to the host failed.")
  51. ->DataElement(AZ::Edit::UIHandlers::Default, &UiGameLiftConnectWithPlayerSessionData::m_jsonParseFailTextUi, "Json Parse Fail Text", "UI to inform the user that current JSON string is missing some expected data.")
  52. ;
  53. }
  54. }
  55. }
  56. void UiGameLiftConnectWithPlayerSessionData::Activate()
  57. {
  58. UiCursorBus::Broadcast(&UiCursorInterface::IncrementVisibleCounter);
  59. // Listen for button presses
  60. UiButtonBus::Event(m_quitButtonUi, &UiButtonInterface::SetOnClickCallback, [this](AZ::EntityId buttonEntityId, [[maybe_unused]] AZ::Vector2 position) { OnButtonClicked(buttonEntityId); });
  61. UiButtonBus::Event(m_connectButtonUi, &UiButtonInterface::SetOnClickCallback, [this](AZ::EntityId buttonEntityId, [[maybe_unused]] AZ::Vector2 position) { OnButtonClicked(buttonEntityId); });
  62. UiButtonBus::Event(m_connectToHostFailedUi, &UiButtonInterface::SetOnClickCallback, [this](AZ::EntityId buttonEntityId, [[maybe_unused]] AZ::Vector2 position) { OnButtonClicked(buttonEntityId); });
  63. UiTextInputBus::Event(m_playerSessionDataJsonInputUi, &UiTextInputInterface::SetOnChangeCallback, [this]([[maybe_unused]] AZ::EntityId entityId, const AZStd::string& gameLiftJsonString) { OnJSONChanged(gameLiftJsonString); });
  64. // Hide the attempting connection ui until the player tries to connect
  65. UiElementBus::Event(m_attemptConnectionBlockerUi, &UiElementInterface::SetIsEnabled, false);
  66. // Listen for disconnect events to know if connecting to the host server failed
  67. AZ::Interface<Multiplayer::IMultiplayer>::Get()->AddEndpointDisconnectedHandler(m_onConnectToHostFailed);
  68. UiElementBus::Event(m_jsonParseFailTextUi, &UiElementInterface::SetIsEnabled, true);
  69. UiTextBus::Event(m_jsonParseFailTextUi, &UiTextInterface::SetText, "");
  70. UiInteractableBus::Event(m_connectButtonUi, &UiInteractableInterface::SetIsHandlingEvents, false);
  71. OnJSONChanged("");
  72. }
  73. void UiGameLiftConnectWithPlayerSessionData::Deactivate()
  74. {
  75. m_onConnectToHostFailed.Disconnect();
  76. UiCursorBus::Broadcast(&UiCursorInterface::DecrementVisibleCounter);
  77. }
  78. void UiGameLiftConnectWithPlayerSessionData::OnJSONChanged(const AZStd::string& gameLiftJsonString)
  79. {
  80. // Disable the connect button until checking to make sure the user has provided the proper GameLift information in JSON format
  81. UiInteractableBus::Event(m_connectButtonUi, &UiInteractableInterface::SetIsHandlingEvents, false);
  82. if (gameLiftJsonString.empty())
  83. {
  84. UiTextBus::Event(m_jsonParseFailTextUi, &UiTextInterface::SetText, "Please provide GameLift player connection information in JSON format!");
  85. return;
  86. }
  87. // Parse GameLift JSON
  88. m_sessionConnectionConfig = {};
  89. m_region.clear();
  90. rapidjson::Document document;
  91. document.Parse(gameLiftJsonString.c_str());
  92. if (document.HasParseError())
  93. {
  94. UiTextBus::Event(m_jsonParseFailTextUi, &UiTextInterface::SetText, "Invalid JSON format!");
  95. return;
  96. }
  97. // Extract the AWS region from either a fleet arn or game session arn
  98. AZStd::string gameLiftArn;
  99. if (document.HasMember("GameSessionId"))
  100. {
  101. // Example game session format: "arn:aws:gamelift:us-west-2::gamesession<id>"
  102. const rapidjson::Value& gameSessionId = document["GameSessionId"];
  103. gameLiftArn = gameSessionId.GetString();
  104. }
  105. else if (document.HasMember("FleetArn"))
  106. {
  107. // Example fleet arn format: "arn:aws:gamelift:us-west-2:353687041169:fleet<id>"
  108. const rapidjson::Value& fleetArn = document["FleetArn"];
  109. gameLiftArn = fleetArn.GetString();
  110. }
  111. m_region = AWSCore::Util::ExtractRegion(gameLiftArn);
  112. if (m_region.empty())
  113. {
  114. UiTextBus::Event(m_jsonParseFailTextUi, &UiTextInterface::SetText, "Failed to extract AWS region. Provide either a valid GameSessionId or FleetArn!");
  115. return;
  116. }
  117. // Alert the user if any other information is missing from the JSON they provided
  118. if (!document.HasMember("PlayerSessionId"))
  119. {
  120. UiTextBus::Event(m_jsonParseFailTextUi, &UiTextInterface::SetText, "Missing PlayerSessionId!");
  121. return;
  122. }
  123. if (!document.HasMember("IpAddress") && !document.HasMember("DnsName"))
  124. {
  125. UiTextBus::Event(m_jsonParseFailTextUi, &UiTextInterface::SetText, "Must provide either an IpAddress or DnsName!");
  126. return;
  127. }
  128. if (!document.HasMember("Port"))
  129. {
  130. UiTextBus::Event(m_jsonParseFailTextUi, &UiTextInterface::SetText, "Missing Port!");
  131. return;
  132. }
  133. const rapidjson::Value& port = document["Port"];
  134. if (!port.IsUint())
  135. {
  136. UiTextBus::Event(m_jsonParseFailTextUi, &UiTextInterface::SetText, "Invalid Port!");
  137. return;
  138. }
  139. // Fill out SessionConnectionConfig and try connecting to host
  140. if (document.HasMember("IpAddress"))
  141. {
  142. const rapidjson::Value& ipAddress = document["IpAddress"];
  143. m_sessionConnectionConfig.m_ipAddress = ipAddress.GetString();
  144. }
  145. if (document.HasMember("DnsName"))
  146. {
  147. const rapidjson::Value& dnsName = document["DnsName"];
  148. m_sessionConnectionConfig.m_dnsName = dnsName.GetString();
  149. }
  150. const rapidjson::Value& playerSessionId = document["PlayerSessionId"];
  151. m_sessionConnectionConfig.m_port = aznumeric_cast<uint16_t>(port.GetUint());
  152. m_sessionConnectionConfig.m_playerSessionId = playerSessionId.GetString();
  153. UiTextBus::Event(m_jsonParseFailTextUi, &UiTextInterface::SetText, "");
  154. UiInteractableBus::Event(m_connectButtonUi, &UiInteractableInterface::SetIsHandlingEvents, true);
  155. }
  156. void UiGameLiftConnectWithPlayerSessionData::OnButtonClicked(AZ::EntityId buttonEntityId) const
  157. {
  158. const auto console = AZ::Interface<AZ::IConsole>::Get();
  159. if (!console)
  160. {
  161. AZ_Assert(false, "UiGameLiftConnectWithPlayerSessionData attempting to use console commands before AZ::Console is available.");
  162. return;
  163. }
  164. if (buttonEntityId == m_quitButtonUi)
  165. {
  166. console->PerformCommand("quit");
  167. return;
  168. }
  169. if (buttonEntityId == m_connectButtonUi)
  170. {
  171. // Enable blocker ui while we attempt connection
  172. UiElementBus::Event(m_attemptConnectionBlockerUi, &UiElementInterface::SetIsEnabled, true);
  173. // Enable GameLift and connect to host
  174. AWSGameLift::AWSGameLiftRequestBus::Broadcast(&AWSGameLift::AWSGameLiftRequestBus::Events::ConfigureGameLiftClient, m_region);
  175. if (auto clientRequestHandler = AZ::Interface<Multiplayer::ISessionHandlingClientRequests>::Get())
  176. {
  177. clientRequestHandler->RequestPlayerJoinSession(m_sessionConnectionConfig);
  178. }
  179. else
  180. {
  181. AZ_Assert(false, "UiGameLiftConnectWithPlayerSessionData failed to connect because there's no ISessionHandlingClientRequests registered. "
  182. "Please update code to ensure an ISessionHandlingClientRequests has been created before trying to connect this client to a host!");
  183. }
  184. }
  185. if (buttonEntityId == m_connectToHostFailedUi)
  186. {
  187. // Player acknowledged connection failed. Close the warning popup.
  188. UiElementBus::Event(m_connectToHostFailedUi, &UiElementInterface::SetIsEnabled, false);
  189. }
  190. }
  191. void UiGameLiftConnectWithPlayerSessionData::OnConnectToHostFailed()
  192. {
  193. UiElementBus::Event(m_attemptConnectionBlockerUi, &UiElementInterface::SetIsEnabled, false);
  194. UiElementBus::Event(m_connectToHostFailedUi, &UiElementInterface::SetIsEnabled, true);
  195. }
  196. } // namespace MultiplayerSample