Skip to content

Commit

Permalink
fix: 在 readyRoom 開始遊戲時會創建新的遊戲
Browse files Browse the repository at this point in the history
  • Loading branch information
aa89227 committed Jun 20, 2024
1 parent 4667c92 commit 0bed64f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Monopoly.ApplicationLayer.Application.Common;
using Monopoly.DomainLayer.Common;
using Monopoly.DomainLayer.Domain.Builders;
using Monopoly.DomainLayer.Domain.Maps;

namespace Monopoly.ApplicationLayer.Application.Usecases.ReadyRoom;

Expand All @@ -8,20 +10,32 @@ public record StartGameRequest(string GameId, string PlayerId)

public record StartGameResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public class StartGameUsecase(IReadyRoomRepository repository, IEventBus<DomainEvent> eventBus)
public class StartGameUsecase(IReadyRoomRepository readyRoomRepository, ICommandRepository gameRepository, IEventBus<DomainEvent> eventBus)
: Usecase<StartGameRequest, StartGameResponse>
{
public override async Task ExecuteAsync(StartGameRequest gameRequest, IPresenter<StartGameResponse> presenter,
CancellationToken cancellationToken = default)
{
//查
var readyRoom = await repository.GetReadyRoomAsync(gameRequest.GameId);
var readyRoom = await readyRoomRepository.GetReadyRoomAsync(gameRequest.GameId);

//改
readyRoom.StartGame(gameRequest.PlayerId);

var builder = new MonopolyBuilder();
foreach (var player in readyRoom.Players)
{
builder.WithPlayer(player.Id);
}
builder.WithId(readyRoom.GameId)
.WithHost(readyRoom.HostId)
.WithCurrentPlayer(readyRoom.Players[0].Id)
.WithMap(new SevenXSevenMap());
var game = builder.Build();

//存
await repository.SaveReadyRoomAsync(readyRoom);
await readyRoomRepository.SaveReadyRoomAsync(readyRoom);
gameRepository.Save(game);

//推
await eventBus.PublishAsync(readyRoom.DomainEvents, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Monopoly.DomainLayer.Domain.Builders;

public class MonopolyBuilder
{
public string GameId { get; private set; }
public string? GameId { get; private set; }

public List<PlayerBuilder> PlayerBuilders { get; private set; }

Expand All @@ -25,7 +25,7 @@ public MonopolyBuilder()
PlayerBuilders = new();
}

public MonopolyBuilder WithId(string id)
public MonopolyBuilder WithId(string? id)
{
GameId = id;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public sealed class ReadyRoomAggregate(string id, List<Player> players, string h
public IGameIdProvider GameIdProvider { get; set; } = new GameIdProvider();
public IReadOnlyList<Player> Players => players;
public string HostId => hostId;
public string? GameId { get; private set; }

private Player GetPlayer(string playerId)
{
Expand Down Expand Up @@ -55,8 +56,8 @@ public void StartGame(string playerId)
var unreadyPlayers = players.Where(p => p.IsReady is not true && p.Id != hostId);
if (unreadyPlayers.Any()) throw new HostCannotStartGameException();

var gameId = GameIdProvider.GetGameId();
AddDomainEvent(new GameStartedEvent(gameId));
GameId = GameIdProvider.GetGameId();
AddDomainEvent(new GameStartedEvent(GameId));
}

public void PlayerJoin(string playerId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Monopoly.ApplicationLayer.Application.Common;
using Monopoly.DomainLayer.ReadyRoom.Builders;
using Monopoly.DomainLayer.ReadyRoom.Common;
using Moq;
Expand All @@ -15,6 +17,7 @@ public class GameStartTest : AbstractReadyRoomTestBase
Given: 房內有4名玩家,除了玩家A以外的所有玩家都已經準備
When: 房主按下開始遊戲
Then: 遊戲開始
repository 的遊戲存在
""")]
public async Task 房主成功開始遊戲()
{
Expand Down Expand Up @@ -56,6 +59,9 @@ public async Task 房主成功開始遊戲()

// Assert
hub.FluentAssert.GameStartedEvent(new GameStartedEventArgs(gameId));

var gameRepository = Server.Services.GetRequiredService<ICommandRepository>();
var game = gameRepository.FindGameById(gameId);
}

[TestMethod]
Expand Down

0 comments on commit 0bed64f

Please sign in to comment.