1 // Copyright (C) 2013, 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>
\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
21 namespace KancolleSniffer
\r
23 public class Sniffer
\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 ConditionTimer _conditionTimer;
\r
32 private readonly DockInfo _dockInfo;
\r
33 private readonly AkashiTimer _akashiTimer;
\r
34 private readonly Achievement _achievement = new Achievement();
\r
35 private readonly BattleInfo _battleInfo;
\r
36 private readonly Logger _logger;
\r
37 private readonly Status _status = new Status();
\r
38 private bool _saveState;
\r
57 _shipInfo = new ShipInfo(_shipMaster, _itemInfo);
\r
58 _conditionTimer = new ConditionTimer(_shipInfo);
\r
59 _dockInfo = new DockInfo(_shipInfo, _itemInfo);
\r
60 _akashiTimer = new AkashiTimer(_shipInfo, _itemInfo, _dockInfo);
\r
61 _battleInfo = new BattleInfo(_shipMaster, _shipInfo, _itemInfo);
\r
62 _logger = new Logger(_shipMaster, _shipInfo, _itemInfo);
\r
65 private void SaveState()
\r
69 if (!_achievement.NeedSave && !_itemInfo.NeedSave && !_conditionTimer.NeedSave)
\r
71 _achievement.SaveState(_status);
\r
72 _itemInfo.SaveState(_status);
\r
73 _conditionTimer.SaveState(_status);
\r
77 public void LoadState()
\r
80 _achievement.LoadState(_status);
\r
81 _itemInfo.LoadSate(_status);
\r
82 _conditionTimer.LoadState(_status);
\r
86 public Update Sniff(string url, string request, dynamic json)
\r
88 var data = json.IsDefined("api_data") ? json.api_data : new object();
\r
90 if (url.EndsWith("api_start2"))
\r
93 _shipMaster.Inspect(data);
\r
94 _missionInfo.InspectMaster(data.api_mst_mission);
\r
95 _itemInfo.InspectMaster(data);
\r
96 return Update.Start;
\r
100 if (url.EndsWith("api_port/port"))
\r
102 _itemInfo.InspectBasic(data.api_basic);
\r
103 _itemInfo.InspectMaterial(data.api_material);
\r
104 _logger.InspectBasic(data.api_basic);
\r
105 _logger.InspectMaterial(data.api_material);
\r
106 _shipInfo.InspectShip(data);
\r
107 _conditionTimer.CalcRegenTime();
\r
108 _missionInfo.InspectDeck(data.api_deck_port);
\r
109 _dockInfo.InspectNDock(data.api_ndock);
\r
110 _akashiTimer.SetTimer(true);
\r
111 _achievement.InspectBasic(data.api_basic);
\r
112 if (data.api_parallel_quest_count()) // 昔のログにはないので
\r
113 _questInfo.QuestCount = (int)data.api_parallel_quest_count;
\r
114 _battleInfo.InBattle = false;
\r
115 _battleInfo.HasDamagedShip = false;
\r
116 _shipInfo.ClearEscapedShips();
\r
120 if (url.EndsWith("api_get_member/basic"))
\r
122 _itemInfo.InspectBasic(data);
\r
123 _logger.InspectBasic(data);
\r
124 return Update.None;
\r
126 if (url.EndsWith("api_get_member/slot_item"))
\r
128 _itemInfo.InspectSlotItem(data, true);
\r
129 return Update.None;
\r
131 if (url.EndsWith("api_get_member/kdock"))
\r
133 _dockInfo.InspectKDock(data);
\r
134 _logger.InspectKDock(data);
\r
135 return Update.Timer;
\r
137 if (url.EndsWith("api_get_member/ndock"))
\r
139 _dockInfo.InspectNDock(data);
\r
140 _conditionTimer.CheckCond();
\r
141 _akashiTimer.SetTimer();
\r
142 return Update.NDock | Update.Timer | Update.Ship;
\r
144 if (url.EndsWith("api_req_hensei/change"))
\r
146 _shipInfo.InspectChange(request);
\r
147 _akashiTimer.SetTimer();
\r
148 return Update.Ship;
\r
150 if (url.EndsWith("api_get_member/questlist"))
\r
152 _questInfo.Inspect(data);
\r
153 return Update.QuestList;
\r
155 if (url.EndsWith("api_get_member/deck"))
\r
157 _shipInfo.InspectDeck(data);
\r
158 _missionInfo.InspectDeck(data);
\r
159 _akashiTimer.SetTimer();
\r
160 return Update.Mission | Update.Timer;
\r
162 if (url.EndsWith("api_get_member/ship2"))
\r
165 _shipInfo.InspectShip(json);
\r
166 _akashiTimer.SetTimer();
\r
167 _battleInfo.InBattle = false;
\r
168 return Update.Item | Update.Ship | Update.Battle;
\r
170 if (url.EndsWith("api_get_member/ship3"))
\r
172 _shipInfo.InspectShip(data);
\r
173 _akashiTimer.SetTimer();
\r
174 _conditionTimer.CheckCond();
\r
175 return Update.Ship;
\r
177 if (url.EndsWith("api_get_member/material"))
\r
179 _itemInfo.InspectMaterial(data);
\r
180 return Update.Item;
\r
182 if (url.EndsWith("api_req_hokyu/charge"))
\r
184 _shipInfo.InspectCharge(data);
\r
185 return Update.Item | Update.Ship;
\r
187 if (url.EndsWith("api_req_kousyou/createitem"))
\r
189 _itemInfo.InspectCreateItem(data);
\r
190 _logger.InspectCreateItem(request, data);
\r
191 return Update.Item;
\r
193 if (url.EndsWith("api_req_kousyou/getship"))
\r
195 _itemInfo.InspectGetShip(data);
\r
196 _shipInfo.InspectShip(data);
\r
197 _dockInfo.InspectKDock(data.api_kdock);
\r
198 _conditionTimer.CheckCond();
\r
199 return Update.Item | Update.Timer;
\r
201 if (url.EndsWith("api_req_kousyou/destroyship"))
\r
203 _shipInfo.InspectDestroyShip(request, data);
\r
204 _conditionTimer.CheckCond();
\r
205 _akashiTimer.SetTimer();
\r
206 return Update.Item | Update.Ship;
\r
208 if (url.EndsWith("api_req_kousyou/destroyitem2"))
\r
210 _itemInfo.InspectDestroyItem(request, data);
\r
211 return Update.Item;
\r
213 if (url.EndsWith("api_req_kousyou/remodel_slot"))
\r
215 _itemInfo.InspectRemodelSlot(data);
\r
216 return Update.Item;
\r
218 if (url.EndsWith("api_req_kousyou/createship"))
\r
220 _logger.InspectCreateShip(request);
\r
221 return Update.None;
\r
223 if (url.EndsWith("api_req_kaisou/powerup"))
\r
225 _shipInfo.InspectPowerup(request, data);
\r
226 _conditionTimer.CheckCond();
\r
227 _akashiTimer.SetTimer();
\r
228 return Update.Item | Update.Ship;
\r
230 if (url.EndsWith("api_req_nyukyo/start"))
\r
232 _dockInfo.InspectNyukyo(request);
\r
233 _conditionTimer.CheckCond();
\r
234 _akashiTimer.SetTimer();
\r
235 return Update.Item | Update.Ship;
\r
237 if (url.EndsWith("api_req_nyukyo/speedchange"))
\r
239 _dockInfo.InspectSpeedChange(request);
\r
240 _conditionTimer.CheckCond();
\r
241 return Update.NDock | Update.Timer | Update.Ship;
\r
243 if (IsNormalBattleAPI(url))
\r
245 _battleInfo.InspectBattle(data);
\r
246 _logger.InspectBattle(data);
\r
247 return Update.Ship | Update.Battle;
\r
249 if (url.EndsWith("api_req_practice/battle") || url.EndsWith("api_req_practice/midnight_battle"))
\r
251 if (url.EndsWith("/battle"))
\r
253 _shipInfo.StartSortie(request); // 演習を出撃中とみなす
\r
254 _conditionTimer.InvalidateCond();
\r
256 _battleInfo.InspectBattle(data);
\r
257 return Update.Ship | Update.Battle | Update.Timer;
\r
259 if (url.EndsWith("api_req_sortie/battleresult"))
\r
261 _battleInfo.InspectBattleResult(json);
\r
262 _logger.InspectBattleResult(data);
\r
263 return Update.Ship;
\r
265 if (url.EndsWith("api_req_practice/battle_result"))
\r
267 _battleInfo.InspectPracticeResult(json);
\r
268 return Update.Ship;
\r
270 if (IsCombinedBattleAPI(url))
\r
272 _battleInfo.InspectCombinedBattle(data, url.EndsWith("battle_water"));
\r
273 _logger.InspectBattle(data);
\r
274 return Update.Ship | Update.Battle;
\r
276 if (url.EndsWith("api_req_combined_battle/battleresult"))
\r
278 _battleInfo.InspectCombinedBattleResult(data);
\r
279 _logger.InspectBattleResult(data);
\r
280 return Update.Ship;
\r
282 if (url.EndsWith("api_req_combined_battle/goback_port"))
\r
284 _battleInfo.CauseCombinedBattleEscape();
\r
285 return Update.Ship;
\r
287 if (url.EndsWith("api_req_map/start"))
\r
289 _shipInfo.StartSortie(request);
\r
290 _conditionTimer.InvalidateCond();
\r
291 _logger.InspectMap(data);
\r
292 return Update.Timer;
\r
294 if (url.EndsWith("api_req_map/next"))
\r
296 _logger.InspectMap(data);
\r
297 return Update.None;
\r
299 if (url.EndsWith("api_req_mission/result"))
\r
301 _itemInfo.InspectMissionResult(data);
\r
302 _logger.InspectMissionResult(data);
\r
303 return Update.Item;
\r
305 return Update.None;
\r
308 private bool IsNormalBattleAPI(string url)
\r
310 return url.EndsWith("api_req_sortie/battle") ||
\r
311 url.EndsWith("api_req_sortie/airbattle") ||
\r
312 url.EndsWith("api_req_battle_midnight/battle") ||
\r
313 url.EndsWith("api_req_battle_midnight/sp_midnight");
\r
316 private bool IsCombinedBattleAPI(string url)
\r
318 return url.EndsWith("api_req_combined_battle/battle") ||
\r
319 url.EndsWith("api_req_combined_battle/airbattle") ||
\r
320 url.EndsWith("api_req_combined_battle/battle_water") ||
\r
321 url.EndsWith("api_req_combined_battle/midnight_battle") ||
\r
322 url.EndsWith("api_req_combined_battle/sp_midnight");
\r
325 public NameAndTimer[] NDock
\r
327 get { return _dockInfo.NDock; }
\r
330 public RingTimer[] KDock
\r
332 get { return _dockInfo.KDock; }
\r
335 public ItemInfo Item
\r
337 get { return _itemInfo; }
\r
340 public QuestStatus[] Quests
\r
342 get { return _questInfo.Quests; }
\r
345 public NameAndTimer[] Missions
\r
347 get { return _missionInfo.Missions; }
\r
350 public DateTime GetConditionTimer(int fleet)
\r
352 return _conditionTimer.GetTimer(fleet);
\r
355 public int[] GetConditionNotice()
\r
357 return _conditionTimer.GetNotice();
\r
360 public ShipStatus[] GetShipStatuses(int fleet)
\r
362 return _shipInfo.GetShipStatuses(fleet);
\r
365 public int[] GetDeck(int fleet)
\r
367 return _shipInfo.GetDeck(fleet);
\r
370 public ChargeStatus[] ChargeStatuses
\r
372 get { return _shipInfo.ChargeStatuses; }
\r
375 public int GetFighterPower(int fleet)
\r
377 return _shipInfo.GetFighterPower(fleet);
\r
380 public double GetFleetLineOfSights(int fleet)
\r
382 return _shipInfo.GetLineOfSights(fleet);
\r
385 public ShipStatus[] DamagedShipList
\r
387 get { return _shipInfo.GetDamagedShipList(_dockInfo); }
\r
390 public ShipStatus[] ShipList
\r
392 get { return _shipInfo.ShipList; }
\r
395 public ItemStatus[] ItemList
\r
399 _itemInfo.SetItemOwner(ShipList);
\r
400 return (from e in _itemInfo.ItemDict where e.Key != -1 select e.Value).ToArray();
\r
404 public AkashiTimer.RepairSpan[] GetAkashiTimers(int fleet)
\r
406 return _akashiTimer.GetTimers(fleet);
\r
409 public string[] GetAkashiTimerNotice()
\r
411 return _akashiTimer.GetNotice();
\r
414 public Achievement Achievement
\r
416 get { return _achievement; }
\r
419 public BattleInfo Battle
\r
421 get { return _battleInfo; }
\r
424 public void SetLogWriter(Action<string, string, string> writer, Func<DateTime> nowFunc)
\r
426 _logger.SetWriter(writer, nowFunc);
\r
429 public void SkipMaster()
\r
434 public void EnableLog(LogType type)
\r
436 _logger.EnableLog(type);
\r
439 public int MaterialLogInterval
\r
441 set { _logger.MaterialLogInterval = value; }
\r
444 public string LogOutputDir
\r
446 set { _logger.OutputDir = value; }
\r
450 public class NameAndTimer
\r
452 public string Name { get; set; }
\r
453 public RingTimer Timer { get; set; }
\r
455 public NameAndTimer()
\r
457 Timer = new RingTimer();
\r
461 public class RingTimer
\r
463 private DateTime _endTime;
\r
464 private readonly TimeSpan _spare;
\r
466 public TimeSpan Rest { get; private set; }
\r
468 public bool IsFinished
\r
470 get { return _endTime != DateTime.MinValue && Rest <= _spare; }
\r
473 public bool NeedRing { get; set; }
\r
475 public RingTimer(int spare = 60)
\r
477 _spare = TimeSpan.FromSeconds(spare);
\r
480 public void SetEndTime(double time)
\r
482 SetEndTime((int)time == 0
\r
483 ? DateTime.MinValue
\r
484 : new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time / 1000));
\r
487 public void SetEndTime(DateTime time)
\r
492 public void Update()
\r
494 if (_endTime == DateTime.MinValue)
\r
496 Rest = TimeSpan.Zero;
\r
500 Rest = _endTime - DateTime.Now;
\r
501 if (Rest < TimeSpan.Zero)
\r
502 Rest = TimeSpan.Zero;
\r
503 if (prev > _spare && _spare >= Rest)
\r