1 // Copyright (C) 2020 Kazuhiro Fujieda <fujieda@users.osdn.me>
\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
7 // http://www.apache.org/licenses/LICENSE-2.0
\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
16 using System.Collections.Generic;
\r
20 namespace KancolleSniffer.Notification
\r
22 public class Formatter
\r
24 public class Message
\r
26 public string Title { get; set; }
\r
27 public string Body { get; set; }
\r
28 public string Name { get; set; }
\r
31 private readonly Dictionary<string, Message> _config = new Dictionary<string, Message>();
\r
33 private readonly Dictionary<string, Message> _default = new Dictionary<string, Message>
\r
38 Title = "遠征が終わりました",
\r
45 Title = "入渠が終わりました",
\r
52 Title = "建造が終わりました",
\r
57 "艦娘数超過", new Message
\r
64 "装備数超過", new Message
\r
73 Title = "大破した艦娘がいます",
\r
78 "泊地修理20分経過", new Message
\r
80 Title = "泊地修理 %f艦隊",
\r
85 "泊地修理進行", new Message
\r
87 Title = "泊地修理 %f艦隊",
\r
92 "泊地修理完了", new Message
\r
94 Title = "泊地修理 %f艦隊",
\r
99 "疲労回復40", new Message
\r
101 Title = "疲労が回復しました",
\r
106 "疲労回復49", new Message
\r
108 Title = "疲労が回復しました",
\r
113 "任務達成", new Message
\r
115 Title = "任務を達成しました",
\r
121 public static string KeyToName(string key) => key.StartsWith("疲労回復") ? key.Substring(0, 4) : key;
\r
123 private void LoadConfig()
\r
125 const string fileName = "notification.json";
\r
129 dynamic config = JsonObject.Parse(File.ReadAllText(fileName));
\r
130 foreach (var entry in config)
\r
132 if (!_default.ContainsKey(entry.key))
\r
134 _config[entry.key] = new Message
\r
136 Title = entry.title,
\r
141 catch (FileNotFoundException)
\r
144 catch (Exception ex)
\r
146 throw new Exception($"{fileName}に誤りがあります。: ${ex.Message}", ex);
\r
150 public Message GenerateMessage(Scheduler.Notification notification)
\r
153 var format = _config.TryGetValue(notification.Key, out Message value)
\r
155 : _default[notification.Key];
\r
156 var prefix = new[] {"", "[リピート] ", "[継続] ", "[予告] "}[(int)notification.Mode];
\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
165 private string ProcessFormatString(string format, int fleet, string subject)
\r
167 var fn = new[] {"第一", "第二", "第三", "第四"};
\r
169 var percent = false;
\r
170 foreach (var ch in format)
\r
190 result += fn[fleet];
\r
196 result += '%'.ToString() + ch;
\r