From 3bc80f9f3c31d8ae232ebe80d638f8fd26c923f6 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Thu, 13 Jun 2024 00:33:40 +0200 Subject: [PATCH] WIP quest progress sharing --- .../CoopSharedQuestController.cs | 67 +++++++++++++++++++ .../Coop/PacketHandlers/BotPacketSender.cs | 6 ++ .../Coop/PacketHandlers/ClientPacketSender.cs | 7 ++ .../Coop/PacketHandlers/IPacketSender.cs | 2 + .../PacketHandlers/ObservedPacketSender.cs | 6 ++ .../Coop/PacketHandlers/ServerPacketSender.cs | 7 ++ Fika.Core/Coop/Players/CoopPlayer.cs | 2 +- Fika.Core/Networking/FikaClient.cs | 11 +++ Fika.Core/Networking/FikaServer.cs | 14 ++++ .../Communication/QuestConditionPacket.cs | 23 +++++++ 10 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 Fika.Core/Coop/ClientClasses/CoopSharedQuestController.cs create mode 100644 Fika.Core/Networking/Packets/Communication/QuestConditionPacket.cs diff --git a/Fika.Core/Coop/ClientClasses/CoopSharedQuestController.cs b/Fika.Core/Coop/ClientClasses/CoopSharedQuestController.cs new file mode 100644 index 00000000..9cc00266 --- /dev/null +++ b/Fika.Core/Coop/ClientClasses/CoopSharedQuestController.cs @@ -0,0 +1,67 @@ +using EFT; +using EFT.Quests; +using EFT.UI; +using Fika.Core.Coop.Players; +using Fika.Core.Networking.Packets; +using System; +using System.Collections.Generic; + +namespace Fika.Core.Coop.ClientClasses +{ + public sealed class CoopSharedQuestController(Profile profile, InventoryControllerClass inventoryController, + GInterface161 session, CoopPlayer player, bool fromServer = true) : GClass3228(profile, inventoryController, session, fromServer) + { + private readonly CoopPlayer player = player; + private readonly List lastFromNetwork = []; + + public override void OnConditionValueChanged(IConditionCounter conditional, EQuestStatus status, Condition condition, bool notify = true) + { + base.OnConditionValueChanged(conditional, status, condition, notify); + if (lastFromNetwork.Contains(condition.id)) + { + lastFromNetwork.Remove(condition.id); + return; + } + SendQuestPacket(conditional, condition.id); + } + + public void AddNetworkId(string id) + { + if (!lastFromNetwork.Contains(id)) + { + lastFromNetwork.Add(id); + } + } + + private void SendQuestPacket(IConditionCounter conditional, string conditionId) + { + if (conditional is GClass1258 quest) + { + GClass3241 counter = quest.ConditionCountersManager.GetCounter(conditionId); + if (counter != null) + { + QuestConditionPacket packet = new(player.Profile.Info.MainProfileNickname, counter.Id, counter.SourceId); + player.PacketSender.SendQuestPacket(ref packet); + } + } + } + + internal void ReceiveQuestPacket(ref QuestConditionPacket packet) + { + AddNetworkId(packet.Id); + foreach (GClass1258 quest in Quests) + { + if (quest.Id == packet.SourceId && quest.QuestStatus == EQuestStatus.Started) + { + GClass3241 counter = quest.ConditionCountersManager.GetCounter(packet.Id); + if (counter != null) + { + counter.Value++; + NotificationManagerClass.DisplayMessageNotification($"Received shared quest progression from {packet.Nickname}", + iconType: EFT.Communications.ENotificationIconType.Quest); + } + } + } + } + } +} diff --git a/Fika.Core/Coop/PacketHandlers/BotPacketSender.cs b/Fika.Core/Coop/PacketHandlers/BotPacketSender.cs index 1101d161..c9abf3c7 100644 --- a/Fika.Core/Coop/PacketHandlers/BotPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/BotPacketSender.cs @@ -3,6 +3,7 @@ using Comfort.Common; using Fika.Core.Coop.Players; using Fika.Core.Networking; +using Fika.Core.Networking.Packets; using LiteNetLib; using LiteNetLib.Utils; using System.Collections.Generic; @@ -34,6 +35,11 @@ public void Init() } + public void SendQuestPacket(ref QuestConditionPacket packet) + { + + } + protected void FixedUpdate() { if (!Enabled) diff --git a/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs b/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs index f7eae972..d5ac1b07 100644 --- a/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs @@ -6,6 +6,7 @@ using Fika.Core.Coop.GameMode; using Fika.Core.Coop.Players; using Fika.Core.Networking; +using Fika.Core.Networking.Packets; using LiteNetLib; using LiteNetLib.Utils; using System.Collections; @@ -43,6 +44,12 @@ public void Init() StartCoroutine(SyncWeather()); } + public void SendQuestPacket(ref QuestConditionPacket packet) + { + Writer.Reset(); + Client.SendData(Writer, ref packet, DeliveryMethod.ReliableUnordered); + } + protected void FixedUpdate() { if (player == null || Writer == null) diff --git a/Fika.Core/Coop/PacketHandlers/IPacketSender.cs b/Fika.Core/Coop/PacketHandlers/IPacketSender.cs index 773f4aed..b3cbdb9b 100644 --- a/Fika.Core/Coop/PacketHandlers/IPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/IPacketSender.cs @@ -1,6 +1,7 @@ // © 2024 Lacyway All Rights Reserved using Fika.Core.Networking; +using Fika.Core.Networking.Packets; using LiteNetLib.Utils; using System.Collections.Generic; @@ -19,6 +20,7 @@ public interface IPacketSender public Queue HealthSyncPackets { get; set; } public void Init(); + public void SendQuestPacket(ref QuestConditionPacket packet); public void DestroyThis(); } } diff --git a/Fika.Core/Coop/PacketHandlers/ObservedPacketSender.cs b/Fika.Core/Coop/PacketHandlers/ObservedPacketSender.cs index 9dbe68a8..9390eda4 100644 --- a/Fika.Core/Coop/PacketHandlers/ObservedPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/ObservedPacketSender.cs @@ -4,6 +4,7 @@ using Fika.Core.Coop.Matchmaker; using Fika.Core.Coop.Players; using Fika.Core.Networking; +using Fika.Core.Networking.Packets; using LiteNetLib; using LiteNetLib.Utils; using System.Collections.Generic; @@ -44,6 +45,11 @@ public void Init() } + public void SendQuestPacket(ref QuestConditionPacket packet) + { + + } + protected void Update() { if (player == null || Writer == null) diff --git a/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs b/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs index 65d9ac65..9f65a902 100644 --- a/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs +++ b/Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs @@ -7,6 +7,7 @@ using Fika.Core.Coop.GameMode; using Fika.Core.Coop.Players; using Fika.Core.Networking; +using Fika.Core.Networking.Packets; using HarmonyLib; using LiteNetLib; using LiteNetLib.Utils; @@ -47,6 +48,12 @@ public void Init() StartCoroutine(SendTrainTime()); } + public void SendQuestPacket(ref QuestConditionPacket packet) + { + Writer.Reset(); + Server.SendDataToAll(Writer, ref packet, DeliveryMethod.ReliableUnordered); + } + protected void FixedUpdate() { if (player == null || Writer == null || Server == null) diff --git a/Fika.Core/Coop/Players/CoopPlayer.cs b/Fika.Core/Coop/Players/CoopPlayer.cs index 1083d929..f0eeac14 100644 --- a/Fika.Core/Coop/Players/CoopPlayer.cs +++ b/Fika.Core/Coop/Players/CoopPlayer.cs @@ -66,7 +66,7 @@ public static async Task Create(int playerId, Vector3 position, Qua ISession session = Singleton>.Instance.GetClientBackEndSession(); - GClass3228 questController = new(profile, inventoryController, session, true); + CoopSharedQuestController questController = new(profile, inventoryController, session, player); questController.Init(); questController.Run(); diff --git a/Fika.Core/Networking/FikaClient.cs b/Fika.Core/Networking/FikaClient.cs index e90e0f26..63f335e0 100644 --- a/Fika.Core/Networking/FikaClient.cs +++ b/Fika.Core/Networking/FikaClient.cs @@ -9,6 +9,7 @@ using EFT.MovingPlatforms; using EFT.UI; using EFT.Weather; +using Fika.Core.Coop.ClientClasses; using Fika.Core.Coop.Components; using Fika.Core.Coop.Custom; using Fika.Core.Coop.GameMode; @@ -16,6 +17,7 @@ using Fika.Core.Coop.Players; using Fika.Core.Modding; using Fika.Core.Modding.Events; +using Fika.Core.Networking.Packets; using Fika.Core.Networking.Packets.Communication; using Fika.Core.Networking.Packets.GameWorld; using Fika.Core.Networking.Packets.Player; @@ -90,6 +92,7 @@ public void Init() packetProcessor.SubscribeNetSerializable(OnSyncNetIdPacketReceived); packetProcessor.SubscribeNetSerializable(OnOperationCallbackPacketReceived); packetProcessor.SubscribeNetSerializable(OnTextMessagePacketReceived); + packetProcessor.SubscribeNetSerializable(OnQuestConditionPacketReceived); _netClient = new NetManager(this) { @@ -119,6 +122,14 @@ public void Init() FikaEventDispatcher.DispatchEvent(new FikaClientCreatedEvent(this)); } + private void OnQuestConditionPacketReceived(QuestConditionPacket packet) + { + if (MyPlayer.GClass3226_0 is CoopSharedQuestController sharedQuestController) + { + sharedQuestController.ReceiveQuestPacket(ref packet); + } + } + private void OnTextMessagePacketReceived(TextMessagePacket packet) { logger.LogInfo($"Received message from: {packet.Nickname}, Message: {packet.Message}"); diff --git a/Fika.Core/Networking/FikaServer.cs b/Fika.Core/Networking/FikaServer.cs index c5b718c7..08212f1c 100644 --- a/Fika.Core/Networking/FikaServer.cs +++ b/Fika.Core/Networking/FikaServer.cs @@ -7,6 +7,7 @@ using EFT.Interactive; using EFT.InventoryLogic; using EFT.UI; +using Fika.Core.Coop.ClientClasses; using Fika.Core.Coop.Components; using Fika.Core.Coop.Custom; using Fika.Core.Coop.GameMode; @@ -16,6 +17,7 @@ using Fika.Core.Modding.Events; using Fika.Core.Networking.Http; using Fika.Core.Networking.Http.Models; +using Fika.Core.Networking.Packets; using Fika.Core.Networking.Packets.Communication; using Fika.Core.Networking.Packets.GameWorld; using Fika.Core.Networking.Packets.Player; @@ -94,6 +96,7 @@ public async Task Init() packetProcessor.SubscribeNetSerializable(OnBorderZonePacketReceived); packetProcessor.SubscribeNetSerializable(OnSendCharacterPacketReceived); packetProcessor.SubscribeNetSerializable(OnTextMessagePacketReceived); + packetProcessor.SubscribeNetSerializable(OnQuestConditionPacketReceived); _netServer = new NetManager(this) { @@ -187,6 +190,17 @@ public async Task Init() FikaEventDispatcher.DispatchEvent(new FikaServerCreatedEvent(this)); } + private void OnQuestConditionPacketReceived(QuestConditionPacket packet, NetPeer peer) + { + _dataWriter.Reset(); + SendDataToAll(_dataWriter, ref packet, DeliveryMethod.ReliableUnordered, peer); + + if (MyPlayer.GClass3226_0 is CoopSharedQuestController sharedQuestController) + { + sharedQuestController.ReceiveQuestPacket(ref packet); + } + } + private void OnTextMessagePacketReceived(TextMessagePacket packet, NetPeer peer) { logger.LogInfo($"Received message from: {packet.Nickname}, Message: {packet.Message}"); diff --git a/Fika.Core/Networking/Packets/Communication/QuestConditionPacket.cs b/Fika.Core/Networking/Packets/Communication/QuestConditionPacket.cs new file mode 100644 index 00000000..24e1309e --- /dev/null +++ b/Fika.Core/Networking/Packets/Communication/QuestConditionPacket.cs @@ -0,0 +1,23 @@ +using LiteNetLib.Utils; + +namespace Fika.Core.Networking.Packets +{ + public struct QuestConditionPacket(string nickname, string id, string sourceId) : INetSerializable + { + public string Nickname = nickname; + public string Id = id; + public string SourceId = sourceId; + + public void Deserialize(NetDataReader reader) + { + Id = reader.GetString(); + SourceId = reader.GetString(); + } + + public void Serialize(NetDataWriter writer) + { + writer.Put(Id); + writer.Put(SourceId); + } + } +}