Advertisement
Guest User

Untitled

a guest
Jul 14th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. #include "TheLegendsOfArthurInstance.h"
  2. #include "Engine/Engine.h"
  3. #include "UObject/ConstructorHelpers.h"
  4. #include "Blueprint/UserWidget.h"
  5.  
  6. #include "OnlineSessionSettings.h"
  7.  
  8. #include "PlatFormTrigger.h"
  9. #include "Widgets/Login.h"
  10. #include "Widgets/LoginWidget.h"
  11.  
  12. const static FName SESSION_NAME = TEXT("Game");
  13. const static FName SERVER_NAME_SETTINGS_KEY = TEXT("ServerName");
  14.  
  15. UTheLegendsOfArthurInstance::UTheLegendsOfArthurInstance(const FObjectInitializer & ObjectInitializer)
  16. {
  17. ConstructorHelpers::FClassFinder<UUserWidget>LoginBPClass(TEXT("/Game/Assets/widgets/Menu/WDBP_Login"));
  18. if (!ensure(LoginBPClass.Class != nullptr)) return;
  19.  
  20. LoginClass = LoginBPClass.Class;
  21.  
  22. ConstructorHelpers::FClassFinder<UUserWidget>IngameMenuBPClass(TEXT("/Game/Assets/widgets/Menu/ExitOptions"));
  23. if (!ensure(IngameMenuBPClass.Class != nullptr)) return;
  24.  
  25. IngameMenuClass = IngameMenuBPClass.Class;
  26. }
  27.  
  28. void UTheLegendsOfArthurInstance::Init()
  29. {
  30. Super::Init();
  31. GSObj = NewObject<UGameSparksObject>(GetWorld(), UGameSparksObject::StaticClass());
  32. if (!ensure(GSObj != nullptr)) return;
  33. GSMessageListener = NewObject<UGSMessageListenersObject>(GetWorld(), UGSMessageListenersObject::StaticClass());
  34. if (!ensure(GSMessageListener != nullptr)) return;
  35. if (GSObj != nullptr) {
  36.  
  37.  
  38.  
  39. //Set a listener for OnScriptMessage
  40. GSMessageListener->OnScriptMessage.AddDynamic(this, &UTheLegendsOfArthurInstance::onScriptMessage);
  41.  
  42. //Set the OnAvailable delegate
  43. GSObj->OnGameSparksAvailableDelegate.AddDynamic(this, &UTheLegendsOfArthurInstance::onAvailable);
  44. //Disconnected the module just incase it's connected (Refresh)
  45. GSObj->Disconnect();
  46. //Connect module
  47. GSObj->Connect("","", true, false);
  48.  
  49. }
  50. IOnlineSubsystem* SubSystem=IOnlineSubsystem::Get();
  51. if(SubSystem!=nullptr){
  52. UE_LOG(LogTemp, Warning, TEXT("Found Substem %s"), *SubSystem->GetSubsystemName().ToString());
  53. SessionInterface=SubSystem->GetSessionInterface();
  54. if (SessionInterface.IsValid())
  55. {
  56.  
  57. SessionInterface->OnCreateSessionCompleteDelegates.AddUObject(this, &UTheLegendsOfArthurInstance::OnCreateSessionComplete);
  58. SessionInterface->OnDestroySessionCompleteDelegates.AddUObject(this, &UTheLegendsOfArthurInstance::OnCreateSessionComplete);
  59. SessionInterface->OnFindSessionsCompleteDelegates.AddUObject(this, &UTheLegendsOfArthurInstance::OnFindSessionComplete);
  60. SessionInterface->OnJoinSessionCompleteDelegates.AddUObject(this, &UTheLegendsOfArthurInstance::OnJoinSessionComplete);
  61. //Host("ola");
  62. }
  63.  
  64. }
  65. else {
  66. UE_LOG(LogTemp, Warning, TEXT("Found no Substem"));
  67. }
  68. }
  69.  
  70. void UTheLegendsOfArthurInstance::LoadLoginMenu()
  71. {
  72. if (!ensure(LoginClass != nullptr)) return;
  73. loginMenu = CreateWidget<ULogin>(this, LoginClass);
  74. if (!ensure(loginMenu != nullptr)) return;
  75. loginMenu->Setup();
  76. loginMenu->SetMenuInterface(this);
  77.  
  78. }
  79.  
  80. void UTheLegendsOfArthurInstance::LoadIngameMenu()
  81. {
  82. if (!ensure(IngameMenuClass != nullptr)) return;
  83. ULoginWidget* InGameMenu = CreateWidget<ULoginWidget>(this, IngameMenuClass);
  84. if (!ensure(InGameMenu != nullptr)) return;
  85. InGameMenu->Setup();
  86. InGameMenu->SetMenuInterface(this);
  87. }
  88.  
  89. void UTheLegendsOfArthurInstance::LoadMainMenu()
  90. {
  91. APlayerController* PlayerController = GetFirstLocalPlayerController();
  92. if (!ensure(PlayerController != nullptr)) return;
  93. PlayerController->ClientTravel("/Game/Assets/Maps/MainMenu", ETravelType::TRAVEL_Absolute);
  94. }
  95.  
  96.  
  97. void UTheLegendsOfArthurInstance::OnCreateSessionComplete(FName SessionName, bool Sucess) {
  98.  
  99. if (!Sucess)
  100. {
  101. UE_LOG(LogTemp, Warning, TEXT("Could not Create Session"));
  102. return;
  103. }
  104. if (loginMenu != nullptr)
  105. {
  106. loginMenu->TearDown();
  107. }
  108. UEngine* Engine = GetEngine();
  109. if (!ensure(Engine != nullptr)) return;
  110. Engine->AddOnScreenDebugMessage(0, 5, FColor::Red, TEXT("Hosting"));
  111. UWorld* World = GetWorld();
  112. if (!ensure(World != nullptr)) return;
  113. World->ServerTravel("/Game/Assets/Maps/Lobby?listen");
  114. }
  115.  
  116. void UTheLegendsOfArthurInstance::OndestroySessionComplete(FName SessionName, bool Sucess)
  117. {
  118. if (Sucess) {
  119. CreateSession();
  120. }
  121. }
  122.  
  123. void UTheLegendsOfArthurInstance::OnFindSessionComplete(bool Sucess)
  124. {
  125.  
  126.  
  127. if (Sucess && SessionSearch.IsValid() && loginMenu!=nullptr)
  128. {
  129. UE_LOG(LogTemp, Warning, TEXT("Finished Find Session"));
  130.  
  131. TArray<FServerData> ServerName;
  132.  
  133. for (const FOnlineSessionSearchResult& SearchResult : SessionSearch->SearchResults) {
  134. UE_LOG(LogTemp, Warning, TEXT("Found Session Name %s"),*SearchResult.GetSessionIdStr());
  135. FServerData Data;
  136.  
  137. Data.MaxPlayers = SearchResult.Session.SessionSettings.NumPublicConnections;
  138. Data.CurrentPlayers = Data.MaxPlayers- SearchResult.Session.NumOpenPublicConnections;
  139. Data.HostUserName = SearchResult.Session.OwningUserName;
  140. FString ServerNames;
  141. if (SearchResult.Session.SessionSettings.Get(SERVER_NAME_SETTINGS_KEY, ServerNames))
  142. {
  143. Data.Name = ServerNames;
  144. UE_LOG(LogTemp, Warning, TEXT("Data Found Setting "));
  145. }
  146. else
  147. {
  148. Data.Name = "Could Not Find the Server";
  149. // UE_LOG(LogTemp, Warning, TEXT("Data Not Found Setting"));
  150. }
  151. ServerName.Add(Data);
  152. }
  153. loginMenu->SetServerList(ServerName);
  154. }
  155.  
  156. }
  157.  
  158. void UTheLegendsOfArthurInstance::Host(FString Servername)
  159. {
  160. DiresedServername = Servername;
  161. if (SessionInterface.IsValid()) {
  162.  
  163. auto ExistingSession=SessionInterface->GetNamedSession(SESSION_NAME);
  164. if (ExistingSession != nullptr)
  165. {
  166. SessionInterface->DestroySession(SESSION_NAME);
  167. }
  168. else
  169. {
  170. CreateSession();
  171. }
  172.  
  173. }
  174.  
  175. }
  176. void UTheLegendsOfArthurInstance::CreateSession()
  177. {
  178. if (SessionInterface.IsValid()) {
  179. FOnlineSessionSettings SessionSettings;
  180.  
  181. if (IOnlineSubsystem::Get()->GetSubsystemName() == "NULL") {
  182. SessionSettings.bIsLANMatch = true;
  183. }else{
  184. SessionSettings.bIsLANMatch = false;
  185. }
  186.  
  187. SessionSettings.NumPublicConnections = 2;
  188. SessionSettings.bShouldAdvertise = true;
  189. SessionSettings.bUsesPresence = true;
  190. SessionSettings.Set(SERVER_NAME_SETTINGS_KEY, DiresedServername,EOnlineDataAdvertisementType::ViaOnlineServiceAndPing);
  191.  
  192. SessionInterface->CreateSession(0, SESSION_NAME, SessionSettings);
  193.  
  194. }
  195. }
  196. void UTheLegendsOfArthurInstance::RefreshServerList()
  197. {
  198. SessionSearch = MakeShareable(new FOnlineSessionSearch());
  199.  
  200. if (SessionSearch.IsValid())
  201. {
  202. //SessionSearch->bIsLanQuery = true;
  203. SessionSearch->MaxSearchResults = 100;
  204. SessionSearch->QuerySettings.Set(SEARCH_PRESENCE,true, EOnlineComparisonOp::Equals);
  205. // SessionSearch->QuerySettings;
  206. UE_LOG(LogTemp, Warning, TEXT("Starting Find Session"));
  207. SessionInterface->FindSessions(0, SessionSearch.ToSharedRef());
  208. }
  209.  
  210. }
  211.  
  212. void UTheLegendsOfArthurInstance::Join(uint32 Index)
  213. {
  214.  
  215. if (!SessionInterface.IsValid()) return;
  216. if (!SessionSearch.IsValid()) return;
  217. if (loginMenu != nullptr)
  218. {
  219.  
  220.  
  221. loginMenu->TearDown();
  222.  
  223. }
  224.  
  225. SessionInterface->JoinSession(0,GameSessionName,SessionSearch->SearchResults[Index]);
  226.  
  227. }
  228.  
  229. void UTheLegendsOfArthurInstance::StartSession()
  230. {
  231. if (SessionInterface.IsValid())
  232. {
  233. SessionInterface->StartSession(GameSessionName);
  234. }
  235. }
  236.  
  237. void UTheLegendsOfArthurInstance::OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result)
  238. {
  239. if (!SessionInterface.IsValid()) return;
  240. FString Address;
  241. if (!SessionInterface->GetResolvedConnectString(SessionName, Address))
  242. {
  243. UE_LOG(LogTemp, Warning, TEXT("Could not Get Connection"));
  244. return;
  245. }
  246. UEngine* Engine = GetEngine();
  247. if (!ensure(Engine != nullptr)) return;
  248. Engine->AddOnScreenDebugMessage(0, 5, FColor::Red, FString::Printf( TEXT("Joining %s"),*Address));
  249. APlayerController* PlayerController = GetFirstLocalPlayerController();
  250. if (!ensure(PlayerController != nullptr)) return;
  251. PlayerController->ClientTravel(Address,ETravelType::TRAVEL_Absolute);
  252.  
  253.  
  254. }
  255.  
  256. void UTheLegendsOfArthurInstance::onScriptMessage(FGSScriptMessage message) {
  257. //Handle script message
  258.  
  259. GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, TEXT("Message!"));
  260. const FString test = message.Data->GetString("title");
  261. GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, test);
  262. }
  263.  
  264. void UTheLegendsOfArthurInstance::onAvailable(bool available)
  265. {
  266. if (available)
  267. {
  268. //GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, TEXT("Connected"));
  269.  
  270. GameSparks::Core::GS& gs = UGameSparksModule::GetModulePtr()->GetGSInstance();
  271. //UE_LOG(LogTemp, Warning, TEXT("Connected"));
  272.  
  273. }
  274.  
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement