public AtBatSupervisor()
        {
            Receive <BatterEventMessage>(msg =>
            {
                Boolean isHit           = false;
                BatterEventType hitType = BatterEventParser.BatterEventResult(msg.BatterEvent);

                switch (hitType)
                {
                case BatterEventType.Single:
                case BatterEventType.Double:
                case BatterEventType.Triple:
                case BatterEventType.Homer:
                    isHit = true;
                    break;

                default:
                    isHit = false;
                    break;
                }

                var atBatActor = Context.Child(msg.PlayerId + "-" + msg.Count);
                if (atBatActor == ActorRefs.Nobody)
                {
                    atBatActor = Context.ActorOf(AtBatEventWriterActor.Create(msg.PlayerId, msg.Count), msg.PlayerId + "-" + msg.Count);
                }
                atBatActor.Tell(new AtBatInfoMessage(msg.PlayerId, msg.Count, isHit, hitType.ToString()));
            });

            Receive <EndOfFeed>(msg =>
            {
                Console.WriteLine("About to write events to store");
                var atBatActors = Context.GetChildren();
                foreach (var actor in atBatActors)
                {
                    actor.Tell(new Complete());
                }
            });
        }
Пример #2
0
        public BatterActor(String playerId)
        {
            _playerId      = playerId;
            _batterInfo    = new BatterInfo();
            _batterInfo.Id = playerId;

            var url    = new MongoUrl("mongodb://localhost:27020/BaseballRetro");
            var client = new MongoClient(url);
            var db     = client.GetDatabase(url.DatabaseName);

            Receive <BatterEventMessage>(msg =>
            {
                BatterEventType hitType = BatterEventParser.BatterEventResult(msg.BatterEvent);
                switch (hitType)
                {
                case BatterEventType.Double:
                    _batterInfo.Doubles++;
                    _batterInfo.Hits++;
                    _batterInfo.PlateAppearances++;
                    _batterInfo.AtBats++;
                    break;

                case BatterEventType.Homer:
                    _batterInfo.HomeRuns++;
                    _batterInfo.Hits++;
                    _batterInfo.PlateAppearances++;
                    _batterInfo.AtBats++;
                    break;

                case BatterEventType.Single:
                    _batterInfo.Hits++;
                    _batterInfo.PlateAppearances++;
                    _batterInfo.AtBats++;
                    break;

                case BatterEventType.Triple:
                    _batterInfo.Hits++;
                    _batterInfo.Triples++;
                    _batterInfo.PlateAppearances++;
                    _batterInfo.AtBats++;
                    break;

                case BatterEventType.Walk:
                    _batterInfo.Walks++;
                    _batterInfo.PlateAppearances++;
                    break;

                case BatterEventType.NoPlay:
                    _batterInfo.PlateAppearances++;
                    break;

                default:
                    _batterInfo.PlateAppearances++;
                    _batterInfo.AtBats++;
                    break;
                }
            });

            Receive <EndOfFeed>(msg =>
            {
                var coll = db.GetCollection <BatterInfo>("batterInfo");
                coll.ReplaceOneAsync(new BsonDocument("_id", _batterInfo.Id), _batterInfo, new UpdateOptions {
                    IsUpsert = true
                });
            });
        }