generated from Game-as-a-Service/Gaas-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c275b11
commit 4a93d63
Showing
10 changed files
with
205 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Application.Common; | ||
using Domain.Common; | ||
|
||
namespace Application.Usecases; | ||
|
||
public record MortgageRequest(string GameId, string PlayerId, string BlockId) | ||
: Request(GameId, PlayerId); | ||
|
||
public class MortgageUsecase : Usecase<MortgageRequest> | ||
{ | ||
public MortgageUsecase(IRepository repository, IEventBus<DomainEvent> eventBus) | ||
: base(repository, eventBus) | ||
{ | ||
} | ||
|
||
public override async Task ExecuteAsync(MortgageRequest request) | ||
{ | ||
//查 | ||
var game = Repository.FindGameById(request.GameId); | ||
|
||
//改 | ||
game.MortgageLandContract(request.PlayerId, request.BlockId); | ||
|
||
//存 | ||
Repository.Save(game); | ||
|
||
//推 | ||
await EventBus.PublishAsync(game.DomainEvents); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Domain.Common; | ||
|
||
namespace Domain.Events; | ||
|
||
public record PlayerMortgageEvent(string GameId, string PlayerId, decimal PlayerMoney, string BlockId, int DeadLine) | ||
: DomainEvent(GameId); | ||
|
||
public record PlayerCannotMortgageEvent(string GameId, string PlayerId, decimal PlayerMoney, string BlockId) | ||
: DomainEvent(GameId); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using Domain; | ||
using Server.Hubs; | ||
using static Domain.Map; | ||
using static ServerTests.Utils; | ||
|
||
namespace ServerTests.AcceptanceTests; | ||
|
||
[TestClass] | ||
public class MortgageTest | ||
{ | ||
private MonopolyTestServer server = default!; | ||
|
||
[TestInitialize] | ||
|
||
public void SetUp() | ||
{ | ||
server = new MonopolyTestServer(); | ||
} | ||
|
||
[TestMethod] | ||
[Description( | ||
""" | ||
Given: A 持有 A1,價值 1000元 | ||
A 持有 5000元 | ||
When: A 抵押 A1 | ||
Then: A 持有 5000+1000*70% = 5700元 | ||
A 持有 A1 | ||
A1 在 10回合 後收回 | ||
""")] | ||
public async Task 玩家抵押房地產() | ||
{ | ||
Player A = new("A", 5000); | ||
|
||
const string gameId = "1"; | ||
var monopolyBuilder = new MonopolyBuilder("1") | ||
.WithPlayer( | ||
new MonopolyPlayer(A.Id) | ||
.WithMoney(A.Money) | ||
.WithPosition("Start", Direction.Right.ToString()) | ||
.WithLandContract("A1") | ||
) | ||
.WithCurrentPlayer(nameof(A)); | ||
|
||
monopolyBuilder.Save(server); | ||
|
||
var hub = await server.CreateHubConnectionAsync(gameId, "A"); | ||
|
||
// Act | ||
await hub.SendAsync(nameof(MonopolyHub.PlayerMortgage), gameId, "A", "A1"); | ||
|
||
// Assert | ||
// A 抵押房地產 | ||
hub.Verify<string, decimal, string, int>( | ||
nameof(IMonopolyResponses.PlayerMortgageEvent), | ||
(playerId, playerMoney, blockId, deadLine) | ||
=> playerId == "A" && playerMoney == 5700 && blockId == "A1" && deadLine == 10); | ||
hub.VerifyNoElseEvent(); | ||
} | ||
|
||
[TestMethod] | ||
[Description( | ||
""" | ||
Given: A 持有 A1,價值 1000元,抵押中 | ||
A 持有 1000元 | ||
When: A 抵押 A1 | ||
Then: A 無法再次抵押 | ||
A 持有 1000元 | ||
""")] | ||
public async Task 玩家不能抵押已抵押房地產() | ||
{ | ||
Player A = new("A", 1000); | ||
|
||
const string gameId = "1"; | ||
var monopolyBuilder = new MonopolyBuilder("1") | ||
.WithPlayer( | ||
new MonopolyPlayer(A.Id) | ||
.WithMoney(A.Money) | ||
.WithPosition("Start", Direction.Right.ToString()) | ||
.WithLandContract("A1") | ||
.WithMortgage("A1") | ||
) | ||
.WithCurrentPlayer(nameof(A)); | ||
|
||
monopolyBuilder.Save(server); | ||
|
||
var hub = await server.CreateHubConnectionAsync(gameId, "A"); | ||
|
||
// Act | ||
await hub.SendAsync(nameof(MonopolyHub.PlayerMortgage), gameId, "A", "A1"); | ||
|
||
// Assert | ||
// A 抵押房地產 | ||
hub.Verify<string, decimal, string>( | ||
nameof(IMonopolyResponses.PlayerCannotMortgageEvent), | ||
(playerId, playerMoney, blockId) | ||
=> playerId == "A" && playerMoney == 1000 && blockId == "A1"); | ||
hub.VerifyNoElseEvent(); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters