等到正确的项目添加到队列

时间:2018-03-20 23:21:23

标签: c# .net async-await

这不是严格正确的假装它。在现场扑克游戏中,您不能下注。在线你可以在没有人看到你的情况下下注。服务器只是排队下注并在轮到你时处理它。当轮到你时,如果你还没有排队,那么服务器会等待x秒让你下注或弃你(消除)。

在此模拟中,pp3不排队下注,所以当轮到他们时,没有赌注。调用WaitFor。即使clearToGo == false WaitFor仍然会调用return。

我需要WaitFor在返回之前等待clearToGo为真。即使clearToGo为假,我也需要它在x秒后返回。

布尔clearToGo可能不是正确的做法。或者其他东西可能不是正确的方法。我想表明我累了。

服务器如何让玩家提前下注,如果他们不提前下注只给他们x秒,然后忽略他们?

public class PkrTable 
{
    List<PkrAction> pkrActions = new List<Program.PkrAction>();
    List<PkrPlayer> pkrPlayers = new List<PkrPlayer>();
    public void SbmtAction (PkrAction pkrAction)
    {
        if (!BeenWaitinOnYou(pkrAction))
        {
            pkrActions.Add(pkrAction);
        }
    }
    private void ProcessAction(PkrAction pkrAction)
    {
        //process               
    }
    private bool BeenWaitinOnYou(PkrAction pkrAction)
    {
        if(waitingOn != null && waitingOn == pkrAction.PkrPlayer)
        {
            ProcessAction(pkrAction);
            //this is where WaitFor can return
            clearToGo = true; 
            return true;
        }
        return false;
    }
    private PkrPlayer waitingOn = null;
    private bool clearToGo = false;
    private async Task WaitFor()
    {
        if (clearToGo)
        {
            waitingOn = null;
            return;
            //problem is this returns even if clearToGo is flase 
            //even if clearToGo is false I need to return in x seconds
        }
    }
    private async Task ProcessQueue()
    {
        foreach(PkrPlayer pp in pkrPlayers)
        {
            PkrAction pa = pkrActions.FirstOrDefault(x => x.PkrPlayer == pp);
            if(pa == null)
            {
                waitingOn = pp;
                clearToGo = false;
                await WaitFor(); 
            }
            else
            {
                ProcessAction(pa); 
                pkrActions.Remove(pa);                      
            }
        }
    }
    public PkrTable()
    {
        PkrPlayer pp = new PkrPlayer(1, this);
        pp.SbmtAction(12);
        pkrPlayers.Add(pp);
        pp = new PkrPlayer(2, this);
        pp.SbmtAction(12);
        pkrPlayers.Add(pp);
        PkrPlayer pp3 = new PkrPlayer(3, this);
        pkrPlayers.Add(pp3);
        pp = new PkrPlayer(4, this);
        pp.SbmtAction(12);
        pkrPlayers.Add(pp);

        ProcessQueue();
        Thread.Sleep(10000);
        pp3.SbmtAction(14);               
    }

}
public class PkrAction
{
    public PkrPlayer PkrPlayer { get; }
    public int Amt { get; }
    public PkrAction(PkrPlayer pkrPlayer, int amt)
    {
        PkrPlayer = pkrPlayer;
        Amt = amt;
    }
}
public class PkrPlayer
{
    private PkrTable pkrTable;
    public int ID { get; }

    public void SbmtAction(int amount)
    {
        PkrAction pa = new PkrAction(this, amount);
        pkrTable.SbmtAction(pa);
    }

    public override int GetHashCode()
    {
        return ID;
    }
    public override bool Equals(object obj)
    {
        if (obj == null || !(obj is PokerPlayer))
            return false;
        else
            return ID == ((PokerPlayer)obj).ID;
    }
    public PkrPlayer(int id, PkrTable _pkrTable)
    {
        ID = id;
        pkrTable = _pkrTable;
    }
}

0 个答案:

没有答案