Advertisement
Guest User

Untitled

a guest
May 24th, 2019
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.20 KB | None | 0 0
  1. // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
  2.  
  3. #include "SGGameMode.h"
  4. #include "SGCharacter.h"
  5. #include "Blueprint/UserWidget.h"
  6. #include "UObject/ConstructorHelpers.h"
  7. #include "SIOJConvert.h"
  8. #include "UnrealNetwork.h"
  9. #include "Runtime/UMG/Public/Components/EditableTextBox.h"
  10. #include "Runtime/UMG/Public/Blueprint/WidgetTree.h"
  11.  
  12. ASGGameMode::ASGGameMode()
  13. {
  14.     // set default pawn class to our Blueprinted character
  15.     static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter"));
  16.     if (PlayerPawnBPClass.Class != NULL)
  17.     {
  18.         DefaultPawnClass = PlayerPawnBPClass.Class;
  19.         PlayerControllerClass = ASPlayerController::StaticClass();
  20.     }
  21.  
  22.     if (Role == ROLE_Authority)
  23.         SocketIo = CreateDefaultSubobject<USocketIOClientComponent>(TEXT("SocketIOClientComponent"));
  24.  
  25.     if (!IsRunningDedicatedServer())
  26.     {
  27.         static ConstructorHelpers::FClassFinder<UUserWidget> BackgroundVideoWidget(TEXT("/Game/UI/User/W_BackgroundVideo"));
  28.         static ConstructorHelpers::FClassFinder<UUserWidget> TitleVideoWidget(TEXT("/Game/UI/User/W_TitleVideo"));
  29.         static ConstructorHelpers::FClassFinder<UUserWidget> NewsInterfaceWidget(TEXT("/Game/UI/News/W_NewsInterface"));
  30.         static ConstructorHelpers::FClassFinder<UUserWidget> MainMenuWidget(TEXT("/Game/UI/MainMenu/W_MainMenu"));
  31.         static ConstructorHelpers::FClassFinder<UUserWidget> LoginInterfaceWidget(TEXT("/Game/UI/User/W_LoginInterface"));
  32.         static ConstructorHelpers::FClassFinder<UUserWidget> RegisterInterfaceWidget(TEXT("/Game/UI/User/W_RegisterInterface"));
  33.         static ConstructorHelpers::FClassFinder<UUserWidget> ResetPasswordInterfaceWidget(TEXT("/Game/UI/User/W_ResetPasswordInterface"));
  34.  
  35.         if (BackgroundVideoWidget.Class != NULL)
  36.             WBackgroundVideo = BackgroundVideoWidget.Class;
  37.         if (TitleVideoWidget.Class != NULL)
  38.             WTitleVideo = TitleVideoWidget.Class;
  39.         if (NewsInterfaceWidget.Class != NULL)
  40.             WNewsInterface = NewsInterfaceWidget.Class;
  41.         if (MainMenuWidget.Class != NULL)
  42.             WMainMenu = MainMenuWidget.Class;
  43.         if (LoginInterfaceWidget.Class != NULL)
  44.             WLoginInterface = LoginInterfaceWidget.Class;
  45.         if (RegisterInterfaceWidget.Class != NULL)
  46.             WRegisterInterface = RegisterInterfaceWidget.Class;
  47.         if (ResetPasswordInterfaceWidget.Class != NULL)
  48.             WResetPasswordInterface = ResetPasswordInterfaceWidget.Class;
  49.     }
  50. }
  51.  
  52. // Save to GameDir\Saved\Config\Windows\Game.ini
  53. void ASGGameMode::SaveAccountName(const FString& name)
  54. {
  55.     if (!GConfig) return;
  56.  
  57.     FString VictorySection = "SG.Auth";
  58.     GConfig->SetString(*VictorySection, TEXT("account_name"), *name, GGameIni);
  59.  
  60.     //ConfigCacheIni.h
  61.     //void Flush( bool Read, const FString& Filename=TEXT("") );
  62.     GConfig->Flush(false, GGameIni);
  63. }
  64.  
  65. void ASGGameMode::BeginPlay()
  66. {
  67.     Super::BeginPlay();
  68.  
  69.     if (!IsRunningDedicatedServer())
  70.     {
  71.         APlayerController* playerController = GEngine->GetFirstLocalPlayerController(GWorld);
  72.  
  73.         if (playerController != nullptr)
  74.         {
  75.             playerController->SetInputMode(FInputModeUIOnly());
  76.             playerController->bShowMouseCursor = true;
  77.  
  78.             if (WBackgroundVideo && !this->isLogged) {
  79.                 BackgroundVideo = CreateWidget<UUserWidget>(playerController, WBackgroundVideo);
  80.                 BackgroundVideo->AddToViewport();
  81.             }
  82.  
  83.             if (WTitleVideo && !this->isLogged) {
  84.                 TitleVideo = CreateWidget<UUserWidget>(playerController, WTitleVideo);
  85.                 TitleVideo->AddToViewport();
  86.             }
  87.  
  88.             if (WNewsInterface && !this->isLogged) {
  89.                 NewsInterface = CreateWidget<UUserWidget>(playerController, WNewsInterface);
  90.                 NewsInterface->AddToViewport();
  91.  
  92.                 SocketIo->EmitNative(FString("posts_index"), true);
  93.  
  94.                 SocketIo->OnNativeEvent(FString("posts_index_result"), [&](const FString& Event, const TSharedPtr<FJsonValue>& Payload)
  95.                 {
  96.                     FString json = USIOJConvert::ToJsonString(Payload);
  97.                     TArray<TSharedPtr<FJsonValue>> posts = USIOJConvert::JsonStringToJsonArray(json);
  98.  
  99.                     for (auto& post : posts) {
  100.                         FString str = USIOJConvert::ToJsonString(post);
  101.                         TSharedPtr<FJsonObject> data = USIOJConvert::ToJsonObject(str);
  102.  
  103.                         FString id      = data->GetStringField("id");
  104.                         FString title   = data->GetStringField("title");
  105.                         FString content = data->GetStringField("content");
  106.                         FString image   = data->GetStringField("image");
  107.                         OnPostResult.Broadcast(id, title, content, image);
  108.                     }
  109.                 });
  110.             }
  111.  
  112.             if (WMainMenu)
  113.                 MainMenu = CreateWidget<UUserWidget>(playerController, WMainMenu);
  114.  
  115.             if (WLoginInterface && !this->isLogged)
  116.             {
  117.                 LoginInterface = CreateWidget<UUserWidget>(playerController, WLoginInterface);
  118.  
  119.                 if (WLoginInterface)
  120.                     LoginInterface->AddToViewport();
  121.             }
  122.  
  123.             if (WRegisterInterface && !this->isLogged)
  124.                 RegisterInterface = CreateWidget<UUserWidget>(playerController, WRegisterInterface);
  125.  
  126.             if (WResetPasswordInterface && !this->isLogged)
  127.                 ResetPasswordInterface = CreateWidget<UUserWidget>(playerController, WResetPasswordInterface);
  128.  
  129.             this->GetLastAccountName();
  130.         }
  131.     }
  132.  
  133.     if (SocketIo != nullptr && Role == ROLE_Authority)
  134.     {
  135.         //SocketIo->AddressAndPort = FString("http://virax.net");
  136.         SocketIo->AddressAndPort = FString("http://127.0.0.1:3000");
  137.         //SocketIo->bShouldAutoConnect = false;
  138.         //SocketIo->Connect(SocketIo->AddressAndPort);
  139.     }
  140.     else
  141.     {
  142.         SocketIo = nullptr;
  143.     }
  144. }
  145. void ASGGameMode::GetLastAccountName()
  146. {
  147.     FString Section = "SG.Auth";
  148.     FString name;
  149.  
  150.     GConfig->GetString(*Section, TEXT("account_name"), name, GGameIni);
  151.  
  152.     UEditableTextBox* input = nullptr;
  153.  
  154.     const FName TextControlName = FName(TEXT("InputEmail"));
  155.  
  156.     if (input == nullptr)
  157.     {
  158.         input = (UEditableTextBox*)(LoginInterface->WidgetTree->FindWidget(TextControlName));
  159.         input->SetText(FText::FromString(name));
  160.     }
  161. }
  162.  
  163. void ASGGameMode::UserLogin(const FString& email, const FString& password, bool saveAccountName)
  164. {
  165.     if (!SocketIo->bIsConnected) {
  166.         OnLoginResult.Broadcast(FString("Connection issues, retry later"), false);
  167.         return;
  168.     }
  169.  
  170.     TSharedPtr<FJsonObject> obj = MakeShareable(new FJsonObject);
  171.     obj->SetStringField(FString("email"), email);
  172.     obj->SetStringField(FString("password"), password);
  173.  
  174.     SocketIo->EmitNative(FString("login"), obj);
  175.  
  176.     this->SaveAccountName(saveAccountName ? email : "");
  177.  
  178.     SocketIo->OnNativeEvent(FString("logged"), [&](const FString& Event, const TSharedPtr<FJsonValue>& Payload)
  179.     {
  180.         FString json = USIOJConvert::ToJsonString(Payload);
  181.         TSharedPtr<FJsonObject> data = USIOJConvert::ToJsonObject(json);
  182.  
  183.         bool state;
  184.         if (data->TryGetBoolField("state", state)) {
  185.             if (state) {
  186.                 FString token;
  187.                 if (data->TryGetStringField("token", token)) {
  188.                     this->SessionToken = token;
  189.  
  190.                     FString msg;
  191.                     if (data->TryGetStringField("msg", msg))
  192.                         OnLoginResult.Broadcast(msg, true);
  193.  
  194.                     APlayerController* playerController = GEngine->GetFirstLocalPlayerController(GWorld);
  195.  
  196.                     if (playerController != nullptr)
  197.                         playerController->SetInputMode(FInputModeGameAndUI());
  198.                 }
  199.                 else {
  200.                     OnLoginResult.Broadcast("Unable to get authentication token", false);
  201.                 }
  202.             }
  203.             else {
  204.                 FString error;
  205.                 if (data->TryGetStringField("error", error)) {
  206.                     OnLoginResult.Broadcast(error, false);
  207.                 }
  208.             }
  209.         }
  210.     });
  211. }
  212.  
  213. void ASGGameMode::UserResetPassword(const FString& email)
  214. {
  215.     if (!SocketIo->bIsConnected) {
  216.         OnRegisterResult.Broadcast(FString("Connection issues, retry later"), false);
  217.         return;
  218.     }
  219.  
  220.     TSharedPtr<FJsonObject> obj = MakeShareable(new FJsonObject);
  221.     obj->SetStringField(FString("email"), email);
  222.  
  223.     SocketIo->EmitNative(FString("reset_password"), obj);
  224.  
  225.     SocketIo->OnNativeEvent(FString("password_reseted"), [&](const FString& Event, const TSharedPtr<FJsonValue>& Payload)
  226.     {
  227.         FString json = USIOJConvert::ToJsonString(Payload);
  228.         TSharedPtr<FJsonObject> data = USIOJConvert::ToJsonObject(json);
  229.  
  230.         bool state;
  231.         if (data->TryGetBoolField("state", state)) {
  232.             if (state) {
  233.                 FString msg;
  234.                 if (data->TryGetStringField("msg", msg)) {
  235.                     OnResetPasswordResult.Broadcast(msg, true);
  236.                 }
  237.             }
  238.             else {
  239.                 FString error;
  240.                 if (data->TryGetStringField("error", error)) {
  241.                     OnResetPasswordResult.Broadcast(error, false);
  242.                 }
  243.             }
  244.         }
  245.     });
  246. }
  247.  
  248. void ASGGameMode::UserRegister(const FString& email, const FString& nickname, const FString& password)
  249. {
  250.     if (!SocketIo->bIsConnected) {
  251.         OnRegisterResult.Broadcast(FString("Connection issues, retry later"), false);
  252.         return;
  253.     }
  254.  
  255.     TSharedPtr<FJsonObject> obj = MakeShareable(new FJsonObject);
  256.     obj->SetStringField(FString("nickname"), nickname);
  257.     obj->SetStringField(FString("email"), email);
  258.     obj->SetStringField(FString("password"), password);
  259.  
  260.     SocketIo->EmitNative(FString("register"), obj);
  261.  
  262.     SocketIo->OnNativeEvent(FString("registered"), [&](const FString& Event, const TSharedPtr<FJsonValue>& Payload)
  263.     {
  264.         FString json = USIOJConvert::ToJsonString(Payload);
  265.         TSharedPtr<FJsonObject> data = USIOJConvert::ToJsonObject(json);
  266.  
  267.         bool state;
  268.         if (data->TryGetBoolField("state", state)) {
  269.             if (state) {
  270.                 FString msg;
  271.                 if (data->TryGetStringField("msg", msg)) {
  272.                     OnRegisterResult.Broadcast(msg, true);
  273.                 }
  274.             }
  275.             else {
  276.                 FString error;
  277.                 if (data->TryGetStringField("error", error)) {
  278.                     OnRegisterResult.Broadcast(error, false);
  279.                 }
  280.             }
  281.         }
  282.     });
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement