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
37 private bool _saveState;
\r
56 _shipInfo = new ShipInfo(_shipMaster, _itemInfo);
\r
57 _conditionTimer = new ConditionTimer(_shipInfo);
\r
58 _dockInfo = new DockInfo(_shipInfo, _itemInfo);
\r
59 _akashiTimer = new AkashiTimer(_shipInfo, _itemInfo, _dockInfo);
\r
60 _battleInfo = new BattleInfo(_shipMaster, _shipInfo, _itemInfo);
\r
61 _logger = new Logger(_shipMaster, _shipInfo, _itemInfo);
\r
64 private void SaveState()
\r
68 if (!_achievement.NeedSave && !_itemInfo.NeedSave && !_conditionTimer.NeedSave)
\r
70 _achievement.SaveState(_status);
\r
71 _itemInfo.SaveState(_status);
\r
72 _conditionTimer.SaveState(_status);
\r
76 public void LoadState()
\r
79 _achievement.LoadState(_status);
\r
80 _itemInfo.LoadSate(_status);
\r
81 _conditionTimer.LoadState(_status);
\r
85 public Update Sniff(string url, string request, dynamic json)
\r
87 var data = json.IsDefined("api_data") ? json.api_data : new object();
\r
89 if (url.EndsWith("api_start2"))
\r
92 _shipMaster.Inspect(data);
\r
93 _missionInfo.InspectMaster(data.api_mst_mission);
\r
94 _itemInfo.InspectMaster(data);
\r
95 return Update.Start;
\r
99 if (url.EndsWith("api_port/port"))
\r
101 _itemInfo.InspectBasic(data.api_basic);
\r
102 _itemInfo.InspectMaterial(data.api_material);
\r
103 _logger.InspectBasic(data.api_basic);
\r
104 _logger.InspectMaterial(data.api_material);
\r
105 _shipInfo.InspectShip(data);
\r
106 _conditionTimer.CalcRegenTime();
\r
107 _missionInfo.InspectDeck(data.api_deck_port);
\r
108 _dockInfo.InspectNDock(data.api_ndock);
\r
109 _akashiTimer.SetTimer(true);
\r
110 _achievement.InspectBasic(data.api_basic);
\r
111 if (data.api_parallel_quest_count()) // 昔のログにはないので
\r
112 _questInfo.QuestCount = (int)data.api_parallel_quest_count;
\r
113 _battleInfo.InBattle = false;
\r
114 _battleInfo.HasDamagedShip = false;
\r
115 _shipInfo.ClearEscapedShips();
\r
119 if (url.EndsWith("api_get_member/basic"))
\r
121 _itemInfo.InspectBasic(data);
\r
122 _logger.InspectBasic(data);
\r
123 return Update.None;
\r
125 if (url.EndsWith("api_get_member/slot_item"))
\r
127 _itemInfo.InspectSlotItem(data, true);
\r
128 return Update.None;
\r
130 if (url.EndsWith("api_get_member/kdock"))
\r
132 _dockInfo.InspectKDock(data);
\r
133 _logger.InspectKDock(data);
\r
134 return Update.Timer;
\r
136 if (url.EndsWith("api_get_member/ndock"))
\r
138 _dockInfo.InspectNDock(data);
\r
139 _conditionTimer.CheckCond();
\r
140 _akashiTimer.SetTimer();
\r
141 return Update.NDock | Update.Timer | Update.Ship;
\r
143 if (url.EndsWith("api_req_hensei/change"))
\r
145 _shipInfo.InspectChange(request);
\r
146 _akashiTimer.SetTimer();
\r
147 return Update.Ship;
\r
149 if (url.EndsWith("api_get_member/questlist"))
\r
151 _questInfo.Inspect(data);
\r
152 return Update.QuestList;
\r
154 if (url.EndsWith("api_get_member/deck"))
\r
156 _shipInfo.InspectDeck(data);
\r
157 _missionInfo.InspectDeck(data);
\r
158 _akashiTimer.SetTimer();
\r
159 return Update.Mission | Update.Timer;
\r
161 if (url.EndsWith("api_get_member/ship2"))
\r
164 _shipInfo.InspectShip(json);
\r
165 _akashiTimer.SetTimer();
\r
166 _battleInfo.InBattle = false;
\r
167 return Update.Item | Update.Ship | Update.Battle;
\r
169 if (url.EndsWith("api_get_member/ship3"))
\r
171 _shipInfo.InspectShip(data);
\r
172 _akashiTimer.SetTimer();
\r
173 _conditionTimer.CheckCond();
\r
174 return Update.Ship;
\r
176 if (url.EndsWith("api_get_member/material"))
\r
178 _itemInfo.InspectMaterial(data);
\r
179 return Update.Item;
\r
181 if (url.EndsWith("api_req_hokyu/charge"))
\r
183 _shipInfo.InspectCharge(data);
\r
184 return Update.Item | Update.Ship;
\r
186 if (url.EndsWith("api_req_kousyou/createitem"))
\r
188 _itemInfo.InspectCreateItem(data);
\r
189 _logger.InspectCreateItem(request, data);
\r
190 return Update.Item;
\r
192 if (url.EndsWith("api_req_kousyou/getship"))
\r
194 _itemInfo.InspectGetShip(data);
\r
195 _shipInfo.InspectShip(data);
\r
196 _dockInfo.InspectKDock(data.api_kdock);
\r
197 _conditionTimer.CheckCond();
\r
198 return Update.Item | Update.Timer;
\r
200 if (url.EndsWith("api_req_kousyou/destroyship"))
\r
202 _shipInfo.InspectDestroyShip(request, data);
\r
203 _conditionTimer.CheckCond();
\r
204 _akashiTimer.SetTimer();
\r
205 return Update.Item | Update.Ship;
\r
207 if (url.EndsWith("api_req_kousyou/destroyitem2"))
\r
209 _itemInfo.InspectDestroyItem(request, data);
\r
210 return Update.Item;
\r
212 if (url.EndsWith("api_req_kousyou/remodel_slot"))
\r
214 _itemInfo.InspectRemodelSlot(data);
\r
215 return Update.Item;
\r
217 if (url.EndsWith("api_req_kousyou/createship"))
\r
219 _logger.InspectCreateShip(request);
\r
220 return Update.None;
\r
222 if (url.EndsWith("api_req_kaisou/powerup"))
\r
224 _shipInfo.InspectPowerup(request, data);
\r
225 _conditionTimer.CheckCond();
\r
226 _akashiTimer.SetTimer();
\r
227 return Update.Item | Update.Ship;
\r
229 if (url.EndsWith("api_req_nyukyo/start"))
\r
231 _dockInfo.InspectNyukyo(request);
\r
232 _conditionTimer.CheckCond();
\r
233 _akashiTimer.SetTimer();
\r
234 return Update.Item | Update.Ship;
\r
236 if (url.EndsWith("api_req_nyukyo/speedchange"))
\r
238 _dockInfo.InspectSpeedChange(request);
\r
239 _conditionTimer.CheckCond();
\r
240 return Update.NDock | Update.Timer | Update.Ship;
\r
242 if (IsNormalBattleAPI(url))
\r
244 _battleInfo.InspectBattle(data);
\r
245 _logger.InspectBattle(data);
\r
246 return Update.Ship | Update.Battle;
\r
248 if (url.EndsWith("api_req_practice/battle") || url.EndsWith("api_req_practice/midnight_battle"))
\r
250 if (url.EndsWith("/battle"))
\r
252 _shipInfo.StartSortie(request); // 演習を出撃中とみなす
\r
253 _conditionTimer.InvalidateCond();
\r
255 _battleInfo.InspectPracticeBattle(data);
\r
256 return Update.Ship | Update.Battle | Update.Timer;
\r
258 if (url.EndsWith("api_req_sortie/battleresult"))
\r
260 _battleInfo.CauseDamage();
\r
261 _logger.InspectBattleResult(data);
\r
262 return Update.Ship;
\r
264 if (url.EndsWith("api_req_practice/battle_result"))
\r
266 _battleInfo.CausePracticeDamage();
\r
267 return Update.Ship;
\r
269 if (IsCombinedBattleAPI(url))
\r
271 _battleInfo.InspectCombinedBattle(data, url.EndsWith("battle_water"));
\r
272 _logger.InspectBattle(data);
\r
273 return Update.Ship | Update.Battle;
\r
275 if (url.EndsWith("api_req_combined_battle/battleresult"))
\r
277 _battleInfo.InspectCombinedBattleResult(data);
\r
278 _logger.InspectBattleResult(data);
\r
279 return Update.Ship;
\r
281 if (url.EndsWith("api_req_combined_battle/goback_port"))
\r
283 _battleInfo.CauseCombinedBattleEscape();
\r
284 return Update.Ship;
\r
286 if (url.EndsWith("api_req_map/start"))
\r
288 _shipInfo.StartSortie(request);
\r
289 _conditionTimer.InvalidateCond();
\r
290 _logger.InspectMap(data);
\r
291 return Update.Timer;
\r
293 if (url.EndsWith("api_req_map/next"))
\r
295 _logger.InspectMap(data);
\r
296 return Update.None;
\r
298 if (url.EndsWith("api_req_mission/result"))
\r
300 _itemInfo.InspectMissionResult(data);
\r
301 _logger.InspectMissionResult(data);
\r
302 return Update.Item;
\r
304 return Update.None;
\r
307 private bool IsNormalBattleAPI(string url)
\r
309 return url.EndsWith("api_req_sortie/battle") ||
\r
310 url.EndsWith("api_req_battle_midnight/battle") ||
\r
311 url.EndsWith("api_req_battle_midnight/sp_midnight");
\r
314 private bool IsCombinedBattleAPI(string url)
\r
316 return url.EndsWith("api_req_combined_battle/battle") ||
\r
317 url.EndsWith("api_req_combined_battle/airbattle") ||
\r
318 url.EndsWith("api_req_combined_battle/battle_water") ||
\r
319 url.EndsWith("api_req_combined_battle/midnight_battle") ||
\r
320 url.EndsWith("api_req_combined_battle/sp_midnight");
\r
323 public NameAndTimer[] NDock
\r
325 get { return _dockInfo.NDock; }
\r
328 public RingTimer[] KDock
\r
330 get { return _dockInfo.KDock; }
\r
333 public ItemInfo Item
\r
335 get { return _itemInfo; }
\r
338 public QuestInfo.NameAndProgress[] Quests
\r
340 get { return _questInfo.Quests; }
\r
343 public NameAndTimer[] Missions
\r
345 get { return _missionInfo.Missions; }
\r
348 public DateTime GetConditionTimer(int fleet)
\r
350 return _conditionTimer.GetTimer(fleet);
\r
353 public int[] GetConditionNotice()
\r
355 return _conditionTimer.GetNotice();
\r
358 public ShipStatus[] GetShipStatuses(int fleet)
\r
360 return _shipInfo.GetShipStatuses(fleet);
\r
363 public int[] GetDeck(int fleet)
\r
365 return _shipInfo.GetDeck(fleet);
\r
368 public ChargeStatus[] ChargeStatuses
\r
370 get { return _shipInfo.ChargeStatuses; }
\r
373 public int GetAirSuperiority(int fleet)
\r
375 return _shipInfo.GetAirSuperiority(fleet);
\r
378 public double GetFleetLineOfSights(int fleet)
\r
380 return _shipInfo.GetLineOfSights(fleet);
\r
383 public ShipStatus[] DamagedShipList
\r
385 get { return _shipInfo.GetDamagedShipList(_dockInfo); }
\r
388 public ShipStatus[] ShipList
\r
390 get { return _shipInfo.ShipList; }
\r
393 public ItemStatus[] ItemList
\r
395 get { return _itemInfo.GetItemList(_shipInfo); }
\r
398 public AkashiTimer.RepairSpan[] GetAkashiTimers(int fleet)
\r
400 return _akashiTimer.GetTimers(fleet);
\r
403 public string[] GetAkashiTimerNotice()
\r
405 return _akashiTimer.GetNotice();
\r
408 public Achievement Achievement
\r
410 get { return _achievement; }
\r
413 public BattleInfo Battle
\r
415 get { return _battleInfo; }
\r
418 public void SetLogWriter(Action<string, string, string> writer, Func<DateTime> nowFunc)
\r
420 _logger.SetWriter(writer, nowFunc);
\r
423 public void SkipMaster()
\r
428 public void EnableLog(LogType type)
\r
430 _logger.EnableLog(type);
\r
433 public int MaterialLogInterval
\r
435 set { _logger.MaterialLogInterval = value; }
\r
438 public string LogOutputDir
\r
440 set { _logger.OutputDir = value; }
\r
444 public class NameAndTimer
\r
446 public string Name { get; set; }
\r
447 public RingTimer Timer { get; set; }
\r
449 public NameAndTimer()
\r
451 Timer = new RingTimer();
\r
455 public class RingTimer
\r
457 private DateTime _endTime;
\r
458 private readonly TimeSpan _spare;
\r
460 public TimeSpan Rest { get; private set; }
\r
462 public bool IsFinished
\r
464 get { return _endTime != DateTime.MinValue && Rest <= _spare; }
\r
467 public bool NeedRing { get; set; }
\r
469 public RingTimer(int spare = 60)
\r
471 _spare = TimeSpan.FromSeconds(spare);
\r
474 public void SetEndTime(double time)
\r
476 SetEndTime((int)time == 0
\r
477 ? DateTime.MinValue
\r
478 : new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time / 1000));
\r
481 public void SetEndTime(DateTime time)
\r
486 public void Update()
\r
488 if (_endTime == DateTime.MinValue)
\r
490 Rest = TimeSpan.Zero;
\r
494 Rest = _endTime - DateTime.Now;
\r
495 if (Rest < TimeSpan.Zero)
\r
496 Rest = TimeSpan.Zero;
\r
497 if (prev > _spare && _spare >= Rest)
\r