Files
DTFluxAPI/Source/DTFluxCoreSubsystem/Private/DTFluxCoreSubsystem.cpp

247 lines
7.6 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "DTFluxCoreSubsystem.h"
#include "DTFluxCoreSubsystemModule.h"
#include "DTFluxGeneralSettings.h"
#include "FileHelpers.h"
#include "Assets/DTFluxModelAsset.h"
#include "Subsystems/DTFluxNetworkSubsystem.h"
#include "UObject/SavePackage.h"
void UDTFluxCoreSubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
Super::Initialize(Collection);
UE_LOG(logDTFluxCoreSubsystem, Log, TEXT("[UDTFluxCoreSubsystem] Initializing..."));
if(!DataStorage)
{
const UDTFluxGeneralSettings* GeneralSettings = GetDefault<UDTFluxGeneralSettings>();
TSoftObjectPtr<UDTFluxModelAsset> ModelAsset = GeneralSettings->ModelAsset;
// if(ModelAsset.IsValid())
// {
// }
// UE_LOG(logDTFluxCore, Error, TEXT("ModelAsset Not Valid"));
DataStorage = DataStorage = Cast<UDTFluxModelAsset>(ModelAsset.LoadSynchronous());
if(!DataStorage)
{
UE_LOG(logDTFluxCore, Error, TEXT("DataStorage Not Valid"));
}
// else
// {
// MakeStorageEditable(DataStorage);
// }
}
//TODO REMOVE This as it's only for testing purpose
NetworkSubsystem = GEngine->GetEngineSubsystem<UDTFluxNetworkSubsystem>();
if(NetworkSubsystem->WsStatus != EDTFluxConnectionStatus::Connected)
{
RegisterDelegates();
}
}
void UDTFluxCoreSubsystem::Deinitialize()
{
Super::Deinitialize();
}
void UDTFluxCoreSubsystem::SaveDataStorage()
{
if(DataStorage)
{
DataStorage->MarkPackageDirty();
UPackage* Package = DataStorage->GetPackage();
if(Package->IsDirty())
{
FString PackageName = Package->GetName();
FString FileExtension = FPackageName::GetAssetPackageExtension();
FString FilePath = FPaths::ConvertRelativePathToFull(FPaths::ProjectContentDir() / PackageName.Replace(TEXT("/"), TEXT("/")) + FileExtension);
FString LongPackageName = DataStorage->GetOutermost()->GetName();
FString RealAssetPath;
bool bExists = FPackageName::DoesPackageExist(PackageName);
if (!bExists)
{
UE_LOG(logDTFluxCoreSubsystem, Error, TEXT("Le package n'existe pas ou est un redirecteur"));
}
bool bSuccess = FPackageName::SearchForPackageOnDisk(LongPackageName, &RealAssetPath);
if (bSuccess && !RealAssetPath.IsEmpty())
{
UE_LOG(logDTFluxCoreSubsystem, Log, TEXT("Vrai path trouvé : %s"), *RealAssetPath);
}
else
{
UE_LOG(logDTFluxCoreSubsystem, Warning, TEXT("Aucun path valide trouvé pour sauvegarder l'asset"));
}
UE_LOG(logDTFluxCoreSubsystem, Warning, TEXT("Saving DataAsset to %s"), *FilePath);
TSoftObjectPtr<UDTFluxModelAsset> ModelAsset = GetDefault<UDTFluxGeneralSettings>()->ModelAsset;
FString RealPath = ModelAsset->GetPathName();
UE_LOG(logDTFluxCoreSubsystem, Warning, TEXT("SoftObjectPath to %s"), *RealPath);
FSavePackageArgs Args;
Args.TopLevelFlags = RF_Public | RF_Standalone;
Args.bSlowTask = false;
Args.SaveFlags = SAVE_None;
GEditor->SavePackage(Package, DataStorage, *FilePath, Args);
UE_LOG(logDTFluxCoreSubsystem, Warning, TEXT("DataAsset Saved"))
}
}
}
void UDTFluxCoreSubsystem::RegisterDelegates()
{
if(NetworkSubsystem)
{
NetworkSubsystem->OnReceivedRaceData().BindUFunction(this, "ProcessRaceData");
NetworkSubsystem->OnReceivedTeamList().BindUFunction(this, "ProcessTeamList");
NetworkSubsystem->OnReceivedContestRanking().BindUFunction(this, "ProcessContestRanking");
NetworkSubsystem->OnReceivedStageRanking().BindUFunction(this, "ProcessStageRanking");
NetworkSubsystem->OnReceivedSplitRanking().BindUFunction(this, "ProcessSplitRanking");
NetworkSubsystem->OnReceivedTeamUpdate().BindUFunction(this, "ProcessTeamList");
NetworkSubsystem->OnReceivedTeamStatusUpdate().BindUFunction(this, "ProcessTeamStatusUpdate");
NetworkSubsystem->OnReceivedSplitSensor().BindUFunction(this, "ProcessSplitSensor");
}
}
void UDTFluxCoreSubsystem::ProcessRaceData(const FDTFluxRaceData& RaceDataDefinition)
{
if( RaceDataDefinition.Datas.Num() > 0 )
{
UE_LOG(logDTFluxCoreSubsystem, Warning, TEXT("Receiving RaceDataDefinition [%s]"), *RaceDataDefinition.Datas[0].Name);
if(DataStorage != nullptr)
{
UE_LOG(logDTFluxCoreSubsystem, Error, TEXT("DataStorage Name %s"), *DataStorage->EventName);
for(auto Contest : RaceDataDefinition.Datas)
{
DataStorage->AddContest(Contest);
}
}
else
{
UE_LOG(logDTFluxCoreSubsystem, Error, TEXT("DataStorage is null"));
}
SaveDataStorage();
return;
}
UE_LOG(logDTFluxCoreSubsystem, Error, TEXT("RaceDataDefinition is empty !!!"));
}
void UDTFluxCoreSubsystem::ProcessTeamList(const FDTFluxTeamListDefinition& TeamListDefinition)
{
UE_LOG(logDTFluxCoreSubsystem, Warning, TEXT("Received TeamList with %i Items"), TeamListDefinition.Participants.Num());
for(const auto& Participant : TeamListDefinition.Participants)
{
UE_LOG(logDTFluxCoreSubsystem, Warning, TEXT("Add Participant %i in %i ContestId"),
Participant.Bib, Participant.ContestId );
DataStorage->AddParticipant(Participant, Participant.ContestId);
}
}
void UDTFluxCoreSubsystem::ProcessContestRanking(const FDTFluxContestRankings& ContestRankings)
{
UE_LOG(logDTFluxCoreSubsystem, Warning, TEXT("Received ContestRankings with %i Items"), ContestRankings.Rankings.Num());
FDTFluxContestRankings NewContestRankings = ContestRankings;
NewContestRankings.SetName( DataStorage->GetContestNameForId(ContestRankings.ContestId));
DataStorage->AddContestRanking(NewContestRankings);
UE_LOG(logDTFluxCoreSubsystem, Warning, TEXT("ContestRankings added for Contest %s"), *NewContestRankings.ContestName);
DataStorage->MarkPackageDirty();
}
void UDTFluxCoreSubsystem::ProcessStageRanking(const FDTFluxStageRankings& StageRankings)
{
UE_LOG(logDTFluxCoreSubsystem, Warning, TEXT("Received StageRankings with %i Items"), StageRankings.Rankings.Num());
DataStorage->UpdateOrCreateStageRanking(StageRankings);
DataStorage->MarkPackageDirty();
}
void UDTFluxCoreSubsystem::ProcessSplitRanking(const FDTFluxSplitRankings& SplitRankings)
{
UE_LOG(logDTFluxCoreSubsystem, Warning, TEXT("Received SplitRanking with %i Items"), SplitRankings.Rankings.Num());
DataStorage->UpdateOrCreateSplitRanking(SplitRankings);
DataStorage->MarkPackageDirty();
}
void UDTFluxCoreSubsystem::ProcessTeamStatusUpdate()
{
//TODO IMPLEMENT ME !!!!
}
void UDTFluxCoreSubsystem::ProcessSplitSensor()
{
//TODO IMPLEMENT ME !!!!
}
void UDTFluxCoreSubsystem::SendRequest(const FString& Message)
{
if(NetworkSubsystem)
{
NetworkSubsystem->SendMessage(Message);
}
}
void UDTFluxCoreSubsystem::SendTeamListRequest()
{
if (NetworkSubsystem)
{
NetworkSubsystem->SendRequest(EDTFluxRequestType::TeamList);
}
}
void UDTFluxCoreSubsystem::SendRaceDataRequest()
{
if (NetworkSubsystem)
{
NetworkSubsystem->SendRequest(EDTFluxRequestType::RaceData);
}
}
void UDTFluxCoreSubsystem::SendContestRankingRequest(int InContestId)
{
if (NetworkSubsystem)
{
NetworkSubsystem->SendRequest(EDTFluxRequestType::ContestRanking, InContestId);
}
}
void UDTFluxCoreSubsystem::SendStageRankingRequest(int InContestId, int InStageId, bool bShouldIncludeSplitRanking)
{
// TODO Implement this
}
void UDTFluxCoreSubsystem::RequestAllStageRankingOfContest(int InContestId, int InStageId,
bool bShouldIncludeSplitRanking)
{
// TODO Implement this
}
void UDTFluxCoreSubsystem::SendSplitRankingRequest(int InContestId, int InStageId, int InSplitId)
{
// TODO Implement this
}
void UDTFluxCoreSubsystem::RequestAllSplitRankingOfContest(int InContestId, int InStageId)
{
// TODO Implement this
}
void UDTFluxCoreSubsystem::RequestAllSplitRankingOfStage(int InContestId, int InStageId, int InSplitId)
{
// TODO Implement this
}
void UDTFluxCoreSubsystem::RefreshStorage()
{
// TODO Implement this
}