1 // Copyright (C) 2013, 2014, 2015 Kazuhiro Fujieda <fujieda@users.sourceforge.jp>
\r
3 // This program is part of KancolleSniffer.
\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
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
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
20 namespace KancolleSniffer
\r
22 public class Sniffer
\r
24 private bool _start;
\r
25 private readonly ShipMaster _shipMaster = new ShipMaster();
\r
26 private readonly ItemInfo _itemInfo = new ItemInfo();
\r
27 private readonly QuestInfo _questInfo = new QuestInfo();
\r
28 private readonly MissionInfo _missionInfo = new MissionInfo();
\r
29 private readonly ShipInfo _shipInfo;
\r
30 private readonly ConditionTimer _conditionTimer;
\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 Logger _logger;
\r
36 private readonly Status _status = new Status();
\r
55 _shipInfo = new ShipInfo(_shipMaster, _itemInfo);
\r
56 _conditionTimer = new ConditionTimer(_shipInfo);
\r
57 _dockInfo = new DockInfo(_shipInfo, _itemInfo);
\r
58 _akashiTimer = new AkashiTimer(_shipInfo, _itemInfo, _dockInfo);
\r
59 _battleInfo = new BattleInfo(_shipMaster, _shipInfo, _itemInfo);
\r
60 _logger = new Logger(_shipMaster, _shipInfo, _itemInfo);
\r
63 private void SaveState()
\r
65 if (!_achievement.NeedSave && !_itemInfo.NeedSave && !_conditionTimer.NeedSave)
\r
67 _achievement.SaveState(_status);
\r
68 _itemInfo.SaveState(_status);
\r
69 _conditionTimer.SaveState(_status);
\r
73 public void LoadState()
\r
76 _achievement.LoadState(_status);
\r
77 _itemInfo.LoadSate(_status);
\r
78 _conditionTimer.LoadState(_status);
\r
81 public Update Sniff(string url, string request, dynamic json)
\r
83 var data = json.IsDefined("api_data") ? json.api_data : new object();
\r
85 if (url.EndsWith("api_start2"))
\r
88 _shipMaster.Inspect(data);
\r
89 _missionInfo.InspectMaster(data.api_mst_mission);
\r
90 _itemInfo.InspectMaster(data);
\r
91 return Update.Start;
\r
95 if (url.EndsWith("api_port/port"))
\r
97 _itemInfo.InspectBasic(data.api_basic);
\r
98 _itemInfo.InspectMaterial(data.api_material);
\r
99 _logger.InspectBasic(data.api_basic);
\r
100 _logger.InspectMaterial(data.api_material);
\r
101 _shipInfo.InspectShip(data);
\r
102 _conditionTimer.CalcRegenTime();
\r
103 _missionInfo.InspectDeck(data.api_deck_port);
\r
104 _dockInfo.InspectNDock(data.api_ndock);
\r
105 _akashiTimer.SetTimer(true);
\r
106 _achievement.InspectBasic(data.api_basic);
\r
107 _battleInfo.InBattle = false;
\r
108 _battleInfo.HasDamagedShip = false;
\r
109 _shipInfo.ClearEscapedShips();
\r
113 if (url.EndsWith("api_get_member/basic"))
\r
115 _itemInfo.InspectBasic(data);
\r
116 _logger.InspectBasic(data);
\r
117 return Update.None;
\r
119 if (url.EndsWith("api_get_member/slot_item"))
\r
121 _itemInfo.InspectSlotItem(data, true);
\r
122 return Update.None;
\r
124 if (url.EndsWith("api_get_member/kdock"))
\r
126 _dockInfo.InspectKDock(data);
\r
127 _logger.InspectKDock(data);
\r
128 return Update.Timer;
\r
130 if (url.EndsWith("api_get_member/ndock"))
\r
132 _dockInfo.InspectNDock(data);
\r
133 _conditionTimer.CheckCond();
\r
134 _akashiTimer.SetTimer();
\r
135 return Update.NDock | Update.Timer | Update.Ship;
\r
137 if (url.EndsWith("api_req_hensei/change"))
\r
139 _shipInfo.InspectChange(request);
\r
140 _akashiTimer.SetTimer();
\r
141 return Update.Ship;
\r
143 if (url.EndsWith("api_get_member/questlist"))
\r
145 _questInfo.Inspect(data);
\r
146 return Update.QuestList;
\r
148 if (url.EndsWith("api_get_member/deck"))
\r
150 _shipInfo.InspectDeck(data);
\r
151 _missionInfo.InspectDeck(data);
\r
152 _akashiTimer.SetTimer();
\r
153 return Update.Mission | Update.Timer;
\r
155 if (url.EndsWith("api_get_member/ship2"))
\r
158 _shipInfo.InspectShip(json);
\r
159 _akashiTimer.SetTimer();
\r
160 _battleInfo.InBattle = false;
\r
161 return Update.Item | Update.Ship | Update.Battle;
\r
163 if (url.EndsWith("api_get_member/ship3"))
\r
165 _shipInfo.InspectShip(data);
\r
166 _akashiTimer.SetTimer();
\r
167 _conditionTimer.CheckCond();
\r
168 return Update.Ship;
\r
170 if (url.EndsWith("api_get_member/material"))
\r
172 _itemInfo.InspectMaterial(data);
\r
173 return Update.Item;
\r
175 if (url.EndsWith("api_req_hokyu/charge"))
\r
177 _shipInfo.InspectCharge(data);
\r
178 return Update.Item | Update.Ship;
\r
180 if (url.EndsWith("api_req_kousyou/createitem"))
\r
182 _itemInfo.InspectCreateItem(data);
\r
183 _logger.InspectCreateItem(request, data);
\r
184 return Update.Item;
\r
186 if (url.EndsWith("api_req_kousyou/getship"))
\r
188 _itemInfo.InspectGetShip(data);
\r
189 _shipInfo.InspectShip(data);
\r
190 _dockInfo.InspectKDock(data.api_kdock);
\r
191 _conditionTimer.CheckCond();
\r
192 return Update.Item | Update.Timer;
\r
194 if (url.EndsWith("api_req_kousyou/destroyship"))
\r
196 _shipInfo.InspectDestroyShip(request, data);
\r
197 _conditionTimer.CheckCond();
\r
198 _akashiTimer.SetTimer();
\r
199 return Update.Item | Update.Ship;
\r
201 if (url.EndsWith("api_req_kousyou/destroyitem2"))
\r
203 _itemInfo.InspectDestroyItem(request, data);
\r
204 return Update.Item;
\r
206 if (url.EndsWith("api_req_kousyou/remodel_slot"))
\r
208 _itemInfo.InspectRemodelSlot(data);
\r
209 return Update.Item;
\r
211 if (url.EndsWith("api_req_kousyou/createship"))
\r
213 _logger.InspectCreateShip(request);
\r
214 return Update.None;
\r
216 if (url.EndsWith("api_req_kaisou/powerup"))
\r
218 _shipInfo.InspectPowerup(request, data);
\r
219 _conditionTimer.CheckCond();
\r
220 _akashiTimer.SetTimer();
\r
221 return Update.Item | Update.Ship;
\r
223 if (url.EndsWith("api_req_nyukyo/start"))
\r
225 _dockInfo.InspectNyukyo(request);
\r
226 _conditionTimer.CheckCond();
\r
227 _akashiTimer.SetTimer();
\r
228 return Update.Item | Update.Ship;
\r
230 if (url.EndsWith("api_req_nyukyo/speedchange"))
\r
232 _dockInfo.InspectSpeedChange(request);
\r
233 _conditionTimer.CheckCond();
\r
234 return Update.NDock | Update.Timer | Update.Ship;
\r
236 if (IsNormalBattleAPI(url))
\r
238 _battleInfo.InspectBattle(data);
\r
239 _logger.InspectBattle(data);
\r
240 return Update.Ship | Update.Battle;
\r
242 if (url.EndsWith("api_req_practice/battle") || url.EndsWith("api_req_practice/midnight_battle"))
\r
244 if (url.EndsWith("/battle"))
\r
246 _shipInfo.StartSortie(request); // 演習を出撃中とみなす
\r
247 _conditionTimer.InvalidateCond();
\r
249 _battleInfo.InspectPracticeBattle(data);
\r
250 return Update.Ship | Update.Battle | Update.Timer;
\r
252 if (url.EndsWith("api_req_sortie/battleresult"))
\r
254 _battleInfo.CauseDamage();
\r
255 _logger.InspectBattleResult(data);
\r
256 return Update.Ship;
\r
258 if (url.EndsWith("api_req_practice/battle_result"))
\r
260 _battleInfo.CausePracticeDamage();
\r
261 return Update.Ship;
\r
263 if (IsCombinedBattleAPI(url))
\r
265 _battleInfo.InspectCombinedBattle(data, url.EndsWith("battle_water"));
\r
266 _logger.InspectBattle(data);
\r
267 return Update.Ship | Update.Battle;
\r
269 if (url.EndsWith("api_req_combined_battle/battleresult"))
\r
271 _battleInfo.InspectCombinedBattleResult(data);
\r
272 _logger.InspectBattleResult(data);
\r
273 return Update.Ship;
\r
275 if (url.EndsWith("api_req_combined_battle/goback_port"))
\r
277 _battleInfo.CauseCombinedBattleEscape();
\r
278 return Update.Ship;
\r
280 if (url.EndsWith("api_req_map/start"))
\r
282 _shipInfo.StartSortie(request);
\r
283 _conditionTimer.InvalidateCond();
\r
284 _logger.InspectMap(data);
\r
285 return Update.Timer;
\r
287 if (url.EndsWith("api_req_map/next"))
\r
289 _logger.InspectMap(data);
\r
290 return Update.None;
\r
292 if (url.EndsWith("api_req_mission/result"))
\r
294 _itemInfo.InspectMissionResult(data);
\r
295 _logger.InspectMissionResult(data);
\r
296 return Update.Item;
\r
298 return Update.None;
\r
301 private bool IsNormalBattleAPI(string url)
\r
303 return url.EndsWith("api_req_sortie/battle") ||
\r
304 url.EndsWith("api_req_battle_midnight/battle") ||
\r
305 url.EndsWith("api_req_battle_midnight/sp_midnight");
\r
308 private bool IsCombinedBattleAPI(string url)
\r
310 return url.EndsWith("api_req_combined_battle/battle") ||
\r
311 url.EndsWith("api_req_combined_battle/airbattle") ||
\r
312 url.EndsWith("api_req_combined_battle/battle_water") ||
\r
313 url.EndsWith("api_req_combined_battle/midnight_battle") ||
\r
314 url.EndsWith("api_req_combined_battle/sp_midnight");
\r
317 public NameAndTimer[] NDock
\r
319 get { return _dockInfo.NDock; }
\r
322 public RingTimer[] KDock
\r
324 get { return _dockInfo.KDock; }
\r
327 public ItemInfo Item
\r
329 get { return _itemInfo; }
\r
332 public QuestInfo.NameAndProgress[] Quests
\r
334 get { return _questInfo.Quests; }
\r
337 public NameAndTimer[] Missions
\r
339 get { return _missionInfo.Missions; }
\r
342 public DateTime GetConditionTimer(int fleet)
\r
344 return _conditionTimer.GetTimer(fleet);
\r
347 public int[] GetConditionNotice()
\r
349 return _conditionTimer.GetNotice();
\r
352 public ShipStatus[] GetShipStatuses(int fleet)
\r
354 return _shipInfo.GetShipStatuses(fleet);
\r
357 public int[] GetDeck(int fleet)
\r
359 return _shipInfo.GetDeck(fleet);
\r
362 public ChargeStatus[] ChargeStatuses
\r
364 get { return _shipInfo.ChargeStatuses; }
\r
367 public int GetAirSuperiority(int fleet)
\r
369 return _shipInfo.GetAirSuperiority(fleet);
\r
372 public double GetFleetLineOfSights(int fleet)
\r
374 return _shipInfo.GetLineOfSights(fleet);
\r
377 public ShipStatus[] DamagedShipList
\r
379 get { return _shipInfo.GetDamagedShipList(_dockInfo); }
\r
382 public ShipStatus[] ShipList
\r
384 get { return _shipInfo.ShipList; }
\r
387 public ItemStatus[] ItemList
\r
389 get { return _itemInfo.GetItemList(_shipInfo); }
\r
392 public AkashiTimer.RepairSpan[] GetAkashiTimers(int fleet)
\r
394 return _akashiTimer.GetTimers(fleet);
\r
397 public string[] GetAkashiTimerNotice()
\r
399 return _akashiTimer.GetNotice();
\r
402 public Achievement Achievement
\r
404 get { return _achievement; }
\r
407 public BattleInfo Battle
\r
409 get { return _battleInfo; }
\r
412 public void SetLogWriter(Action<string, string, string> writer, Func<DateTime> nowFunc)
\r
414 _logger.SetWriter(writer, nowFunc);
\r
417 public void SkipMaster()
\r
422 public void EnableLog(LogType type)
\r
424 _logger.EnableLog(type);
\r
427 public int MaterialLogInterval
\r
429 set { _logger.MaterialLogInterval = value; }
\r
432 public string LogOutputDir
\r
434 set { _logger.OutputDir = value; }
\r
438 public class NameAndTimer
\r
440 public string Name { get; set; }
\r
441 public RingTimer Timer { get; set; }
\r
443 public NameAndTimer()
\r
445 Timer = new RingTimer();
\r
449 public class RingTimer
\r
451 private DateTime _endTime;
\r
452 private readonly TimeSpan _spare;
\r
454 public TimeSpan Rest { get; private set; }
\r
456 public bool IsFinished
\r
458 get { return _endTime != DateTime.MinValue && Rest <= _spare; }
\r
461 public bool NeedRing { get; set; }
\r
463 public RingTimer(int spare = 60)
\r
465 _spare = TimeSpan.FromSeconds(spare);
\r
468 public void SetEndTime(double time)
\r
470 SetEndTime((int)time == 0
\r
471 ? DateTime.MinValue
\r
472 : new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time / 1000));
\r
475 public void SetEndTime(DateTime time)
\r
480 public void Update()
\r
482 if (_endTime == DateTime.MinValue)
\r
484 Rest = TimeSpan.Zero;
\r
488 Rest = _endTime - DateTime.Now;
\r
489 if (Rest < TimeSpan.Zero)
\r
490 Rest = TimeSpan.Zero;
\r
491 if (prev > _spare && _spare >= Rest)
\r