コード例 #1
0
        public override void AgentCycle(YeOldePub yeOldePub)
        {
            while (hasGoneHome is false)
            {
                switch (CheckState(yeOldePub))
                {
                case RunState.Working:
                    Thread.Sleep(GetLeadTime());
                    NumOfPatronsToLetInside = 1;
                    if (partyBusMode is true && DataManager.time <= TimeSpan.FromSeconds(YeOldePub.TimePubIsOpen - 20.0))
                    {
                        NumOfPatronsToLetInside = 20;
                        partyBusMode            = false;
                    }
                    for (int patron = 0; patron < NumOfPatronsToLetInside; patron++)
                    {
                        var newPatron = new Patron(yeOldePub);
                        yeOldePub.Patrons.TryAdd(newPatron.Name, newPatron);
                    }
                    break;

                case RunState.LeavingThePub:
                    DataManager.RefreshList(this, "Bouncer is going home");
                    hasGoneHome = true;
                    break;
                }
            }
        }
コード例 #2
0
 public RunState CheckState(YeOldePub yeOldePub)
 {
     if (yeOldePub.currentPubState is PubState.Closed)
     {
         return(RunState.LeavingThePub);
     }
     return(RunState.Working);
 }
コード例 #3
0
        //Constructor
        public Patron(YeOldePub yeOldePub)
        {
            YeOldePub   = yeOldePub;
            DataManager = yeOldePub.DataManager;
            Random rnd = new Random();

            Name = GetRandomPatronName(rnd);
            TimeSpentDrinkingBeer = 1000 * (rnd.Next(20, 31));
            CurrentState          = RunState.WalkingToBar;

            Run(yeOldePub);
        }
コード例 #4
0
 public RunState CheckState(YeOldePub yeOldePub)
 {
     if (yeOldePub.Patrons.IsEmpty && yeOldePub.Tables.Count is 0 && yeOldePub.currentPubState is PubState.Closed)
     {
         return(RunState.LeavingThePub);
     }
     if (yeOldePub.Tables.Count > 0)
     {
         return(RunState.Working);
     }
     return(RunState.Idling);
 }
コード例 #5
0
        public override void AgentCycle(YeOldePub yeOldePub)
        {
            while (hasGoneHome is false)
            {
                switch (CheckState(yeOldePub))
                {
                case RunState.Idling:
                    Thread.Sleep(TimeSpentIdling);
                    if (hasBeenProductive is true)
                    {
                        DataManager.RefreshList(this, "Is currently idling");
                    }
                    hasBeenProductive = false;
                    break;

                case RunState.Working:
                    //Gather empty pints from Tables
                    DataManager.RefreshList(this, "Gathering dirty pints from tables");
                    foreach (var pintGlass in yeOldePub.Tables.Where(g => g.HasBeer is false && g.IsClean is false))
                    {
                        PintGlass gatheredPintGlass = null;
                        while (gatheredPintGlass is null)
                        {
                            yeOldePub.Tables.TryTake(out gatheredPintGlass);
                        }
                        tray.Add(gatheredPintGlass);
                    }
                    Thread.Sleep(TimeSpentCollectingPintGlass);

                    //Clean glass and place on Shelves
                    DataManager.RefreshList(this, $"Cleaning {tray.Count} pint(s)");
                    Thread.Sleep(TimeSpentWashingPintGlass);
                    DataManager.RefreshList(this, "Placing clean pints on the shelves");
                    foreach (var pintGlass in tray)
                    {
                        pintGlass.IsClean = true;
                        if (yeOldePub.Shelves.TryAdd(pintGlass))
                        {
                            tray.TryTake(out PintGlass glass);
                        }
                    }
                    hasBeenProductive = true;
                    break;

                case RunState.LeavingThePub:
                    DataManager.RefreshList(this, "Going home");
                    hasGoneHome = true;
                    break;
                }
            }
        }
コード例 #6
0
 public RunState CheckState(YeOldePub yeOldePub)
 {
     //Check to see if bartender should work or go home
     if (yeOldePub.Patrons.IsEmpty && yeOldePub.currentPubState is PubState.Closed)
     {
         return(RunState.LeavingThePub);
     }
     else if (yeOldePub.PatronsWaitingForBeer.IsEmpty == false && yeOldePub.Shelves.Count > 0)
     {
         return(RunState.Working);
     }
     else
     {
         return(RunState.Idling);
     }
 }
コード例 #7
0
        public void OpenPub()
        {
            YeOldePub = new YeOldePub(this);
            time      = TimeSpan.FromSeconds(YeOldePub.TimePubIsOpen);
            timer     = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
            {
                MainWindow.PubCountdown.Content = time.ToString("c");
                if (time == TimeSpan.Zero && YeOldePub.currentPubState is PubState.Open)
                {
                    YeOldePub.currentPubState = PubState.Closed;
                }
                if (
                    YeOldePub.currentPubState is PubState.Closed &&
                    YeOldePub.Bartender.hasGoneHome is true &&
                    YeOldePub.Bouncer.hasGoneHome is true &&
                    YeOldePub.Waitress.hasGoneHome is true
                    )
                {
                    timer.Stop();
                }
                time = time.Add(TimeSpan.FromSeconds(-1));

                RefreshLabels();
            }, Application.Current.Dispatcher);
コード例 #8
0
ファイル: Agent.cs プロジェクト: JohanCardell/Labb6-YeOldePub
 public void Run(YeOldePub yeOldePub)
 {
     Task.Run(() => AgentCycle(yeOldePub));
 }
コード例 #9
0
ファイル: Agent.cs プロジェクト: JohanCardell/Labb6-YeOldePub
 public abstract void AgentCycle(YeOldePub yeOldePub);
コード例 #10
0
        public override void AgentCycle(YeOldePub yeOldePub)
        {
            while (hasGoneHome is false)
            {
                switch (CurrentState)
                {
                case RunState.WalkingToBar:
                    DataManager.RefreshList(this, $"{Name} entered and is walking to the bar");
                    yeOldePub.PatronsWaitingForBeer.Enqueue(this);
                    CurrentQueue = yeOldePub.PatronsWaitingForBeer;
                    Thread.Sleep(1000);
                    CurrentState = RunState.WaitingForBeer;
                    break;

                case RunState.WaitingForBeer:
                    DataManager.RefreshList(this, $"{Name} is waiting for a pint of beer");
                    while (pintGlass is null)
                    {
                        Thread.Sleep(TimeSpentWaiting);     // Give the bartender som time before trying again
                    }
                    DataManager.RefreshList(this, $"{Name} got a pint of beer");
                    DequeuePatron(CurrentQueue, this);
                    CurrentState = RunState.WaitingForChair;
                    break;

                case RunState.WaitingForChair:
                    yeOldePub.PatronsWaitingForChair.Enqueue(this);
                    CurrentQueue = yeOldePub.PatronsWaitingForChair;
                    DataManager.RefreshList(this, $"{Name} is waiting for an available chair");
                    //Check to see if patron is first in line
                    var isFirstInQueue = false;
                    while (isFirstInQueue is false)
                    {
                        //Spend time checking
                        Thread.Sleep(TimeSpentWaiting);
                        yeOldePub.PatronsWaitingForChair.TryPeek(out var result);
                        if (this.Equals(result))
                        {
                            isFirstInQueue = true;
                            while (chair is null)
                            {
                                foreach (var chair in yeOldePub.Chairs)
                                {
                                    if (!(chair.IsAvailable))
                                    {
                                        continue;
                                    }
                                    this.chair        = chair; //Dibs on available chair
                                    chair.IsAvailable = false;
                                    DequeuePatron(CurrentQueue, this);
                                    DataManager.RefreshLabels();
                                    break;
                                }
                            }
                        }
                    }
                    CurrentState = RunState.WalkingToChair;
                    break;

                case RunState.WalkingToChair:
                    DataManager.RefreshList(this, $"{Name} is walking to a chair");
                    Thread.Sleep(TimeSpentWalkingToChair);
                    CurrentState = RunState.DrinkingBeer;
                    break;

                case RunState.DrinkingBeer:
                    //Drink beer
                    DataManager.RefreshList(this, $"{Name} is drinking a pint of beer");
                    Thread.Sleep(TimeSpentDrinkingBeer);
                    pintGlass.HasBeer = false;

                    //Place empty glass on table
                    yeOldePub.Tables.Add(pintGlass);
                    chair.IsAvailable = true;
                    CurrentState      = RunState.LeavingThePub;
                    break;

                case RunState.LeavingThePub:
                    //Remove patron from pub
                    DataManager.RefreshList(this, $"{Name} finished the beer and is going home");
                    while (hasGoneHome is false)
                    {
                        hasGoneHome = yeOldePub.Patrons.TryRemove(Name, out _);
                    }
                    hasGoneHome = true;
                    break;
                }
            }
        }
コード例 #11
0
 //Constructor
 public Waitress(YeOldePub yeOldePub)
 {
     YeOldePub   = yeOldePub;
     DataManager = yeOldePub.DataManager;
     tray        = new ConcurrentBag <PintGlass>();
 }
コード例 #12
0
        public override void AgentCycle(YeOldePub yeOldePub)
        {
            while (hasGoneHome is false)
            {
                switch (CheckState(yeOldePub))
                {
                case RunState.Idling:
                    if (hasBeenProductive is true)
                    {
                        if (yeOldePub.PatronsWaitingForBeer.IsEmpty)
                        {
                            DataManager.RefreshList(this, "Waiting for a patron");
                        }
                        else if (yeOldePub.Shelves.Count is 0)
                        {
                            DataManager.RefreshList(this, "Waiting for clean pints");
                        }
                    }
                    hasBeenProductive = false;
                    Thread.Sleep(100);
                    break;

                case RunState.Working:
                    //Identify patron in first in queue
                    Patron patronBeingServed = null;
                    while (patronBeingServed is null)
                    {
                        yeOldePub.PatronsWaitingForBeer.TryPeek(out patronBeingServed);
                    }
                    if (patronBeingServed.pintGlass is null)
                    {
                        DataManager.RefreshList(this, $"Taking order from {patronBeingServed.Name}");

                        //Get clean glass from Shelves
                        while (pintGlass is null)
                        {
                            yeOldePub.Shelves.TryTake(out pintGlass);
                        }
                        DataManager.RefreshList(this, "Getting a glass from the shelves");
                        Thread.Sleep(TimeSpentGettingGlass);

                        //Fill glass with beer
                        pintGlass.HasBeer = true;
                        pintGlass.IsClean = false;
                        DataManager.RefreshList(this, "Filling glass with beer");
                        Thread.Sleep(TimeSpentFillingGlassWithBeer);

                        //Give glass to customer
                        patronBeingServed.pintGlass = pintGlass;
                        DataManager.RefreshList(this, $"Giving beer to {patronBeingServed.Name}");
                        pintGlass         = null;
                        hasBeenProductive = true;
                    }
                    break;

                case RunState.LeavingThePub:
                    DataManager.RefreshList(this, "Going home");
                    hasGoneHome = true;
                    break;
                }
            }
        }
コード例 #13
0
 //Constructor
 public Bartender(YeOldePub yeOldePub)
 {
     YeOldePub   = yeOldePub;
     DataManager = yeOldePub.DataManager;
 }
コード例 #14
0
 //Constructor
 public Bouncer(YeOldePub yeOldePub)
 {
     YeOldePub   = yeOldePub;
     DataManager = yeOldePub.DataManager;
 }