Skip to content

Commit

Permalink
switch list to dictionary and clean up airdrops manager
Browse files Browse the repository at this point in the history
  • Loading branch information
artehe committed May 31, 2024
1 parent 524626b commit 87842d8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 72 deletions.
4 changes: 4 additions & 0 deletions Source/AkiSupport/Airdrops/AirdropBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class AirdropBox : MonoBehaviour
private readonly int CROSSFADE = Shader.PropertyToID("_Crossfade");
private readonly int COLLISION = Animator.StringToHash("collision");

private static int AirdropContainerCount = 0;

public LootableContainer Container { get; set; }
private float fallSpeed;
private AirdropSynchronizableObject boxSync;
Expand Down Expand Up @@ -54,6 +56,8 @@ public static async Task<AirdropBox> Init(float crateFallSpeed)
instance.soundsDictionary = await LoadSounds();

instance.Container = instance.GetComponentInChildren<LootableContainer>();
instance.Container.Id = $"AirdropBox_{AirdropContainerCount}";
AirdropContainerCount++;

instance.boxSync = instance.GetComponent<AirdropSynchronizableObject>();
instance.boxLogic = new AirdropLogicClass();
Expand Down
88 changes: 35 additions & 53 deletions Source/Coop/Airdrops/SITAirdropsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using BepInEx.Logging;
using Comfort.Common;
using EFT;
using EFT.Interactive;
using StayInTarkov;
using StayInTarkov.AkiSupport.Airdrops;
using StayInTarkov.AkiSupport.Airdrops.Models;
Expand All @@ -14,7 +13,6 @@
using StayInTarkov.Networking;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

Expand All @@ -28,16 +26,23 @@ namespace Aki.Custom.Airdrops
public class SITAirdropsManager : MonoBehaviour
{
private AirdropPlane airdropPlane;
public AirdropBox AirdropBox { get; private set; }
private ItemFactoryUtil factory;

public bool isFlareDrop;
private float distanceTravelled = 0;

private DateTime LastSyncTime { get; set; }
private ManualLogSource Logger { get; } = BepInEx.Logging.Logger.CreateLogSource(nameof(SITAirdropsManager));

public AirdropBox AirdropBox { get; private set; }
public AirdropParametersModel AirdropParameters { get; set; }
private ManualLogSource Logger { get; set; }
public bool ClientPlaneSpawned { get; private set; }
public AirdropLootResultModel ClientAirdropLootResultModel { get; private set; }
public AirdropConfigModel ClientAirdropConfigModel { get; private set; }
public bool ClientLootBuilt { get; private set; }
public bool IsFlareDrop { get; set; }

void Awake()
{
Logger = BepInEx.Logging.Logger.CreateLogSource(nameof(SITAirdropsManager));
Logger.LogInfo("Awake");
Singleton<SITAirdropsManager>.Create(this);
}
Expand Down Expand Up @@ -67,7 +72,7 @@ public async void Start()

// The server will generate stuff ready for the packet

AirdropParameters = await AirdropUtil.InitAirdropParams(gameWorld, isFlareDrop);
AirdropParameters = await AirdropUtil.InitAirdropParams(gameWorld, IsFlareDrop);

if (!AirdropParameters.AirdropAvailable)
{
Expand Down Expand Up @@ -101,7 +106,6 @@ public async void Start()
await BuildLootContainer(AirdropParameters.Config);

StartCoroutine(SendParamsToClients());

}

public IEnumerator SendParamsToClients()
Expand All @@ -112,13 +116,10 @@ public IEnumerator SendParamsToClients()
yield return new WaitForSeconds(AirdropParameters.TimeToStart);

Logger.LogDebug("Sending Airdrop Params");
//var packet = new Dictionary<string, object>();
//packet.Add("serverId", SITGameComponent.GetServerId());
//packet.Add("m", "AirdropPacket");
//packet.Add("model", AirdropParameters);
//GameClient.SendData(packet.SITToJson());
var airdropPacket = new AirdropPacket();
airdropPacket.AirdropParametersModelJson = AirdropParameters.SITToJson();
AirdropPacket airdropPacket = new()
{
AirdropParametersModelJson = AirdropParameters.SITToJson()
};
GameClient.SendData(airdropPacket.Serialize());

yield break;
Expand Down Expand Up @@ -150,16 +151,11 @@ public async void FixedUpdate()

factory.BuildContainer(AirdropBox.Container, ClientAirdropConfigModel, ClientAirdropLootResultModel.DropType);
factory.AddLoot(AirdropBox.Container, ClientAirdropLootResultModel);
if (AirdropBox.Container != null)

if (AirdropBox.Container != null && SITGameComponent.TryGetCoopGameComponent(out SITGameComponent coopGameComponent))
{
if (SITGameComponent.TryGetCoopGameComponent(out var coopGameComponent))
{
List<WorldInteractiveObject> oldInteractiveObjectList = new(coopGameComponent.ListOfInteractiveObjects)
{
AirdropBox.Container
};
coopGameComponent.ListOfInteractiveObjects = [.. oldInteractiveObjectList];
}
Logger.LogDebug($"Adding Airdrop box with id {AirdropBox.Container.Id}");
coopGameComponent.WorldnteractiveObjects.TryAdd(AirdropBox.Container.Id, AirdropBox.Container);
}
}

Expand All @@ -183,8 +179,6 @@ public async void FixedUpdate()
{
return;
}


}
else
{
Expand All @@ -208,8 +202,10 @@ public async void FixedUpdate()
{
this.LastSyncTime = DateTime.Now;

AirdropBoxPositionSyncPacket packet = new();
packet.Position = AirdropBox.transform.position;
AirdropBoxPositionSyncPacket packet = new()
{
Position = AirdropBox.transform.position
};
GameClient.SendData(packet.Serialize());
}
}
Expand All @@ -227,8 +223,6 @@ public async void FixedUpdate()
}
}

float distanceTravelled = 0;

private void StartPlane()
{
airdropPlane.gameObject.SetActive(true);
Expand All @@ -244,42 +238,30 @@ private void StartBox()
AirdropBox.StartCoroutine(AirdropBox.DropCrate(dropPos));
}

private DateTime LastSyncTime { get; set; }
public bool ClientPlaneSpawned { get; private set; }
public AirdropLootResultModel ClientAirdropLootResultModel { get; private set; }
public AirdropConfigModel ClientAirdropConfigModel { get; private set; }
public bool ClientLootBuilt { get; private set; }

private async Task BuildLootContainer(AirdropConfigModel config)
{
if (!SITMatchmaking.IsServer)
return;

// Get the lootData for this Raid
var lootData = await factory.GetLoot();
AirdropLootResultModel lootData = await factory.GetLoot() ?? throw new Exception("Airdrops. Tried to BuildLootContainer without any Loot.");

// Send the lootData to Clients.
var airdropLootPacket = new AirdropLootPacket();
airdropLootPacket.AirdropLootResultModelJson = lootData.SITToJson();
airdropLootPacket.AirdropConfigModelJson = config.SITToJson();
AirdropLootPacket airdropLootPacket = new()
{
AirdropLootResultModelJson = lootData.SITToJson(),
AirdropConfigModelJson = config.SITToJson()
};
GameClient.SendData(airdropLootPacket.Serialize());

if (lootData == null)
throw new System.Exception("Airdrops. Tried to BuildLootContainer without any Loot.");

factory.BuildContainer(AirdropBox.Container, config, lootData.DropType);
factory.AddLoot(AirdropBox.Container, lootData);
ClientLootBuilt = true;
if (AirdropBox.Container != null)

if (AirdropBox.Container != null && SITGameComponent.TryGetCoopGameComponent(out SITGameComponent coopGameComponent))
{
if (SITGameComponent.TryGetCoopGameComponent(out var coopGameComponent))
{
List<WorldInteractiveObject> oldInteractiveObjectList = new(coopGameComponent.ListOfInteractiveObjects)
{
AirdropBox.Container
};
coopGameComponent.ListOfInteractiveObjects = [.. oldInteractiveObjectList];
}
Logger.LogDebug($"Adding Airdrop box with id {AirdropBox.Container.Id}");
coopGameComponent.WorldnteractiveObjects.TryAdd(AirdropBox.Container.Id, AirdropBox.Container);
}
}

Expand All @@ -297,7 +279,7 @@ private void SetDistanceToDrop()
airdropPlane.transform.position);
}

void OnDestroy()
protected void OnDestroy()
{
if (Singleton<SITAirdropsManager>.Instantiated)
Singleton<SITAirdropsManager>.Release(Singleton<SITAirdropsManager>.Instance);
Expand Down
7 changes: 5 additions & 2 deletions Source/Coop/Components/CoopGameComponents/SITGameComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace StayInTarkov.Coop.Components.CoopGameComponents
public class SITGameComponent : MonoBehaviour
{
#region Fields/Properties
public WorldInteractiveObject[] ListOfInteractiveObjects { get; set; }
public ConcurrentDictionary<string, WorldInteractiveObject> WorldnteractiveObjects { get; set; } = [];
private AkiBackendCommunication RequestingObj { get; set; }
public SITConfig SITConfig { get; private set; } = new SITConfig();
public string ServerId { get; set; } = null;
Expand Down Expand Up @@ -379,7 +379,10 @@ private void GameWorld_AfterGameStarted()
}

// Get a List of Interactive Objects (this is a slow method), so run once here to maintain a reference
ListOfInteractiveObjects = FindObjectsOfType<WorldInteractiveObject>();
foreach (WorldInteractiveObject worldInteractiveObject in FindObjectsOfType<WorldInteractiveObject>())
{
this.WorldnteractiveObjects.TryAdd(worldInteractiveObject.Id, worldInteractiveObject);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,11 @@ public override bool ToggleLauncher()

public override void CreateFlareShot(BulletClass flareItem, Vector3 shotPosition, Vector3 forward)
{
Logger.LogDebug(nameof(CreateFlareShot));
var createFlareShotPacket = new CreateFlareShotPacket(_player.ProfileId, shotPosition, forward, flareItem.TemplateId);
GameClient.SendData(createFlareShotPacket.Serialize());

base.CreateFlareShot(flareItem, shotPosition, forward);
}

}
}
11 changes: 2 additions & 9 deletions Source/Coop/NetworkPacket/Player/PlayerInteractWithDoorPacket.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
using Comfort.Common;
using EFT;
using EFT;
using EFT.Interactive;
using EFT.InventoryLogic;
using Newtonsoft.Json.Linq;
using StayInTarkov.Coop.Components.CoopGameComponents;
using StayInTarkov.Coop.Players;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StayInTarkov.Coop.NetworkPacket.Player
{
Expand Down Expand Up @@ -95,9 +90,7 @@ protected override void Process(CoopPlayerClient client)
}
}


WorldInteractiveObject worldInteractiveObject = SITGameComponent.GetCoopGameComponent().ListOfInteractiveObjects.FirstOrDefault(x => x.Id == ProcessJson["WIOId"].ToString());
if (worldInteractiveObject == null)
if (!SITGameComponent.GetCoopGameComponent().WorldnteractiveObjects.TryGetValue(ProcessJson["WIOId"].ToString(), out WorldInteractiveObject worldInteractiveObject))
{
StayInTarkovHelperConstants.Logger.LogError($"Player_ExecuteDoorInteraction_Patch:Replicated:Could not find {nameof(worldInteractiveObject)}");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using StayInTarkov.Coop.Components.CoopGameComponents;
using StayInTarkov.Coop.Players;
using System.IO;
using System.Linq;
using System.Text;

namespace StayInTarkov.Coop.NetworkPacket.Player
Expand Down Expand Up @@ -56,9 +55,7 @@ protected override void Process(CoopPlayerClient client)
return;
}

WorldInteractiveObject worldInteractiveObject = SITGameComponent.GetCoopGameComponent().ListOfInteractiveObjects.FirstOrDefault(x => x.Id == ProcessJson["WIOId"].ToString());

if (worldInteractiveObject == null)
if (!SITGameComponent.GetCoopGameComponent().WorldnteractiveObjects.TryGetValue(ProcessJson["WIOId"].ToString(), out WorldInteractiveObject worldInteractiveObject))
{
StayInTarkovHelperConstants.Logger.LogError($"Player_ExecuteDoorInteraction_Patch:Replicated:Could not find {nameof(worldInteractiveObject)}");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ public override ISITPacket Deserialize(byte[] bytes)
public override void Process()
{
SITGameComponent coopGameComponent = SITGameComponent.GetCoopGameComponent();
LootableContainer lootableContainer = coopGameComponent.ListOfInteractiveObjects.FirstOrDefault(x => x.Id == this.LootableContainerId) as LootableContainer;

if (lootableContainer == null)
if (!coopGameComponent.WorldnteractiveObjects.TryGetValue(LootableContainerId, out WorldInteractiveObject worldInteractiveObject))
{
StayInTarkovHelperConstants.Logger.LogError($"LootableContainerInteractionPacket:Process:Could not find {nameof(worldInteractiveObject)}");
return;
}
LootableContainer lootableContainer = worldInteractiveObject as LootableContainer;

string methodName = string.Empty;
switch (InteractionType)
Expand Down

0 comments on commit 87842d8

Please sign in to comment.