Skip to content

Commit

Permalink
Refactored some handlers and added comments to classes/methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
WakooMan committed Feb 9, 2024
1 parent e000b27 commit 0404ddb
Show file tree
Hide file tree
Showing 24 changed files with 204 additions and 194 deletions.
26 changes: 0 additions & 26 deletions source/Coop.Core/Client/Services/Kingdoms/ClientKingdomHandler.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Common.Messaging;
using Common.Network;
using Coop.Core.Server.Services.Kingdoms.Messages;
using GameInterface.Services.Kingdoms.Messages;

namespace Coop.Core.Client.Services.Kingdoms.Handlers;

/// <summary>
/// Client side handler for Kingdom internal and network messages
/// </summary>
public class ClientKingdomHandler : IHandler
{

private readonly IMessageBroker messageBroker;
private readonly INetwork network;

public ClientKingdomHandler(IMessageBroker messageBroker, INetwork network)
{
this.messageBroker = messageBroker;
this.network = network;
messageBroker.Subscribe<NetworkAddDecision>(HandleNetworkAddDecision);
messageBroker.Subscribe<NetworkRemoveDecision>(HandleNetworkRemoveDecision);
}

private void HandleNetworkRemoveDecision(MessagePayload<NetworkRemoveDecision> obj)
{
var payload = obj.What;
var message = new RemoveDecision(payload.KingdomId, payload.Index);
messageBroker.Publish(this, message);
}

private void HandleNetworkAddDecision(MessagePayload<NetworkAddDecision> obj)
{
var payload = obj.What;
var message = new AddDecision(payload.KingdomId, payload.Data, payload.IgnoreInfluenceCost);
messageBroker.Publish(this, message);
}

public void Dispose()
{
messageBroker.Unsubscribe<NetworkAddDecision>(HandleNetworkAddDecision);
messageBroker.Unsubscribe<NetworkRemoveDecision>(HandleNetworkRemoveDecision);
}
}


This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Common.Messaging;
using Common.Network;

using Coop.Core.Server.Services.Kingdoms.Messages;
using GameInterface.Services.Kingdoms.Messages;

namespace Coop.Core.Server.Services.Kingdoms.Handlers;

Expand All @@ -16,9 +17,28 @@ public ServerKingdomHandler(IMessageBroker messageBroker, INetwork network)
{
this.messageBroker = messageBroker;
this.network = network;
messageBroker.Subscribe<LocalDecisionAdded>(HandleLocalDecisionAdded);
messageBroker.Subscribe<LocalDecisionRemoved>(HandleLocalDecisionRemoved);
}


private void HandleLocalDecisionRemoved(MessagePayload<LocalDecisionRemoved> obj)
{
var payload = obj.What;
var message = new NetworkRemoveDecision(payload.KingdomId, payload.Index);
network.SendAll(message);
}

private void HandleLocalDecisionAdded(MessagePayload<LocalDecisionAdded> obj)
{
var payload = obj.What;
var message = new NetworkAddDecision(payload.KingdomId, payload.Data, payload.IgnoreInfluenceCost);
network.SendAll(message);
}


public void Dispose()
{
messageBroker.Unsubscribe<LocalDecisionAdded>(HandleLocalDecisionAdded);
messageBroker.Unsubscribe<LocalDecisionRemoved>(HandleLocalDecisionRemoved);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Coop.Core.Server.Services.Kingdoms.Messages
{
/// <summary>
/// Add decision network message.
/// </summary>
[ProtoContract(SkipConstructor = true)]
public class NetworkAddDecision : ICommand
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace Coop.Core.Server.Services.Kingdoms.Messages
{
/// <summary>
/// Remove decision network message.
/// </summary>
[ProtoContract(SkipConstructor = true)]
public class NetworkRemoveDecision : ICommand
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

namespace Coop.IntegrationTests.Kingdoms
{
/// <summary>
/// Test class for NetworkAddDecision message handling.
/// </summary>
public class KingdomAddDecisionTest
{
// Creates a test environment with 1 server and 2 clients by default
internal TestEnvironment TestEnvironment { get; } = new TestEnvironment();

/// <summary>
/// Used to Test that client recieves AddDecision messages.
/// Used to Test that client recieves NetworkAddDecision messages.
/// </summary>
[Fact]
public void ServerKingdom_AddDecision_Publishes_AllClients()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

namespace Coop.IntegrationTests.Kingdoms
{
/// <summary>
/// Test class for NetworkRemoveDecision message handling.
/// </summary>
public class KingdomRemoveDecisionTest
{
// Creates a test environment with 1 server and 2 clients by default
internal TestEnvironment TestEnvironment { get; } = new TestEnvironment();

/// <summary>
/// Used to Test that client recieves RemoveDecision messages.
/// Used to Test that client recieves NetworkRemoveDecision messages.
/// </summary>
[Fact]
public void ServerKingdom_RemoveDecision_Publishes_AllClients()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace GameInterface.Services.Kingdoms.Data
{
/// <summary>
/// Class for serializing <see cref="DeclareWarDecision"> class.
/// </summary>
[ProtoContract(SkipConstructor = true)]
public class DeclareWarDecisionData : KingdomDecisionData
{
Expand All @@ -23,6 +26,7 @@ public DeclareWarDecisionData(string proposedClanId, string kingdomId, long trig
FactionToDeclareWarOnId = factionToDeclareWarOnId;
}

/// <inheritdoc/>
public override bool TryGetKingdomDecision(IObjectManager objectManager,out KingdomDecision kingdomDecision)
{
if (!TryGetProposerClanAndKingdom(objectManager, out Clan proposerClan, out Kingdom kingdom) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace GameInterface.Services.Kingdoms.Data
{
/// <summary>
/// Class for serializing <see cref="ExpelClanFromKingdomDecision"> class.
/// </summary>
[ProtoContract(SkipConstructor = true)]
public class ExpelClanFromKingdomDecisionData : KingdomDecisionData
{
Expand All @@ -24,6 +27,7 @@ public ExpelClanFromKingdomDecisionData(string proposedClanId, string kingdomId,
OldKingdomId = oldKingdomId;
}

/// <inheritdoc/>
public override bool TryGetKingdomDecision(IObjectManager objectManager, out KingdomDecision kingdomDecision)
{
if (!TryGetProposerClanAndKingdom(objectManager, out Clan proposerClan, out Kingdom kingdom) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace GameInterface.Services.Kingdoms.Data
{
/// <summary>
/// Class for serializing <see cref="KingSelectionKingdomDecision"> class.
/// </summary>
[ProtoContract(SkipConstructor = true)]
public class KingSelectionKingdomDecisionData : KingdomDecisionData
{
Expand All @@ -21,6 +24,7 @@ public KingSelectionKingdomDecisionData(string proposedClanId, string kingdomId,
ClanToExcludeId = clanToExcludeId;
}

/// <inheritdoc/>
public override bool TryGetKingdomDecision(IObjectManager objectManager, out KingdomDecision kingdomDecision)
{
if (!TryGetProposerClanAndKingdom(objectManager, out Clan proposerClan, out Kingdom kingdom) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace GameInterface.Services.Kingdoms.Data
{
/// <summary>
/// Base class for serializing <see cref="KingdomDecision"> class.
/// </summary>
[ProtoContract(SkipConstructor = true)]
[ProtoInclude(100, typeof(DeclareWarDecisionData))]
[ProtoInclude(101, typeof(ExpelClanFromKingdomDecisionData))]
Expand Down Expand Up @@ -46,6 +49,13 @@ protected KingdomDecisionData(string proposedClanId, string kingdomId, long trig
PlayerExamined = playerExamined;
}

/// <summary>
/// Tries to get the proposerClan and kingdom based on the deserialized Ids.
/// </summary>
/// <param name="objectManager">object manager.</param>
/// <param name="proposerClan">proposer clan.</param>
/// <param name="kingdom">kingdom.</param>
/// <returns>True if proposerClan and kingdom is successfully received from the object manager, else false.</returns>
protected bool TryGetProposerClanAndKingdom(IObjectManager objectManager, out Clan proposerClan, out Kingdom kingdom)
{
if (!objectManager.TryGetObject(ProposerClanId, out proposerClan) |
Expand All @@ -56,6 +66,12 @@ protected bool TryGetProposerClanAndKingdom(IObjectManager objectManager, out Cl
return true;
}

/// <summary>
/// Sets the base class's properties for the KingdomDecision object.
/// </summary>
/// <param name="kingdomDecision">kingdom decision object.</param>
/// <param name="proposerClan">proposer clan.</param>
/// <param name="kingdom">kingdom.</param>
protected void SetKingdomDecisionProperties(KingdomDecision kingdomDecision, Clan proposerClan, Kingdom kingdom)
{
SetProposerClanMethod(kingdomDecision, proposerClan);
Expand All @@ -66,7 +82,10 @@ protected void SetKingdomDecisionProperties(KingdomDecision kingdomDecision, Cla
SetTriggerTimeMethod(kingdomDecision, (CampaignTime)CampaignTimeCtr.Invoke(new object[] { TriggerTime }));
}


/// <summary>
/// Tries to get/create the kingdom decision object from the current KingdomDecisionData object.
/// Implemented in derived class.
/// </summary>
public abstract bool TryGetKingdomDecision(IObjectManager objectManager, out KingdomDecision kingdomDecision);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace GameInterface.Services.Kingdoms.Data
{
/// <summary>
/// Class for serializing <see cref="KingdomPolicyDecision"> class.
/// </summary>
[ProtoContract(SkipConstructor = true)]
public class KingdomPolicyDecisionData : KingdomDecisionData
{
Expand All @@ -31,6 +34,7 @@ public KingdomPolicyDecisionData(string proposedClanId, string kingdomId, long t
KingdomPolicies = kingdomPolicies;
}

/// <inheritdoc/>
public override bool TryGetKingdomDecision(IObjectManager objectManager, out KingdomDecision kingdomDecision)
{
if (!TryGetProposerClanAndKingdom(objectManager, out Clan proposerClan, out Kingdom kingdom) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace GameInterface.Services.Kingdoms.Data
{
/// <summary>
/// Class for serializing <see cref="MakePeaceKingdomDecision"> class.
/// </summary>
[ProtoContract(SkipConstructor = true)]
public class MakePeaceKingdomDecisionData : KingdomDecisionData
{
Expand All @@ -31,6 +34,7 @@ public MakePeaceKingdomDecisionData(string proposedClanId, string kingdomId, lon
ApplyResults= applyResults;
}

/// <inheritdoc/>
public override bool TryGetKingdomDecision(IObjectManager objectManager, out KingdomDecision kingdomDecision)
{
if (!TryGetProposerClanAndKingdom(objectManager, out Clan proposerClan, out Kingdom kingdom) ||
Expand Down
Loading

0 comments on commit 0404ddb

Please sign in to comment.