Skip to content

Commit

Permalink
Change CoopGame instance handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed Jun 8, 2024
1 parent aa1728a commit 69a6790
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 45 deletions.
12 changes: 5 additions & 7 deletions Fika.Core/Coop/Components/CoopHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public class CoopHandler : MonoBehaviour
{
#region Fields/Properties
public Dictionary<string, WorldInteractiveObject> ListOfInteractiveObjects { get; private set; } = [];
public CoopGame LocalGameInstance { get; internal set; }
public string ServerId { get; set; } = null;
public Dictionary<int, CoopPlayer> Players { get; } = new();
public Dictionary<int, CoopPlayer> Players = [];
public int HumanPlayers = 1;
public List<int> ExtractedPlayers { get; set; } = [];
public List<int> ExtractedPlayers = [];
ManualLogSource Logger;
public CoopPlayer MyPlayer => (CoopPlayer)Singleton<GameWorld>.Instance.MainPlayer;

public List<string> queuedProfileIds = [];
private Queue<SpawnObject> spawnQueue = new(50);

Expand Down Expand Up @@ -340,7 +340,7 @@ await Singleton<PoolManager>.Instance.LoadBundlesAndCreatePools(PoolManager.Pool
{
if (LocalGameInstance != null)
{
CoopGame coopGame = (CoopGame)LocalGameInstance;
CoopGame coopGame = LocalGameInstance;
BotsController botController = coopGame.BotsController;
if (botController != null)
{
Expand Down Expand Up @@ -507,7 +507,7 @@ private ObservedCoopPlayer SpawnObservedPlayer(Profile profile, Vector3 position

private IEnumerator AddClientToBotEnemies(BotsController botController, LocalPlayer playerToAdd)
{
CoopGame coopGame = (CoopGame)LocalGameInstance;
CoopGame coopGame = LocalGameInstance;

Logger.LogInfo($"AddClientToBotEnemies: " + playerToAdd.Profile.Nickname);

Expand Down Expand Up @@ -601,8 +601,6 @@ public void SetWeaponInHandsOfNewPlayer(Player person, Action successCallback)
}
});
}

public BaseLocalGame<EftGamePlayerOwner> LocalGameInstance { get; internal set; }
}

public enum ESpawnState
Expand Down
2 changes: 1 addition & 1 deletion Fika.Core/Coop/FreeCamera/FreeCameraController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ protected void Update()

if (quitState == CoopHandler.EQuitState.YouHaveExtracted && !extracted)
{
CoopGame coopGame = (CoopGame)coopHandler.LocalGameInstance;
CoopGame coopGame = coopHandler.LocalGameInstance;
if (coopGame.ExtractedPlayers.Contains((_player).NetId))
{
extracted = true;
Expand Down
6 changes: 2 additions & 4 deletions Fika.Core/Coop/GameMode/CoopGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@
namespace Fika.Core.Coop.GameMode
{
/// <summary>
/// A custom Game Type
/// Coop game used in Fika
/// </summary>
internal sealed class CoopGame : BaseLocalGame<EftGamePlayerOwner>, IBotGame, IFikaGame
public sealed class CoopGame : BaseLocalGame<EftGamePlayerOwner>, IBotGame, IFikaGame
{
public string InfiltrationPoint;
public bool HasAddedFenceRep = false;
//public bool forceStart = false;
public ExitStatus MyExitStatus { get; set; } = ExitStatus.Survived;
public string MyExitLocation { get; set; } = null;
public ISpawnSystem SpawnSystem;
Expand Down Expand Up @@ -752,7 +751,6 @@ public override async Task<LocalPlayer> vmethod_2(int playerId, Vector3 position
{
Logger.LogInfo("Creating CoopHandler");
await CreateCoopHandler();
CoopHandler.GetCoopHandler().LocalGameInstance = this;

profile.SetSpawnedInSession(profile.Side == EPlayerSide.Savage);

Expand Down
2 changes: 1 addition & 1 deletion Fika.Core/Coop/Players/CoopBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public override void OnDead(EDamageType damageType)
}
}

CoopGame coopGame = (CoopGame)Singleton<IBotGame>.Instance;
CoopGame coopGame = (CoopGame)Singleton<IFikaGame>.Instance;
if (coopGame.Bots.ContainsKey(ProfileId))
{
coopGame.Bots.Remove(ProfileId);
Expand Down
41 changes: 13 additions & 28 deletions Fika.Core/Networking/FikaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public NetManager NetClient
}
}
public NetPeer ServerConnection { get; private set; }
/*public string IP { get; private set; }
public int Port { get; private set; }*/
public bool SpawnPointsReceived { get; private set; } = false;
private readonly ManualLogSource logger = BepInEx.Logging.Logger.CreateLogSource("Fika.Client");
public bool Started
Expand Down Expand Up @@ -106,12 +104,6 @@ public void Init()

_netClient.Start();

/*GetHostRequest body = new(MatchmakerAcceptPatches.GetGroupId());
GetHostResponse result = FikaRequestHandler.GetHost(body);
IP = result.Ip;
Port = result.Port;*/

string ip = MatchmakerAcceptPatches.RemoteIp;
int port = MatchmakerAcceptPatches.RemotePort;

Expand Down Expand Up @@ -299,7 +291,7 @@ private void OnExfiltrationPacketReceived(ExfiltrationPacket packet)
if (exfilController.ExfiltrationPoints == null)
return;

CoopGame coopGame = (CoopGame)Singleton<IFikaGame>.Instance;
CoopGame coopGame = coopHandler.LocalGameInstance;

CarExtraction carExtraction = FindObjectOfType<CarExtraction>();

Expand Down Expand Up @@ -387,7 +379,7 @@ private void OnGenericPacketReceived(GenericPacket packet)
if (!coopHandler.ExtractedPlayers.Contains(packet.NetId))
{
coopHandler.ExtractedPlayers.Add(packet.NetId);
CoopGame coopGame = (CoopGame)coopHandler.LocalGameInstance;
CoopGame coopGame = coopHandler.LocalGameInstance;
coopGame.ExtractedPlayers.Add(packet.NetId);
coopGame.ClearHostAI(playerToApply);

Expand Down Expand Up @@ -435,7 +427,7 @@ private void OnGenericPacketReceived(GenericPacket packet)
ExfiltrationPoint exfilPoint = exfilController.ExfiltrationPoints.FirstOrDefault(x => x.Settings.Name == packet.ExfilName);
if (exfilPoint != null)
{
CoopGame game = (CoopGame)Singleton<AbstractGame>.Instance;
CoopGame game = coopHandler.LocalGameInstance;
exfilPoint.ExfiltrationStartTime = game != null ? game.PastTime : packet.ExfilStartTime;

if (exfilPoint.Status != EExfiltrationStatus.Countdown)
Expand Down Expand Up @@ -647,30 +639,23 @@ private void OnFirearmPacketReceived(WeaponPacket packet)

private void OnGameTimerPacketReceived(GameTimerPacket packet)
{
CoopHandler coopHandler = CoopHandler.GetCoopHandler();
if (coopHandler == null)
{
return;
}

TimeSpan sessionTime = new(packet.Tick);

if (coopHandler.LocalGameInstance is CoopGame coopGame)
CoopGame coopGame = coopHandler.LocalGameInstance;

GameTimerClass gameTimer = coopGame.GameTimer;
if (gameTimer.StartDateTime.HasValue && gameTimer.SessionTime.HasValue)
{
GameTimerClass gameTimer = coopGame.GameTimer;
if (gameTimer.StartDateTime.HasValue && gameTimer.SessionTime.HasValue)
if (gameTimer.PastTime.TotalSeconds < 3)
{
if (gameTimer.PastTime.TotalSeconds < 3)
{
return;
}
return;
}

TimeSpan timeRemain = gameTimer.PastTime + sessionTime;
TimeSpan timeRemain = gameTimer.PastTime + sessionTime;

gameTimer.ChangeSessionTime(timeRemain);
gameTimer.ChangeSessionTime(timeRemain);

Traverse.Create(coopGame.GameUi.TimerPanel).Field("dateTime_0").SetValue(gameTimer.StartDateTime.Value);
}
Traverse.Create(coopGame.GameUi.TimerPanel).Field("dateTime_0").SetValue(gameTimer.StartDateTime.Value);
}
}

Expand Down
8 changes: 4 additions & 4 deletions Fika.Core/Networking/FikaServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ private void OnGenericPacketReceived(GenericPacket packet, NetPeer peer)
if (!coopHandler.ExtractedPlayers.Contains(packet.NetId))
{
coopHandler.ExtractedPlayers.Add(packet.NetId);
CoopGame coopGame = (CoopGame)coopHandler.LocalGameInstance;
CoopGame coopGame = coopHandler.LocalGameInstance;
coopGame.ExtractedPlayers.Add(packet.NetId);
coopGame.ClearHostAI(playerToApply);

Expand All @@ -392,7 +392,7 @@ private void OnGenericPacketReceived(GenericPacket packet, NetPeer peer)
}
else if (packet.PacketType == EPackageType.LoadBot)
{
CoopGame coopGame = (CoopGame)Singleton<IFikaGame>.Instance;
CoopGame coopGame = coopHandler.LocalGameInstance;
coopGame.IncreaseLoadedPlayers(packet.BotNetId);

return;
Expand All @@ -406,7 +406,7 @@ private void OnGenericPacketReceived(GenericPacket packet, NetPeer peer)
ExfiltrationPoint exfilPoint = exfilController.ExfiltrationPoints.FirstOrDefault(x => x.Settings.Name == packet.ExfilName);
if (exfilPoint != null)
{
CoopGame game = (CoopGame)Singleton<AbstractGame>.Instance;
CoopGame game = coopHandler.LocalGameInstance;
exfilPoint.ExfiltrationStartTime = game != null ? game.PastTime : packet.ExfilStartTime;

if (exfilPoint.Status != EExfiltrationStatus.Countdown)
Expand Down Expand Up @@ -625,7 +625,7 @@ private void OnGameTimerPacketReceived(GameTimerPacket packet, NetPeer peer)
if (!packet.IsRequest)
return;

CoopGame game = (CoopGame)Singleton<AbstractGame>.Instance;
CoopGame game = coopHandler.LocalGameInstance;
if (game != null)
{
GameTimerPacket gameTimerPacket = new(false, (game.GameTimer.SessionTime - game.GameTimer.PastTime).Value.Ticks);
Expand Down

0 comments on commit 69a6790

Please sign in to comment.