OSDN Git Service

タイマーの時刻を保持するTimeStepクラスを導入する
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Notification / Formatter.cs
1 // Copyright (C) 2020 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 //\r
3 // Licensed under the Apache License, Version 2.0 (the "License");\r
4 // you may not use this file except in compliance with the License.\r
5 // You may obtain a copy of the License at\r
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\r
9 // Unless required by applicable law or agreed to in writing, software\r
10 // distributed under the License is distributed on an "AS IS" BASIS,\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 // See the License for the specific language governing permissions and\r
13 // limitations under the License.\r
14 \r
15 using System;\r
16 using System.Collections.Generic;\r
17 using System.IO;\r
18 using DynaJson;\r
19 \r
20 namespace KancolleSniffer.Notification\r
21 {\r
22     public class Formatter\r
23     {\r
24         public class Message\r
25         {\r
26             public string Title { get; set; }\r
27             public string Body { get; set; }\r
28             public string Name { get; set; }\r
29         }\r
30 \r
31         private readonly Dictionary<string, Message> _config = new Dictionary<string, Message>();\r
32 \r
33         private readonly Dictionary<string, Message> _default = new Dictionary<string, Message>\r
34         {\r
35             {\r
36                 "遠征終了", new Message\r
37                 {\r
38                     Title = "遠征が終わりました",\r
39                     Body = "%f艦隊 %s"\r
40                 }\r
41             },\r
42             {\r
43                 "入渠終了", new Message\r
44                 {\r
45                     Title = "入渠が終わりました",\r
46                     Body = "%fドック %s"\r
47                 }\r
48             },\r
49             {\r
50                 "建造完了", new Message\r
51                 {\r
52                     Title = "建造が終わりました",\r
53                     Body = "%fドック"\r
54                 }\r
55             },\r
56             {\r
57                 "艦娘数超過", new Message\r
58                 {\r
59                     Title = "艦娘が多すぎます",\r
60                     Body = "%s"\r
61                 }\r
62             },\r
63             {\r
64                 "装備数超過", new Message\r
65                 {\r
66                     Title = "装備が多すぎます",\r
67                     Body = "%s"\r
68                 }\r
69             },\r
70             {\r
71                 "大破警告", new Message\r
72                 {\r
73                     Title = "大破した艦娘がいます",\r
74                     Body = "%s"\r
75                 }\r
76             },\r
77             {\r
78                 "泊地修理20分経過", new Message\r
79                 {\r
80                     Title = "泊地修理 %f艦隊",\r
81                     Body = "20分経過しました。"\r
82                 }\r
83             },\r
84             {\r
85                 "泊地修理進行", new Message\r
86                 {\r
87                     Title = "泊地修理 %f艦隊",\r
88                     Body = "修理進行:%s"\r
89                 }\r
90             },\r
91             {\r
92                 "泊地修理完了", new Message\r
93                 {\r
94                     Title = "泊地修理 %f艦隊",\r
95                     Body = "修理完了:%s"\r
96                 }\r
97             },\r
98             {\r
99                 "疲労回復40", new Message\r
100                 {\r
101                     Title = "疲労が回復しました",\r
102                     Body = "%f艦隊 残り9分"\r
103                 }\r
104             },\r
105             {\r
106                 "疲労回復49", new Message\r
107                 {\r
108                     Title = "疲労が回復しました",\r
109                     Body = "%f艦隊"\r
110                 }\r
111             },\r
112             {\r
113                 "任務達成", new Message\r
114                 {\r
115                     Title = "任務を達成しました",\r
116                     Body = "%s"\r
117                 }\r
118             }\r
119         };\r
120 \r
121         public static string KeyToName(string key) => key.StartsWith("疲労回復") ? key.Substring(0, 4) : key;\r
122 \r
123         private void LoadConfig()\r
124         {\r
125             const string fileName = "notification.json";\r
126             _config.Clear();\r
127             try\r
128             {\r
129                 dynamic config = JsonObject.Parse(File.ReadAllText(fileName));\r
130                 foreach (var entry in config)\r
131                 {\r
132                     if (!_default.ContainsKey(entry.key))\r
133                         continue;\r
134                     _config[entry.key] = new Message\r
135                     {\r
136                         Title = entry.title,\r
137                         Body = entry.body\r
138                     };\r
139                 }\r
140             }\r
141             catch (FileNotFoundException)\r
142             {\r
143             }\r
144             catch (Exception ex)\r
145             {\r
146                 throw new Exception($"{fileName}に誤りがあります。: ${ex.Message}", ex);\r
147             }\r
148         }\r
149 \r
150         public Message GenerateMessage(Scheduler.Notification notification)\r
151         {\r
152             LoadConfig();\r
153             var format = _config.TryGetValue(notification.Key, out Message value)\r
154                 ? value\r
155                 : _default[notification.Key];\r
156             var prefix = new[] {"", "[リピート] ", "[継続] ", "[予告] "}[(int)notification.Mode];\r
157             return new Message\r
158             {\r
159                 Title = prefix + ProcessFormatString(format.Title, notification.Fleet, notification.Subject),\r
160                 Body = ProcessFormatString(format.Body, notification.Fleet, notification.Subject),\r
161                 Name = KeyToName(notification.Key)\r
162             };\r
163         }\r
164 \r
165         private string ProcessFormatString(string format, int fleet, string subject)\r
166         {\r
167             var fn = new[] {"第一", "第二", "第三", "第四"};\r
168             var result = "";\r
169             var percent = false;\r
170             foreach (var ch in format)\r
171             {\r
172                 if (ch == '%')\r
173                 {\r
174                     if (percent)\r
175                     {\r
176                         percent = false;\r
177                         result += '%';\r
178                     }\r
179                     else\r
180                     {\r
181                         percent = true;\r
182                     }\r
183                 }\r
184                 else if (percent)\r
185                 {\r
186                     percent = false;\r
187                     switch (ch)\r
188                     {\r
189                         case 'f':\r
190                             result += fn[fleet];\r
191                             break;\r
192                         case 's':\r
193                             result += subject;\r
194                             break;\r
195                         default:\r
196                             result += '%'.ToString() + ch;\r
197                             break;\r
198                     }\r
199                 }\r
200                 else\r
201                 {\r
202                     result += ch;\r
203                 }\r
204             }\r
205             return result;\r
206         }\r
207     }\r
208 }