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);\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                 _shipInfo.InspectDeck(data);\r
143                 _missionInfo.InspectDeck(data);\r
144                 _akashiTimer.SetTimer();\r
145                 return Update.Mission | Update.Timer;\r
146             }\r
147             if (url.EndsWith("api_get_member/ship2"))\r
148             {\r
149                 // ここだけjsonなので注意\r
150                 _shipInfo.InspectShip(json);\r
151                 _akashiTimer.SetTimer();\r
152                 _battleInfo.InBattle = false;\r
153                 return Update.Item | Update.Ship | Update.Battle;\r
154             }\r
155             if (url.EndsWith("api_get_member/ship3"))\r
156             {\r
157                 _shipInfo.InspectShip(data);\r
158                 _akashiTimer.SetTimer();\r
159                 return Update.Ship;\r
160             }\r
161             if (url.EndsWith("api_get_member/material"))\r
162             {\r
163                 _itemInfo.InspectMaterial(data);\r
164                 return Update.Item;\r
165             }\r
166             if (url.EndsWith("api_req_hokyu/charge"))\r
167             {\r
168                 _shipInfo.InspectCharge(data);\r
169                 return Update.Item | Update.Ship;\r
170             }\r
171             if (url.EndsWith("api_req_kousyou/createitem"))\r
172             {\r
173                 _itemInfo.InspectCreateItem(data);\r
174                 return Update.Item;\r
175             }\r
176             if (url.EndsWith("api_req_kousyou/getship"))\r
177             {\r
178                 _itemInfo.InspectGetShip(data);\r
179                 _shipInfo.InspectShip(data);\r
180                 _dockInfo.InspectKDock(data.api_kdock);\r
181                 return Update.Item | Update.Timer;\r
182             }\r
183             if (url.EndsWith("api_req_kousyou/destroyship"))\r
184             {\r
185                 _shipInfo.InspectDestroyShip(request, data);\r
186                 _akashiTimer.SetTimer();\r
187                 return Update.Item | Update.Ship;\r
188             }\r
189             if (url.EndsWith("api_req_kousyou/destroyitem2"))\r
190             {\r
191                 _itemInfo.InspectDestroyItem(request, data);\r
192                 return Update.Item;\r
193             }\r
194             if (url.EndsWith("api_req_kaisou/powerup"))\r
195             {\r
196                 _shipInfo.InspectPowerup(request, data);\r
197                 return Update.Item | Update.Ship;\r
198             }\r
199             if (url.EndsWith("api_req_nyukyo/start"))\r
200             {\r
201                 _dockInfo.InspectNyukyo(request);\r
202                 _akashiTimer.SetTimer();\r
203                 return Update.Item | Update.Ship;\r
204             }\r
205             if (url.EndsWith("api_req_nyukyo/speedchange"))\r
206             {\r
207                 _dockInfo.InspectSpeedChange(request);\r
208                 return Update.NDock | Update.Timer | Update.Ship;\r
209             }\r
210             if (IsNormalBattleAPI(url))\r
211             {\r
212                 _battleInfo.InspectBattle(data);\r
213                 if (!url.EndsWith("api_req_practice/battle"))\r
214                     return Update.Battle;\r
215                 _shipInfo.StartSortie(request);\r
216                 return Update.Battle | Update.Timer;\r
217             }\r
218             if (url.EndsWith("api_req_sortie/battleresult") || url.EndsWith("api_req_practice/battleresult"))\r
219             {\r
220                 _battleInfo.CauseDamage();\r
221                 return Update.Ship;\r
222             }\r
223             if (IsCombinedBattleAPI(url))\r
224             {\r
225                 _battleInfo.InspectCombinedBattle(data);\r
226                 return Update.Battle;\r
227             }\r
228             if (url.EndsWith("api_req_combined_battle/battleresult"))\r
229             {\r
230                 _battleInfo.CauseDamageCombined();\r
231                 return Update.Ship;\r
232             }\r
233             if (url.EndsWith("api_req_map/start"))\r
234             {\r
235                 _shipInfo.StartSortie(request);\r
236                 return Update.Timer;\r
237             }\r
238             if (url.EndsWith("api_req_mission/result"))\r
239             {\r
240                 _itemInfo.InspectMissionResult(data);\r
241                 return Update.Item;\r
242             }\r
243             return Update.None;\r
244         }\r
245 \r
246         public bool IsBattleAPI(string url)\r
247         {\r
248             return IsNormalBattleAPI(url) || IsCombinedBattleAPI(url);\r
249         }\r
250 \r
251         public bool IsNormalBattleAPI(string url)\r
252         {\r
253             return url.EndsWith("api_req_sortie/battle") || url.EndsWith("api_req_practice/battle") ||\r
254                    url.EndsWith("api_req_battle_midnight/sp_midnight") ||\r
255                    url.EndsWith("api_req_practice/midnight_battle");\r
256         }\r
257 \r
258         public bool IsCombinedBattleAPI(string url)\r
259         {\r
260             return url.EndsWith("api_req_combined_battle/battle") || url.EndsWith("api_req_combined_battle/airbattle") ||\r
261                    url.EndsWith("api_req_combined_battle/midnight_battle") ||\r
262                    url.EndsWith("api_req_combined_battle/sp_midnight");\r
263         }\r
264 \r
265         public NameAndTimer[] NDock\r
266         {\r
267             get { return _dockInfo.NDock; }\r
268         }\r
269 \r
270         public RingTimer[] KDock\r
271         {\r
272             get { return _dockInfo.KDock; }\r
273         }\r
274 \r
275         public ItemInfo Item\r
276         {\r
277             get { return _itemInfo; }\r
278         }\r
279 \r
280         public QuestInfo.NameAndProgress[] Quests\r
281         {\r
282             get { return _questInfo.Quests; }\r
283         }\r
284 \r
285         public NameAndTimer[] Missions\r
286         {\r
287             get { return _missionInfo.Missions; }\r
288         }\r
289 \r
290         public DateTime GetConditionTimer(int fleet)\r
291         {\r
292             return _shipInfo.GetConditionTiemr(fleet);\r
293         }\r
294 \r
295         public int[] GetConditionNotice()\r
296         {\r
297             return _shipInfo.GetConditionNotice();\r
298         }\r
299 \r
300         public ShipStatus[] GetShipStatuses(int fleet)\r
301         {\r
302             return _shipInfo.GetShipStatuses(fleet);\r
303         }\r
304 \r
305         public ChargeStatus[] ChargeStatuses\r
306         {\r
307             get { return _shipInfo.ChargeStatuses; }\r
308         }\r
309 \r
310         public int GetAirSuperiority(int fleet)\r
311         {\r
312             return _shipInfo.GetAirSuperiority(fleet);\r
313         }\r
314 \r
315         public DamageStatus[] DamagedShipList\r
316         {\r
317             get { return _shipInfo.GetDamagedShipList(_dockInfo); }\r
318         }\r
319 \r
320         public ShipStatus[] ShipList\r
321         {\r
322             get { return _shipInfo.ShipList; }\r
323         }\r
324 \r
325         public ShipType[] ShipTypeList\r
326         {\r
327             get { return _shipMaster.ShipTypeList; }\r
328         }\r
329 \r
330         public AkashiTimer.RepairSpan[] GetAkashiTimers(int fleet)\r
331         {\r
332             return _akashiTimer.GetTimers(fleet);\r
333         }\r
334 \r
335         public string[] GetAkashiTimerNotice()\r
336         {\r
337             return _akashiTimer.GetNotice();\r
338         }\r
339 \r
340         public Achievement Achievement\r
341         {\r
342             get { return _achievement; }\r
343         }\r
344 \r
345         public BattleInfo Battle\r
346         {\r
347             get { return _battleInfo; }\r
348         }\r
349 \r
350         public string LogFile { get; set; }\r
351     }\r
352 \r
353     public class NameAndTimer\r
354     {\r
355         public string Name { get; set; }\r
356         public RingTimer Timer { get; set; }\r
357 \r
358         public NameAndTimer()\r
359         {\r
360             Timer = new RingTimer();\r
361         }\r
362     }\r
363 \r
364     public class RingTimer\r
365     {\r
366         private DateTime _endTime;\r
367         private TimeSpan _rest;\r
368         private readonly TimeSpan _spare;\r
369 \r
370         public RingTimer(int spare = 60)\r
371         {\r
372             _spare = TimeSpan.FromSeconds(spare);\r
373         }\r
374 \r
375         public void SetEndTime(double time)\r
376         {\r
377             SetEndTime((int)time == 0\r
378                 ? DateTime.MinValue\r
379                 : new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time / 1000));\r
380         }\r
381 \r
382         public void SetEndTime(DateTime time)\r
383         {\r
384             _endTime = time;\r
385             if (_endTime == DateTime.MinValue)\r
386                 IsFinished = false;\r
387         }\r
388 \r
389         public void Update()\r
390         {\r
391             if (_endTime == DateTime.MinValue)\r
392             {\r
393                 _rest = TimeSpan.Zero;\r
394                 return;\r
395             }\r
396             _rest = _endTime - DateTime.Now;\r
397             if (_rest < TimeSpan.Zero)\r
398                 _rest = TimeSpan.Zero;\r
399             if (_rest > _spare || IsFinished)\r
400                 return;\r
401             IsFinished = true;\r
402             NeedRing = true;\r
403         }\r
404 \r
405         public bool IsFinished { get; private set; }\r
406         public bool NeedRing { get; set; }\r
407 \r
408         public override string ToString()\r
409         {\r
410             return _rest.Days == 0 ? _rest.ToString(@"hh\:mm\:ss") : _rest.ToString(@"d\.hh\:mm");\r
411         }\r
412     }\r
413 }