Skip to content

Commit

Permalink
add: Road class
Browse files Browse the repository at this point in the history
  • Loading branch information
takeiteasy23 committed May 16, 2024
1 parent 8b7e8b2 commit 80a9f25
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Application/Common/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ private static Block ToApplicationBlock(this Domain.Block domainBlock)
Domain.Land landBlock => new Land(landBlock.Id),
Domain.ParkingLot parkingLotBlock => new ParkingLot(parkingLotBlock.Id),
Domain.Jail prisonBlock => new Jail(prisonBlock.Id),
Domain.Road roadBlock => new Road(roadBlock.Id),
null => new EmptyBlock(),
_ => throw new NotImplementedException(),
};
Expand Down Expand Up @@ -173,6 +174,7 @@ private static Domain.Builders.PlayerBuilder WithLandContracts(this Domain.Build
Land landBlock => new Domain.Land(landBlock.Id),
ParkingLot parkingLotBlock => new Domain.ParkingLot(parkingLotBlock.Id),
Jail prisonBlock => new Domain.Jail(prisonBlock.Id),
Road roadBlock => new Domain.Road(roadBlock.Id),
EmptyBlock => null,
_ => throw new NotImplementedException(),
};
Expand Down
2 changes: 2 additions & 0 deletions Application/DataModels/Monopoly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public record StartPoint(string Id) : Block(Id);

public record Station(string Id) : Land(Id);

public record Road(string Id) : Block(Id);

public enum GameStage
{
Preparing,
Expand Down
16 changes: 16 additions & 0 deletions Domain/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,20 @@ internal override void DoBlockAction(Player player)
player.EndRoundFlag = false;
}
}
}

public class Road : Block
{
public Road(string id) : base(id)
{
}

internal override void DoBlockAction(Player player)
{
}

internal override DomainEvent OnBlockEvent(Player player)
{
return DomainEvent.EmptyEvent;
}
}
2 changes: 1 addition & 1 deletion Domain/Maps/SevenXSevenMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public SevenXSevenMap() : base("7x7", Blocks)
new Block?[] { new Land("F3", lot:"F"), null, new Land("B4", lot:"B"), null, new Land("B1", lot:"B"), null, new Land("C3", lot:"C") },
new Block?[] { new Land("F2", lot:"F"), new Land("F1", lot:"F"), new Jail("Jail"), new Land("B3", lot:"B"), new Land("B2", lot:"B"), null, new Station("Station2") },
new Block?[] { null, null, new Land("E3", lot:"E"), null, null, null, new Land("D1", lot:"D") },
new Block?[] { null, null, new Land("E2", lot:"E"), new Land("E1", lot:"E"), new Station("Station3"), new Land("D3", lot:"D"), new Land("D2", lot:"D") },
new Block?[] { null, null, new Land("E2", lot:"E"), new Land("E1", lot:"E"), new Station("Station3"), new Land("D3", lot:"D"), new Road("R2") },
};
}
40 changes: 40 additions & 0 deletions Test/DomainTests/Testcases/BuyLandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,44 @@ public void 無法購買非腳下的土地()
.NextShouldBe(new PlayerBuyBlockMissedLandEvent(A.Id, F4.Id))
.NoMore();
}

[TestMethod]
[Description(
"""
Given: 玩家A資產5000元,沒有房地產
目前輪到A,A在R2上
When: 玩家A購買土地R2
Then: 顯示錯誤訊息"無法購買道路"
玩家A持有金額為5000,持有房地產數量為0
""")]
public void 無法購買道路()
{
// Arrange
// 玩家A持有的金額 5000
// 玩家A目前踩在道路R2上
var A = new { Id = "A", Money = 5000m, CurrentBlockId = "R2", CurrentDirection = "Left" };
var R2 = new { Id = "R2" };

var monopoly = new MonopolyBuilder()
.WithMap(map)
.WithPlayer(A.Id, pa => pa.WithMoney(A.Money)
.WithPosition(A.CurrentBlockId, A.CurrentDirection))
.WithCurrentPlayer(A.Id)
.Build();

// Act
// 玩家B進行購買R2
monopoly.BuyLand(A.Id, R2.Id);

// Assert
// 玩家A持有金額為5000
// 玩家A無持有的房地產
var player_a = monopoly.Players.First(p => p.Id == A.Id);
Assert.AreEqual(5000, player_a.Money);
Assert.AreEqual(0, player_a.LandContractList.Count);

monopoly.DomainEvents
.NextShouldBe(new PlayerBuyBlockOccupiedByOtherPlayerEvent(A.Id, R2.Id))
.NoMore();
}
}
44 changes: 44 additions & 0 deletions Test/ServerTests/AcceptanceTests/BuyBlockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,48 @@ public void 玩家無法購買非腳下的土地()

//hub.VerifyNoElseEvent();
}

[TestMethod]
[Description(
"""
Given: 玩家A資產5000元,沒有房地產
目前輪到A,A在R2上
When: 玩家A購買土地R4
Then: 顯示錯誤訊息"無法購買道路"
玩家A持有金額為5000
玩家A持有房地產數量為0
""")]
public async Task 玩家無法購買道路()
{
// Arrange
var a = new { Id = "A", Money = 5000m };
var r2 = new { Id = "R2" };

const string gameId = "1";
var monopolyBuilder = new MonopolyBuilder("1")
.WithPlayer(
new PlayerBuilder(a.Id)
.WithMoney(a.Money)
.WithPosition(r2.Id, Direction.Left)
.Build()
)
.WithCurrentPlayer(new CurrentPlayerStateBuilder(a.Id)
.Build()
);

monopolyBuilder.Save(server);

var hub = await server.CreateHubConnectionAsync(gameId, "A");

// Act

await hub.SendAsync(nameof(MonopolyHub.PlayerBuyLand), "R2");

// Assert
// A 無法購買道路
hub.Verify(nameof(IMonopolyResponses.PlayerBuyBlockOccupiedByOtherPlayerEvent),
(PlayerBuyBlockOccupiedByOtherPlayerEventArgs e) => e is { PlayerId: "A", LandId: "R2" });

hub.VerifyNoElseEvent();
}
}
38 changes: 38 additions & 0 deletions Test/ServerTests/AcceptanceTests/RollDiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,42 @@ public async Task 玩家擲骰後移動棋子到空地()
(PlayerCanBuyLandEventArgs e) => e is { PlayerId:"A", LandId: "A2", Price: 1000 });
hub.VerifyNoElseEvent();
}

[TestMethod]
[Description("""
Given: 目前玩家在Station2
When: 玩家擲骰得到2點
Then: 玩家移動到 R2
玩家剩餘步數為 0
""")]
public async Task 玩家擲骰後移動棋子到道路()
{
// Arrange
var a = new { Id = "A" };

const string gameId = "1";
var monopolyBuilder = new MonopolyBuilder("1")
.WithPlayer(
new PlayerBuilder(a.Id)
.WithPosition("Station2", Direction.Down)
.Build()
)
.WithMockDice([2])
.WithCurrentPlayer(new CurrentPlayerStateBuilder(a.Id).Build());

monopolyBuilder.Save(_server);

var hub = await _server.CreateHubConnectionAsync(gameId, "A");
// Act
await hub.SendAsync(nameof(MonopolyHub.PlayerRollDice));
// Assert
// A 擲了 2 點
// A 移動到 R2,方向為 Left,剩下 0 步
// A 可以購買空地
hub.Verify(nameof(IMonopolyResponses.PlayerRolledDiceEvent),
(PlayerRolledDiceEventArgs e) => e is { PlayerId: "A", DiceCount: 2 });
VerifyChessMovedEvent(hub, "A", "D1", "Down", 1);
VerifyChessMovedEvent(hub, "A", "R2", "Left", 0);
hub.VerifyNoElseEvent();
}
}

0 comments on commit 80a9f25

Please sign in to comment.