OSDN Git Service

艦種別に並べて艦娘一覧を表示する
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Sniffer.cs
1 // Copyright (C) 2013, 2014 Kazuhiro Fujieda <fujieda@users.sourceforge.jp>\r
2 // \r
3 // This program is part of KancolleSniffer.\r
4 //\r
5 // KancolleSniffer is free software: you can redistribute it and/or modify\r
6 // it under the terms of the GNU General Public License as published by\r
7 // the Free Software Foundation, either version 3 of the License, or\r
8 // (at your option) any later version.\r
9 //\r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 //\r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, see <http://www.gnu.org/licenses/>.\r
17 \r
18 using System;\r
19 using System.IO;\r
20 \r
21 namespace KancolleSniffer\r
22 {\r
23     public class Sniffer\r
24     {\r
25         private bool _start;\r
26         private readonly ShipMaster _shipMaster = new ShipMaster();\r
27         private readonly ItemInfo _itemInfo = new ItemInfo();\r
28         private readonly QuestInfo _questInfo = new QuestInfo();\r
29         private readonly MissionInfo _missionInfo = new MissionInfo();\r
30         private readonly ShipInfo _shipInfo;\r
31         private readonly DockInfo _dockInfo;\r
32         private readonly AkashiTimer _akashiTimer;\r
33         private readonly Achievement _achievement = new Achievement();\r
34         private readonly BattleInfo _battleInfo;\r
35         private readonly Status _status = new Status();\r
36 \r
37         [Flags]\r
38         public enum Update\r
39         {\r
40             None = 0,\r
41             Start = 1,\r
42             Item = 2,\r
43             Ship = 4,\r
44             Timer = 8,\r
45             NDock = 16,\r
46             Mission = 32,\r
47             QuestList = 64,\r
48             Battle = 128,\r
49             All = 255\r
50         }\r
51 \r
52         public Sniffer()\r
53         {\r
54             _shipInfo = new ShipInfo(_shipMaster, _itemInfo);\r
55             _dockInfo = new DockInfo(_shipInfo, _itemInfo);\r
56             _akashiTimer = new AkashiTimer(_shipInfo, _itemInfo, _dockInfo, _missionInfo);\r
57             _battleInfo = new BattleInfo(_shipMaster, _shipInfo, _itemInfo);\r
58         }\r
59 \r
60         public void SaveState()\r
61         {\r
62             _achievement.SaveState(_status);\r
63             _itemInfo.SaveState(_status);\r
64             _status.Save();\r
65         }\r
66 \r
67         public void LoadState()\r
68         {\r
69             _status.Load();\r
70             _achievement.LoadState(_status);\r
71             _itemInfo.LoadSate(_status);\r
72         }\r
73 \r
74         public Update Sniff(string url, string request, dynamic json)\r
75         {\r
76             var data = json.IsDefined("api_data") ? json.api_data : new object();\r
77 \r
78             if (LogFile != null)\r
79             {\r
80                 File.AppendAllText(LogFile,\r
81                     string.Format("url: {0}\nrequest: {1}\nresponse: {2}\n", url, request, json.ToString()));                \r
82             }\r
83 \r
84             if (url.EndsWith("api_start2"))\r
85             {\r
86                 _start = true;\r
87                 _shipMaster.Inspect(data.api_mst_ship);\r
88                 _shipMaster.InspectStype(data.api_mst_stype);\r
89                 _missionInfo.InspectMaster(data.api_mst_mission);\r
90                 _itemInfo.InspectMaster(data.api_mst_slotitem);\r
91                 return Update.Start;\r
92             }\r
93             if (!_start)\r
94                 return Update.None;\r
95             if (url.EndsWith("api_port/port"))\r
96             {\r
97                 _itemInfo.InspectBasic(data.api_basic);\r
98                 _itemInfo.InspectMaterial(data.api_material);\r
99                 _shipInfo.InspectShip(data);\r
100                 _missionInfo.InspectDeck(data.api_deck_port);\r
101                 _dockInfo.InspectNDock(data.api_ndock);\r
102                 _akashiTimer.SetTimer(true);\r
103                 _achievement.InspectBasic(data.api_basic);\r
104                 _battleInfo.InBattle = false;\r
105                 _battleInfo.HasDamagedShip = false;\r
106                 return Update.All;\r
107             }\r
108             if (url.EndsWith("api_get_member/basic"))\r
109             {\r
110                 _itemInfo.InspectBasic(data);\r
111                 return Update.None;\r
112             }\r
113             if (url.EndsWith("api_get_member/slot_item"))\r
114             {\r
115                 _itemInfo.InspectSlotItem(data, true);\r
116                 return Update.None;\r
117             }\r
118             if (url.EndsWith("api_get_member/kdock"))\r
119             {\r
120                 _dockInfo.InspectKDock(data);\r
121                 return Update.Timer;\r
122             }\r
123             if (url.EndsWith("api_get_member/ndock"))\r
124             {\r
125                 _dockInfo.InspectNDock(data);\r
126                 _akashiTimer.SetTimer();\r
127                 return Update.NDock | Update.Timer | Update.Ship;\r
128             }\r
129             if (url.EndsWith("api_req_hensei/change"))\r
130             {\r
131                 _shipInfo.InspectChange(request);\r
132                 _akashiTimer.SetTimer();\r
133                 return Update.Ship;\r
134             }\r
135             if (url.EndsWith("api_get_member/questlist"))\r
136             {\r
137                 _questInfo.Inspect(data);\r
138                 return Update.QuestList;\r
139             }\r
140             if (url.EndsWith("api_get_member/deck"))\r
141             {\r
142                 _missionInfo.InspectDeck(data);\r
143                 _akashiTimer.SetTimer();\r
144                 return Update.Mission | Update.Timer;\r
145             }\r
146             if (url.EndsWith("api_get_member/ship2"))\r
147             {\r
148                 // ここだけjsonなので注意\r
149                 _shipInfo.InspectShip(json);\r
150                 _akashiTimer.SetTimer();\r
151                 _battleInfo.InBattle = false;\r
152                 return Update.Item | Update.Ship | Update.Battle;\r
153             }\r
154             if (url.EndsWith("api_get_member/ship3"))\r
155             {\r
156                 _shipInfo.InspectShip(data);\r
157                 _akashiTimer.SetTimer();\r
158                 return Update.Ship;\r
159             }\r
160             if (url.EndsWith("api_get_member/material"))\r
161             {\r
162                 _itemInfo.InspectMaterial(data);\r
163                 return Update.Item;\r
164             }\r
165             if (url.EndsWith("api_req_hokyu/charge"))\r
166             {\r
167                 _shipInfo.InspectCharge(data);\r
168                 return Update.Item | Update.Ship;\r
169             }\r
170             if (url.EndsWith("api_req_kousyou/createitem"))\r
171             {\r
172                 _itemInfo.InspectCreateItem(data);\r
173                 return Update.Item;\r
174             }\r
175             if (url.EndsWith("api_req_kousyou/getship"))\r
176             {\r
177                 _itemInfo.InspectGetShip(data);\r
178                 _shipInfo.InspectShip(data);\r
179                 _dockInfo.InspectKDock(data.api_kdock);\r
180                 return Update.Item | Update.Timer;\r
181             }\r
182             if (url.EndsWith("api_req_kousyou/destroyship"))\r
183             {\r
184                 _shipInfo.InspectDestroyShip(request, data);\r
185                 _akashiTimer.SetTimer();\r
186                 return Update.Item | Update.Ship;\r
187             }\r
188             if (url.EndsWith("api_req_kousyou/destroyitem2"))\r
189             {\r
190                 _itemInfo.InspectDestroyItem(request, data);\r
191                 return Update.Item;\r
192             }\r
193             if (url.EndsWith("api_req_kaisou/powerup"))\r
194             {\r
195                 _shipInfo.InspectPowerup(request, data);\r
196                 return Update.Item | Update.Ship;\r
197             }\r
198             if (url.EndsWith("api_req_nyukyo/start"))\r
199             {\r
200                 _dockInfo.InspectNyukyo(request);\r
201                 _akashiTimer.SetTimer();\r
202                 return Update.Item | Update.Ship;\r
203             }\r
204             if (url.EndsWith("api_req_nyukyo/speedchange"))\r
205             {\r
206                 _dockInfo.InspectSpeedChange(request);\r
207                 return Update.NDock | Update.Timer | Update.Ship;\r
208             }\r
209             if (IsNormalBattleAPI(url))\r
210             {\r
211                 _battleInfo.InspectBattle(data);\r
212                 return Update.Battle;\r
213             }\r
214             if (url.EndsWith("api_req_sortie/battleresult") || url.EndsWith("api_req_practice/battleresult"))\r
215             {\r
216                 _battleInfo.CauseDamage();\r
217                 return Update.Ship;\r
218             }\r
219             if (url.EndsWith("api_req_mission/result"))\r
220             {\r
221                 _itemInfo.InspectMissionResult(data);\r
222                 return Update.Item;\r
223             }\r
224             if (IsCombinedBattleAPI(url))\r
225             {\r
226                 _battleInfo.InspectCombinedBattle(data);\r
227                 return Update.Battle;\r
228             }\r
229             if (url.EndsWith("api_req_combined_battle/battleresult"))\r
230             {\r
231                 _battleInfo.CauseDamageCombined();\r
232                 return Update.Ship;\r
233             }\r
234             return Update.None;\r
235         }\r
236 \r
237         public bool IsBattleAPI(string url)\r
238         {\r
239             return IsNormalBattleAPI(url) || IsCombinedBattleAPI(url);\r
240         }\r
241 \r
242         public bool IsNormalBattleAPI(string url)\r
243         {\r
244             return url.EndsWith("api_req_sortie/battle") || url.EndsWith("api_req_practice/battle") ||\r
245                    url.EndsWith("api_req_battle_midnight/sp_midnight") ||\r
246                    url.EndsWith("api_req_practice/midnight_battle");\r
247         }\r
248 \r
249         public bool IsCombinedBattleAPI(string url)\r
250         {\r
251             return url.EndsWith("api_req_combined_battle/battle") || url.EndsWith("api_req_combined_battle/airbattle") ||\r
252                    url.EndsWith("api_req_combined_battle/midnight_battle") ||\r
253                    url.EndsWith("api_req_combined_battle/sp_midnight");\r
254         }\r
255 \r
256         public NameAndTimer[] NDock\r
257         {\r
258             get { return _dockInfo.NDock; }\r
259         }\r
260 \r
261         public RingTimer[] KDock\r
262         {\r
263             get { return _dockInfo.KDock; }\r
264         }\r
265 \r
266         public ItemInfo Item\r
267         {\r
268             get { return _itemInfo; }\r
269         }\r
270 \r
271         public QuestInfo.NameAndProgress[] Quests\r
272         {\r
273             get { return _questInfo.Quests; }\r
274         }\r
275 \r
276         public NameAndTimer[] Missions\r
277         {\r
278             get { return _missionInfo.Missions; }\r
279         }\r
280 \r
281         public string[] GetConditionTimers(int fleet)\r
282         {\r
283             return _shipInfo.GetConditionTimers(fleet);\r
284         }\r
285 \r
286         public ShipStatus[] GetShipStatuses(int fleet)\r
287         {\r
288             return _shipInfo.GetShipStatuses(fleet);\r
289         }\r
290 \r
291         public ChargeStatus[] ChargeStatuses\r
292         {\r
293             get { return _shipInfo.ChargeStatuses; }\r
294         }\r
295 \r
296         public int GetAirSuperiority(int fleet)\r
297         {\r
298             return _shipInfo.GetAirSuperiority(fleet);\r
299         }\r
300 \r
301         public DamageStatus[] DamagedShipList\r
302         {\r
303             get { return _shipInfo.GetDamagedShipList(_dockInfo); }\r
304         }\r
305 \r
306         public ShipStatus[] ShipList\r
307         {\r
308             get { return _shipInfo.ShipList; }\r
309         }\r
310 \r
311         public ShipType[] ShipTypeList\r
312         {\r
313             get { return _shipMaster.ShipTypeList; }\r
314         }\r
315 \r
316         public AkashiTimer.RepairSpan[] GetAkashiTimers(int fleet)\r
317         {\r
318             return _akashiTimer.GetTimers(fleet);\r
319         }\r
320 \r
321         public string[] GetAkashiTimerNotice()\r
322         {\r
323             return _akashiTimer.GetNotice();\r
324         }\r
325 \r
326         public Achievement Achievement\r
327         {\r
328             get { return _achievement; }\r
329         }\r
330 \r
331         public BattleInfo Battle\r
332         {\r
333             get { return _battleInfo; }\r
334         }\r
335 \r
336         public string LogFile { get; set; }\r
337     }\r
338 \r
339     public class NameAndTimer\r
340     {\r
341         public string Name { get; set; }\r
342         public RingTimer Timer { get; set; }\r
343 \r
344         public NameAndTimer()\r
345         {\r
346             Timer = new RingTimer();\r
347         }\r
348     }\r
349 \r
350     public class RingTimer\r
351     {\r
352         private DateTime _endTime;\r
353         private TimeSpan _rest;\r
354         private readonly TimeSpan _spare;\r
355 \r
356         public RingTimer(int spare = 60)\r
357         {\r
358             _spare = TimeSpan.FromSeconds(spare);\r
359         }\r
360 \r
361         public void SetEndTime(double time)\r
362         {\r
363             SetEndTime((int)time == 0\r
364                 ? DateTime.MinValue\r
365                 : new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time / 1000));\r
366         }\r
367 \r
368         public void SetEndTime(DateTime time)\r
369         {\r
370             _endTime = time;\r
371             if (_endTime == DateTime.MinValue)\r
372                 IsFinished = false;\r
373         }\r
374 \r
375         public void Update()\r
376         {\r
377             if (_endTime == DateTime.MinValue)\r
378             {\r
379                 _rest = TimeSpan.Zero;\r
380                 return;\r
381             }\r
382             _rest = _endTime - DateTime.Now;\r
383             if (_rest < TimeSpan.Zero)\r
384                 _rest = TimeSpan.Zero;\r
385             if (_rest > _spare || IsFinished)\r
386                 return;\r
387             IsFinished = true;\r
388             NeedRing = true;\r
389         }\r
390 \r
391         public bool IsFinished { get; private set; }\r
392         public bool NeedRing { get; set; }\r
393 \r
394         public override string ToString()\r
395         {\r
396             return _rest.Days == 0 ? _rest.ToString(@"hh\:mm\:ss") : _rest.ToString(@"d\.hh\:mm");\r
397         }\r
398     }\r
399 }