예제 #1
0
        public RsdBossActor(string name, IActorRef printerActorRef)
        {
            _name            = name;
            _printerActorRef = printerActorRef;

            Random r = new Random();
            var    nrOfLocalityCiefs = r.Next(1, 4);

            for (int i = 0; i < nrOfLocalityCiefs; i++)
            {
                var g             = r.Next(1, 3);
                var randomGender  = (GenderEnum)g;
                var randomName    = RomanianNameSurnameUtils.GetName(randomGender);
                var randomSurname = RomanianNameSurnameUtils.GetSurname();

                var childName = $"{randomSurname}~{randomName}";

                var childActorRef = Context.ActorOf(RsdCountyCiefActor.Props(childName, randomGender, printerActorRef), childName);
                Context.Watch(childActorRef);
            }

            Receive <TaxIncomeMessage>(x =>
            {
                _privateCapital += x.Amount;
                Print();
            });

            Print();

            Receive <GothcaMessage>(x =>
            {
                printerActorRef.Tell(new PrintBustedMessage(_name));
                throw new GotMeException("Ho my god they cought me");
            });
        }
예제 #2
0
        public RsdLocalityCiefActor(string name, GenderEnum gender, IActorRef printerActorRef)
        {
            _name            = name;
            _gender          = gender;
            _printerActorRef = printerActorRef;

            Random r           = new Random();
            var    nrOfWorkers = r.Next(2, 6);

            for (int i = 0; i < nrOfWorkers; i++)
            {
                var g             = r.Next(1, 3);
                var randomGender  = (GenderEnum)g;
                var randomName    = RomanianNameSurnameUtils.GetName(randomGender);
                var randomSurname = RomanianNameSurnameUtils.GetSurname();

                var childName     = $"{randomSurname}~{randomName}";
                var workerProps   = RsdSimpleWorkerActor.Props(childName, randomGender, printerActorRef);
                var childActorRef = Context.ActorOf(workerProps, childName);
                _childActorRefs.Add(childActorRef);
                Context.Watch(childActorRef);
            }

            if (!StateHolder.StartedActors.Contains(_name))
            {
                StateHolder.StartedActors.TryAdd(_name);
                Print();
            }

            Receive <TaxIncomeMessage>(x =>
            {
                _privateCapital += x.Amount;
                Print();
            });

            Receive <GimmyTaxMessage>(x =>
            {
                if (Sender.Equals(Context.Parent))
                {
                    var taxAmount = _privateCapital * 0.65;
                    if (_privateCapital - taxAmount >= 0)
                    {
                        Sender.Tell(new TaxIncomeMessage((int)taxAmount));
                        _printerActorRef.Tell(new MoneyFlowMessage(Context.Parent.Path.Name, (int)taxAmount));
                        _privateCapital -= (int)taxAmount;
                        Print();
                    }
                }
            });
            Receive <Terminated>(t =>
            {
                _childActorRefs.Remove(Sender);
            });
            Receive <GothcaMessage>(x =>
            {
                printerActorRef.Tell(new PrintBustedMessage(_name));
                throw new GotMeException("Ho my god they cought me");
            });

            Receive <CollectTaxesMessage>(x =>
            {
                foreach (var worker in _childActorRefs)
                {
                    worker.Tell(GimmyTaxMessage.Instance);
                }
            });

            Context.System.Scheduler
            .ScheduleTellRepeatedly(TimeSpan.FromSeconds(20),
                                    TimeSpan.FromSeconds(20),
                                    Self,
                                    CollectTaxesMessage.Instance,
                                    ActorRefs.Nobody);
        }