From 55b3793d518a860a2eccbe2d6c2c65a5ae6a19c5 Mon Sep 17 00:00:00 2001 From: Sean Owen Date: Mon, 21 Sep 2020 23:12:55 +0100 Subject: [PATCH 1/4] Add palyer mode packet and corresponding packet writer - packet writer currently unfinished --- .../Outgoing/PlayerModePacket.cs | 42 +++++++++++++++++ .../PacketWriters/PlayerModePacketWriter.cs | 47 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/Fibula.Communications.Packets/Outgoing/PlayerModePacket.cs create mode 100644 src/Fibula.Protocol.V772/PacketWriters/PlayerModePacketWriter.cs diff --git a/src/Fibula.Communications.Packets/Outgoing/PlayerModePacket.cs b/src/Fibula.Communications.Packets/Outgoing/PlayerModePacket.cs new file mode 100644 index 0000000..8b04376 --- /dev/null +++ b/src/Fibula.Communications.Packets/Outgoing/PlayerModePacket.cs @@ -0,0 +1,42 @@ +// ----------------------------------------------------------------- +// +// Copyright (c) | Jose L. Nunez de Caceres et al. +// https://linkedin.com/in/nunezdecaceres +// +// All Rights Reserved. +// +// Licensed under the MIT License. See LICENSE in the project root for license information. +// +// ----------------------------------------------------------------- + +namespace Fibula.Communications.Packets.Outgoing +{ + using Fibula.Common.Contracts.Enumerations; + using Fibula.Communications.Contracts.Abstractions; + using Fibula.Communications.Contracts.Enumerations; + + /// + /// Class that represents a player's modes packet. + /// + public class PlayerModePacket : IOutboundPacket + { + /// + /// Initializes a new instance of the class. + /// + /// The chase mode. + public PlayerModePacket(ChaseMode chaseMode) + { + this.ChaseMode = chaseMode; + } + + /// + /// Gets the type of this packet. + /// + public OutgoingPacketType PacketType => OutgoingPacketType.PlayerModes; + + /// + /// Gets the chase mode to set. + /// + public ChaseMode ChaseMode { get; } + } +} diff --git a/src/Fibula.Protocol.V772/PacketWriters/PlayerModePacketWriter.cs b/src/Fibula.Protocol.V772/PacketWriters/PlayerModePacketWriter.cs new file mode 100644 index 0000000..85e52da --- /dev/null +++ b/src/Fibula.Protocol.V772/PacketWriters/PlayerModePacketWriter.cs @@ -0,0 +1,47 @@ +// ----------------------------------------------------------------- +// +// Copyright (c) | Jose L. Nunez de Caceres et al. +// https://linkedin.com/in/nunezdecaceres +// +// All Rights Reserved. +// +// Licensed under the MIT License. See LICENSE in the project root for license information. +// +// ----------------------------------------------------------------- + +namespace Fibula.Protocol.V772.PacketWriters +{ + using Fibula.Communications; + using Fibula.Communications.Contracts.Abstractions; + using Fibula.Communications.Packets.Outgoing; + using Fibula.Protocol.V772.Extensions; + using Serilog; + + /// + /// Class that represents a player mode packet writer for the game server. + /// + public class PlayerModePacketWriter : BasePacketWriter + { + /// + /// Initializes a new instance of the class. + /// + /// A reference to the logger in use. + public PlayerModePacketWriter(ILogger logger) + : base(logger) + { + } + + /// + public override void WriteToMessage(IOutboundPacket packet, ref INetworkMessage message) + { + if (!(packet is PlayerModePacket playerModePacket)) + { + this.Logger.Warning($"Invalid packet {packet.GetType().Name} routed to {this.GetType().Name}"); + + return; + } + + message.AddByte(playerModePacket.PacketType.ToByte()); + } + } +} From 83b6ec9a1eb99269783cdaf3b52edb56d0f44765 Mon Sep 17 00:00:00 2001 From: Sean Owen Date: Mon, 21 Sep 2020 23:45:31 +0100 Subject: [PATCH 2/4] add send notification to change modes operation --- src/Fibula.Mechanics/Operations/ChangeModesOperation.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Fibula.Mechanics/Operations/ChangeModesOperation.cs b/src/Fibula.Mechanics/Operations/ChangeModesOperation.cs index 1a2746b..caf4b9f 100644 --- a/src/Fibula.Mechanics/Operations/ChangeModesOperation.cs +++ b/src/Fibula.Mechanics/Operations/ChangeModesOperation.cs @@ -12,7 +12,11 @@ namespace Fibula.Mechanics.Operations { using Fibula.Common.Contracts.Enumerations; + using Fibula.Communications.Packets.Outgoing; + using Fibula.Creatures.Contracts.Abstractions; using Fibula.Mechanics.Contracts.Abstractions; + using Fibula.Mechanics.Notifications; + using System.Collections.Generic; /// /// Class that represents a change modes operation. @@ -77,6 +81,8 @@ protected override void Execute(IOperationContext context) } /* combatantCreature.SafeMode = this.IsSafeModeOn; */ + + this.SendNotification(context, new GenericNotification(() => new List() { combatantCreature as IPlayer }, new PlayerModePacket(this.ChaseMode))); } } } From efa878edab9b957f07f8ac68cfa5e7af4d6f9eed Mon Sep 17 00:00:00 2001 From: Sean Owen Date: Mon, 21 Sep 2020 23:49:42 +0100 Subject: [PATCH 3/4] add player mode packet writer to outgoing packet type dictionary in composition root extensions --- .../Extensions/CompositionRootExtensions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Fibula.Protocol.V772/Extensions/CompositionRootExtensions.cs b/src/Fibula.Protocol.V772/Extensions/CompositionRootExtensions.cs index 1de2479..32f04a1 100644 --- a/src/Fibula.Protocol.V772/Extensions/CompositionRootExtensions.cs +++ b/src/Fibula.Protocol.V772/Extensions/CompositionRootExtensions.cs @@ -119,6 +119,8 @@ public static void AddProtocol772GameServerComponents(this IServiceCollection se { OutgoingPacketType.CancelAttack, typeof(PlayerCancelAttackPacketWriter) }, { OutgoingPacketType.CancelWalk, typeof(PlayerCancelWalkPacketWriter) }, { OutgoingPacketType.WorldLight, typeof(WorldLightPacketWriter) }, + { OutgoingPacketType.PlayerModes, typeof(PlayerModePacketWriter) }, + }; foreach (var (packetType, type) in packetWritersToAdd) From a817664c644fd7ac212963c303d518d151f4fc87 Mon Sep 17 00:00:00 2001 From: Sean Owen Date: Fri, 25 Sep 2020 13:43:47 +0100 Subject: [PATCH 4/4] update naming to match the existing convention and update change modes oepration to take ICreature instead of simply an ID so that the GenericNotification can be used --- ...ayerModePacket.cs => PlayerModesPacket.cs} | 8 ++++---- src/Fibula.Mechanics/Game.cs | 2 +- .../Operations/ChangeModesOperation.cs | 19 +++++++++++++------ .../Extensions/CompositionRootExtensions.cs | 3 +-- ...etWriter.cs => PlayerModesPacketWriter.cs} | 10 +++++----- 5 files changed, 24 insertions(+), 18 deletions(-) rename src/Fibula.Communications.Packets/Outgoing/{PlayerModePacket.cs => PlayerModesPacket.cs} (86%) rename src/Fibula.Protocol.V772/PacketWriters/{PlayerModePacketWriter.cs => PlayerModesPacketWriter.cs} (83%) diff --git a/src/Fibula.Communications.Packets/Outgoing/PlayerModePacket.cs b/src/Fibula.Communications.Packets/Outgoing/PlayerModesPacket.cs similarity index 86% rename from src/Fibula.Communications.Packets/Outgoing/PlayerModePacket.cs rename to src/Fibula.Communications.Packets/Outgoing/PlayerModesPacket.cs index 8b04376..841f087 100644 --- a/src/Fibula.Communications.Packets/Outgoing/PlayerModePacket.cs +++ b/src/Fibula.Communications.Packets/Outgoing/PlayerModesPacket.cs @@ -1,5 +1,5 @@ // ----------------------------------------------------------------- -// +// // Copyright (c) | Jose L. Nunez de Caceres et al. // https://linkedin.com/in/nunezdecaceres // @@ -18,13 +18,13 @@ namespace Fibula.Communications.Packets.Outgoing /// /// Class that represents a player's modes packet. /// - public class PlayerModePacket : IOutboundPacket + public class PlayerModesPacket : IOutboundPacket { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The chase mode. - public PlayerModePacket(ChaseMode chaseMode) + public PlayerModesPacket(ChaseMode chaseMode) { this.ChaseMode = chaseMode; } diff --git a/src/Fibula.Mechanics/Game.cs b/src/Fibula.Mechanics/Game.cs index 5073136..933a5f7 100644 --- a/src/Fibula.Mechanics/Game.cs +++ b/src/Fibula.Mechanics/Game.cs @@ -614,7 +614,7 @@ public void SetCombatantModes(ICombatant combatant, FightMode fightMode, ChaseMo return; } - var changeModesOp = new ChangeModesOperation(combatant.Id, fightMode, chaseMode, safeModeOn); + var changeModesOp = new ChangeModesOperation(combatant, fightMode, chaseMode, safeModeOn); this.DispatchOperation(changeModesOp); } diff --git a/src/Fibula.Mechanics/Operations/ChangeModesOperation.cs b/src/Fibula.Mechanics/Operations/ChangeModesOperation.cs index caf4b9f..0cbf1b1 100644 --- a/src/Fibula.Mechanics/Operations/ChangeModesOperation.cs +++ b/src/Fibula.Mechanics/Operations/ChangeModesOperation.cs @@ -11,12 +11,13 @@ namespace Fibula.Mechanics.Operations { + using System.Collections.Generic; using Fibula.Common.Contracts.Enumerations; using Fibula.Communications.Packets.Outgoing; using Fibula.Creatures.Contracts.Abstractions; + using Fibula.Map.Contracts.Extensions; using Fibula.Mechanics.Contracts.Abstractions; using Fibula.Mechanics.Notifications; - using System.Collections.Generic; /// /// Class that represents a change modes operation. @@ -26,18 +27,24 @@ public class ChangeModesOperation : Operation /// /// Initializes a new instance of the class. /// - /// The id of the creature setting the modes. + /// The creature which is changing mode. /// The fight mode to set. /// The chase mode to set. /// A value indicating whether the safety mode is on. - public ChangeModesOperation(uint requestorId, FightMode fightMode, ChaseMode chaseMode, bool safeModeOn) - : base(requestorId) + public ChangeModesOperation(ICreature creature, FightMode fightMode, ChaseMode chaseMode, bool safeModeOn) + : base(creature.Id) { + this.Creature = creature; this.FightMode = fightMode; this.ChaseMode = chaseMode; this.IsSafeModeOn = safeModeOn; } + /// + /// Gets a reference to the creature turning. + /// + public ICreature Creature { get; } + /// /// Gets the fight mode to set. /// @@ -59,7 +66,7 @@ public ChangeModesOperation(uint requestorId, FightMode fightMode, ChaseMode cha /// A reference to the operation context. protected override void Execute(IOperationContext context) { - var onCreature = this.GetRequestor(context.CreatureFinder); + ICreature onCreature = this.GetRequestor(context.CreatureFinder); if (onCreature == null || !(onCreature is ICombatant combatantCreature)) { @@ -82,7 +89,7 @@ protected override void Execute(IOperationContext context) /* combatantCreature.SafeMode = this.IsSafeModeOn; */ - this.SendNotification(context, new GenericNotification(() => new List() { combatantCreature as IPlayer }, new PlayerModePacket(this.ChaseMode))); + this.SendNotification(context, new GenericNotification(() => context.Map.PlayersThatCanSee(this.Creature.Location), new PlayerModesPacket(this.ChaseMode))); } } } diff --git a/src/Fibula.Protocol.V772/Extensions/CompositionRootExtensions.cs b/src/Fibula.Protocol.V772/Extensions/CompositionRootExtensions.cs index 32f04a1..78434ff 100644 --- a/src/Fibula.Protocol.V772/Extensions/CompositionRootExtensions.cs +++ b/src/Fibula.Protocol.V772/Extensions/CompositionRootExtensions.cs @@ -119,8 +119,7 @@ public static void AddProtocol772GameServerComponents(this IServiceCollection se { OutgoingPacketType.CancelAttack, typeof(PlayerCancelAttackPacketWriter) }, { OutgoingPacketType.CancelWalk, typeof(PlayerCancelWalkPacketWriter) }, { OutgoingPacketType.WorldLight, typeof(WorldLightPacketWriter) }, - { OutgoingPacketType.PlayerModes, typeof(PlayerModePacketWriter) }, - + { OutgoingPacketType.PlayerModes, typeof(PlayerModesPacketWriter) }, }; foreach (var (packetType, type) in packetWritersToAdd) diff --git a/src/Fibula.Protocol.V772/PacketWriters/PlayerModePacketWriter.cs b/src/Fibula.Protocol.V772/PacketWriters/PlayerModesPacketWriter.cs similarity index 83% rename from src/Fibula.Protocol.V772/PacketWriters/PlayerModePacketWriter.cs rename to src/Fibula.Protocol.V772/PacketWriters/PlayerModesPacketWriter.cs index 85e52da..d03fb20 100644 --- a/src/Fibula.Protocol.V772/PacketWriters/PlayerModePacketWriter.cs +++ b/src/Fibula.Protocol.V772/PacketWriters/PlayerModesPacketWriter.cs @@ -1,5 +1,5 @@ // ----------------------------------------------------------------- -// +// // Copyright (c) | Jose L. Nunez de Caceres et al. // https://linkedin.com/in/nunezdecaceres // @@ -20,13 +20,13 @@ namespace Fibula.Protocol.V772.PacketWriters /// /// Class that represents a player mode packet writer for the game server. /// - public class PlayerModePacketWriter : BasePacketWriter + public class PlayerModesPacketWriter : BasePacketWriter { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// A reference to the logger in use. - public PlayerModePacketWriter(ILogger logger) + public PlayerModesPacketWriter(ILogger logger) : base(logger) { } @@ -34,7 +34,7 @@ public PlayerModePacketWriter(ILogger logger) /// public override void WriteToMessage(IOutboundPacket packet, ref INetworkMessage message) { - if (!(packet is PlayerModePacket playerModePacket)) + if (!(packet is PlayerModesPacket playerModePacket)) { this.Logger.Warning($"Invalid packet {packet.GetType().Name} routed to {this.GetType().Name}");