Skip to content

Commit

Permalink
improve RoundResultMessage and Emulator ApplyAction
Browse files Browse the repository at this point in the history
  • Loading branch information
miekhra committed Sep 7, 2023
1 parent 2886448 commit 950f608
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/NPokerEngine.Tests/Engine/MessageBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void RoundResultMessageTest()
var winners = state.Table.Seats.Players.Skip(1).Take(1);
var handInfo = new string[] { "dummy", "info" };

var message = MessageBuilder.Instance.BuildRoundResultMessage(7, winners, handInfo, state);
var message = MessageBuilder.Instance.BuildRoundResultMessage(7, winners, handInfo, state, null);

using (new AssertionScope())
{
Expand Down
8 changes: 7 additions & 1 deletion src/NPokerEngine/Emulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,20 @@ public void RegisterPlayer(PokerPlayer player)
}

public (GameState gameState, List<IMessage> messages) ApplyAction(GameState gameState, ActionType actionType, float betAmount = 0)
{
ActionHistoryEntry actionHistoryEntry;
return ApplyAction(gameState, actionType, out actionHistoryEntry, betAmount);
}

public (GameState gameState, List<IMessage> messages) ApplyAction(GameState gameState, ActionType actionType, out ActionHistoryEntry actionHistoryEntry, float betAmount = 0)
{
var messages = new List<IMessage>();
if (gameState.Street == StreetType.FINISHED)
{
(gameState, messages) = StartNextRound(gameState);
}

var (updatedState, msgs) = RoundManager.Instance.ApplyAction(gameState, actionType, betAmount);
var (updatedState, msgs) = RoundManager.Instance.ApplyAction(gameState, actionType, betAmount, out actionHistoryEntry);
gameState = updatedState;
messages.AddRange(msgs);

Expand Down
25 changes: 3 additions & 22 deletions src/NPokerEngine/Engine/MessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,34 +117,15 @@ public virtual GameUpdateMessage BuildGameUpdateMessage(int playerPos, ActionTyp
};
}

public virtual RoundResultMessage BuildRoundResultMessage(int round_count, IEnumerable<Player> winners, object hand_info, GameState state)
public virtual RoundResultMessage BuildRoundResultMessage(int round_count, IEnumerable<Player> winners, object hand_info, GameState state, Dictionary<int, float> prizeMap)
{
return new RoundResultMessage
{
RoundCount = round_count,
Winners = winners.ToList(),
State = state
State = state,
PrizeMap = prizeMap
};
//var message = new Dictionary<string, object> {
// {
// "message_type",
// ROUND_RESULT_MESSAGE},
// {
// "round_count",
// round_count},
// {
// "hand_info",
// hand_info},
// {
// "round_state",
// DataEncoder.Instance.EncodeRoundState(state)}};

//foreach (var item in DataEncoder.Instance.EncodeWinners(winners))
//{
// message[item.Key] = item.Value;
//}

//return this.BuildNotificationMessage(message);
}

public GameResultMessage BuildGameResultMessage(GameConfig config, Seats seats)
Expand Down
22 changes: 14 additions & 8 deletions src/NPokerEngine/Engine/RoundManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ internal class RoundManager : Singleton<RoundManager>
}

public Tuple<GameState, List<IMessage>> ApplyAction(GameState original_state, ActionType action, float bet_amount)
{
ActionHistoryEntry appliedActionHistoryEntry;
return ApplyAction(original_state, action, bet_amount, out appliedActionHistoryEntry);
}

public Tuple<GameState, List<IMessage>> ApplyAction(GameState original_state, ActionType action, float bet_amount, out ActionHistoryEntry appliedActionHistoryEntry)
{

var state = this.DeepCopyState(original_state);
state = this.UpdateStateByAction(state, action, bet_amount);
state = this.UpdateStateByAction(state, action, bet_amount, out appliedActionHistoryEntry);
var table = state.Table;
var update_msg = this.UpdateMessage(state, action, bet_amount);
if (this.IsEveryoneAgreed(state))
Expand Down Expand Up @@ -174,7 +180,7 @@ private void DealHolecard(Deck deck, IEnumerable<Player> players)
{
var (winners, hand_info, prize_map) = GameEvaluator.Instance.Judge((Table)state.Table);
this.PrizeToWinners(state.Table.Seats.Players, prize_map);
var result_message = MessageBuilder.Instance.BuildRoundResultMessage(state.RoundCount, winners, hand_info, state);
var result_message = MessageBuilder.Instance.BuildRoundResultMessage(state.RoundCount, winners, hand_info, state, prize_map);
state.Table.Reset();
state.Street = (StreetType)(Convert.ToByte(state.Street) + 1);
return (state, new List<IMessage>() { result_message });
Expand Down Expand Up @@ -224,34 +230,34 @@ private List<IMessage> RoundStartMessage(int round_count, Table table)
}
}

private GameState UpdateStateByAction(GameState state, ActionType action, float bet_amount)
private GameState UpdateStateByAction(GameState state, ActionType action, float bet_amount, out ActionHistoryEntry actionHistoryEntry)
{
(action, bet_amount) = ActionChecker.Instance.CorrectAction(state.Table.Seats.Players, state.NextPlayerIx, state.SmallBlindAmount, action, bet_amount);
var next_player = state.Table.Seats.Players[state.NextPlayerIx];
if (ActionChecker.Instance.IsAllin(next_player, action, bet_amount))
{
next_player.PayInfo.UpdateToAllin();
}
return this.AcceptAction(state, action, bet_amount);
return this.AcceptAction(state, action, bet_amount, out actionHistoryEntry);
}

private GameState AcceptAction(GameState state, ActionType action, float bet_amount)
private GameState AcceptAction(GameState state, ActionType action, float bet_amount, out ActionHistoryEntry actionHistoryEntry)
{
var player = state.Table.Seats.Players[state.NextPlayerIx];
if (action == ActionType.CALL)
{
this.ChipTransaction(player, bet_amount);
player.AddActionHistory(ActionType.CALL, bet_amount);
actionHistoryEntry = player.AddActionHistory(ActionType.CALL, bet_amount);
}
else if (action == ActionType.RAISE)
{
this.ChipTransaction(player, bet_amount);
var add_amount = bet_amount - ActionChecker.Instance.AgreeAmount(state.Table.Seats.Players);
player.AddActionHistory(ActionType.RAISE, bet_amount, add_amount);
actionHistoryEntry = player.AddActionHistory(ActionType.RAISE, bet_amount, add_amount);
}
else if (action == ActionType.FOLD)
{
player.AddActionHistory(ActionType.FOLD);
actionHistoryEntry = player.AddActionHistory(ActionType.FOLD);
player.PayInfo.UpdateToFold();
}
else
Expand Down
1 change: 1 addition & 0 deletions src/NPokerEngine/Messages/RoundResultMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class RoundResultMessage : IMessage
public int RoundCount { get; set; }
public GameState State { get; set; }
public List<Player> Winners { get; set; }
public Dictionary<int, float> PrizeMap { get; set; }

public MessageType MessageType => MessageType.ROUND_RESULT_MESSAGE;
}
Expand Down
3 changes: 2 additions & 1 deletion src/NPokerEngine/Types/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public bool IsWaitingAsk()
return this._payInfo.Status == PayInfoStatus.PAY_TILL_END;
}

public void AddActionHistory(ActionType kind, float chipAmount = 0, float addAmount = 0, float sbAmount = 0)
public ActionHistoryEntry AddActionHistory(ActionType kind, float chipAmount = 0, float addAmount = 0, float sbAmount = 0)
{
ActionHistoryEntry history = null;
if (kind == ActionType.FOLD)
Expand Down Expand Up @@ -117,6 +117,7 @@ public void AddActionHistory(ActionType kind, float chipAmount = 0, float addAmo
}
history.Uuid = this._uuid;
this._actionHistories.Add(history);
return history;
}

public void SaveStreetActionHistories(StreetType street)
Expand Down

0 comments on commit 950f608

Please sign in to comment.