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
19 using System.Collections.Generic;
\r
22 namespace KancolleSniffer
\r
24 public class Sniffer
\r
26 private bool _start;
\r
27 private readonly ShipMaster _shipMaster = new ShipMaster();
\r
28 private readonly ItemInfo _itemInfo = new ItemInfo();
\r
29 private readonly QuestInfo _questInfo = new QuestInfo();
\r
30 private readonly MissionInfo _missionInfo = new MissionInfo();
\r
31 private readonly ShipInfo _shipInfo;
\r
32 private readonly ConditionTimer _conditionTimer;
\r
33 private readonly DockInfo _dockInfo;
\r
34 private readonly AkashiTimer _akashiTimer;
\r
35 private readonly Achievement _achievement = new Achievement();
\r
36 private readonly BattleInfo _battleInfo;
\r
37 private readonly Logger _logger;
\r
38 private readonly ExMapInfo _exMapInfo = new ExMapInfo();
\r
39 private readonly Status _status = new Status();
\r
40 private bool _saveState;
\r
41 private readonly List<IHaveState> _haveState;
\r
60 _shipInfo = new ShipInfo(_shipMaster, _itemInfo);
\r
61 _conditionTimer = new ConditionTimer(_shipInfo);
\r
62 _dockInfo = new DockInfo(_shipInfo, _itemInfo);
\r
63 _akashiTimer = new AkashiTimer(_shipInfo, _itemInfo, _dockInfo);
\r
64 _battleInfo = new BattleInfo(_shipMaster, _shipInfo, _itemInfo);
\r
65 _logger = new Logger(_shipMaster, _shipInfo, _itemInfo);
\r
66 _haveState = new List<IHaveState> {_achievement, _itemInfo, _conditionTimer, _exMapInfo};
\r
69 private void SaveState()
\r
73 if (!_haveState.Any(x=>x.NeedSave))
\r
75 foreach (var x in _haveState)
\r
76 x.SaveState(_status);
\r
80 public void LoadState()
\r
83 foreach (var x in _haveState)
\r
84 x.LoadState(_status);
\r
88 public Update Sniff(string url, string request, dynamic json)
\r
90 var data = json.IsDefined("api_data") ? json.api_data : new object();
\r
92 if (url.EndsWith("api_start2"))
\r
95 _shipMaster.Inspect(data);
\r
96 _missionInfo.InspectMaster(data.api_mst_mission);
\r
97 _itemInfo.InspectMaster(data);
\r
98 _exMapInfo.ResetIfNeeded();
\r
99 return Update.Start;
\r
102 return Update.None;
\r
103 if (url.EndsWith("api_port/port"))
\r
105 _itemInfo.InspectBasic(data.api_basic);
\r
106 _itemInfo.InspectMaterial(data.api_material);
\r
107 _logger.InspectBasic(data.api_basic);
\r
108 _logger.InspectMaterial(data.api_material);
\r
109 _shipInfo.InspectShip(data);
\r
110 _conditionTimer.CalcRegenTime();
\r
111 _missionInfo.InspectDeck(data.api_deck_port);
\r
112 _dockInfo.InspectNDock(data.api_ndock);
\r
113 _akashiTimer.SetTimer(true);
\r
114 _achievement.InspectBasic(data.api_basic);
\r
115 if (data.api_parallel_quest_count()) // 昔のログにはないので
\r
116 _questInfo.QuestCount = (int)data.api_parallel_quest_count;
\r
117 _battleInfo.CleanupResult();
\r
118 _battleInfo.InBattle = false;
\r
119 _battleInfo.HasDamagedShip = false;
\r
120 _shipInfo.ClearEscapedShips();
\r
124 if (url.EndsWith("api_get_member/basic"))
\r
126 _itemInfo.InspectBasic(data);
\r
127 _logger.InspectBasic(data);
\r
128 return Update.None;
\r
130 if (url.EndsWith("api_get_member/slot_item"))
\r
132 _itemInfo.InspectSlotItem(data, true);
\r
133 return Update.None;
\r
135 if (url.EndsWith("api_get_member/kdock"))
\r
137 _dockInfo.InspectKDock(data);
\r
138 _logger.InspectKDock(data);
\r
139 return Update.Timer;
\r
141 if (url.EndsWith("api_get_member/ndock"))
\r
143 _dockInfo.InspectNDock(data);
\r
144 _conditionTimer.CheckCond();
\r
145 _akashiTimer.SetTimer();
\r
146 return Update.NDock | Update.Timer | Update.Ship;
\r
148 if (url.EndsWith("api_req_hensei/change"))
\r
150 _shipInfo.InspectChange(request);
\r
151 _akashiTimer.SetTimer();
\r
152 return Update.Ship;
\r
154 if (url.EndsWith("api_get_member/questlist"))
\r
156 _questInfo.Inspect(data);
\r
157 return Update.QuestList;
\r
159 if (url.EndsWith("api_get_member/deck"))
\r
161 _shipInfo.InspectDeck(data);
\r
162 _missionInfo.InspectDeck(data);
\r
163 _akashiTimer.SetTimer();
\r
164 return Update.Mission | Update.Timer;
\r
166 if (url.EndsWith("api_get_member/ship2"))
\r
169 _shipInfo.InspectShip(json);
\r
170 _akashiTimer.SetTimer();
\r
171 _battleInfo.InBattle = false;
\r
172 return Update.Item | Update.Ship | Update.Battle;
\r
174 if (url.EndsWith("api_get_member/ship_deck"))
\r
176 _shipInfo.InspectShip(data);
\r
177 _akashiTimer.SetTimer();
\r
178 _battleInfo.InBattle = false;
\r
179 return Update.Ship | Update.Battle;
\r
181 if (url.EndsWith("api_get_member/ship3"))
\r
183 _shipInfo.InspectShip(data);
\r
184 _akashiTimer.SetTimer();
\r
185 _conditionTimer.CheckCond();
\r
186 return Update.Ship;
\r
188 if (url.EndsWith("api_get_member/material"))
\r
190 _itemInfo.InspectMaterial(data);
\r
191 return Update.Item;
\r
193 if (url.EndsWith("api_req_hokyu/charge"))
\r
195 _shipInfo.InspectCharge(data);
\r
196 return Update.Item | Update.Ship;
\r
198 if (url.EndsWith("api_req_kousyou/createitem"))
\r
200 _itemInfo.InspectCreateItem(data);
\r
201 _logger.InspectCreateItem(request, data);
\r
202 return Update.Item;
\r
204 if (url.EndsWith("api_req_kousyou/getship"))
\r
206 _itemInfo.InspectGetShip(data);
\r
207 _shipInfo.InspectShip(data);
\r
208 _dockInfo.InspectKDock(data.api_kdock);
\r
209 _conditionTimer.CheckCond();
\r
210 return Update.Item | Update.Timer;
\r
212 if (url.EndsWith("api_req_kousyou/destroyship"))
\r
214 _shipInfo.InspectDestroyShip(request, data);
\r
215 _conditionTimer.CheckCond();
\r
216 _akashiTimer.SetTimer();
\r
217 return Update.Item | Update.Ship;
\r
219 if (url.EndsWith("api_req_kousyou/destroyitem2"))
\r
221 _itemInfo.InspectDestroyItem(request, data);
\r
222 return Update.Item;
\r
224 if (url.EndsWith("api_req_kousyou/remodel_slot"))
\r
226 _logger.InspectRemodelSlot(request, data); // 資材の差が必要なので_itemInfoより前
\r
227 _itemInfo.InspectRemodelSlot(data);
\r
228 return Update.Item;
\r
230 if (url.EndsWith("api_req_kousyou/createship"))
\r
232 _logger.InspectCreateShip(request);
\r
233 return Update.None;
\r
235 if (url.EndsWith("api_req_kaisou/powerup"))
\r
237 _shipInfo.InspectPowerup(request, data);
\r
238 _conditionTimer.CheckCond();
\r
239 _akashiTimer.SetTimer();
\r
240 return Update.Item | Update.Ship;
\r
242 if (url.EndsWith("api_req_nyukyo/start"))
\r
244 _dockInfo.InspectNyukyo(request);
\r
245 _conditionTimer.CheckCond();
\r
246 _akashiTimer.SetTimer();
\r
247 return Update.Item | Update.Ship;
\r
249 if (url.EndsWith("api_req_nyukyo/speedchange"))
\r
251 _dockInfo.InspectSpeedChange(request);
\r
252 _conditionTimer.CheckCond();
\r
253 return Update.NDock | Update.Timer | Update.Ship;
\r
255 if (IsNormalBattleAPI(url))
\r
257 _battleInfo.InspectBattle(data);
\r
258 _logger.InspectBattle(data);
\r
259 return Update.Ship | Update.Battle;
\r
261 if (url.EndsWith("api_req_practice/battle") || url.EndsWith("api_req_practice/midnight_battle"))
\r
263 if (url.EndsWith("/battle"))
\r
265 _shipInfo.StartSortie(request); // 演習を出撃中とみなす
\r
266 _conditionTimer.InvalidateCond();
\r
268 _battleInfo.InspectBattle(data);
\r
269 return Update.Ship | Update.Battle | Update.Timer;
\r
271 if (url.EndsWith("api_req_sortie/battleresult"))
\r
273 _battleInfo.InspectBattleResult(json);
\r
274 _exMapInfo.InspectBattleResult(data);
\r
275 _logger.InspectBattleResult(data);
\r
276 return Update.Ship;
\r
278 if (url.EndsWith("api_req_practice/battle_result"))
\r
280 _battleInfo.InspectPracticeResult(json);
\r
281 return Update.Ship;
\r
283 if (IsCombinedBattleAPI(url))
\r
285 _battleInfo.InspectCombinedBattle(data, url.EndsWith("battle_water"));
\r
286 _logger.InspectBattle(data);
\r
287 return Update.Ship | Update.Battle;
\r
289 if (url.EndsWith("api_req_combined_battle/battleresult"))
\r
291 _battleInfo.InspectCombinedBattleResult(data);
\r
292 _logger.InspectBattleResult(data);
\r
293 return Update.Ship;
\r
295 if (url.EndsWith("api_req_combined_battle/goback_port"))
\r
297 _battleInfo.CauseCombinedBattleEscape();
\r
298 return Update.Ship;
\r
300 if (url.EndsWith("api_req_map/start"))
\r
302 _shipInfo.StartSortie(request);
\r
303 _conditionTimer.InvalidateCond();
\r
304 _exMapInfo.InspectMapStart(data);
\r
305 _logger.InspectMapStart(data);
\r
306 return Update.Timer;
\r
308 if (url.EndsWith("api_req_map/next"))
\r
310 _exMapInfo.InspectMapNext(data);
\r
311 _logger.InspectMapNext(data);
\r
312 return Update.None;
\r
314 if (url.EndsWith("api_req_mission/result"))
\r
316 _itemInfo.InspectMissionResult(data);
\r
317 _logger.InspectMissionResult(data);
\r
318 return Update.Item;
\r
320 if (url.EndsWith("api_get_member/mapinfo"))
\r
322 _exMapInfo.InspectMapInfo(data);
\r
323 return Update.None;
\r
325 return Update.None;
\r
328 private bool IsNormalBattleAPI(string url)
\r
330 return url.EndsWith("api_req_sortie/battle") ||
\r
331 url.EndsWith("api_req_sortie/airbattle") ||
\r
332 url.EndsWith("api_req_battle_midnight/battle") ||
\r
333 url.EndsWith("api_req_battle_midnight/sp_midnight");
\r
336 private bool IsCombinedBattleAPI(string url)
\r
338 return url.EndsWith("api_req_combined_battle/battle") ||
\r
339 url.EndsWith("api_req_combined_battle/airbattle") ||
\r
340 url.EndsWith("api_req_combined_battle/battle_water") ||
\r
341 url.EndsWith("api_req_combined_battle/midnight_battle") ||
\r
342 url.EndsWith("api_req_combined_battle/sp_midnight");
\r
345 public NameAndTimer[] NDock
\r
347 get { return _dockInfo.NDock; }
\r
350 public RingTimer[] KDock
\r
352 get { return _dockInfo.KDock; }
\r
355 public ItemInfo Item
\r
357 get { return _itemInfo; }
\r
360 public QuestStatus[] Quests
\r
362 get { return _questInfo.Quests; }
\r
365 public NameAndTimer[] Missions
\r
367 get { return _missionInfo.Missions; }
\r
370 public DateTime GetConditionTimer(int fleet)
\r
372 return _conditionTimer.GetTimer(fleet);
\r
375 public int[] GetConditionNotice()
\r
377 return _conditionTimer.GetNotice();
\r
380 public ShipStatus[] GetShipStatuses(int fleet)
\r
382 return _shipInfo.GetShipStatuses(fleet);
\r
385 public int[] GetDeck(int fleet)
\r
387 return _shipInfo.GetDeck(fleet);
\r
390 public ChargeStatus[] ChargeStatuses
\r
392 get { return _shipInfo.ChargeStatuses; }
\r
395 public int GetFighterPower(int fleet)
\r
397 return _shipInfo.GetFighterPower(fleet);
\r
400 public double GetFleetLineOfSights(int fleet)
\r
402 return _shipInfo.GetLineOfSights(fleet);
\r
405 public ShipStatus[] DamagedShipList
\r
407 get { return _shipInfo.GetDamagedShipList(_dockInfo); }
\r
410 public ShipStatus[] ShipList
\r
412 get { return _shipInfo.ShipList; }
\r
415 public ItemStatus[] ItemList
\r
419 _itemInfo.SetItemOwner(ShipList);
\r
420 return (from e in _itemInfo.ItemDict where e.Key != -1 select e.Value).ToArray();
\r
424 public AkashiTimer.RepairSpan[] GetAkashiTimers(int fleet)
\r
426 return _akashiTimer.GetTimers(fleet);
\r
429 public string[] GetAkashiTimerNotice()
\r
431 return _akashiTimer.GetNotice();
\r
434 public Achievement Achievement
\r
436 get { return _achievement; }
\r
439 public BattleInfo Battle
\r
441 get { return _battleInfo; }
\r
444 public ExMapInfo ExMap
\r
446 get { return _exMapInfo; }
\r
449 public void SetLogWriter(Action<string, string, string> writer, Func<DateTime> nowFunc)
\r
451 _logger.SetWriter(writer, nowFunc);
\r
454 public void SkipMaster()
\r
459 public void EnableLog(LogType type)
\r
461 _logger.EnableLog(type);
\r
464 public int MaterialLogInterval
\r
466 set { _logger.MaterialLogInterval = value; }
\r
469 public string LogOutputDir
\r
471 set { _logger.OutputDir = value; }
\r
475 public class NameAndTimer
\r
477 public string Name { get; set; }
\r
478 public RingTimer Timer { get; set; }
\r
480 public NameAndTimer()
\r
482 Timer = new RingTimer();
\r
486 public class RingTimer
\r
488 private DateTime _endTime;
\r
489 private readonly TimeSpan _spare;
\r
491 public TimeSpan Rest { get; private set; }
\r
493 public bool IsFinished
\r
495 get { return _endTime != DateTime.MinValue && Rest <= _spare; }
\r
498 public bool NeedRing { get; set; }
\r
500 public RingTimer(int spare = 60)
\r
502 _spare = TimeSpan.FromSeconds(spare);
\r
505 public void SetEndTime(double time)
\r
507 SetEndTime((int)time == 0
\r
508 ? DateTime.MinValue
\r
509 : new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time / 1000));
\r
512 public void SetEndTime(DateTime time)
\r
517 public void Update()
\r
519 if (_endTime == DateTime.MinValue)
\r
521 Rest = TimeSpan.Zero;
\r
525 Rest = _endTime - DateTime.Now;
\r
526 if (Rest < TimeSpan.Zero)
\r
527 Rest = TimeSpan.Zero;
\r
528 if (prev > _spare && _spare >= Rest)
\r