Skip to content

Commit

Permalink
Improve game init
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed May 24, 2024
1 parent f56290d commit a9afc34
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 48 deletions.
87 changes: 48 additions & 39 deletions Fika.Core/Coop/GameMode/CoopGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ private void SetupCustomWeather(TimeAndWeatherSettings timeAndWeather)
WeatherController.Instance.method_0([weather, weather2]);
}

public override void SetMatchmakerStatus(string status, float? progress = null)
{
if (GClass3126.Instance.CurrentScreenController is MatchmakerTimeHasCome.GClass3182 gclass)
{
gclass.ChangeStatus(status, progress);
}
}

public async Task CreateCoopHandler()
{
CoopHandler coopHandler = CoopHandler.GetCoopHandler();
Expand Down Expand Up @@ -539,20 +547,26 @@ private void DespawnBot(CoopHandler coopHandler, Player bot)
}

/// <summary>
/// We use <see cref="WaitScreen(float)"/> instead
/// The countdown deploy screen
/// </summary>
/// <param name="timeBeforeDeploy"></param>
/// <returns></returns>
public override IEnumerator vmethod_1()
{
// Do nothing
yield break;
int timeBeforeDeployLocal = Singleton<BackendConfigSettingsClass>.Instance.TimeBeforeDeployLocal;
DateTime dateTime = GClass1304.Now.AddSeconds(timeBeforeDeployLocal);
new MatchmakerFinalCountdown.GClass3181(Profile_0, dateTime).ShowScreen(EScreenState.Root);
MonoBehaviourSingleton<BetterAudio>.Instance.FadeInVolumeBeforeRaid(timeBeforeDeployLocal);
Singleton<GUISounds>.Instance.StopMenuBackgroundMusicWithDelay(timeBeforeDeployLocal);
GameUi.gameObject.SetActive(true);
GameUi.TimerPanel.ProfileId = ProfileId;
yield return new WaitForSeconds(timeBeforeDeployLocal);
}

/// <summary>
/// Matchmaker countdown
/// This task ensures that all players are joined and loaded before continuing
/// </summary>
/// <param name="timeBeforeDeploy">Time in seconds to count down</param>
private async Task WaitScreen(int timeBeforeDeploy)
/// <returns></returns>
private IEnumerator WaitForOtherPlayers()
{
if (CoopHandler.TryGetCoopHandler(out CoopHandler coopHandler))
{
Expand All @@ -569,10 +583,15 @@ private async Task WaitScreen(int timeBeforeDeploy)
}

SetStatusModel status = new(coopHandler.MyPlayer.ProfileId, LobbyEntry.ELobbyStatus.IN_GAME);
await FikaRequestHandler.UpdateSetStatus(status);
Task updateStatus = FikaRequestHandler.UpdateSetStatus(status);

while (!updateStatus.IsCompleted)
{
yield return null;
}

Singleton<FikaServer>.Instance.ReadyClients++;
return;
yield break;
}

NetDataWriter writer = new();
Expand All @@ -588,11 +607,16 @@ private async Task WaitScreen(int timeBeforeDeploy)
if (MatchmakerAcceptPatches.IsServer)
{
SetStatusModel status = new(coopHandler.MyPlayer.ProfileId, LobbyEntry.ELobbyStatus.IN_GAME);
await FikaRequestHandler.UpdateSetStatus(status);
Task updateStatus = FikaRequestHandler.UpdateSetStatus(status);

while (!updateStatus.IsCompleted)
{
yield return null;
}

do
{
await Task.Delay(100);
yield return null;
} while (coopHandler.HumanPlayers < MatchmakerAcceptPatches.HostExpectedNumberOfPlayers && !forceStart);

FikaServer server = Singleton<FikaServer>.Instance;
Expand All @@ -607,7 +631,7 @@ private async Task WaitScreen(int timeBeforeDeploy)

do
{
await Task.Delay(250);
yield return null;
} while (Singleton<FikaServer>.Instance.ReadyClients < MatchmakerAcceptPatches.HostExpectedNumberOfPlayers && !forceStart);

foreach (CoopPlayer player in coopHandler.Players.Values)
Expand All @@ -627,7 +651,7 @@ private async Task WaitScreen(int timeBeforeDeploy)
{
do
{
await Task.Delay(100);
yield return null;
} while (coopHandler.HumanPlayers < MatchmakerAcceptPatches.HostExpectedNumberOfPlayers && !forceStart);

FikaClient client = Singleton<FikaClient>.Instance;
Expand All @@ -640,7 +664,7 @@ private async Task WaitScreen(int timeBeforeDeploy)

do
{
await Task.Delay(250);
yield return null;
} while (Singleton<FikaClient>.Instance.ReadyClients < MatchmakerAcceptPatches.HostExpectedNumberOfPlayers && !forceStart);
}

Expand All @@ -651,17 +675,6 @@ private async Task WaitScreen(int timeBeforeDeploy)
}
}

private async Task DeployScreen(int timeBeforeDeploy)
{
DateTime dateTime = GClass1304.Now.AddSeconds(timeBeforeDeploy);
new MatchmakerFinalCountdown.GClass3181(Profile_0, dateTime).ShowScreen(EScreenState.Root);
MonoBehaviourSingleton<BetterAudio>.Instance.FadeInVolumeBeforeRaid(timeBeforeDeploy);
Singleton<GUISounds>.Instance.StopMenuBackgroundMusicWithDelay(timeBeforeDeploy);
GameUi.gameObject.SetActive(true);
GameUi.TimerPanel.ProfileId = ProfileId;
await Task.Delay(timeBeforeDeploy * 1000);
}

private async Task SendOrReceiveSpawnPoint()
{
if (MatchmakerAcceptPatches.IsServer)
Expand Down Expand Up @@ -1220,12 +1233,7 @@ public override IEnumerator vmethod_4(BotControllerSettings controllerSettings,
NotificationManagerClass.DisplayWarningNotification("You have set the deploy timer too low, resetting to 5!");
}

Task waitTask = WaitScreen(timeBeforeDeployLocal);

while (!waitTask.IsCompleted)
{
yield return null;
}
yield return WaitForOtherPlayers();

if (MatchmakerAcceptPatches.IsServer)
{
Expand Down Expand Up @@ -1283,19 +1291,17 @@ public override IEnumerator vmethod_4(BotControllerSettings controllerSettings,
}
}

Task deployTask = DeployScreen(timeBeforeDeployLocal);
/*Task deployTask = DeployScreen(timeBeforeDeployLocal);
while (!deployTask.IsCompleted)
{
yield return null;
}

yield return new WaitForEndOfFrame();
}*/

using (GClass21.StartWithToken("SessionRun"))
/*using (GClass21.StartWithToken("SessionRun"))
{
CreateExfiltrationPointAndInitDeathHandler();
}
}*/

// Add FreeCamController to GameWorld GameObject
Singleton<GameWorld>.Instance.gameObject.GetOrAddComponent<FreeCameraController>();
Expand All @@ -1309,8 +1315,11 @@ public override IEnumerator vmethod_4(BotControllerSettings controllerSettings,
Singleton<GameWorld>.Instance.MineManager.OnExplosion += OnMineExplode;
}

runCallback.Succeed();
/*runCallback.Succeed();*/

Singleton<BackendConfigSettingsClass>.Instance.TimeBeforeDeployLocal = Math.Max(Singleton<BackendConfigSettingsClass>.Instance.TimeBeforeDeployLocal, 5);

yield return base.vmethod_4(controllerSettings, spawnSystem, runCallback);
yield break;
}

Expand Down Expand Up @@ -1386,7 +1395,7 @@ private void OnMineExplode(MineDirectional directional)
}
}

public void CreateExfiltrationPointAndInitDeathHandler()
public override void vmethod_5()
{
Logger.LogInfo("CreateExfiltrationPointAndInitDeathHandler");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public static bool Prefix(TarkovApplication __instance)
Logger.LogDebug("TarkovApplication_LocalGameCreator_Patch:Prefix");

if (MatchmakerAcceptPatches.IsSinglePlayer)
{
return true;
}

ISession session = __instance.GetClientBackEndSession();
if (session == null)
Expand Down Expand Up @@ -96,7 +98,7 @@ public static async Task Postfix(Task __result, TarkovApplication __instance, Ti

if (MatchmakerAcceptPatches.IsClient)
{
timeHasComeScreenController.ChangeStatus("Joining Coop Game");
timeHasComeScreenController.ChangeStatus("Joining coop game...");

RaidSettingsRequest data = new();
RaidSettingsResponse raidSettingsResponse = await FikaRequestHandler.GetRaidSettings(data);
Expand All @@ -106,28 +108,30 @@ public static async Task Postfix(Task __result, TarkovApplication __instance, Ti
}
else
{
timeHasComeScreenController.ChangeStatus("Creating Coop Game");
timeHasComeScreenController.ChangeStatus("Creating coop game...");
}

StartHandler startHandler = new(__instance, session.Profile, session.ProfileOfPet, ____raidSettings.SelectedLocation, timeHasComeScreenController);

CoopGame coopgAme = CoopGame.Create(____inputTree, profile, ____localGameDateTime, session.InsuranceCompany, MonoBehaviourSingleton<MenuUI>.Instance, MonoBehaviourSingleton<GameUI>.Instance,
TimeSpan raidLimits = __instance.method_48(____raidSettings.SelectedLocation.EscapeTimeLimit);

CoopGame coopGame = CoopGame.Create(____inputTree, profile, ____localGameDateTime, session.InsuranceCompany, MonoBehaviourSingleton<MenuUI>.Instance, MonoBehaviourSingleton<GameUI>.Instance,
____raidSettings.SelectedLocation, timeAndWeather, ____raidSettings.WavesSettings, ____raidSettings.SelectedDateTime, new Callback<ExitStatus, TimeSpan, MetricsClass>(startHandler.HandleStart),
____fixedDeltaTime, EUpdateQueue.Update, session, TimeSpan.FromSeconds(60 * ____raidSettings.SelectedLocation.EscapeTimeLimit), ____raidSettings);
____fixedDeltaTime, EUpdateQueue.Update, session, raidLimits, ____raidSettings);

Singleton<AbstractGame>.Create(coopgAme);
FikaEventDispatcher.DispatchEvent(new AbstractGameCreatedEvent(coopgAme));
Singleton<AbstractGame>.Create(coopGame);
FikaEventDispatcher.DispatchEvent(new AbstractGameCreatedEvent(coopGame));

if (MatchmakerAcceptPatches.IsClient)
{
timeHasComeScreenController.ChangeStatus("Joined Coop Game");
coopGame.SetMatchmakerStatus("Coop game joined");
}
else
{
timeHasComeScreenController.ChangeStatus("Created Coop Game");
coopGame.SetMatchmakerStatus("Coop game created");
}

Task finishTask = coopgAme.InitPlayer(____raidSettings.BotSettings, ____backendUrl, null, new Callback(startHandler.HandleLoadComplete));
Task finishTask = coopGame.InitPlayer(____raidSettings.BotSettings, ____backendUrl, null, new Callback(startHandler.HandleLoadComplete));
__result = Task.WhenAll(finishTask);
}

Expand Down

0 comments on commit a9afc34

Please sign in to comment.