Пример #1
0
        public override void Activate(YeOldePub yeOldePub)
        {
            while (_hasGoneHome is false)
            {
                switch (CheckState(yeOldePub))
                {
                case RunState.Idle:
                    //Wait before checking for new patron
                    Thread.Sleep(1000);
                    MessageLog.Enqueue($"{DateTime.UtcNow}: Waiting for a patron");
                    //OnMessageLogged(new MessageLogEventArgs($"{DateTime.UtcNow}: Waiting for a patron"));
                    break;

                case RunState.Work:
                    //Identify patron in first in queue
                    Patron patronBeingServed = null;
                    while (patronBeingServed is null)
                    {
                        _ = yeOldePub.PatronsWaitingForBeer.TryPeek(out patronBeingServed);
                    }
                    MessageLog.Enqueue($"{DateTime.UtcNow}: Taking order from {patronBeingServed}");

                    //Get clean glass from Shelves
                    while (_pintGlass is null)
                    {
                        _ = yeOldePub.Shelves.TryTake(out _pintGlass);
                    }
                    Thread.Sleep(TimeSpentGettingGlass);
                    MessageLog.Enqueue($"{DateTime.UtcNow}: Getting a glass from the shelves");

                    //Fill glass with beer
                    _pintGlass.HasBeer = true;
                    _pintGlass.IsClean = false;
                    Thread.Sleep(TimeSpentFillingGlassWithBeer);
                    MessageLog.Enqueue($"{DateTime.UtcNow}: Filling glass with beer");

                    //Give glass to customer
                    patronBeingServed.PintGlass = _pintGlass;
                    _pintGlass = null;
                    MessageLog.Enqueue($"{DateTime.UtcNow}: Giving beer to {patronBeingServed}");
                    break;

                case RunState.LeavingThePub:
                    MessageLog.Enqueue($"{DateTime.UtcNow}: Going home");
                    _hasGoneHome = true;
                    break;
                }
            }
        }
Пример #2
0
        public override void Activate(YeOldePub yeOldePub)
        {
            while (_hasGoneHome is false)
            {
                switch (CurrentState)
                {
                case RunState.WalkingToBar:
                    MessageLog.Enqueue($"{DateTime.UtcNow}: {this.Name} entered and is walking to the bar");
                    Thread.Sleep(1000);
                    yeOldePub.PatronsWaitingForBeer.Enqueue(this);
                    CurrentState = RunState.WaitingForBeer;
                    break;

                case RunState.WaitingForBeer:
                    if (PintGlass is null)
                    {
                        MessageLog.Enqueue($"{DateTime.UtcNow}: {this.Name} is waiting for a pint of beer");
                        Thread.Sleep(TimeSpentWaiting);     // Give the bartender som time before trying again
                    }
                    else
                    {
                        MessageLog.Enqueue($"{DateTime.UtcNow}: {this.Name} got a pint of beer");
                        MessageLog.Enqueue($"{DateTime.UtcNow}: {this.Name} is waiting for an available chair");
                        DequeuePatron(CurrentQueue, this);
                        CurrentState = RunState.WaitingForChair;
                    }

                    break;

                case RunState.WaitingForChair:
                    yeOldePub.PatronsWaitingForChair.Enqueue(this);

                    //Check to see if patron is first in line
                    var isFirstInQueue = false;
                    while (isFirstInQueue is false)
                    {
                        //Spend time checking
                        Thread.Sleep(TimeSpentWaiting);
                        isFirstInQueue = yeOldePub.PatronsWaitingForChair.TryPeek(out var result);
                        if (this != result)
                        {
                            continue;
                        }
                        foreach (var chair in yeOldePub.Chairs)
                        {
                            if (!(chair.IsAvailable))
                            {
                                continue;
                            }
                            this.Chair        = chair; //Dibs on available chair
                            chair.IsAvailable = false;
                            DequeuePatron(CurrentQueue, this);
                            CurrentState = RunState.GoingToChair;
                        }
                    }

                    break;

                case RunState.GoingToChair:
                    MessageLog.Enqueue($"{DateTime.UtcNow}: {this.Name} is walking to a chair");
                    Thread.Sleep(TimeSpentWalkingToChair);
                    //_isSitting = true;
                    CurrentState = RunState.DrinkingBeer;
                    break;

                case RunState.DrinkingBeer:
                    //Drink beer
                    MessageLog.Enqueue($"{DateTime.UtcNow}: {this.Name} is drinking a pint of beer");
                    Thread.Sleep(TimeSpentDrinkingBeer);
                    PintGlass.HasBeer = false;
                    //_isThirsty = false;

                    //Place empty glass on table
                    MessageLog.Enqueue($"{DateTime.UtcNow}: {this.Name} is done drinking and is getting ready to leave");
                    yeOldePub.Tables.Add(PintGlass);
                    //_isSitting = false;
                    Chair.IsAvailable = true;
                    CurrentState      = RunState.LeavingThePub;
                    break;

                case RunState.LeavingThePub:
                    //Remove patron from pub
                    MessageLog.Enqueue($"{DateTime.UtcNow}: {this.Name} is going home");
                    while (_hasGoneHome is false)
                    {
                        _hasGoneHome = yeOldePub.Patrons.TryRemove(this.Name, out _);
                    }
                    _hasGoneHome = true;
                    break;
                }
                //CheckState(yeOldePub);
            }
        }