public Guard BeginShift(DateTime dateTime)
        {
            var action = new NightEvent(Id, dateTime, Actions.BeginShift);

            _schedule.Add(action);
            return(new Guard(Id, _schedule));
        }
        public Guard FallAsleep(DateTime dateTime)
        {
            var action = new NightEvent(Id, dateTime, Actions.FallAsleep);

            _schedule.Add(action);
            return(new Guard(Id, _schedule));
        }
        public void AddGuard(NightEvent e)
        {
            if (!_ticksAsleep.ContainsKey(e.GuardId))
            {
                _ticksAsleep.Add(e.GuardId, 0);
            }

            LastEvent = e;
        }
        public void WakeUp(NightEvent e)
        {
            if (LastEvent.GuardId != e.GuardId && LastEvent.Action != Actions.FallAsleep)
            {
                throw new InvalidOperationException("Cannot compute time asleep for 'wake up' if last known action is not fall asleep");
            }

            var currentTally = _ticksAsleep[e.GuardId];

            if (LastEvent.EventTime.Day != e.EventTime.Day)
            {
                throw new InvalidOperationException();
            }

            currentTally           += LastEvent.EventTime.SubtractMinutesFromOther(e.EventTime);
            _ticksAsleep[e.GuardId] = currentTally;
        }
        public void WakeUp(NightEvent e)
        {
            if (LastEvent.Action == Actions.FallAsleep)
            {
                var minutes = Enumerable.Range(LastEvent.EventTime.Minute, e.EventTime.Minute - 1);
                foreach (var minute in minutes)
                {
                    if (_minuteMap.ContainsKey(minute))
                    {
                        var minuteMapValue = _minuteMap[minute];
                        minuteMapValue++;
                        _minuteMap[minute] = minuteMapValue;
                    }
                }
            }

            LastEvent = e;
        }
        public void AddAsleepEvent(NightEvent e)
        {
            if (LastEvent.GuardId != e.GuardId)
            {
                return;
            }

//            if (_minutesCount.ContainsKey(e.EventTime.Minute))
//            {
//                var count = _minutesCount[e.EventTime.Minute];
//                count++;
//                _minutesCount[e.EventTime.Minute] = count;
//            }
//            else
//            {
//                _minutesCount.Add(e.EventTime.Minute, 1);
//            }

            LastEvent = e;
        }
Exemple #7
0
        public IObservable <NightEvent> GetScheduleStream()
        {
            var regex  = new Regex("\\d");
            var lastId = -1;

            return(Observable.Create <NightEvent>(obs =>
            {
                _rawSchedule.ForEach(x =>
                {
                    if (x.Metadata.Contains("Guard"))
                    {
                        var match = regex.Match(x.Metadata);
                        if (match.Success)
                        {
                            var id = int.Parse(match.Value);
                            lastId = id;
                            obs.OnNext(new NightEvent(lastId, x.Time, Actions.BeginShift));
                        }
                    }

                    if (x.Metadata.Contains("wake"))
                    {
                        var nightEvent = new NightEvent(lastId, x.Time, Actions.WakeUp);
                        obs.OnNext(nightEvent);
                    }

                    if (x.Metadata.Contains("falls"))
                    {
                        obs.OnNext(new NightEvent(lastId, x.Time, Actions.FallAsleep));
                    }
                });

                obs.OnCompleted();

                return Disposable.Empty;
            }));
        }
 public void FallAsleep(NightEvent e)
 {
     LastEvent = e;
 }