OSDN Git Service

先制対潜の判定を最新の検証結果に合わせる
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / NotificationManager.cs
index 18c13da..6c1ccd3 100644 (file)
@@ -16,7 +16,7 @@ using System;
 using System.Collections.Generic;\r
 using System.IO;\r
 using System.Linq;\r
-using System.Windows.Forms;\r
+using KancolleSniffer.Util;\r
 \r
 namespace KancolleSniffer\r
 {\r
@@ -42,9 +42,9 @@ namespace KancolleSniffer
             public DateTime Schedule { get; set; }\r
         }\r
 \r
-        public NotificationManager(Action<string, string, string> ring, ITimer timer = null)\r
+        public NotificationManager(Action<string, string, string> alarm, Func<DateTime> nowFunc = null)\r
         {\r
-            _notificationQueue = new NotificationQueue(ring, timer);\r
+            _notificationQueue = new NotificationQueue(alarm, nowFunc);\r
         }\r
 \r
         public void Enqueue(string key, int fleet, string subject, int repeat = 0, bool preliminary = false)\r
@@ -79,9 +79,19 @@ namespace KancolleSniffer
             _notificationQueue.StopRepeat(key, fleet);\r
         }\r
 \r
-        public void SuspendRepeat()\r
+        public void StopRepeat(string key, string subject)\r
         {\r
-            _notificationQueue.SuspendRepeat();\r
+            _notificationQueue.StopRepeat(key, subject);\r
+        }\r
+\r
+        public void StopAllRepeat()\r
+        {\r
+            _notificationQueue.StopAllRepeat();\r
+        }\r
+\r
+        public void SuspendRepeat(string exception = "")\r
+        {\r
+            _notificationQueue.SuspendRepeat(exception);\r
         }\r
 \r
         public void ResumeRepeat()\r
@@ -180,6 +190,13 @@ namespace KancolleSniffer
                         Title = "疲労が回復しました",\r
                         Body = "%f艦隊"\r
                     }\r
+                },\r
+                {\r
+                    "任務達成", new Message\r
+                    {\r
+                        Title = "任務を達成しました",\r
+                        Body = "%s"\r
+                    }\r
                 }\r
             };\r
 \r
@@ -271,70 +288,21 @@ namespace KancolleSniffer
             }\r
         }\r
 \r
-        public interface ITimer\r
-        {\r
-            int Interval { get; set; }\r
-            bool Enabled { get; set; }\r
-            event EventHandler Tick;\r
-            void Start();\r
-            void Stop();\r
-            DateTime Now { get; }\r
-        }\r
-\r
-        private class TimerWrapper : ITimer\r
-        {\r
-            private readonly Timer _timer = new Timer();\r
-\r
-            public int Interval\r
-            {\r
-                get => _timer.Interval;\r
-                set => _timer.Interval = value;\r
-            }\r
-\r
-            public bool Enabled\r
-            {\r
-                get => _timer.Enabled;\r
-                set => _timer.Enabled = value;\r
-            }\r
-\r
-            public event EventHandler Tick\r
-            {\r
-                add => _timer.Tick += value;\r
-                remove => _timer.Tick -= value;\r
-            }\r
-\r
-            public void Start() => _timer.Start();\r
-\r
-            public void Stop() => _timer.Stop();\r
-\r
-            public DateTime Now => DateTime.Now;\r
-        }\r
-\r
         private class NotificationQueue\r
         {\r
-            private readonly Action<string, string, string> _ring;\r
+            private readonly Action<string, string, string> _alarm;\r
             private readonly List<Notification> _queue = new List<Notification>();\r
-            private readonly ITimer _timer;\r
+            private readonly Func<DateTime> _nowFunc = () => DateTime.Now;\r
             private readonly NotificationConfig _notificationConfig = new NotificationConfig();\r
-            private DateTime _lastRing;\r
+            private DateTime _lastAlarm;\r
             private bool _suspend;\r
+            private string _suspendException;\r
 \r
-            public NotificationQueue(Action<string, string, string> ring, ITimer timer = null)\r
-            {\r
-                _ring = ring;\r
-                _timer = timer ?? new TimerWrapper();\r
-                _timer.Interval = 1000;\r
-                _timer.Tick += TimerOnTick;\r
-            }\r
-\r
-            private void TimerOnTick(object obj, EventArgs e)\r
+            public NotificationQueue(Action<string, string, string> alarm, Func<DateTime> nowFunc = null)\r
             {\r
-                if (_queue.Count == 0)\r
-                {\r
-                    _timer.Stop();\r
-                    return;\r
-                }\r
-                Ring();\r
+                _alarm = alarm;\r
+                if (nowFunc != null)\r
+                    _nowFunc = nowFunc;\r
             }\r
 \r
             public void Enqueue(Notification notification)\r
@@ -344,9 +312,7 @@ namespace KancolleSniffer
 \r
             public void Flash()\r
             {\r
-                Ring();\r
-                if (_queue.Count > 0)\r
-                    _timer.Start();\r
+                Alarm();\r
             }\r
 \r
             public void StopRepeat(string key, bool cont = false)\r
@@ -370,12 +336,23 @@ namespace KancolleSniffer
                 _queue.RemoveAll(n => IsMatch(n, key) && n.Fleet == fleet);\r
             }\r
 \r
+            public void StopRepeat(string key, string subject)\r
+            {\r
+                _queue.RemoveAll(n => IsMatch(n, key) && n.Subject == subject);\r
+            }\r
+\r
             private bool IsMatch(Notification n, string key) =>\r
                 n.Key.Substring(0, 4) == key.Substring(0, 4) && n.Schedule != default;\r
 \r
-            public void SuspendRepeat()\r
+            public void StopAllRepeat()\r
+            {\r
+                _queue.RemoveAll(n => n.Schedule != default);\r
+            }\r
+\r
+            public void SuspendRepeat(string exception = null)\r
             {\r
                 _suspend = true;\r
+                _suspendException = exception;\r
             }\r
 \r
             public void ResumeRepeat()\r
@@ -383,13 +360,13 @@ namespace KancolleSniffer
                 _suspend = false;\r
             }\r
 \r
-            private void Ring()\r
+            private void Alarm()\r
             {\r
-                var now = _timer.Now;\r
-                if (now - _lastRing < TimeSpan.FromSeconds(2))\r
+                var now = _nowFunc();\r
+                if (now - _lastAlarm < TimeSpan.FromSeconds(2))\r
                     return;\r
                 var first = _queue.FirstOrDefault(n => n.Schedule.CompareTo(now) <= 0 &&\r
-                                                       !(_suspend && n.Schedule != default));\r
+                                                       !(n.Schedule != default && _suspend && n.Key != _suspendException));\r
                 if (first == null)\r
                     return;\r
                 var message = _notificationConfig.GenerateMessage(first);\r
@@ -405,13 +382,13 @@ namespace KancolleSniffer
                     }\r
                     else\r
                     {\r
-                        n.Schedule = _timer.Now + TimeSpan.FromSeconds(n.Repeat);\r
+                        n.Schedule = now + TimeSpan.FromSeconds(n.Repeat);\r
                         if (n.Mode == Mode.Normal)\r
                             n.Mode = Mode.Repeat;\r
                     }\r
                 }\r
-                _ring(message.Title, body, message.Name);\r
-                _lastRing = now;\r
+                _alarm(message.Title, body, message.Name);\r
+                _lastAlarm = now;\r
             }\r
         }\r
     }\r