1 // Copyright (C) 2013, 2014 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 DockInfo _dockInfo;
\r
31 private readonly AkashiTimer _akashiTimer;
\r
32 private readonly Achievement _achievement = new Achievement();
\r
33 private readonly BattleInfo _battleInfo;
\r
34 private readonly Logger _logger;
\r
35 private readonly Status _status = new Status();
\r
54 _shipInfo = new ShipInfo(_shipMaster, _itemInfo);
\r
55 _dockInfo = new DockInfo(_shipInfo, _itemInfo);
\r
56 _akashiTimer = new AkashiTimer(_shipInfo, _itemInfo, _dockInfo);
\r
57 _battleInfo = new BattleInfo(_shipMaster, _shipInfo, _itemInfo);
\r
58 _logger = new Logger(_shipMaster, _shipInfo, _itemInfo);
\r
61 private void SaveState()
\r
63 if (!_achievement.NeedSave && !_itemInfo.NeedSave)
\r
65 _achievement.SaveState(_status);
\r
66 _itemInfo.SaveState(_status);
\r
70 public void LoadState()
\r
73 _achievement.LoadState(_status);
\r
74 _itemInfo.LoadSate(_status);
\r
77 public Update Sniff(string url, string request, dynamic json)
\r
79 var data = json.IsDefined("api_data") ? json.api_data : new object();
\r
81 if (url.EndsWith("api_start2"))
\r
84 _shipMaster.Inspect(data);
\r
85 _missionInfo.InspectMaster(data.api_mst_mission);
\r
86 _itemInfo.InspectMaster(data);
\r
87 return Update.Start;
\r
91 if (url.EndsWith("api_port/port"))
\r
93 _itemInfo.InspectBasic(data.api_basic);
\r
94 _itemInfo.InspectMaterial(data.api_material);
\r
95 _logger.InspectBasic(data.api_basic);
\r
96 _logger.InspectMaterial(data.api_material);
\r
97 _shipInfo.InspectShip(data);
\r
98 _missionInfo.InspectDeck(data.api_deck_port);
\r
99 _dockInfo.InspectNDock(data.api_ndock);
\r
100 _akashiTimer.SetTimer(true);
\r
101 _achievement.InspectBasic(data.api_basic);
\r
102 _battleInfo.InBattle = false;
\r
103 _battleInfo.HasDamagedShip = false;
\r
104 _shipInfo.ClearEscapedShips();
\r
108 if (url.EndsWith("api_get_member/basic"))
\r
110 _itemInfo.InspectBasic(data);
\r
111 _logger.InspectBasic(data);
\r
112 return Update.None;
\r
114 if (url.EndsWith("api_get_member/slot_item"))
\r
116 _itemInfo.InspectSlotItem(data, true);
\r
117 return Update.None;
\r
119 if (url.EndsWith("api_get_member/kdock"))
\r
121 _dockInfo.InspectKDock(data);
\r
122 _logger.InspectKDock(data);
\r
123 return Update.Timer;
\r
125 if (url.EndsWith("api_get_member/ndock"))
\r
127 _dockInfo.InspectNDock(data);
\r
128 _akashiTimer.SetTimer();
\r
129 return Update.NDock | Update.Timer | Update.Ship;
\r
131 if (url.EndsWith("api_req_hensei/change"))
\r
133 _shipInfo.InspectChange(request);
\r
134 _akashiTimer.SetTimer();
\r
135 return Update.Ship;
\r
137 if (url.EndsWith("api_get_member/questlist"))
\r
139 _questInfo.Inspect(data);
\r
140 return Update.QuestList;
\r
142 if (url.EndsWith("api_get_member/deck"))
\r
144 _shipInfo.InspectDeck(data);
\r
145 _missionInfo.InspectDeck(data);
\r
146 _akashiTimer.SetTimer();
\r
147 return Update.Mission | Update.Timer;
\r
149 if (url.EndsWith("api_get_member/ship2"))
\r
152 _shipInfo.InspectShip(json);
\r
153 _akashiTimer.SetTimer();
\r
154 _battleInfo.InBattle = false;
\r
155 return Update.Item | Update.Ship | Update.Battle;
\r
157 if (url.EndsWith("api_get_member/ship3"))
\r
159 _shipInfo.InspectShip(data);
\r
160 _akashiTimer.SetTimer();
\r
161 return Update.Ship;
\r
163 if (url.EndsWith("api_get_member/material"))
\r
165 _itemInfo.InspectMaterial(data);
\r
166 return Update.Item;
\r
168 if (url.EndsWith("api_req_hokyu/charge"))
\r
170 _shipInfo.InspectCharge(data);
\r
171 return Update.Item | Update.Ship;
\r
173 if (url.EndsWith("api_req_kousyou/createitem"))
\r
175 _itemInfo.InspectCreateItem(data);
\r
176 _logger.InspectCreateItem(request, data);
\r
177 return Update.Item;
\r
179 if (url.EndsWith("api_req_kousyou/getship"))
\r
181 _itemInfo.InspectGetShip(data);
\r
182 _shipInfo.InspectShip(data);
\r
183 _dockInfo.InspectKDock(data.api_kdock);
\r
184 return Update.Item | Update.Timer;
\r
186 if (url.EndsWith("api_req_kousyou/destroyship"))
\r
188 _shipInfo.InspectDestroyShip(request, data);
\r
189 _akashiTimer.SetTimer();
\r
190 return Update.Item | Update.Ship;
\r
192 if (url.EndsWith("api_req_kousyou/destroyitem2"))
\r
194 _itemInfo.InspectDestroyItem(request, data);
\r
195 return Update.Item;
\r
197 if (url.EndsWith("api_req_kousyou/remodel_slot"))
\r
199 _itemInfo.InspectRemodelSlot(data);
\r
200 return Update.Item;
\r
202 if (url.EndsWith("api_req_kousyou/createship"))
\r
204 _logger.InspectCreateShip(request);
\r
205 return Update.None;
\r
207 if (url.EndsWith("api_req_kaisou/powerup"))
\r
209 _shipInfo.InspectPowerup(request, data);
\r
210 _akashiTimer.SetTimer();
\r
211 return Update.Item | Update.Ship;
\r
213 if (url.EndsWith("api_req_nyukyo/start"))
\r
215 _dockInfo.InspectNyukyo(request);
\r
216 _akashiTimer.SetTimer();
\r
217 return Update.Item | Update.Ship;
\r
219 if (url.EndsWith("api_req_nyukyo/speedchange"))
\r
221 _dockInfo.InspectSpeedChange(request);
\r
222 return Update.NDock | Update.Timer | Update.Ship;
\r
224 if (IsNormalBattleAPI(url))
\r
226 _battleInfo.InspectBattle(data);
\r
227 _logger.InspectBattle(data);
\r
228 return Update.Ship | Update.Battle;
\r
230 if (url.EndsWith("api_req_practice/battle") || url.EndsWith("api_req_practice/midnight_battle"))
\r
232 if (url.EndsWith("/battle"))
\r
233 _shipInfo.StartSortie(request); // 演習を出撃中とみなす
\r
234 _battleInfo.InspectPracticeBattle(data);
\r
235 return Update.Ship | Update.Battle | Update.Timer;
\r
237 if (url.EndsWith("api_req_sortie/battleresult"))
\r
239 _battleInfo.CauseDamage();
\r
240 _logger.InspectBattleResult(data);
\r
241 return Update.Ship;
\r
243 if (url.EndsWith("api_req_practice/battle_result"))
\r
245 _battleInfo.CausePracticeDamage();
\r
246 return Update.Ship;
\r
248 if (IsCombinedBattleAPI(url))
\r
250 _battleInfo.InspectCombinedBattle(data, url.EndsWith("battle_water"));
\r
251 _logger.InspectBattle(data);
\r
252 return Update.Ship | Update.Battle;
\r
254 if (url.EndsWith("api_req_combined_battle/battleresult"))
\r
256 _battleInfo.InspectCombinedBattleResult(data);
\r
257 _logger.InspectBattleResult(data);
\r
258 return Update.Ship;
\r
260 if (url.EndsWith("api_req_combined_battle/goback_port"))
\r
262 _battleInfo.CauseCombinedBattleEscape();
\r
263 return Update.Ship;
\r
265 if (url.EndsWith("api_req_map/start"))
\r
267 _shipInfo.StartSortie(request);
\r
268 _logger.InspectMap(data);
\r
269 return Update.Timer;
\r
271 if (url.EndsWith("api_req_map/next"))
\r
273 _logger.InspectMap(data);
\r
274 return Update.None;
\r
276 if (url.EndsWith("api_req_mission/result"))
\r
278 _itemInfo.InspectMissionResult(data);
\r
279 _logger.InspectMissionResult(data);
\r
280 return Update.Item;
\r
282 return Update.None;
\r
285 private bool IsNormalBattleAPI(string url)
\r
287 return url.EndsWith("api_req_sortie/battle") ||
\r
288 url.EndsWith("api_req_battle_midnight/battle") ||
\r
289 url.EndsWith("api_req_battle_midnight/sp_midnight");
\r
292 private bool IsCombinedBattleAPI(string url)
\r
294 return url.EndsWith("api_req_combined_battle/battle") ||
\r
295 url.EndsWith("api_req_combined_battle/airbattle") ||
\r
296 url.EndsWith("api_req_combined_battle/battle_water") ||
\r
297 url.EndsWith("api_req_combined_battle/midnight_battle") ||
\r
298 url.EndsWith("api_req_combined_battle/sp_midnight");
\r
301 public NameAndTimer[] NDock
\r
303 get { return _dockInfo.NDock; }
\r
306 public RingTimer[] KDock
\r
308 get { return _dockInfo.KDock; }
\r
311 public ItemInfo Item
\r
313 get { return _itemInfo; }
\r
316 public QuestInfo.NameAndProgress[] Quests
\r
318 get { return _questInfo.Quests; }
\r
321 public NameAndTimer[] Missions
\r
323 get { return _missionInfo.Missions; }
\r
326 public DateTime GetConditionTimer(int fleet)
\r
328 return _shipInfo.GetConditionTiemr(fleet);
\r
331 public int[] GetConditionNotice()
\r
333 return _shipInfo.GetConditionNotice();
\r
336 public ShipStatus[] GetShipStatuses(int fleet)
\r
338 return _shipInfo.GetShipStatuses(fleet);
\r
341 public int[] GetDeck(int fleet)
\r
343 return _shipInfo.GetDeck(fleet);
\r
346 public ChargeStatus[] ChargeStatuses
\r
348 get { return _shipInfo.ChargeStatuses; }
\r
351 public int GetAirSuperiority(int fleet)
\r
353 return _shipInfo.GetAirSuperiority(fleet);
\r
356 public double GetFleetLineOfSights(int fleet)
\r
358 return _shipInfo.GetLineOfSights(fleet);
\r
361 public ShipStatus[] DamagedShipList
\r
363 get { return _shipInfo.GetDamagedShipList(_dockInfo); }
\r
366 public ShipStatus[] ShipList
\r
368 get { return _shipInfo.ShipList; }
\r
371 public ItemStatus[] ItemList
\r
373 get { return _itemInfo.GetItemList(_shipInfo); }
\r
376 public AkashiTimer.RepairSpan[] GetAkashiTimers(int fleet)
\r
378 return _akashiTimer.GetTimers(fleet);
\r
381 public string[] GetAkashiTimerNotice()
\r
383 return _akashiTimer.GetNotice();
\r
386 public Achievement Achievement
\r
388 get { return _achievement; }
\r
391 public BattleInfo Battle
\r
393 get { return _battleInfo; }
\r
396 public void SetLogWriter(Action<string, string, string> writer, Func<DateTime> nowFunc)
\r
398 _logger.SetWriter(writer, nowFunc);
\r
401 public void SkipMaster()
\r
406 public void EnableLog(LogType type)
\r
408 _logger.EnableLog(type);
\r
411 public int MaterialLogInterval
\r
413 set { _logger.MaterialLogInterval = value; }
\r
416 public string LogOutputDir
\r
418 set { _logger.OutputDir = value; }
\r
422 public class NameAndTimer
\r
424 public string Name { get; set; }
\r
425 public RingTimer Timer { get; set; }
\r
427 public NameAndTimer()
\r
429 Timer = new RingTimer();
\r
433 public class RingTimer
\r
435 private DateTime _endTime;
\r
436 private readonly TimeSpan _spare;
\r
438 public TimeSpan Rest { get; private set; }
\r
440 public bool IsFinished
\r
442 get { return _endTime != DateTime.MinValue && Rest <= _spare; }
\r
445 public bool NeedRing { get; set; }
\r
447 public RingTimer(int spare = 60)
\r
449 _spare = TimeSpan.FromSeconds(spare);
\r
452 public void SetEndTime(double time)
\r
454 SetEndTime((int)time == 0
\r
455 ? DateTime.MinValue
\r
456 : new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time / 1000));
\r
459 public void SetEndTime(DateTime time)
\r
464 public void Update()
\r
466 if (_endTime == DateTime.MinValue)
\r
468 Rest = TimeSpan.Zero;
\r
472 Rest = _endTime - DateTime.Now;
\r
473 if (Rest < TimeSpan.Zero)
\r
474 Rest = TimeSpan.Zero;
\r
475 if (prev > _spare && _spare >= Rest)
\r