Advertisement
BorrowTheProgrammer

UGameInstance

Dec 12th, 2022
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.93 KB | None | 0 0
  1. PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore",
  2.             "HeadMountedDisplay", "UMG", "XmlParser", "OnlineSubsystemEOS", "OnlineSubsystem", "EngineSettings", "EOSSDK" });
  3.  
  4. #include "Lab4GameInstance.h"
  5.  
  6. #include <eos_auth.h>
  7.  
  8. #include "EOSSettings.h"
  9. #include "eos_init.h"
  10. #include "Blueprint/UserWidget.h"
  11. #include "Interfaces/OnlineIdentityInterface.h"
  12. #include "Kismet/GameplayStatics.h"
  13. #include "Lab4/Lab4Character.h"
  14. #include "Lab4/Actors/MainMenuInitializer.h"
  15. #include "Engine/Engine.h"
  16. #include "GameFramework/GameModeBase.h"
  17. #include "GameFramework/PlayerState.h"
  18. #include "Lab4/Lab4GameMode.h"
  19. #include "Lab4/UserWidgets/GameOverMenu.h"
  20. #include "Lab4/UserWidgets/WinnerWiddget.h"
  21.  
  22. void ULab4GameInstance::Init()
  23. {
  24.     Super::Init();
  25.  
  26.     bIsLanGame = false;
  27.  
  28.     // Получить адрес подсистемы
  29.     OnlineSubsystem = IOnlineSubsystem::Get();
  30.    
  31.     if (OnlineSubsystem == nullptr) return;
  32.  
  33.     // Получить адрес API сессий
  34.     SessionPtr = OnlineSubsystem->GetSessionInterface();
  35.    
  36.     if(!SessionPtr.IsValid()) return;
  37.    
  38.     SessionPtr->OnCreateSessionCompleteDelegates.AddUObject(this, &ULab4GameInstance::OnCreateSessionComplete);
  39.     SessionPtr->OnDestroySessionCompleteDelegates.AddUObject(this, &ULab4GameInstance::OnDestroySessionComplete);
  40.     SessionPtr->OnJoinSessionCompleteDelegates.AddUObject(this, &ULab4GameInstance::OnJoinSessionComplete);
  41.     SessionPtr->OnFindSessionsCompleteDelegates.AddUObject(this, &ULab4GameInstance::OnFindSessionsComplete);
  42.  
  43.     if (GEngine != nullptr)
  44.     {
  45.         GEngine->OnNetworkFailure().AddUObject(this, &ULab4GameInstance::OnNetworkFailure);
  46.         UE_LOG(LogTemp, Error, TEXT("Ну удалось подключиться к удаленной игровой сессии"));
  47.        
  48.     }
  49.  
  50.     TickDelegateHandle = FTicker::GetCoreTicker().AddTicker(FTickerDelegate::CreateUObject(this, &ULab4GameInstance::Tick), 0.1f);
  51. }
  52.  
  53. void ULab4GameInstance::Shutdown()
  54. {
  55.     FTicker::GetCoreTicker().RemoveTicker(TickDelegateHandle);
  56.     Super::Shutdown();
  57. }
  58.  
  59. bool ULab4GameInstance::Tick(float DeltaSeconds)
  60. {
  61.     if (PlatformInterface != nullptr)
  62.     {
  63.         EOS_Platform_Tick(PlatformInterface);
  64.     }
  65.     return true;
  66. }
  67.  
  68.  
  69. void ULab4GameInstance::ConnectMainMenuInitializer(AMainMenuInitializer* const pInitializer)
  70. {
  71.     m_pMainMenu = pInitializer;
  72. }
  73.  
  74. FString ULab4GameInstance::GetPlayerName()
  75. {
  76.     return m_PlayerName;
  77. }
  78.  
  79. ULab4GameInstance::ULab4GameInstance()
  80. {
  81.     bWasLoggedIn = false;
  82.     bShouldBePaused = false;
  83.    
  84.     static ConstructorHelpers::FClassFinder<UUserWidget> BPInGameMenu(TEXT("/Game/MainMenu/WBP_ToMainMenu"));
  85.     BPInGameMenuClass = BPInGameMenu.Class;
  86.  
  87.     static ConstructorHelpers::FClassFinder<UUserWidget> BPGameOver(TEXT("/Game/MainMenu/WBP_GameOverMenu"));
  88.     if (BPGameOver.Succeeded())
  89.     {
  90.         BPGameOverMenu = BPGameOver.Class;
  91.     }
  92.  
  93.     static ConstructorHelpers::FClassFinder<UUserWidget> BPHealthBar(TEXT("/Game/GameUI/WBP_HealthBar"));
  94.  
  95.     if (BPHealthBar.Succeeded())
  96.     {
  97.         BPHealthBarClass = BPHealthBar.Class;
  98.     }
  99.    
  100.     static ConstructorHelpers::FClassFinder<UUserWidget> BPWinnerWidgetClassFinder(TEXT("/Game/MainMenu/WBP_WinnerWidget"));
  101.  
  102.     if (BPWinnerWidgetClassFinder.Succeeded())
  103.     {
  104.         BPWinnerWidgetClass = BPWinnerWidgetClassFinder.Class;
  105.     }
  106.    
  107. }
  108.  
  109. void ULab4GameInstance::SetupWinnerWidget(FString WinnerName)
  110. {
  111.     if (BPWinnerWidgetClass == nullptr) return;
  112.    
  113.     UWinnerWiddget* p_WinnerWidget = CreateWidget<UWinnerWiddget>(this, BPWinnerWidgetClass);
  114.     p_WinnerWidget->SetupWinnerWidget();
  115.     p_WinnerWidget->SetupWinnerName(WinnerName);
  116. }
  117.  
  118. void ULab4GameInstance::SetPlayerName(const FString& Name)
  119. {
  120.     m_PlayerName = Name;
  121. }
  122.  
  123. void ULab4GameInstance::SetServerName(const FString& SessionName)
  124. {
  125.     m_ServerName = FName(SessionName);
  126. }
  127.  
  128. void ULab4GameInstance::SetJoinIndex(const uint32 Index)
  129. {
  130.     m_JoinIndex = Index;
  131. }
  132.  
  133. void ULab4GameInstance::AddCharacter(ALab4Character* const Character, const FString& Name)
  134. {
  135.     m_PlayerNames.Add(Name);
  136.     m_Characters.Add(Character);
  137.  
  138.     for (int i = 0; i < m_Characters.Num(); ++i)
  139.     {
  140.         m_Characters[i]->SetPlayerNames(m_PlayerNames);
  141.     }
  142. }
  143.  
  144. void ULab4GameInstance::RemoveCharacter(ALab4Character* const Character)
  145. {
  146.     const int32 Index = m_Characters.Find(Character);
  147.  
  148.     if (Index == INDEX_NONE) return;
  149.  
  150.     m_Characters.RemoveAt(Index);
  151.     m_PlayerNames.RemoveAt(Index);
  152.  
  153.     for (int i = 0; i < m_Characters.Num(); ++i)
  154.     {
  155.         m_Characters[i]->SetPlayerNames(m_PlayerNames);
  156.     }
  157. }
  158.  
  159. void ULab4GameInstance::Host() const
  160. {
  161.     if (!SessionPtr.IsValid()) return;
  162.  
  163.     if (!bIsLanGame)
  164.     {
  165.         if (!bWasLoggedIn) return;
  166.     }
  167.    
  168.     const FNamedOnlineSession *pOnlineSession = SessionPtr->GetNamedSession(SessionNameConst);
  169.  
  170.     if (pOnlineSession != nullptr)
  171.     {
  172.         SessionPtr->DestroySession(SessionNameConst);
  173.     }
  174.     else
  175.     {
  176.         CreateSession();
  177.     }
  178. }
  179.  
  180. void ULab4GameInstance::Join() const
  181. {
  182.     if (m_pMainMenu != nullptr)
  183.     {
  184.         m_pMainMenu->TeardownAll();
  185.     }
  186.    
  187.     if (bIsLanGame)
  188.     {
  189.         if (!SessionPtr.IsValid()) return;
  190.    
  191.         if (!m_pSessionSearch.IsValid()) return;
  192.  
  193.         SessionPtr->JoinSession(0, SessionNameConst, m_pSessionSearch->SearchResults[m_JoinIndex]);
  194.         return;
  195.     }
  196.    
  197.     if (!SessionPtr.IsValid()) return;
  198.    
  199.     if (!SearchSettings.IsValid()) return;
  200.  
  201.     SessionPtr->JoinSession(0, SessionNameConst, SearchSettings->SearchResults[m_JoinIndex]);
  202.     UE_LOG(LogTemp, Warning, TEXT("Joining session..."));
  203. }
  204.  
  205. void ULab4GameInstance::RefreshServersList()
  206. {
  207.     if (bIsLanGame)
  208.     {
  209.         m_pSessionSearch = MakeShareable(new FOnlineSessionSearch());
  210.  
  211.         if (m_pSessionSearch == nullptr) return;
  212.            
  213.         m_pSessionSearch->bIsLanQuery = true;
  214.         m_pSessionSearch->QuerySettings.Set(SEARCH_PRESENCE, true, EOnlineComparisonOp::Equals);
  215.         SessionPtr->FindSessions(0, m_pSessionSearch.ToSharedRef());
  216.         return;
  217.     }
  218.    
  219.     SearchSettings = MakeShareable(new FOnlineSessionSearch());
  220.    
  221.     if (SearchSettings.IsValid())
  222.     {
  223.         SearchSettings->QuerySettings.Set(SEARCH_KEYWORDS, FString("Test session"), EOnlineComparisonOp::Equals);
  224.         SearchSettings->QuerySettings.Set(SEARCH_LOBBIES, true, EOnlineComparisonOp::Equals);      
  225.         SessionPtr->FindSessions(0,SearchSettings.ToSharedRef());
  226.     }
  227. }
  228.  
  229. bool ULab4GameInstance::ChangeConfigToOnline()
  230. {
  231.     return true;
  232. }
  233.  
  234. bool ULab4GameInstance::ChangeConfigToLan()
  235. {  
  236.  
  237.     return true;
  238. }
  239.  
  240. void ULab4GameInstance::OnCreateSessionComplete(FName SessionName, bool Success)
  241. {
  242.     UE_LOG(LogTemp, Warning, TEXT("Success: %d"), Success);
  243.     GetWorld()->ServerTravel(TravelGamePath);
  244.    
  245.     if (!Success) return;
  246.    
  247.     if (m_pMainMenu != nullptr)
  248.     {
  249.         m_pMainMenu->TeardownAll();
  250.     }
  251.  
  252.     SessionPtr->ClearOnCreateSessionCompleteDelegates(this);
  253. }
  254.  
  255. void ULab4GameInstance::OnDestroySessionComplete(FName SessionName, bool Success)
  256. {
  257.     UE_LOG(LogTemp, Warning, TEXT("Destroying Session Success %d"), Success);
  258.    
  259.     if (!Success) return;
  260.  
  261.     if (OnlineSubsystem == nullptr) return;
  262.  
  263.     SessionPtr = OnlineSubsystem->GetSessionInterface();
  264.  
  265.     SessionPtr->ClearOnDestroySessionCompleteDelegates(this);
  266.     //CreateSession();
  267. }
  268.  
  269. void ULab4GameInstance::OnFindSessionsComplete(bool Success)
  270. {
  271.     if (Success && m_pSessionSearch.IsValid() && m_pMainMenu != nullptr)
  272.     {
  273.         TArray<FString> ServersList;
  274.        
  275.         for (const FOnlineSessionSearchResult& Session : m_pSessionSearch->SearchResults)
  276.         {
  277.             FString ServerName;
  278.             Session.Session.SessionSettings.Get(ServerNameKey, ServerName);
  279.             ServersList.Add(ServerName);
  280.         }
  281.  
  282.         m_pMainMenu->OnInstanceFoundServers(ServersList);
  283.     }
  284. }
  285.  
  286. void ULab4GameInstance::OnJoinSessionComplete(FName Name, EOnJoinSessionCompleteResult::Type Result)
  287. {
  288.     FString Address;
  289.  
  290.     if (Result == EOnJoinSessionCompleteResult::Type::Success)
  291.     {
  292.         UE_LOG(LogTemp, Warning, TEXT("Join Session Success %d"), Result);
  293.     } else
  294.     {
  295.         UE_LOG(LogTemp, Error, TEXT("Error, while joining online session"));
  296.     }
  297.    
  298.     if (OnlineSubsystem == nullptr) return;
  299.  
  300.     SessionPtr = OnlineSubsystem->GetSessionInterface();
  301.  
  302.     if (SessionPtr == nullptr) return;
  303.    
  304.     SessionPtr->GetResolvedConnectString(Name, Address);
  305.  
  306.     if (Address.IsEmpty()) return;
  307.    
  308.     //GetFirstLocalPlayerController()->ClientTravel(Address, ETravelType::TRAVEL_Absolute);
  309.     if (APlayerController *PC = UGameplayStatics::GetPlayerController(GetWorld(), 0))
  310.     {
  311.         PC->ClientTravel(Address, ETravelType::TRAVEL_Absolute);
  312.     }
  313. }
  314.  
  315. void ULab4GameInstance::OnNetworkFailure(UWorld* World, UNetDriver* NetDriver, ENetworkFailure::Type FailureType, const FString& ErrorString)
  316. {
  317.     UE_LOG(LogTemp, Error, TEXT("Ошибка запуска сессии/поключения к сессии"))
  318.     LoadMainMenu();
  319. }
  320.  
  321. void ULab4GameInstance::LogIn()
  322. {
  323.     if (OnlineSubsystem == nullptr) return;
  324.     IOnlineIdentityPtr IdentyPtr = OnlineSubsystem->GetIdentityInterface();
  325.  
  326.     if (IdentyPtr == nullptr) return;
  327.  
  328.     FOnlineAccountCredentials AccountCredentials;
  329.     AccountCredentials.Id = FString("");
  330.     AccountCredentials.Token = FString("");
  331.     AccountCredentials.Type = FString("accountportal");
  332.  
  333.     IdentyPtr->OnLoginCompleteDelegates->AddUObject(this, &ULab4GameInstance::OnLoginComplete);
  334.     IdentyPtr->Login(0, AccountCredentials);
  335. }
  336.  
  337. void ULab4GameInstance::InitializeSDK()
  338. {
  339.     EOS_InitializeOptions SDKOptions;
  340.     SDKOptions.ApiVersion = EOS_INITIALIZE_API_LATEST;
  341.     SDKOptions.AllocateMemoryFunction = nullptr;
  342.     SDKOptions.ReallocateMemoryFunction = nullptr;
  343.     SDKOptions.ReleaseMemoryFunction = nullptr;
  344.     SDKOptions.ProductName = "OnlineMultiplayerShooter";
  345.     SDKOptions.ProductVersion = "1.0";
  346.     SDKOptions.Reserved = nullptr;
  347.     SDKOptions.SystemInitializeOptions = nullptr;
  348.     EOS_EResult SDKInitializeResult = EOS_Initialize(&SDKOptions);
  349.  
  350.     if (SDKInitializeResult == EOS_EResult::EOS_Success)
  351.     {
  352.         UE_LOG(LogTemp, Warning, TEXT("SDK success"));
  353.     } else if (SDKInitializeResult == EOS_EResult::EOS_AlreadyConfigured)
  354.     {
  355.         UE_LOG(LogTemp, Warning, TEXT("SDK already configured"));
  356.     }
  357.     else
  358.     {
  359.         UE_LOG(LogTemp, Warning, TEXT("SDK another error"));
  360.     }
  361. }
  362.  
  363. void ULab4GameInstance::InitializePlatformInterface()
  364. {
  365.     EOS_Platform_Options PlatformOptions;
  366.     PlatformOptions.ApiVersion = EOS_PLATFORM_OPTIONS_API_LATEST;
  367.     PlatformOptions.Reserved = nullptr;
  368.     PlatformOptions.bIsServer = EOS_FALSE;
  369.     PlatformOptions.EncryptionKey = "1111111111111111111111111111111111111111111111111111111111111111";
  370.     PlatformOptions.OverrideCountryCode = nullptr;
  371.     PlatformOptions.OverrideLocaleCode = nullptr;
  372.     PlatformOptions.Flags = 0;
  373.     PlatformOptions.CacheDirectory = nullptr;
  374.     PlatformOptions.ProductId = "8bd04340aae74d73b3ea092484309e0d";
  375.     PlatformOptions.SandboxId = "080960589ec84e44aaf746e6c31bd352";
  376.     PlatformOptions.DeploymentId = "0a33e5dadf2c410281355e84fe717854";
  377.     PlatformOptions.ClientCredentials.ClientId = "xyza7891o7wcWqcrWKfjyH216I7wCdi8";
  378.     PlatformOptions.ClientCredentials.ClientSecret = "xXOokYk3IXQztbURw2G2EcD+bf84hrwHOkq+aRcj+VU";
  379.     PlatformOptions.RTCOptions = nullptr;
  380.     PlatformInterface = EOS_Platform_Create(&PlatformOptions);
  381.  
  382.     if (PlatformInterface != nullptr)
  383.     {
  384.         UE_LOG(LogTemp, Warning, TEXT("PlatformInterface's been created"));
  385.     }
  386.     else
  387.     {
  388.         UE_LOG(LogTemp, Error, TEXT("Error while getting Platform Interface"));
  389.     }
  390. }
  391.  
  392. void ULab4GameInstance::InitializeAuthInterface()
  393. {
  394.     AuthInterface = EOS_Platform_GetAuthInterface(PlatformInterface);
  395.     FString Token;
  396.     FParse::Value(FCommandLine::Get(), TEXT("AUTH_PASSWORD"), Token);
  397.    
  398.     if (AuthInterface == nullptr)
  399.     {
  400.         UE_LOG(LogTemp, Error, TEXT("AuthInterface has not been initialized"));
  401.         return;
  402.     }
  403.    
  404.     EOS_Auth_LoginOptions LoginOptions;
  405.     EOS_Auth_Credentials AuthCredentials;
  406.    
  407.     LoginOptions.ApiVersion = EOS_AUTH_LOGIN_API_LATEST;
  408.     AuthCredentials.Id = "pavel.ezzzz@gmail.com";
  409.     AuthCredentials.Token = "aezakmi2022";
  410.     AuthCredentials.ApiVersion = EOS_AUTH_CREDENTIALS_API_LATEST;
  411.     AuthCredentials.Type = EOS_ELoginCredentialType::EOS_LCT_Password;
  412.     AuthCredentials.SystemAuthCredentialsOptions = nullptr;
  413.     LoginOptions.Credentials = &AuthCredentials;
  414.  
  415.     EOS_Auth_Login(AuthInterface, &LoginOptions, nullptr, &CompletionDelegate);
  416. }
  417.  
  418. void ULab4GameInstance::LoginViaSDK()
  419. {
  420.     InitializeSDK();
  421.     InitializePlatformInterface();
  422.     InitializeAuthInterface();
  423. }
  424.  
  425. void EOS_CALL ULab4GameInstance::CompletionDelegate(const EOS_Auth_LoginCallbackInfo* Data)
  426. {
  427.     if (Data->ResultCode == EOS_EResult::EOS_InvalidUser) {
  428.         UE_LOG(LogTemp, Warning, TEXT("Authentication error"));
  429.         GEngine->AddOnScreenDebugMessage(-1,
  430.             3.f,
  431.             FColor::Red,
  432.             FString::Printf(TEXT("JWT token is invalid")),
  433.             true,
  434.             FVector2D(3.f)
  435.             );
  436.     }
  437.     if (Data->ResultCode == EOS_EResult::EOS_Success) {
  438.         UE_LOG(LogTemp, Warning, TEXT("User authenticated successfully"));
  439.         GEngine->AddOnScreenDebugMessage(-1,
  440.         3.f,
  441.         FColor::Green,
  442.         FString::Printf(TEXT("Logged in successfully")),
  443.         true,
  444.         FVector2D(3.f)
  445.         );
  446.         // m_pMainMenu->SetWidgetOnLoginComplete();
  447.         // EOS_ProductUserId(Data->LocalUserId);
  448.     }
  449. }
  450.  
  451.  
  452. void ULab4GameInstance::OnLoginComplete(int32 LocalUserNum, bool bWasSuccessful, const FUniqueNetId& UserId,
  453.                                         const FString& Error)
  454. {
  455.    
  456.     UE_LOG(LogTemp, Warning, TEXT("Logged In: %d"), bWasSuccessful);
  457.    
  458.     bWasLoggedIn = bWasSuccessful;
  459.    
  460.     if (OnlineSubsystem == nullptr) return;
  461.  
  462.     IOnlineIdentityPtr IdentyPtr = OnlineSubsystem->GetIdentityInterface();
  463.  
  464.     if (IdentyPtr == nullptr) return;
  465.  
  466.     IdentyPtr->ClearOnLoginCompleteDelegates(0, this);
  467.  
  468.     if (bWasSuccessful)
  469.     {
  470.         GEngine->AddOnScreenDebugMessage(-1, 3.5f, FColor::Green, FString::Printf(TEXT("Logged In Successfuly!")));
  471.         m_pMainMenu->SetWidgetOnLoginComplete();
  472.     } else
  473.     {
  474.         GEngine->AddOnScreenDebugMessage(-1, 3.5f, FColor::Red, FString::Printf(TEXT("Connetcion Error!")));
  475.     }
  476. }
  477.  
  478. void ULab4GameInstance::FindSessions()
  479. {
  480.     if (OnlineSubsystem == nullptr) return;
  481.  
  482.     if (!bIsLanGame)
  483.     {
  484.         if (bWasLoggedIn == false) return;
  485.  
  486.         SessionPtr = OnlineSubsystem->GetSessionInterface();
  487.  
  488.         if (SessionPtr == nullptr) return;
  489.    
  490.         SearchSettings = MakeShareable(new FOnlineSessionSearch());
  491.         SearchSettings->QuerySettings.Set(SEARCH_KEYWORDS, FString("Test session"), EOnlineComparisonOp::Equals);
  492.         SearchSettings->QuerySettings.Set(SEARCH_LOBBIES, true, EOnlineComparisonOp::Equals);
  493.         SessionPtr->OnFindSessionsCompleteDelegates.AddUObject(this, &ULab4GameInstance::OnFindOnlineSessionsComplete);
  494.         SessionPtr->FindSessions(0, SearchSettings.ToSharedRef());
  495.     }
  496. }
  497.  
  498. void ULab4GameInstance::OnFindOnlineSessionsComplete(const bool bWasSuccessful)
  499. {
  500.     const int32 LobbiesCount = SearchSettings->SearchResults.Num();
  501.    
  502.     UE_LOG(LogTemp, Warning, TEXT("Finding Lobbies Success: %d"), bWasSuccessful);
  503.    
  504.     if (bWasSuccessful)
  505.     {
  506.         UE_LOG(LogTemp, Warning, TEXT("Found %d lobbies"), LobbiesCount);
  507.  
  508.         TArray<FString> ServersList;
  509.        
  510.         for (const FOnlineSessionSearchResult& Session : SearchSettings->SearchResults)
  511.         {
  512.             FString ServerName;
  513.             Session.Session.SessionSettings.Get(ServerNameKey, ServerName);
  514.             ServersList.Add(ServerName);
  515.         }
  516.  
  517.         m_pMainMenu->OnInstanceFoundServers(ServersList);
  518.        
  519.         return;
  520.     }
  521.  
  522.     if (OnlineSubsystem == nullptr) return;
  523.  
  524.     SessionPtr = OnlineSubsystem->GetSessionInterface();
  525.  
  526.     if (SessionPtr == nullptr) return;
  527.  
  528.     SessionPtr->ClearOnFindSessionsCompleteDelegates(this);
  529. }
  530.  
  531. void ULab4GameInstance::OnJoinOnlineSessionComplete(const FName SessionName, EOnJoinSessionCompleteResult::Type Result)
  532. {
  533.    
  534. }
  535.  
  536. void ULab4GameInstance::DestroySession()
  537. {
  538.     if (OnlineSubsystem == nullptr) return;
  539.  
  540.     SessionPtr = OnlineSubsystem->GetSessionInterface();
  541.  
  542.     if (SessionPtr == nullptr) return;
  543.  
  544.     SessionPtr->OnDestroySessionCompleteDelegates.AddUObject(this, &ULab4GameInstance::OnDestroySessionComplete);
  545.     SessionPtr->DestroySession(SessionNameConst);
  546. }
  547.  
  548. void ULab4GameInstance::ShowInGameMenu()
  549. {
  550.     if (BPInGameMenuClass == nullptr) return;
  551.    
  552.     UGameMenu *InGameMenu = CreateWidget<UGameMenu>(this, BPInGameMenuClass);
  553.    
  554.     if (InGameMenu == nullptr)
  555.     {
  556.         UE_LOG(LogTemp, Warning, TEXT("Ошибка создания указателя на игровой меню"));
  557.         return;
  558.     }
  559.    
  560.     InGameMenu->SetupInGameMenu();
  561. }
  562.  
  563. void ULab4GameInstance::ShowGameOverMenu()
  564. {
  565.     if (BPGameOverMenu == nullptr) return;
  566.  
  567.     GameOverWidget = CreateWidget<UGameOverMenu>(this, BPGameOverMenu);
  568.  
  569.     if (GameOverWidget == nullptr) return;
  570.  
  571.     GameOverWidget->SetupGameOverMenu();
  572. }
  573.  
  574. void ULab4GameInstance::CheckGameState()
  575. {
  576.     for (int32 i = 0; i < m_Characters.Num(); ++i)
  577.     {
  578.         ALab4GameMode* MyGameMode = static_cast<ALab4GameMode*>(GetWorld()->GetAuthGameMode());
  579.         if (MyGameMode == nullptr) return;
  580.        
  581.         if (static_cast<int32>(m_Characters[i]->GetPlayerState()->GetScore()) >= FragsToWin)
  582.         {
  583.             bShouldBePaused = true;
  584.             MyGameMode->Pause(m_Characters[i]->GetPlayerName());
  585.         }
  586.     }
  587. }
  588.  
  589. void ULab4GameInstance::RefreshGameState()
  590. {
  591.     UE_LOG(LogTemp, Warning, TEXT("Refreshing..."));
  592.     bShouldBePaused = false;
  593.  
  594.     for (int i = 0; i < m_Characters.Num(); ++i)
  595.     {
  596.         m_Characters[i]->GetPlayerState()->SetScore(0.0f);
  597.         m_Characters[i]->SetCurrentHealth(m_Characters[i]->GetPlayerMaxHealth());
  598.     }
  599. }
  600.  
  601. void ULab4GameInstance::ChangeHealthBarState(float Persantage)
  602. {
  603.     // PlayerHealthBar->SetPersantage(Persantage);
  604. }
  605.  
  606. void ULab4GameInstance::SetIsLanGame(const bool bIsLan)
  607. {
  608.     bIsLanGame = bIsLan;
  609.     IOnlineSubsystem::Get(TEXT("NULL"));
  610. }
  611.  
  612. void ULab4GameInstance::SetIsOnlineGame(const bool bIsLan)
  613. {
  614.     bIsLanGame = bIsLan;
  615.     IOnlineSubsystem::Get(TEXT("EOS"));
  616. }
  617.  
  618. bool ULab4GameInstance::GetIsLanGame() const
  619. {
  620.     return bIsLanGame;
  621. }
  622.  
  623. void ULab4GameInstance::HideGameOverMenu()
  624. {
  625.     GameOverWidget->TeardownGameOverMenu();
  626. }
  627.  
  628. void ULab4GameInstance::CreateSession() const
  629. {
  630.     if (!SessionPtr.IsValid()) return;
  631.    
  632.     FOnlineSessionSettings SessionSettings;
  633.    
  634.     if (IOnlineSubsystem::Get()->GetSubsystemName() == "NULL" || bIsLanGame)
  635.     {
  636.         UE_LOG(LogTemp, Warning, TEXT("%s"), *IOnlineSubsystem::Get()->GetSubsystemName().ToString());
  637.         SessionSettings.bIsLANMatch = true;
  638.         SessionSettings.bUsesPresence = false;
  639.         SessionSettings.bUseLobbiesIfAvailable = false;
  640.     }
  641.     else
  642.     {
  643.         SessionSettings.bIsLANMatch = false;
  644.         SessionSettings.bUsesPresence = true;
  645.         SessionSettings.bUseLobbiesIfAvailable = true;
  646.     }
  647.  
  648.     SessionSettings.bShouldAdvertise = true;
  649.     SessionSettings.NumPublicConnections = 10;
  650.     SessionSettings.bAllowJoinInProgress = true;
  651.    
  652.  
  653.     FOnlineSessionSetting setting;
  654.     setting.Data = m_ServerName.ToString();
  655.     setting.AdvertisementType = EOnlineDataAdvertisementType::ViaOnlineServiceAndPing;
  656.    
  657.     SessionSettings.Set(ServerNameKey, setting);
  658.     SessionSettings.Set(SEARCH_KEYWORDS, FString("Test session"), EOnlineDataAdvertisementType::ViaOnlineService);
  659.    
  660.     const FUniqueNetIdPtr NetID = GetFirstGamePlayer()->GetPreferredUniqueNetId().GetUniqueNetId();
  661.     GetFirstGamePlayer()->SetCachedUniqueNetId(NetID);
  662.    
  663.     SessionPtr->CreateSession(0, SessionNameConst, SessionSettings);
  664. }
  665.  
  666. void ULab4GameInstance::LoadMainMenu() const
  667. {
  668.     UWorld* World = GetWorld();
  669.  
  670.     if (World == nullptr) return;
  671.  
  672.     APlayerController* PlayerController = GetFirstLocalPlayerController();
  673.  
  674.     if (PlayerController == nullptr) return;
  675.    
  676.     PlayerController->ClientTravel(TravelMainMenuPath, ETravelType::TRAVEL_Absolute);
  677. }
  678.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement