Skip to content

Commit

Permalink
refactor: remove gameStage of monopoly aggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
aa89227 committed Jun 20, 2024
1 parent acca84d commit 4667c92
Show file tree
Hide file tree
Showing 27 changed files with 73 additions and 160 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using Monopoly.ApplicationLayer.Application.DataModels;
using Monopoly.DomainLayer.Domain;

namespace Monopoly.ApplicationLayer.Application.Common;

public interface IRepository
{
public MonopolyDataModel FindGameById(string id);
public MonopolyAggregate FindGameById(string id);
public string[] GetRooms();
public bool IsExist(string id);
public string Save(MonopolyDataModel monopolyDataModel);
public string Save(MonopolyAggregate monopoly);
}

public interface ICommandRepository : IRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Block = Monopoly.ApplicationLayer.Application.DataModels.Block;
using Chess = Monopoly.ApplicationLayer.Application.DataModels.Chess;
using CurrentPlayerState = Monopoly.ApplicationLayer.Application.DataModels.CurrentPlayerState;
using GameStage = Monopoly.DomainLayer.Domain.GameStage;
using Jail = Monopoly.DomainLayer.Domain.Jail;
using Land = Monopoly.DomainLayer.Domain.Land;
using LandContract = Monopoly.ApplicationLayer.Application.DataModels.LandContract;
Expand All @@ -21,18 +20,12 @@ namespace Monopoly.ApplicationLayer.Application.Common;

public static class RepositoryExtensions
{
public static string Save(this IRepository repository, MonopolyAggregate domainMonopolyAggregate)
{
var monopoly = domainMonopolyAggregate.ToApplication();
return repository.Save(monopoly);
}

/// <summary>
/// (Monopoly) Domain to Application
/// </summary>
/// <param name="domainMonopolyAggregate"></param>
/// <returns></returns>
private static MonopolyDataModel ToApplication(this MonopolyAggregate domainMonopolyAggregate)
public static MonopolyDataModel ToApplication(this MonopolyAggregate domainMonopolyAggregate)
{
var players = domainMonopolyAggregate.Players.Select(player =>
{
Expand All @@ -58,18 +51,6 @@ private static MonopolyDataModel ToApplication(this MonopolyAggregate domainMono
Map map = new(domainMonopolyAggregate.Map.Id, domainMonopolyAggregate.Map.Blocks
.Select(row => { return row.Select(block => block?.ToApplicationBlock()).ToArray(); }).ToArray()
);
var gamestage = domainMonopolyAggregate.GameStage switch
{
GameStage.Ready => DataModels.GameStage.Preparing,
GameStage.Gaming => DataModels.GameStage.Gaming,
_ => throw new NotImplementedException(),
};
if (gamestage == DataModels.GameStage.Preparing)
{
return new MonopolyDataModel(domainMonopolyAggregate.Id, [..players], map, domainMonopolyAggregate.HostId, null!,
null!, gamestage);
}

var currentPlayer =
domainMonopolyAggregate.Players.First(player =>
player.Id == domainMonopolyAggregate.CurrentPlayerState.PlayerId);
Expand All @@ -90,8 +71,8 @@ domainMonopolyAggregate.CurrentPlayerState.Auction is null
.Select(land => new LandHouse(land.Id, land.House)).ToArray();


return new DataModels.MonopolyDataModel(domainMonopolyAggregate.Id, players, map, domainMonopolyAggregate.HostId,
currentPlayerState, LandHouses, gamestage);
return new MonopolyDataModel(domainMonopolyAggregate.Id, players, map, domainMonopolyAggregate.HostId,
currentPlayerState, LandHouses);
}

private static Block ToApplicationBlock(this DomainLayer.Domain.Block domainBlock)
Expand Down Expand Up @@ -137,16 +118,6 @@ public static MonopolyAggregate ToDomain(this MonopolyDataModel monopolyDataMode
.WithRole(p.RoleId)
.WithState(p.PlayerState)
));
builder.WithGameStage(monopolyDataModel.GameStage switch
{
DataModels.GameStage.Preparing => GameStage.Ready,
DataModels.GameStage.Gaming => GameStage.Gaming,
_ => throw new NotImplementedException(),
});
if (monopolyDataModel.GameStage == DataModels.GameStage.Preparing)
{
return builder.Build();
}

var cps = monopolyDataModel.CurrentPlayerState;
if (cps.Auction is null)
Expand Down Expand Up @@ -177,21 +148,6 @@ private static PlayerBuilder WithLandContracts(this PlayerBuilder builder,
return builder;
}

private static DomainLayer.Domain.Block? ToDomainBlock(this Block? block)
{
return block switch
{
DataModels.StartPoint startBlock => new StartPoint(startBlock.Id),
DataModels.Station stationBlock => new Station(stationBlock.Id),
DataModels.Land landBlock => new Land(landBlock.Id),
DataModels.ParkingLot parkingLotBlock => new ParkingLot(parkingLotBlock.Id),
DataModels.Jail prisonBlock => new Jail(prisonBlock.Id),
DataModels.Road roadBlock => new Road(roadBlock.Id),
EmptyBlock => null,
_ => throw new NotImplementedException(),
};
}

private static Direction ToApplicationDirection(this DomainLayer.Domain.Map.Direction direction)
{
return direction switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Monopoly.ApplicationLayer.Application.DataModels;

public record MonopolyDataModel(string Id, Player[] Players, Map Map, string HostId, CurrentPlayerState CurrentPlayerState, LandHouse[] LandHouses, GameStage GameStage);
public record MonopolyDataModel(string Id, Player[] Players, Map Map, string HostId, CurrentPlayerState CurrentPlayerState, LandHouse[] LandHouses);

public record Player(string Id, decimal Money, Chess Chess, LandContract[] LandContracts, PlayerState PlayerState, int BankruptRounds, int LocationId, string? RoleId);
public record CurrentPlayerState(string PlayerId, bool IsPayToll, bool IsBoughtLand, bool IsUpgradeLand, Auction? Auction, int RemainingSteps, bool HadSelectedDirection);
Expand Down Expand Up @@ -34,10 +34,4 @@ 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,
Gaming,
}
public record Road(string Id) : Block(Id);
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override async Task ExecuteAsync(BidRequest request, IPresenter<BidRespon
CancellationToken cancellationToken = default)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
var game = Repository.FindGameById(request.GameId);

//改
game.PlayerBid(request.PlayerId, request.BidPrice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override async Task ExecuteAsync(BuildHouseRequest request, IPresenter<Bu
CancellationToken cancellationToken = default)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
var game = Repository.FindGameById(request.GameId);

//改
game.BuildHouse(request.PlayerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override async Task ExecuteAsync(ChooseDirectionRequest request,
IPresenter<ChooseDirectionResponse> presenter, CancellationToken cancellationToken = default)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
var game = Repository.FindGameById(request.GameId);
//改
game.PlayerSelectDirection(request.PlayerId, request.Direction);
//存
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override async Task ExecuteAsync(EndAuctionRequest request, IPresenter<En
CancellationToken cancellationToken = default)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
var game = Repository.FindGameById(request.GameId);

//改
game.EndAuction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override async Task ExecuteAsync(EndRoundRequest request, IPresenter<EndR
CancellationToken cancellationToken = default)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
var game = Repository.FindGameById(request.GameId);

//改
game.EndRound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override async Task ExecuteAsync(MortgageRequest request, IPresenter<Mort
CancellationToken cancellationToken = default)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
var game = Repository.FindGameById(request.GameId);

//改
game.MortgageLandContract(request.PlayerId, request.BlockId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override async Task ExecuteAsync(PayTollRequest request, IPresenter<PayTo
CancellationToken cancellationToken = default)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
var game = Repository.FindGameById(request.GameId);

//改
game.PayToll(request.PlayerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override async Task ExecuteAsync(PlayerBuyLandRequest request, IPresenter
CancellationToken cancellationToken = default)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
var game = Repository.FindGameById(request.GameId);

//改
game.BuyLand(request.PlayerId, request.LandID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override async Task ExecuteAsync(PlayerRollDiceRequest request,
IPresenter<PlayerRollDiceResponse> presenter, CancellationToken cancellationToken = default)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
var game = Repository.FindGameById(request.GameId);

//改
game.PlayerRollDice(request.PlayerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override async Task ExecuteAsync(RedeemRequest request, IPresenter<Redeem
CancellationToken cancellationToken = default)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
var game = Repository.FindGameById(request.GameId);

//改
game.RedeemLandContract(request.PlayerId, request.BlockId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override async Task ExecuteAsync(SettlementRequest request, IPresenter<Se
CancellationToken cancellationToken = default)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
var game = Repository.FindGameById(request.GameId);

//改
game.Settlement();
Expand Down
15 changes: 14 additions & 1 deletion Clients/Monopoly.Clients.Web/Pages/Ready/ReadyPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Client.Pages.Enums;
using Client.Pages.Ready.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Options;
using SharedLibrary.ResponseArgs.ReadyRoom;
Expand Down Expand Up @@ -119,7 +120,19 @@ private async Task OnSelectColor(ColorEnum color)
{
return;
}
await Connection.SelectLocation(color.ToLocationEnum());

try
{
await Connection.SelectLocation(color.ToLocationEnum());
}
catch (HubException)
{
Popup?.Show(new Popup.PopupParameter
{
Message = "無法選擇此位置",
Delay = 1000
});
}
}

private async Task OnSelectRole(string role)
Expand Down
71 changes: 29 additions & 42 deletions DomainLayer/Monopoly.DomainLayer.Domain/Builders/MonopolyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ public class MonopolyBuilder
public Map Map { get; private set; }
public int Rounds { get; private set; }
public List<(string LandId, int House)> LandHouses { get; private set; } = new();
internal GameStage GameStage { get; private set; }

public MonopolyBuilder()
{
PlayerBuilders = new();
GameStage = GameStage.Ready;
}

public MonopolyBuilder WithId(string id)
Expand Down Expand Up @@ -52,14 +50,16 @@ public MonopolyBuilder WithPlayer(string id, Expression<Func<PlayerBuilder, Play
return this;
}

public MonopolyBuilder WithCurrentPlayer(string id, Expression<Func<CurrentPlayerStateBuilder, CurrentPlayerStateBuilder>>? expression = null)
public MonopolyBuilder WithCurrentPlayer(string id,
Expression<Func<CurrentPlayerStateBuilder, CurrentPlayerStateBuilder>>? expression = null)
{
var currentPlayerStateBuilder = new CurrentPlayerStateBuilder(id);
if (expression is not null)
{
var f = expression.Compile();
f(currentPlayerStateBuilder);
}

CurrentPlayerStateBuilder = currentPlayerStateBuilder;
return this;
}
Expand All @@ -69,6 +69,7 @@ public MonopolyBuilder WithHost(string id)
HostId = id;
return this;
}

public MonopolyBuilder WithLandHouse(string LandId, int House)
{
LandHouses.Add(new(LandId, House));
Expand All @@ -83,41 +84,33 @@ public MonopolyAggregate Build()
builder.WithMap(Map);
players.Add(builder.Build());
});
if ((GameStage == GameStage.Gaming))


Auction? auction = null;
if (CurrentPlayerStateBuilder.HasAuction)
{
var (LandId, HighestBidder, HighestPrice) = CurrentPlayerStateBuilder.Auction;
var currentPlayer = players.First(p => p.Id == CurrentPlayerStateBuilder.PlayerId);
var landContract = currentPlayer.FindLandContract(LandId) ??
throw new InvalidOperationException("LandContract not found");
var highestBidder = players.FirstOrDefault(p => p.Id == HighestBidder);
auction = new Auction(landContract, highestBidder, HighestPrice);
}

foreach (var landHouse in LandHouses)
{
Auction? auction = null;
if (CurrentPlayerStateBuilder.HasAuction)
{
var (LandId, HighestBidder, HighestPrice) = CurrentPlayerStateBuilder.Auction;
var currentPlayer = players.First(p => p.Id == CurrentPlayerStateBuilder.PlayerId);
var landContract = currentPlayer.FindLandContract(LandId) ?? throw new InvalidOperationException("LandContract not found");
var highestBidder = players.FirstOrDefault(p => p.Id == HighestBidder);
auction = new Auction(landContract, highestBidder, HighestPrice);
}
foreach (var landHouse in LandHouses)
{
var land = Map.FindBlockById<Land>(landHouse.LandId);
for (int i = 0; i < landHouse.House; i++) land.Upgrade();
}
return new MonopolyAggregate(GameId,
players.ToArray(),
GameStage,
Map,
HostId,
CurrentPlayerStateBuilder.Build(auction),
Dices,
Rounds
);
var land = Map.FindBlockById<Land>(landHouse.LandId);
for (var i = 0; i < landHouse.House; i++) land.Upgrade();
}

return new MonopolyAggregate(GameId,
players.ToArray(),
GameStage,
Map,
HostId,
null!,
Dices,
Rounds
);
players.ToArray(),
Map,
HostId,
CurrentPlayerStateBuilder.Build(auction),
Dices,
Rounds
);
}

public MonopolyBuilder WithRounds(int rounds)
Expand All @@ -131,10 +124,4 @@ public MonopolyBuilder WithDices(IDice[] dices)
Dices = dices;
return this;
}

public MonopolyBuilder WithGameStage(GameStage gameStage)
{
GameStage = gameStage;
return this;
}
}
}
6 changes: 0 additions & 6 deletions DomainLayer/Monopoly.DomainLayer.Domain/GameStage.cs

This file was deleted.

Loading

0 comments on commit 4667c92

Please sign in to comment.