Skip to content

Commit

Permalink
fix: 修正移動棋子時事件產生的順序
Browse files Browse the repository at this point in the history
  • Loading branch information
aa89227 committed Oct 13, 2024
1 parent 9725622 commit f624ba1
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
20 changes: 14 additions & 6 deletions DomainLayer/Monopoly.DomainLayer.Domain/Chess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class Chess
private int remainingSteps;

public Chess(Player player,
string currentBlockId,
Direction currentDirection,
int remainingSteps)
string currentBlockId,
Direction currentDirection,
int remainingSteps)
{
this.player = player;
this.currentBlockId = currentBlockId;
Expand All @@ -37,29 +37,37 @@ private IEnumerable<DomainEvent> Move(Map map)
{
while (remainingSteps > 0)
{
var nextBlock = map.FindBlockById(currentBlockId).GetDirectionBlock(CurrentDirection) ?? throw new Exception("找不到下一個區塊");
var nextBlock = map.FindBlockById(currentBlockId).GetDirectionBlock(CurrentDirection) ??
throw new Exception("找不到下一個區塊");
currentBlockId = nextBlock.Id;
remainingSteps--;
if (currentBlockId == "Start" && remainingSteps > 0) // 如果移動到起點,且還有剩餘步數,則獲得獎勵金
{
player.Money += 3000;
yield return new ThroughStartEvent(player.Id, 3000, player.Money);
}

var directions = DirectionOptions(map);

if (directions.Count > 1)
{
yield return new ChessMovedEvent(player.Id, currentBlockId, currentDirection.ToString(),
remainingSteps);
// 可選方向多於一個
// 代表棋子會停在這個區塊
yield return new PlayerNeedToChooseDirectionEvent(
player.Id,
directions.Select(d => d.ToString()).ToArray());
yield break;
}

// 只剩一個方向
// 代表棋子會繼續往這個方向移動
currentDirection = directions.First();
yield return new ChessMovedEvent(player.Id, currentBlockId, currentDirection.ToString(), remainingSteps);
yield return new ChessMovedEvent(player.Id, currentBlockId, currentDirection.ToString(),
remainingSteps);
}

map.FindBlockById(currentBlockId).DoBlockAction(player);
var onBlockEvent = map.FindBlockById(currentBlockId).OnBlockEvent(player);
if (onBlockEvent is not null)
Expand All @@ -76,7 +84,7 @@ public IEnumerable<DomainEvent> Move(Map map, int steps)

internal IEnumerable<DomainEvent> ChangeDirection(Map map, Direction direction)
{
List<DomainEvent> events = new() { };
List<DomainEvent> events = [];
currentDirection = direction;
events.Add(new PlayerChooseDirectionEvent(player.Id, direction.ToString()));
events.AddRange(Move(map));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,22 @@

namespace Monopoly.DomainLayer.Domain.Events;

public record PlayerNeedToChooseDirectionEvent(string PlayerId, params string[] Directions) : DomainEvent;
public record PlayerNeedToChooseDirectionEvent(string PlayerId, string[] Directions) : DomainEvent
{
public override string ToString()
{
return $"{nameof(PlayerNeedToChooseDirectionEvent)} {{ PlayerId = {PlayerId}, Directions = [{string.Join(", ", Directions)}] }}";
}

public virtual bool Equals(PlayerNeedToChooseDirectionEvent? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return PlayerId == other.PlayerId && Directions.Order().SequenceEqual(other.Directions.Order());
}

public override int GetHashCode()
{
return HashCode.Combine(base.GetHashCode(), PlayerId, Directions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public void 玩家擲骰後移動棋子到需要選擇方向的地方()

monopoly.DomainEvents
.NextShouldBe(new PlayerRolledDiceEvent(player.Id, dicePoints))
//.NextShouldBe(new ChessMovedEvent(player.Id, "ParkingLot", "Down", 1))
.NextShouldBe(new PlayerNeedToChooseDirectionEvent(player.Id, "Left", "Right", "Down"))
.NextShouldBe(new ChessMovedEvent(player.Id, "ParkingLot", "Down", 1))
.NextShouldBe(new PlayerNeedToChooseDirectionEvent(player.Id, ["Left", "Right", "Down"]))
.NoMore();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ public void 玩家選擇方向後會繼續前進到需要選擇方向的地方()
.NextShouldBe(new ChessMovedEvent(A.Id, "B6", "Left", 3))
.NextShouldBe(new ChessMovedEvent(A.Id, "B5", "Down", 2))
.NextShouldBe(new ChessMovedEvent(A.Id, "B4", "Down", 1))
//.NextShouldBe(new ChessMovedEvent(A.Id, "Jail", "Down", 0))
.NextShouldBe(new PlayerNeedToChooseDirectionEvent(A.Id, "Left", "Right", "Down"))
.NextShouldBe(new ChessMovedEvent(A.Id, "Jail", "Down", 0))
.NextShouldBe(new PlayerNeedToChooseDirectionEvent(A.Id, ["Left", "Right", "Down"]))
.NoMore();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public async Task 玩家擲骰後移動棋子到需要選擇方向的地方()
VerifyChessMovedEvent(hub, "A", "A2", "Right", 4);
VerifyChessMovedEvent(hub, "A", "A3", "Down", 3);
VerifyChessMovedEvent(hub, "A", "A4", "Down", 2);
//VerifyChessMovedEvent(hub, "A", "ParkingLot", "Down", 1);
VerifyChessMovedEvent(hub, "A", "ParkingLot", "Down", 1);
hub.FluentAssert.PlayerNeedToChooseDirectionEvent(new PlayerNeedToChooseDirectionEventArgs
{
PlayerId = a.Id,
Expand Down

0 comments on commit f624ba1

Please sign in to comment.