Newer
Older
SkyFrontier-Project-IADE-UE4-3D / Source / SkyFrontier / Private / MyPlayerState.cpp
@Genexuz Genexuz on 6 Dec 2022 3 KB idk matchmaking?
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "MyPlayerState.h"
  3. #include "MyButton.h"
  4. #include "TCPClient.h"
  5. #include "Blueprint/UserWidget.h"
  6. #include "Components/Button.h"
  7. #include "Components/ScrollBox.h"
  8. #include "Components/TextBlock.h"
  9. #include "Components/VerticalBox.h"
  10. #include "Components/VerticalBoxSlot.h"
  11. void AMyPlayerState::BeginPlay()
  12. {
  13. FString levelName = GetWorld()->GetMapName();
  14. levelName.RemoveFromStart(GetWorld()->StreamingLevelsPrefix);
  15. if (levelName == "StartMap")
  16. {
  17. tcpClient = new TCPClient(this);
  18. canConnectToGameServer = false;
  19. if ((MatchmakingWidgetClass) && (tcpClient->IsConnected()))
  20. {
  21. serversList = new TArray<FSessionInfo*>();
  22. GetWorld()->GetTimerManager().SetTimer(serverListTimerHandle, this, &AMyPlayerState::OnUpdateServerList, 2, true);
  23. MatchmakingWidget = CreateWidget<UUserWidget>(GetWorld(),MatchmakingWidgetClass);
  24. MatchmakingWidget->AddToViewport();
  25. serverListScrollBoxWidget = Cast<UScrollBox>(MatchmakingWidget->GetWidgetFromName(TEXT("MyScrollBox")));
  26. UButton* newSessionButton = Cast<UButton>(MatchmakingWidget->GetWidgetFromName(TEXT("NewSession")));
  27. if (newSessionButton)
  28. {
  29. newSessionButton->SetIsEnabled(true);
  30. newSessionButton->OnClicked.AddDynamic(this, &AMyPlayerState::OnNewSessionClicked);
  31. }
  32. }
  33. }
  34. }
  35. void AMyPlayerState::OnNewSessionClicked()
  36. {
  37. tcpClient->CreateNewGameSession("My test session");
  38. }
  39. void AMyPlayerState::UpdateSessionsList(FString serverinfo)
  40. {
  41. TArray<FString> Out; serverinfo.ParseIntoArray(Out, TEXT("|"), true);
  42. for (int i = 1; i < Out.Num() - 1; i += 2) { FSessionInfo *tempInfo = new FSessionInfo();
  43. tempInfo->id = FCString::Atoi(*Out[i]);
  44. tempInfo->name = Out[i + 1];
  45. tempInfo->serverip = "";
  46. tempInfo->serverport = -1;
  47. serversList->Add(tempInfo); }
  48. }
  49. void AMyPlayerState::ConnectToGameServer(FSessionInfo session)
  50. {
  51. canConnectToGameServer = true; connectToGameServerSession = session;
  52. }
  53. void AMyPlayerState::OnUpdateServerList() {
  54. if (tcpClient) {
  55. if (tcpClient->IsConnected()) {
  56. if (serversList->Num() > 0) {
  57. if ((MatchmakingWidget) && (serverListScrollBoxWidget)) {
  58. TArray<UWidget*> allChildren = serverListScrollBoxWidget-> GetAllChildren();
  59. for (int i = 0; i < allChildren.Num(); i++)
  60. {
  61. allChildren[i]->RemoveFromParent();
  62. }
  63. for (int i = 0; i < serversList->Num(); i++)
  64. {
  65. UVerticalBox* ItemWidgetsBox = NewObject<UVerticalBox>();
  66. serverListScrollBoxWidget->AddChild(ItemWidgetsBox);
  67. UMyButton* ItemWidget = NewObject<UMyButton>(this);
  68. ItemWidget->SetSessionInfo((*serversList)[i]->id, tcpClient);
  69. UTextBlock* ItemWidgetText = NewObject<UTextBlock>();
  70. ItemWidgetText->SetText(FText::FromString( (*serversList)[i]->name));
  71. ItemWidget->AddChild(ItemWidgetText);
  72. UVerticalBoxSlot* Slot = ItemWidgetsBox-> AddChildToVerticalBox(ItemWidget);
  73. static FMargin Padding(5);
  74. Slot->SetPadding(Padding);
  75. }
  76. }
  77. }
  78. if (canConnectToGameServer){ APlayerController* pController = GetWorld()-> GetFirstPlayerController();
  79. if (pController){ FString cmd = "open " + connectToGameServerSession.serverip + ":" + FString::FromInt(connectToGameServerSession.serverport);
  80. tcpClient->Stop();
  81. canConnectToGameServer = false;
  82. MatchmakingWidget->RemoveFromViewport();
  83. pController->ConsoleCommand(cmd);
  84. }
  85. }
  86. }
  87. }
  88. }