KF2-YetAnotherScoreboard/YAS/Classes/YAS.uc

452 lines
9.4 KiB
Ucode
Raw Normal View History

2022-09-03 17:26:40 +00:00
class YAS extends Info
config(YAS);
const LatestVersion = 1;
2022-09-08 19:17:55 +00:00
const CfgRanks = class'Ranks';
const CfgRankRelations = class'RankRelations';
const CfgMessageOfTheDay = class'MessageOfTheDay';
2022-09-07 12:57:47 +00:00
2022-09-08 13:32:28 +00:00
const UpdateInterval = 1.0f;
2022-09-07 12:57:47 +00:00
const MatchUID = "0x";
const MatchPlayerSteamID64 = "76561";
const MatchGroupSteamID64 = "10358279";
2022-09-03 17:26:40 +00:00
var private config int Version;
var private config E_LogLevel LogLevel;
var private KFGameInfo KFGI;
var private KFGameInfo_Survival KFGIS;
var private KFGameInfo_Endless KFGIE;
var private KFGameReplicationInfo KFGRI;
2022-09-07 12:57:47 +00:00
var private KFOnlineGameSettings KFOGS;
var private OnlineSubsystemSteamworks OSS;
2022-09-03 17:26:40 +00:00
2022-09-08 13:32:28 +00:00
var private Array<YAS_RepInfo> RepInfos;
2022-09-03 17:26:40 +00:00
2022-09-07 12:57:47 +00:00
var private Array<CachedRankRelation> PlayerRelations;
var private Array<CachedRankRelation> GroupRelations;
2022-09-08 19:17:55 +00:00
var private int LastMessageID;
2022-09-03 17:26:40 +00:00
public simulated function bool SafeDestroy()
{
`Log_Trace();
return (bPendingDelete || bDeleteMe || Destroy());
}
public event PreBeginPlay()
{
`Log_Trace();
if (WorldInfo.NetMode == NM_Client)
{
`Log_Fatal("NetMode == NM_Client");
SafeDestroy();
return;
}
Super.PreBeginPlay();
PreInit();
}
public event PostBeginPlay()
{
`Log_Trace();
if (bPendingDelete || bDeleteMe) return;
Super.PostBeginPlay();
PostInit();
}
private function PreInit()
{
`Log_Trace();
if (Version == `NO_CONFIG)
{
LogLevel = LL_Info;
SaveConfig();
}
2022-09-07 12:57:47 +00:00
CfgRanks.static.InitConfig(Version, LatestVersion);
CfgRankRelations.static.InitConfig(Version, LatestVersion);
2022-09-08 19:17:55 +00:00
CfgMessageOfTheDay.static.InitConfig(Version, LatestVersion);
2022-09-03 17:26:40 +00:00
switch (Version)
{
case `NO_CONFIG:
`Log_Info("Config created");
case MaxInt:
`Log_Info("Config updated to version" @ LatestVersion);
break;
case LatestVersion:
`Log_Info("Config is up-to-date");
break;
default:
`Log_Warn("The config version is higher than the current version (are you using an old mutator?)");
`Log_Warn("Config version is" @ Version @ "but current version is" @ LatestVersion);
`Log_Warn("The config version will be changed to" @ LatestVersion);
break;
}
if (LatestVersion != Version)
{
Version = LatestVersion;
SaveConfig();
}
if (LogLevel == LL_WrongLevel)
{
LogLevel = LL_Info;
`Log_Warn("Wrong 'LogLevel', return to default value");
SaveConfig();
}
`Log_Base("LogLevel:" @ LogLevel);
2022-09-07 12:57:47 +00:00
OSS = OnlineSubsystemSteamworks(class'GameEngine'.static.GetOnlineSubsystem());
if (OSS != None)
{
InitRanks();
}
else
{
`Log_Error("Can't get online subsystem!");
}
}
private function InitRanks() // TODO: Ref
{
local Array<RankRelation> Relations;
local RankRelation Relation;
Relations = CfgRankRelations.default.Relations;
foreach Relations(Relation)
{
if (IsUID(Relation.ObjectID) || IsPlayerSteamID64(Relation.ObjectID))
{
2022-09-08 13:32:28 +00:00
AddRelation(Relation, PlayerRelations);
2022-09-07 12:57:47 +00:00
}
else if (IsGroupSteamID64(Relation.ObjectID))
{
2022-09-08 13:32:28 +00:00
AddRelation(Relation, GroupRelations);
}
else
{
`Log_Warn("Can't parse ID:" @ Relation.ObjectID);
}
}
}
private function AddRelation(RankRelation Relation, out Array<CachedRankRelation> OutArray)
{
local CachedRankRelation CachedRankRelation;
local Array<Rank> Ranks;
local Rank Rank;
if (AnyToUID(Relation.ObjectID, CachedRankRelation.UID))
{
CachedRankRelation.RawID = Relation.ObjectID;
Ranks = CfgRanks.default.Ranks;
foreach Ranks(Rank)
{
if (Rank.RankID == Relation.RankID)
2022-09-07 12:57:47 +00:00
{
2022-09-08 13:32:28 +00:00
CachedRankRelation.Rank = Rank;
break;
2022-09-07 12:57:47 +00:00
}
}
2022-09-08 13:32:28 +00:00
if (CachedRankRelation.Rank.RankID > 0)
{
OutArray.AddItem(CachedRankRelation);
}
2022-09-07 12:57:47 +00:00
else
{
2022-09-08 13:32:28 +00:00
`Log_Warn("Rank with ID" @ Relation.RankID @ "not found");
2022-09-07 12:57:47 +00:00
}
}
2022-09-08 13:32:28 +00:00
else
{
`Log_Warn("Can't convert to UniqueNetID:" @ Relation.ObjectID);
}
2022-09-07 12:57:47 +00:00
}
private static function bool IsUID(String ID)
{
return (Left(ID, Len(MatchUID)) ~= MatchUID);
}
private static function bool IsPlayerSteamID64(String ID)
{
return (Left(ID, Len(MatchPlayerSteamID64)) ~= MatchPlayerSteamID64);
}
private static function bool IsGroupSteamID64(String ID)
{
return (Left(ID, Len(MatchGroupSteamID64)) ~= MatchGroupSteamID64);
}
private function bool AnyToUID(String ID, out UniqueNetId UID)
{
return IsUID(ID) ? OSS.StringToUniqueNetId(ID, UID) : OSS.Int64ToUniqueNetId(ID, UID);
2022-09-03 17:26:40 +00:00
}
private function PostInit()
{
`Log_Trace();
if (WorldInfo == None || WorldInfo.Game == None)
{
SetTimer(1.0f, false, nameof(PostInit));
return;
}
KFGI = KFGameInfo(WorldInfo.Game);
if (KFGI == None)
{
`Log_Fatal("Incompatible gamemode:" @ WorldInfo.Game);
SafeDestroy();
return;
}
KFGI.HUDType = class'YAS_HUD';
if (KFGI.GameReplicationInfo == None)
{
SetTimer(1.0f, false, nameof(PostInit));
return;
}
KFGRI = KFGameReplicationInfo(KFGI.GameReplicationInfo);
if (KFGRI == None)
{
`Log_Fatal("Incompatible Replication info:" @ KFGI.GameReplicationInfo);
SafeDestroy();
return;
}
2022-09-07 12:57:47 +00:00
if (KFGI.PlayfabInter != None && KFGI.PlayfabInter.GetGameSettings() != None)
{
KFOGS = KFOnlineGameSettings(KFGI.PlayfabInter.GetGameSettings());
}
else if (KFGI.GameInterface != None)
{
KFOGS = KFOnlineGameSettings(
KFGI.GameInterface.GetGameSettings(
KFGI.PlayerReplicationInfoClass.default.SessionName));
}
if (KFOGS == None)
{
SetTimer(1.0f, false, nameof(PostInit));
return;
}
2022-09-03 17:26:40 +00:00
KFGIS = KFGameInfo_Survival(KFGI);
KFGIE = KFGameInfo_Endless(KFGI);
2022-09-07 12:57:47 +00:00
SetTimer(UpdateInterval, true, nameof(UpdateTimer));
2022-09-08 19:17:55 +00:00
`Log_Base("MessageOfTheDayTimer Length:" @ CfgMessageOfTheDay.default.Message.Length);
if (CfgMessageOfTheDay.default.Message.Length > 0)
{
`Log_Base("init MessageOfTheDayTimer:");
MessageOfTheDayTimer();
SetTimer(CfgMessageOfTheDay.default.DisplayTime, true, nameof(MessageOfTheDayTimer));
}
2022-09-07 12:57:47 +00:00
}
private function UpdateTimer()
{
2022-09-08 13:32:28 +00:00
local YAS_RepInfo RepInfo;
foreach RepInfos(RepInfo)
{
RepInfo.DynamicServerName = KFOGS.OwningPlayerName;
RepInfo.UsesStats = KFOGS.bUsesStats;
RepInfo.Custom = KFOGS.bCustom;
RepInfo.PasswordRequired = KFOGS.bRequiresPassword;
}
2022-09-03 17:26:40 +00:00
}
2022-09-08 19:17:55 +00:00
private function MessageOfTheDayTimer()
{
local YAS_RepInfo RepInfo;
local int MessageIndex;
if (CfgMessageOfTheDay.default.bRandomize)
{
MessageIndex = Rand(CfgMessageOfTheDay.default.Message.Length);
}
else
{
MessageIndex = LastMessageID + 1;
}
if (MessageIndex == LastMessageID)
{
++MessageIndex;
}
if (MessageIndex >= CfgMessageOfTheDay.default.Message.Length)
{
MessageIndex = 0;
}
`Log_Base("MessageOfTheDayTimer:" @ MessageIndex);
foreach RepInfos(RepInfo)
{
RepInfo.MessageOfTheDay = CfgMessageOfTheDay.default.Message[MessageIndex];
}
LastMessageID = MessageIndex;
}
2022-09-03 17:26:40 +00:00
public function NotifyLogin(Controller C)
{
2022-09-08 13:32:28 +00:00
local YAS_RepInfo RepInfo;
2022-09-07 12:57:47 +00:00
2022-09-03 17:26:40 +00:00
`Log_Trace();
2022-09-07 12:57:47 +00:00
RepInfo = CreateRepInfo(C);
if (RepInfo == None)
2022-09-03 17:26:40 +00:00
{
`Log_Error("Can't create RepInfo for:" @ C);
2022-09-07 12:57:47 +00:00
return;
2022-09-03 17:26:40 +00:00
}
2022-09-07 12:57:47 +00:00
InitRank(RepInfo);
2022-09-03 17:26:40 +00:00
}
public function NotifyLogout(Controller C)
{
2022-09-08 13:32:28 +00:00
local YAS_RepInfo RepInfo;
2022-09-07 12:57:47 +00:00
2022-09-03 17:26:40 +00:00
`Log_Trace();
2022-09-07 12:57:47 +00:00
RepInfo = FindRepInfo(C);
if (!DestroyRepInfo(RepInfo))
2022-09-03 17:26:40 +00:00
{
`Log_Error("Can't destroy RepInfo of:" @ C);
}
}
2022-09-08 13:32:28 +00:00
public function YAS_RepInfo CreateRepInfo(Controller C)
2022-09-03 17:26:40 +00:00
{
2022-09-08 13:32:28 +00:00
local YAS_RepInfo OwnerRepInfo;
2022-09-07 16:13:13 +00:00
local YAS_RankRepInfo RankRepInfo;
2022-09-03 17:26:40 +00:00
`Log_Trace();
2022-09-08 13:32:28 +00:00
OwnerRepInfo = Spawn(class'YAS_RepInfo', C);
2022-09-07 16:13:13 +00:00
RankRepInfo = Spawn(class'YAS_RankRepInfo', C);
2022-09-03 17:26:40 +00:00
2022-09-07 16:13:13 +00:00
if (OwnerRepInfo != None && RankRepInfo != None)
2022-09-07 12:57:47 +00:00
{
2022-09-07 16:13:13 +00:00
RepInfos.AddItem(OwnerRepInfo);
2022-09-07 12:57:47 +00:00
2022-09-07 16:13:13 +00:00
OwnerRepInfo.RankRepInfo = RankRepInfo;
2022-09-08 18:20:04 +00:00
OwnerRepInfo.YAS = Self;
OwnerRepInfo.LogLevel = LogLevel;
OwnerRepInfo.RankPlayer = CfgRanks.default.Player;
OwnerRepInfo.RankAdmin = CfgRanks.default.Admin;
2022-09-08 19:17:55 +00:00
OwnerRepInfo.MessageOfTheDay = CfgMessageOfTheDay.default.Message[LastMessageID];
2022-09-07 12:57:47 +00:00
}
2022-09-03 17:26:40 +00:00
2022-09-07 16:13:13 +00:00
return OwnerRepInfo;
2022-09-07 12:57:47 +00:00
}
2022-09-08 13:32:28 +00:00
private function YAS_RepInfo FindRepInfo(Controller C)
2022-09-07 12:57:47 +00:00
{
2022-09-08 13:32:28 +00:00
local YAS_RepInfo RepInfo;
2022-09-07 12:57:47 +00:00
if (C == None) return None;
foreach RepInfos(RepInfo)
{
if (RepInfo.Owner == C)
{
return RepInfo;
}
}
return None;
}
2022-09-08 13:32:28 +00:00
public function bool DestroyRepInfo(YAS_RepInfo RepInfo)
2022-09-07 12:57:47 +00:00
{
`Log_Trace();
if (RepInfo == None) return false;
2022-09-03 17:26:40 +00:00
2022-09-07 12:57:47 +00:00
RepInfos.RemoveItem(RepInfo);
2022-09-07 16:13:13 +00:00
RepInfo.RankRepInfo.SafeDestroy();
2022-09-07 12:57:47 +00:00
RepInfo.SafeDestroy();
2022-09-03 17:26:40 +00:00
return true;
}
2022-09-08 13:32:28 +00:00
private function InitRank(YAS_RepInfo RepInfo)
2022-09-07 12:57:47 +00:00
{
local CachedRankRelation Rel;
local String JoinedGroupIDs;
local PlayerReplicationInfo PRI;
local KFPlayerController KFPC;
local Array<String> StringGroupIDs;
`Log_Trace();
KFPC = RepInfo.GetKFPC();
if (KFPC == None) return;
PRI = KFPC.PlayerReplicationInfo;
if (PRI == None) return;
foreach PlayerRelations(Rel)
{
if (Rel.UID.Uid == PRI.UniqueID.Uid)
{
2022-09-07 16:13:13 +00:00
RepInfo.RankRepInfo.Rank = Rel.Rank;
2022-09-07 12:57:47 +00:00
break;
}
}
2022-09-07 16:13:13 +00:00
if (RepInfo.RankRepInfo.Rank.RankID <= 0 && !KFPC.bIsEosPlayer)
2022-09-07 12:57:47 +00:00
{
foreach GroupRelations(Rel)
{
StringGroupIDs.AddItem(Rel.RawID);
}
JoinArray(StringGroupIDs, JoinedGroupIDs);
RepInfo.CheckGroupRanks(JoinedGroupIDs);
}
}
2022-09-07 16:13:13 +00:00
public function Rank RankByGroupID(UniqueNetId GroupUID)
2022-09-03 17:26:40 +00:00
{
2022-09-07 16:13:13 +00:00
local CachedRankRelation Rel;
2022-09-03 17:26:40 +00:00
2022-09-07 16:13:13 +00:00
foreach GroupRelations(Rel) if (Rel.UID == GroupUID) break;
2022-09-07 12:57:47 +00:00
2022-09-07 16:13:13 +00:00
return Rel.Rank;
2022-09-03 17:26:40 +00:00
}
DefaultProperties
{
}