// Fill out your copyright notice in the Description page of Project Settings.


#include "CM_PlayerState.h"

#include "CMGameInstance.h"
#include "CM_GameState.h"
#include "CorruptedMemoryCharacter.h"

void ACM_PlayerState::ClientPlayerJoin_Implementation()
{
	if (GetWorld()->GetAuthGameMode()) return;
	const UCMGameInstance* GameInstance = Cast<UCMGameInstance>(GetWorld()->GetGameInstance());

	if (GetPlayerName() == GameInstance->AccountUsername) return;
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, FString::Printf(TEXT("Player %s Joined %s"), *GetPlayerName(), *FString::FromInt(GameInstance->PlayerIndex)));
	if (!IsValid(Cast<ACorruptedMemoryCharacter>(GetWorld()->GetFirstPlayerController()->GetCharacter())))
	{
		UE_LOG(LogTemp, Warning, TEXT("Invalid Character Queueing Server Request"));
		bServerRequestUserInformation = true;
		return;
	}
	Cast<ACorruptedMemoryCharacter>(GetWorld()->GetFirstPlayerController()->GetCharacter())->ServerPlayerJoin(GameInstance->AccountUsername, GameInstance->PlayerIndex, GetUniqueId());
}
