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 if (data.api_parallel_quest_count()) // 昔のログにはないので
\r
108 _questInfo.QuestCount = (int)data.api_parallel_quest_count;
\r
109 _battleInfo.InBattle = false;
\r
110 _battleInfo.HasDamagedShip = false;
\r
111 _shipInfo.ClearEscapedShips();
\r
115 if (url.EndsWith("api_get_member/basic"))
\r
117 _itemInfo.InspectBasic(data);
\r
118 _logger.InspectBasic(data);
\r
119 return Update.None;
\r
121 if (url.EndsWith("api_get_member/slot_item"))
\r
123 _itemInfo.InspectSlotItem(data, true);
\r
124 return Update.None;
\r
126 if (url.EndsWith("api_get_member/kdock"))
\r
128 _dockInfo.InspectKDock(data);
\r
129 _logger.InspectKDock(data);
\r
130 return Update.Timer;
\r
132 if (url.EndsWith("api_get_member/ndock"))
\r
134 _dockInfo.InspectNDock(data);
\r
135 _conditionTimer.CheckCond();
\r
136 _akashiTimer.SetTimer();
\r
137 return Update.NDock | Update.Timer | Update.Ship;
\r
139 if (url.EndsWith("api_req_hensei/change"))
\r
141 _shipInfo.InspectChange(request);
\r
142 _akashiTimer.SetTimer();
\r
143 return Update.Ship;
\r
145 if (url.EndsWith("api_get_member/questlist"))
\r
147 _questInfo.Inspect(data);
\r
148 return Update.QuestList;
\r
150 if (url.EndsWith("api_get_member/deck"))
\r
152 _shipInfo.InspectDeck(data);
\r
153 _missionInfo.InspectDeck(data);
\r
154 _akashiTimer.SetTimer();
\r
155 return Update.Mission | Update.Timer;
\r
157 if (url.EndsWith("api_get_member/ship2"))
\r
160 _shipInfo.InspectShip(json);
\r
161 _akashiTimer.SetTimer();
\r
162 _battleInfo.InBattle = false;
\r
163 return Update.Item | Update.Ship | Update.Battle;
\r
165 if (url.EndsWith("api_get_member/ship3"))
\r
167 _shipInfo.InspectShip(data);
\r
168 _akashiTimer.SetTimer();
\r
169 _conditionTimer.CheckCond();
\r
170 return Update.Ship;
\r
172 if (url.EndsWith("api_get_member/material"))
\r
174 _itemInfo.InspectMaterial(data);
\r
175 return Update.Item;
\r
177 if (url.EndsWith("api_req_hokyu/charge"))
\r
179 _shipInfo.InspectCharge(data);
\r
180 return Update.Item | Update.Ship;
\r
182 if (url.EndsWith("api_req_kousyou/createitem"))
\r
184 _itemInfo.InspectCreateItem(data);
\r
185 _logger.InspectCreateItem(request, data);
\r
186 return Update.Item;
\r
188 if (url.EndsWith("api_req_kousyou/getship"))
\r
190 _itemInfo.InspectGetShip(data);
\r
191 _shipInfo.InspectShip(data);
\r
192 _dockInfo.InspectKDock(data.api_kdock);
\r
193 _conditionTimer.CheckCond();
\r
194 return Update.Item | Update.Timer;
\r
196 if (url.EndsWith("api_req_kousyou/destroyship"))
\r
198 _shipInfo.InspectDestroyShip(request, data);
\r
199 _conditionTimer.CheckCond();
\r
200 _akashiTimer.SetTimer();
\r
201 return Update.Item | Update.Ship;
\r
203 if (url.EndsWith("api_req_kousyou/destroyitem2"))
\r
205 _itemInfo.InspectDestroyItem(request, data);
\r
206 return Update.Item;
\r
208 if (url.EndsWith("api_req_kousyou/remodel_slot"))
\r
210 _itemInfo.InspectRemodelSlot(data);
\r
211 return Update.Item;
\r
213 if (url.EndsWith("api_req_kousyou/createship"))
\r
215 _logger.InspectCreateShip(request);
\r
216 return Update.None;
\r
218 if (url.EndsWith("api_req_kaisou/powerup"))
\r
220 _shipInfo.InspectPowerup(request, data);
\r
221 _conditionTimer.CheckCond();
\r
222 _akashiTimer.SetTimer();
\r
223 return Update.Item | Update.Ship;
\r
225 if (url.EndsWith("api_req_nyukyo/start"))
\r
227 _dockInfo.InspectNyukyo(request);
\r
228 _conditionTimer.CheckCond();
\r
229 _akashiTimer.SetTimer();
\r
230 return Update.Item | Update.Ship;
\r
232 if (url.EndsWith("api_req_nyukyo/speedchange"))
\r
234 _dockInfo.InspectSpeedChange(request);
\r
235 _conditionTimer.CheckCond();
\r
236 return Update.NDock | Update.Timer | Update.Ship;
\r
238 if (IsNormalBattleAPI(url))
\r
240 _battleInfo.InspectBattle(data);
\r
241 _logger.InspectBattle(data);
\r
242 return Update.Ship | Update.Battle;
\r
244 if (url.EndsWith("api_req_practice/battle") || url.EndsWith("api_req_practice/midnight_battle"))
\r
246 if (url.EndsWith("/battle"))
\r
248 _shipInfo.StartSortie(request); // 演習を出撃中とみなす
\r
249 _conditionTimer.InvalidateCond();
\r
251 _battleInfo.InspectPracticeBattle(data);
\r
252 return Update.Ship | Update.Battle | Update.Timer;
\r
254 if (url.EndsWith("api_req_sortie/battleresult"))
\r
256 _battleInfo.CauseDamage();
\r
257 _logger.InspectBattleResult(data);
\r
258 return Update.Ship;
\r
260 if (url.EndsWith("api_req_practice/battle_result"))
\r
262 _battleInfo.CausePracticeDamage();
\r
263 return Update.Ship;
\r
265 if (IsCombinedBattleAPI(url))
\r
267 _battleInfo.InspectCombinedBattle(data, url.EndsWith("battle_water"));
\r
268 _logger.InspectBattle(data);
\r
269 return Update.Ship | Update.Battle;
\r
271 if (url.EndsWith("api_req_combined_battle/battleresult"))
\r
273 _battleInfo.InspectCombinedBattleResult(data);
\r
274 _logger.InspectBattleResult(data);
\r
275 return Update.Ship;
\r
277 if (url.EndsWith("api_req_combined_battle/goback_port"))
\r
279 _battleInfo.CauseCombinedBattleEscape();
\r
280 return Update.Ship;
\r
282 if (url.EndsWith("api_req_map/start"))
\r
284 _shipInfo.StartSortie(request);
\r
285 _conditionTimer.InvalidateCond();
\r
286 _logger.InspectMap(data);
\r
287 return Update.Timer;
\r
289 if (url.EndsWith("api_req_map/next"))
\r
291 _logger.InspectMap(data);
\r
292 return Update.None;
\r
294 if (url.EndsWith("api_req_mission/result"))
\r
296 _itemInfo.InspectMissionResult(data);
\r
297 _logger.InspectMissionResult(data);
\r
298 return Update.Item;
\r
300 return Update.None;
\r
303 private bool IsNormalBattleAPI(string url)
\r
305 return url.EndsWith("api_req_sortie/battle") ||
\r
306 url.EndsWith("api_req_battle_midnight/battle") ||
\r
307 url.EndsWith("api_req_battle_midnight/sp_midnight");
\r
310 private bool IsCombinedBattleAPI(string url)
\r
312 return url.EndsWith("api_req_combined_battle/battle") ||
\r
313 url.EndsWith("api_req_combined_battle/airbattle") ||
\r
314 url.EndsWith("api_req_combined_battle/battle_water") ||
\r
315 url.EndsWith("api_req_combined_battle/midnight_battle") ||
\r
316 url.EndsWith("api_req_combined_battle/sp_midnight");
\r
319 public NameAndTimer[] NDock
\r
321 get { return _dockInfo.NDock; }
\r
324 public RingTimer[] KDock
\r
326 get { return _dockInfo.KDock; }
\r
329 public ItemInfo Item
\r
331 get { return _itemInfo; }
\r
334 public QuestInfo.NameAndProgress[] Quests
\r
336 get { return _questInfo.Quests; }
\r
339 public NameAndTimer[] Missions
\r
341 get { return _missionInfo.Missions; }
\r
344 public DateTime GetConditionTimer(int fleet)
\r
346 return _conditionTimer.GetTimer(fleet);
\r
349 public int[] GetConditionNotice()
\r
351 return _conditionTimer.GetNotice();
\r
354 public ShipStatus[] GetShipStatuses(int fleet)
\r
356 return _shipInfo.GetShipStatuses(fleet);
\r
359 public int[] GetDeck(int fleet)
\r
361 return _shipInfo.GetDeck(fleet);
\r
364 public ChargeStatus[] ChargeStatuses
\r
366 get { return _shipInfo.ChargeStatuses; }
\r
369 public int GetAirSuperiority(int fleet)
\r
371 return _shipInfo.GetAirSuperiority(fleet);
\r
374 public double GetFleetLineOfSights(int fleet)
\r
376 return _shipInfo.GetLineOfSights(fleet);
\r
379 public ShipStatus[] DamagedShipList
\r
381 get { return _shipInfo.GetDamagedShipList(_dockInfo); }
\r
384 public ShipStatus[] ShipList
\r
386 get { return _shipInfo.ShipList; }
\r
389 public ItemStatus[] ItemList
\r
391 get { return _itemInfo.GetItemList(_shipInfo); }
\r
394 public AkashiTimer.RepairSpan[] GetAkashiTimers(int fleet)
\r
396 return _akashiTimer.GetTimers(fleet);
\r
399 public string[] GetAkashiTimerNotice()
\r
401 return _akashiTimer.GetNotice();
\r
404 public Achievement Achievement
\r
406 get { return _achievement; }
\r
409 public BattleInfo Battle
\r
411 get { return _battleInfo; }
\r
414 public void SetLogWriter(Action<string, string, string> writer, Func<DateTime> nowFunc)
\r
416 _logger.SetWriter(writer, nowFunc);
\r
419 public void SkipMaster()
\r
424 public void EnableLog(LogType type)
\r
426 _logger.EnableLog(type);
\r
429 public int MaterialLogInterval
\r
431 set { _logger.MaterialLogInterval = value; }
\r
434 public string LogOutputDir
\r
436 set { _logger.OutputDir = value; }
\r
440 public class NameAndTimer
\r
442 public string Name { get; set; }
\r
443 public RingTimer Timer { get; set; }
\r
445 public NameAndTimer()
\r
447 Timer = new RingTimer();
\r
451 public class RingTimer
\r
453 private DateTime _endTime;
\r
454 private readonly TimeSpan _spare;
\r
456 public TimeSpan Rest { get; private set; }
\r
458 public bool IsFinished
\r
460 get { return _endTime != DateTime.MinValue && Rest <= _spare; }
\r
463 public bool NeedRing { get; set; }
\r
465 public RingTimer(int spare = 60)
\r
467 _spare = TimeSpan.FromSeconds(spare);
\r
470 public void SetEndTime(double time)
\r
472 SetEndTime((int)time == 0
\r
473 ? DateTime.MinValue
\r
474 : new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time / 1000));
\r
477 public void SetEndTime(DateTime time)
\r
482 public void Update()
\r
484 if (_endTime == DateTime.MinValue)
\r
486 Rest = TimeSpan.Zero;
\r
490 Rest = _endTime - DateTime.Now;
\r
491 if (Rest < TimeSpan.Zero)
\r
492 Rest = TimeSpan.Zero;
\r
493 if (prev > _spare && _spare >= Rest)
\r