Example #1
0
        private async Task SellTicketAndSetEntityToNextNode(Entity customerEntity)
        {
            IsProcessing = true; // TODO properly synchronize this to prevent threading issues
            var customer = customerEntity.GetBehavior <CustomerBehavior>();

            if (customer == null)
            {
                // not a customer -> ignore
                customerEntity.SetParent(NextNode.Entity, Reasons.NotAllowed);
                ResetIsProcessingAndCheckForNextCustomer();
                return;
            }

            if (!customer.Inventory.TryTakeItem(Items.Money, TicketPrice))
            {
                // not enough money -> set forward
                customer.Entity.SetParent(NextNode.Entity, Reasons.DoesNotHaveRequiredItem);
                ResetIsProcessingAndCheckForNextCustomer();
                return;
            }

            // enough money taken -> wait processing time, give ticket and set forward
            if (MaximumProcessingSeconds > 0)
            {
                var waitTime = TimeSpan.FromSeconds(new Random().Next(MinimumProcessingSeconds, MaximumProcessingSeconds));
                await Timing.Delay(waitTime);
            }
            customer.Inventory.AddItem(Items.SkiTicket, 1);
            customer.Entity.IsEnabled = true;
            customer.Entity.SetParent(NextNode.Entity, Reasons.Processing.Finished, SetParentFlags.SuppressChildEnter);
            ResetIsProcessingAndCheckForNextCustomer();
        }