public void AddServicePoint() { ServicePoint s = new ServicePoint(); _spList.Add(s); s.RegisterObserver(this); s.Start(); NotifyObservers(); }
public ServicePointSystem() { _observers = new List<iSPSObserver>(); _spList = new List<ServicePoint>(); for (int i = 0; i < Store.Get().StoreParams.MaximumServicePoints; i++) //#ANDRE { ServicePoint s = new ServicePoint(); _spList.Add(s); s.RegisterObserver(this); if (i < Store.Get().StoreParams.InitialServicePoints) s.Start(); } NotifyObservers(); }
//Calculates the distance from the Customer to SP. used for SP Priority private double _distanceToSP(ServicePoint s) { double dx = s.X - X; double dy = s.Y - Y; return Math.Sqrt(dx * dx + dy * dy); }
public void tryMove() { upToDate = true; List<ServicePoint> sp = Store.Get().SPS.GetAvailableSP(); ServicePoint s = _selectFavoriteSP(sp); if (s != null) { lock (this) { _goToSP = s; } } }
//The Brains of the Customer. The Ugliness is Densest Here. public bool ProcessSelf() { switch (state) { case CustomerState.Shopping: //Wait for Finding Item System.Threading.Thread.Sleep(Store.Get().StoreParams.TimeToBrowsePerItem); //Find Random Item on the List and put in in the cart! lock (this) { Item found = itemList[Store.rand.Next(0, itemList.Count)]; itemList.RemoveAt(Store.rand.Next(0, itemList.Count)); shoppingCart.Add(found); } if (itemList.Count == 0) // No More items on our List so go to queue { //MainQueue State change state = CustomerState.MainQueue; Queue<Customer> l = Store.Get().MainQueue; lock (l) { l.Enqueue(this); Program.Debug("Customer #" + ID + " -> Main Queue in Pos " + l.Count); } } break; case CustomerState.MainQueue: //Just wait until we are at the front of the Main Queue if (Store.Get().MainQueue.Peek() == this) { //We are first now Change States state = CustomerState.FrontOfMainQueue; // -- Start observing the Service Points Store.Get().SPS.RegisterObserver(this); //Check if we can just go OnSPSUpdate(); } break; case CustomerState.FrontOfMainQueue: // _goToSP will be set in a different thread via OnSPSUpdate() //Check if we have been notified and want to go somewhere if (!upToDate) tryMove(); if (_goToSP != null) { if (_goToSP.EnqueueCustomer(this)) //Another customer might have ninja'd it { //Change to SP Queue lock (Store.Get().MainQueue) { Store.Get().MainQueue.Dequeue(); Store.Get().SPS.UnregisterObserver(this); } state = CustomerState.ServicePointQueue; } _goToSP = null; } System.Threading.Thread.Sleep(Store.Get().StoreParams.ReactionTimeCustomer); break; case CustomerState.ServicePointQueue: //Waiting in Line... Waiting in Line... Items Scanning... ladedaa. //This will be broken by the ServicePoint Telling us to get out of the line System.Threading.Thread.Sleep(Store.Get().StoreParams.ReactionTimeCustomer); break; case CustomerState.Exiting: //Bye Bye Store Program.Debug("Customer #" + ID + " -> Finished Paying"); System.Threading.Thread.Sleep(Store.Get().StoreParams.TimeToExitStore); lock (Store.Get().CustomerPool) { Store.Get().CustomerPool.Remove(this); } Program.Debug("Customer #" + ID + " -> Exited Store"); Program.Debug("customer in store count = " + Store.Get().CustomerPool.Count); return false; } return true; }
public void OpenServicePoint(ServicePoint p) { p.Start(); NotifyObservers(); }
public void CloseServicePoint(ServicePoint p) { p.Close(); //_spList.Remove(p); NotifyObservers(); }