~ruther/NosSmooth

d5d868ef2fc037b8ec5e7993743d14b0c843a8ad — František Boháček 3 years ago 18b22db
chore: move data classes to correct namespaces
7 files changed, 127 insertions(+), 112 deletions(-)

D Core/NosSmooth.Game/Data/Characters/Family.cs
A Core/NosSmooth.Game/Data/Info/Position.cs
R Core/NosSmooth.Game/Data/{Position.cs => Inventory/InventoryItem.cs}
D Core/NosSmooth.Game/Data/Inventory/Item.cs
A Core/NosSmooth.Game/Data/Social/Family.cs
R Core/NosSmooth.Game/Events/{Handlers/EventDispatcher.cs => Core/EventDispatcher.cs}
R Core/NosSmooth.Game/Events/{Handlers/IGameResponder.cs => Core/IGameResponder.cs}
D Core/NosSmooth.Game/Data/Characters/Family.cs => Core/NosSmooth.Game/Data/Characters/Family.cs +0 -9
@@ 1,9 0,0 @@
// 
// Family.cs
// 
// Copyright (c) Christofel authors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace NosSmooth.Game.Data.Character;

public record Family();
\ No newline at end of file

A Core/NosSmooth.Game/Data/Info/Position.cs => Core/NosSmooth.Game/Data/Info/Position.cs +23 -0
@@ 0,0 1,23 @@
//
//  Position.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace NosSmooth.Game.Data.Info;

/// <summary>
/// Represents nostale position on map.
/// </summary>
public record Position
{
    /// <summary>
    /// Gets the x coordinate.
    /// </summary>
    public long X { get; internal set; }

    /// <summary>
    /// Gets the y coordinate.
    /// </summary>
    public long Y { get; internal set; }
}
\ No newline at end of file

R Core/NosSmooth.Game/Data/Position.cs => Core/NosSmooth.Game/Data/Inventory/InventoryItem.cs +4 -6
@@ 1,14 1,12 @@
//
//  Position.cs
//  InventoryItem.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace NosSmooth.Game.Data;
namespace NosSmooth.Game.Data.Inventory;

/// <summary>
/// Represents nostale positition on map.
/// Represents item in bag inventory of the character.
/// </summary>
/// <param name="X">The x coordinate.</param>
/// <param name="Y">The y coordinate.</param>
public record Position(int X, int Y);
\ No newline at end of file
public record InventoryItem();
\ No newline at end of file

D Core/NosSmooth.Game/Data/Inventory/Item.cs => Core/NosSmooth.Game/Data/Inventory/Item.cs +0 -12
@@ 1,12 0,0 @@
// 
// Item.cs
// 
// Copyright (c) Christofel authors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace NosSmooth.Game.Data.Inventory;

public class Item
{
    
}
\ No newline at end of file

A Core/NosSmooth.Game/Data/Social/Family.cs => Core/NosSmooth.Game/Data/Social/Family.cs +15 -0
@@ 0,0 1,15 @@
//
//  Family.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace NosSmooth.Game.Data.Social;

/// <summary>
/// Represents nostale family entity.
/// </summary>
/// <param name="Id">The id of the family.</param>
/// <param name="Name">The name of the family.</param>
/// <param name="Level">The level of the entity.</param>
public record Family(string? Id, string? Name, byte Level);
\ No newline at end of file

R Core/NosSmooth.Game/Events/Handlers/EventDispatcher.cs => Core/NosSmooth.Game/Events/Core/EventDispatcher.cs +52 -51
@@ 1,51 1,52 @@
//
//  EventDispatcher.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Extensions.DependencyInjection;
using Remora.Results;

namespace NosSmooth.Game.Events.Handlers;

/// <summary>
/// Dispatches <see cref="IGameResponder"/> with <see cref="IGameEvent"/>.
/// </summary>
public class EventDispatcher
{
    private readonly IServiceProvider _provider;

    /// <summary>
    /// Initializes a new instance of the <see cref="EventDispatcher"/> class.
    /// </summary>
    /// <param name="provider">The services provider.</param>
    public EventDispatcher(IServiceProvider provider)
    {
        _provider = provider;
    }

    /// <summary>
    /// Dispatches game responders that are registered in the service collection.
    /// </summary>
    /// <param name="event">The event to dispatch.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <typeparam name="TEvent">The type of the event.</typeparam>
    /// <returns>A result that may or may not have succeeded.</returns>
    public async Task<Result> DispatchEvent<TEvent>(TEvent @event, CancellationToken ct = default)
        where TEvent : IGameEvent
    {
        var results = await Task.WhenAll(
            _provider
                .GetServices<IGameResponder<TEvent>>()
                .Select(responder => responder.Respond(@event, ct))
        );

        return results.Length switch
        {
            0 => Result.FromSuccess(),
            1 => results[0],
            _ => new AggregateError(results.Cast<IResult>().ToArray()),
        };
    }
}
\ No newline at end of file
//
//  EventDispatcher.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Extensions.DependencyInjection;
using Remora.Results;

namespace NosSmooth.Game.Events.Core;

/// <summary>
/// Dispatches <see cref="IGameResponder"/> with <see cref="IGameEvent"/>.
/// </summary>
public class EventDispatcher
{
    private readonly IServiceProvider _provider;

    /// <summary>
    /// Initializes a new instance of the <see cref="EventDispatcher"/> class.
    /// </summary>
    /// <param name="provider">The services provider.</param>
    public EventDispatcher(IServiceProvider provider)
    {
        _provider = provider;
    }

    /// <summary>
    /// Dispatches game responders that are registered in the service collection.
    /// </summary>
    /// <param name="event">The event to dispatch.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <typeparam name="TEvent">The type of the event.</typeparam>
    /// <returns>A result that may or may not have succeeded.</returns>
    public async Task<Result> DispatchEvent<TEvent>(TEvent @event, CancellationToken ct = default)
        where TEvent : IGameEvent
    {
        using var scope = _provider.CreateScope();
        var results = await Task.WhenAll(
            scope.ServiceProvider
                .GetServices<IGameResponder<TEvent>>()
                .Select(responder => responder.Respond(@event, ct))
        );

        return results.Length switch
        {
            0 => Result.FromSuccess(),
            1 => results[0],
            _ => new AggregateError(results.Cast<IResult>().ToArray()),
        };
    }
}

R Core/NosSmooth.Game/Events/Handlers/IGameResponder.cs => Core/NosSmooth.Game/Events/Core/IGameResponder.cs +33 -34
@@ 1,34 1,33 @@
//
//  IGameResponder.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using OneOf.Types;
using Remora.Results;

namespace NosSmooth.Game.Events.Handlers;

/// <summary>
/// Represents interface for classes that respond to <see cref="IGameEvent"/>.
/// </summary>
public interface IGameResponder
{
}

/// <summary>
/// Represents interface for classes that respond to game events.
/// Responds to <typeparamref name="TPacket"/>.
/// </summary>
/// <typeparam name="TEvent">The event type this responder responds to.</typeparam>
public interface IGameResponder<TEvent> : IGameResponder
    where TEvent : IGameEvent
{
    /// <summary>
    /// Respond to the given packet.
    /// </summary>
    /// <param name="packet">The packet to respond to.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Task<Result> Respond(TEvent packet, CancellationToken ct = default);
}
\ No newline at end of file
//
//  IGameResponder.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Remora.Results;

namespace NosSmooth.Game.Events.Core;

/// <summary>
/// Represents interface for classes that respond to <see cref="IGameEvent"/>.
/// </summary>
public interface IGameResponder
{
}

/// <summary>
/// Represents interface for classes that respond to game events.
/// Responds to <typeparamref name="TEvent"/>.
/// </summary>
/// <typeparam name="TEvent">The event type this responder responds to.</typeparam>
public interface IGameResponder<TEvent> : IGameResponder
    where TEvent : IGameEvent
{
    /// <summary>
    /// Respond to the given packet.
    /// </summary>
    /// <param name="gameEvent">The packet to respond to.</param>
    /// <param name="ct">The cancellation token for cancelling the operation.</param>
    /// <returns>A result that may or may not have succeeded.</returns>
    public Task<Result> Respond(TEvent gameEvent, CancellationToken ct = default);
}

Do not follow this link