Skip to content

Commit

Permalink
refactor: 重構MonopolyEventBus、事件處理及測試
Browse files Browse the repository at this point in the history
  • Loading branch information
aa89227 committed Jan 1, 2024
1 parent e844226 commit 04e741f
Show file tree
Hide file tree
Showing 130 changed files with 1,647 additions and 526 deletions.
3 changes: 2 additions & 1 deletion Client/Pages/Ready/ReadyPage.razor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Client.Pages.Ready.Entities;
using Microsoft.AspNetCore.Components;
using SharedLibrary.ResponseArgs;
using SharedLibrary.ResponseArgs.ReadyRoom;

namespace Client.Pages.Ready;

Expand Down Expand Up @@ -28,7 +29,7 @@ private void OnPlayerJoinGameEvent(PlayerJoinGameEvent e)
Players = Players.Append(player).ToList();
Update();
}
private void OnGetReadyInfoEvent(GetReadyInfoEvent e)
private void OnGetReadyInfoEvent(GetReadyInfoEventArgs e)
{
Players = e.Players.Select(x => new Player
{
Expand Down
9 changes: 5 additions & 4 deletions Client/Pages/TypedHubConnection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.SignalR.Client;
using SharedLibrary.ResponseArgs;
using SharedLibrary.ResponseArgs.ReadyRoom;

namespace Client.Pages;

Expand All @@ -10,15 +11,15 @@ internal class TypedHubConnection
public event PlayerJoinGameEventDelegate? PlayerJoinGameEventHandler;
public delegate void PlayerJoinGameEventDelegate(PlayerJoinGameEvent e);
public event GetReadyInfoEventDelegate? GetReadyInfoEventHandler;
public delegate void GetReadyInfoEventDelegate(GetReadyInfoEvent e);
public delegate void GetReadyInfoEventDelegate(GetReadyInfoEventArgs e);
public event WelcomeEventDelegate? WelcomeEventHandler;
public delegate void WelcomeEventDelegate(WelcomeEvent e);
public delegate void WelcomeEventDelegate(WelcomeEventArgs e);

public TypedHubConnection(HubConnection hubConnection)
{
hubConnection.On<PlayerJoinGameEvent>(nameof(PlayerJoinGameEvent), (e) => PlayerJoinGameEventHandler?.Invoke(e));
hubConnection.On<GetReadyInfoEvent>(nameof(GetReadyInfoEvent), (e) => GetReadyInfoEventHandler?.Invoke(e));
hubConnection.On<WelcomeEvent>(nameof(WelcomeEvent), (e) => WelcomeEventHandler?.Invoke(e));
hubConnection.On<GetReadyInfoEventArgs>(nameof(GetReadyInfoEventArgs), (e) => GetReadyInfoEventHandler?.Invoke(e));
hubConnection.On<WelcomeEventArgs>(nameof(WelcomeEventArgs), (e) => WelcomeEventHandler?.Invoke(e));
this.hubConnection = hubConnection;
}
public async Task GetReadyInfo() => await hubConnection.SendAsync("GetReadyInfo");
Expand Down
2 changes: 1 addition & 1 deletion Domain/Events/EndAuctionEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

namespace Domain.Events;

public record EndAuctionEvent(string PlayerId, decimal PlayerMoney, string BlockId, string? Owner, decimal OwnerMoney)
public record EndAuctionEvent(string PlayerId, decimal PlayerMoney, string LandId, string? OwnerId, decimal OwnerMoney)
: DomainEvent;
4 changes: 2 additions & 2 deletions Domain/Events/GameStartEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Domain.Events;

public record GameStartEvent(string GameStage, string CurrentPlayer)
public record GameStartEvent(string GameStage, string CurrentPlayerId)
: DomainEvent;

public record OnlyOnePersonEvent(string GameStage)
: DomainEvent;

public record SomePlayersPreparingEvent(string GameStage, params string[] Players)
public record SomePlayersPreparingEvent(string GameStage, params string[] PlayerIds)
: DomainEvent;
4 changes: 2 additions & 2 deletions Domain/Events/PlayerBidEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Domain.Events;

public record PlayerBidEvent(string PlayerId, string BlockId, decimal HighestPrice) : DomainEvent;
public record PlayerBidEvent(string PlayerId, string LandId, decimal HighestPrice) : DomainEvent;

public record PlayerBidFailEvent(string PlayerId, string BlockId, decimal BidPrice, decimal HighestPrice) : DomainEvent;
public record PlayerBidFailEvent(string PlayerId, string LandId, decimal BidPrice, decimal HighestPrice) : DomainEvent;

public record PlayerTooPoorToBidEvent(string PlayerId, decimal PlayerMoney, decimal BidPrice, decimal HighestPrice) : DomainEvent;

Expand Down
4 changes: 2 additions & 2 deletions Domain/Events/PlayerBuildHouseEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Domain.Events;

public record PlayerBuildHouseEvent(string PlayerId, string BlockId, decimal PlayerMoney, int House)
public record PlayerBuildHouseEvent(string PlayerId, string LandId, decimal PlayerMoney, int HouseCount)
: DomainEvent;

public record PlayerCannotBuildHouseEvent(string PlayerId, string BlockId)
public record PlayerCannotBuildHouseEvent(string PlayerId, string LandId)
: DomainEvent;

public record PlayerTooPoorToBuildHouseEvent(string PlayerId, string BlockId, decimal PlayerMoney, decimal UpgradePrice)
Expand Down
8 changes: 4 additions & 4 deletions Domain/Events/PlayerBuyBlockEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Domain.Events;

public record PlayerBuyBlockEvent(string PlayerId, string BlockId)
public record PlayerBuyBlockEvent(string PlayerId, string LandId)
: DomainEvent;

public record PlayerBuyBlockMissedLandEvent(string PlayerId, string BlockId)
: DomainEvent;

public record PlayerBuyBlockOccupiedByOtherPlayerEvent(string PlayerId, string BlockId)
public record PlayerBuyBlockOccupiedByOtherPlayerEvent(string PlayerId, string LandId)
: DomainEvent;

public record PlayerBuyBlockInsufficientFundsEvent(string PlayerId, string BlockId, decimal landMoney)
public record PlayerBuyBlockInsufficientFundsEvent(string PlayerId, string LandId, decimal Price)
: DomainEvent;

public record HouseMaxEvent(string PlayerId, string BlockId, int House)
public record HouseMaxEvent(string PlayerId, string LandId, int HouseCount)
: DomainEvent;
2 changes: 1 addition & 1 deletion Domain/Events/PlayerCanBuildHouseEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

namespace Domain.Events;

public record PlayerCanBuildHouseEvent(string PlayerId, string BlockId, int HouseCount, decimal UpgradeMoney) : DomainEvent;
public record PlayerCanBuildHouseEvent(string PlayerId, string LandId, int HouseCount, decimal UpgradePrice) : DomainEvent;
2 changes: 1 addition & 1 deletion Domain/Events/PlayerCanBuyLandEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

namespace Domain.Events;

public record PlayerCanBuyLandEvent(string PlayerId, string BlockId, decimal landMoney)
public record PlayerCanBuyLandEvent(string PlayerId, string LandId, decimal Price)
: DomainEvent;
4 changes: 2 additions & 2 deletions Domain/Events/PlayerMortgageEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace Domain.Events;
public record PlayerMortgageEvent(string PlayerId, decimal PlayerMoney, string BlockId, int DeadLine)
: DomainEvent;

public record PlayerCannotMortgageEvent(string PlayerId, decimal PlayerMoney, string BlockId)
public record PlayerCannotMortgageEvent(string PlayerId, decimal PlayerMoney, string LandId)
: DomainEvent;

public record MortgageDueEvent(string PlayerId, string BlockId)
public record MortgageDueEvent(string PlayerId, string LandId)
: DomainEvent;

public record MortgageCountdownEvent(string PlayerId, string BlockId, int DeadLine)
Expand Down
6 changes: 3 additions & 3 deletions Domain/Events/PlayerPayTollEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Domain.Events;

public record PlayerNeedsToPayTollEvent(string PlayerId, string ownerId, decimal toll) : DomainEvent;
public record PlayerNeedsToPayTollEvent(string PlayerId, string OwnerId, decimal Toll) : DomainEvent;

public record PlayerPayTollEvent(string PlayerId, decimal PlayerMoney, string ownerId, decimal ownerMoney) : DomainEvent;
public record PlayerPayTollEvent(string PlayerId, decimal PlayerMoney, string OwnerId, decimal OwnerMoney) : DomainEvent;

public record PlayerDoesntNeedToPayTollEvent(string PlayerId, decimal PlayerMoney) : DomainEvent;

public record PlayerTooPoorToPayTollEvent(string PlayerId, decimal PlayerMoney, decimal toll) : DomainEvent;
public record PlayerTooPoorToPayTollEvent(string PlayerId, decimal PlayerMoney, decimal Toll) : DomainEvent;
2 changes: 1 addition & 1 deletion Domain/Events/PlayerRedeemEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Domain.Events;

public record PlayerRedeemEvent(string PlayerId, decimal PlayerMoney, string BlockId) : DomainEvent;
public record PlayerRedeemEvent(string PlayerId, decimal PlayerMoney, string LandId) : DomainEvent;

public record PlayerTooPoorToRedeemEvent(string PlayerId, decimal PlayerMoney, string BlockId, decimal RedeemPrice) : DomainEvent;

Expand Down
9 changes: 9 additions & 0 deletions Server/Common/IMonopolyEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Domain.Common;

namespace Server.Common;

internal interface IMonopolyEventHandler
{
public Type EventType { get; }
Task HandleAsync(DomainEvent e);
}
19 changes: 19 additions & 0 deletions Server/Common/MonopolyEventHandlerBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Domain.Common;

namespace Server.Common;

public abstract class MonopolyEventHandlerBase<TEvent> : IMonopolyEventHandler where TEvent : DomainEvent
{
public Type EventType => typeof(TEvent);

public Task HandleAsync(DomainEvent e)
{
if (e is TEvent tEvent)
{
return HandleSpecificEvent(tEvent);
}
throw new InvalidOperationException($"Handler for {e.GetType()} not registered");
}

protected abstract Task HandleSpecificEvent(TEvent e);
}
23 changes: 20 additions & 3 deletions Server/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Application.Common;
using System.Reflection;
using Application.Common;
using Domain.Common;
using Server.Common;
using Server.Hubs;
using Server.Presenters;
using Server.Repositories;

namespace Server;
Expand All @@ -10,8 +13,22 @@ public static class DependencyInjection
public static IServiceCollection AddMonopolyServer(this IServiceCollection services)
{
services.AddSingleton<IRepository, InMemoryRepository>()
.AddScoped<IEventBus<DomainEvent>, MonopolyEventBus>()
.AddSingleton<IEventBus<DomainEvent>, MonopolyEventBus>()
.AddTransient(typeof(SignalrDefaultPresenter<>), typeof(SignalrDefaultPresenter<>));
services.AddSignalREventHandlers();
return services;
}
}

private static IServiceCollection AddSignalREventHandlers(this IServiceCollection services)
{
var handlers = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t is { IsClass: true, IsAbstract: false }
&& t.IsAssignableTo(typeof(IMonopolyEventHandler)))
.ToList();
foreach (var handler in handlers)
{
services.AddSingleton(typeof(IMonopolyEventHandler), handler);
}
return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Domain.Events;
using Microsoft.AspNetCore.SignalR;
using Server.Common;
using SharedLibrary;
using SharedLibrary.ResponseArgs.Monopoly;

namespace Server.Hubs.EventHandlers;

public class CannotGetRewardBecauseStandOnStartEventHandler(IHubContext<MonopolyHub, IMonopolyResponses> hubContext)
: MonopolyEventHandlerBase<CannotGetRewardBecauseStandOnStartEvent>
{
protected override Task HandleSpecificEvent(CannotGetRewardBecauseStandOnStartEvent e)
{
return hubContext.Clients.All.CannotGetRewardBecauseStandOnStartEvent(
new CannotGetRewardBecauseStandOnStartEventArgs
{
PlayerId = e.PlayerId
});
}
}
21 changes: 21 additions & 0 deletions Server/Hubs/EventHandlers/ChessMovedEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Domain.Events;
using Microsoft.AspNetCore.SignalR;
using Server.Common;
using SharedLibrary;
using SharedLibrary.ResponseArgs.Monopoly;

namespace Server.Hubs.EventHandlers;

public class ChessMovedEventHandler(IHubContext<MonopolyHub, IMonopolyResponses> hubContext) : MonopolyEventHandlerBase<ChessMovedEvent>
{
protected override Task HandleSpecificEvent(ChessMovedEvent e)
{
return hubContext.Clients.All.ChessMovedEvent(new ChessMovedEventArgs
{
PlayerId = e.PlayerId,
BlockId = e.BlockId,
Direction = e.Direction,
RemainingSteps = e.RemainingSteps
});
}
}
20 changes: 20 additions & 0 deletions Server/Hubs/EventHandlers/CurrentPlayerCannotBidEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Domain.Events;
using Microsoft.AspNetCore.SignalR;
using Server.Common;
using SharedLibrary;
using SharedLibrary.ResponseArgs.Monopoly;

namespace Server.Hubs.EventHandlers;

public class CurrentPlayerCannotBidEventHandler(IHubContext<MonopolyHub, IMonopolyResponses> hubContext)
: MonopolyEventHandlerBase<CurrentPlayerCannotBidEvent>
{
protected override Task HandleSpecificEvent(CurrentPlayerCannotBidEvent e)
{
return hubContext.Clients.All.CurrentPlayerCannotBidEvent(
new CurrentPlayerCannotBidEventArgs
{
PlayerId = e.PlayerId
});
}
}
24 changes: 24 additions & 0 deletions Server/Hubs/EventHandlers/EndAuctionEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Domain.Events;
using Microsoft.AspNetCore.SignalR;
using Server.Common;
using SharedLibrary;
using SharedLibrary.ResponseArgs.Monopoly;

namespace Server.Hubs.EventHandlers;

public class EndAuctionEventHandler(IHubContext<MonopolyHub, IMonopolyResponses> hubContext)
: MonopolyEventHandlerBase<EndAuctionEvent>
{
protected override Task HandleSpecificEvent(EndAuctionEvent e)
{
return hubContext.Clients.All.EndAuctionEvent(
new EndAuctionEventArgs
{
PlayerId = e.PlayerId,
LandId = e.LandId,
OwnerId = e.OwnerId,
PlayerMoney = e.PlayerMoney,
OwnerMoney = e.OwnerMoney
});
}
}
20 changes: 20 additions & 0 deletions Server/Hubs/EventHandlers/EndRoundEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Domain.Events;
using Microsoft.AspNetCore.SignalR;
using Server.Common;
using SharedLibrary;
using SharedLibrary.ResponseArgs.Monopoly;

namespace Server.Hubs.EventHandlers;

public class EndRoundEventHandler(IHubContext<MonopolyHub, IMonopolyResponses> hubContext)
: MonopolyEventHandlerBase<EndRoundEvent>
{
protected override Task HandleSpecificEvent(EndRoundEvent e)
{
return hubContext.Clients.All.EndRoundEvent(new EndRoundEventArgs
{
PlayerId = e.PlayerId,
NextPlayerId = e.NextPlayerId
});
}
}
19 changes: 19 additions & 0 deletions Server/Hubs/EventHandlers/EndRoundFailEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Domain.Events;
using Microsoft.AspNetCore.SignalR;
using Server.Common;
using SharedLibrary;
using SharedLibrary.ResponseArgs.Monopoly;

namespace Server.Hubs.EventHandlers;

public class EndRoundFailEventHandler(IHubContext<MonopolyHub, IMonopolyResponses> hubContext)
: MonopolyEventHandlerBase<EndRoundFailEvent>
{
protected override Task HandleSpecificEvent(EndRoundFailEvent e)
{
return hubContext.Clients.All.EndRoundFailEvent(new EndRoundFailEventArgs
{
PlayerId = e.PlayerId,
});
}
}
20 changes: 20 additions & 0 deletions Server/Hubs/EventHandlers/GameStartEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Domain.Events;
using Microsoft.AspNetCore.SignalR;
using Server.Common;
using SharedLibrary;
using SharedLibrary.ResponseArgs.Monopoly;

namespace Server.Hubs.EventHandlers;

public class GameStartEventHandler(IHubContext<MonopolyHub, IMonopolyResponses> hubContext)
: MonopolyEventHandlerBase<GameStartEvent>
{
protected override Task HandleSpecificEvent(GameStartEvent e)
{
return hubContext.Clients.All.GameStartEvent(new GameStartEventArgs
{
GameStage = e.GameStage,
CurrentPlayerId = e.CurrentPlayerId,
});
}
}
Loading

0 comments on commit 04e741f

Please sign in to comment.