using MessagePack;
using OECS;
namespace Game.Blackjack;
///
/// Starts a new round after the previous one ended.
///
[MessagePackObject]
public struct NewRoundCommand : ICommand
{
public void Execute(World world)
{
ref var state = ref world.GetSingleton();
if (state.Phase != GamePhase.RoundOver)
return;
// Clear hands from previous round.
var handEntities = new List();
using (var iter = world.Select())
{
while (iter.MoveNext())
{
handEntities.Add(iter.Entity);
}
}
using (var iter = world.Select())
{
while (iter.MoveNext())
{
handEntities.Add(iter.Entity);
}
}
foreach (var hand in handEntities)
{
var cards = world.GetSources(hand);
var deckEntity = world.FindEntity();
foreach (var card in cards)
{
world.RemoveComponent(card);
world.AddComponent(card, new InDeck { Target = deckEntity });
}
}
state.RoundNumber++;
state.Phase = GamePhase.Betting;
state.Result = RoundResult.None;
}
}