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
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 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 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, _missionInfo);
\r
57 _battleInfo = new BattleInfo(_shipMaster, _shipInfo, _itemInfo);
\r
60 public void SaveState()
\r
62 _achievement.SaveState(_status);
\r
63 _itemInfo.SaveState(_status);
\r
67 public void LoadState()
\r
70 _achievement.LoadState(_status);
\r
71 _itemInfo.LoadSate(_status);
\r
74 public Update Sniff(string url, string request, dynamic json)
\r
76 var data = json.IsDefined("api_data") ? json.api_data : new object();
\r
78 if (LogFile != null)
\r
80 File.AppendAllText(LogFile,
\r
81 string.Format("url: {0}\nrequest: {1}\nresponse: {2}\n", url, request, json.ToString()));
\r
84 if (url.EndsWith("api_start2"))
\r
87 _shipMaster.Inspect(data.api_mst_ship);
\r
88 _missionInfo.InspectMaster(data.api_mst_mission);
\r
89 _itemInfo.InspectMaster(data.api_mst_slotitem);
\r
90 return Update.Start;
\r
94 if (url.EndsWith("api_port/port"))
\r
96 _itemInfo.InspectBasic(data.api_basic);
\r
97 _itemInfo.InspectMaterial(data.api_material);
\r
98 _shipInfo.InspectShip(data);
\r
99 _missionInfo.InspectDeck(data.api_deck_port);
\r
100 _dockInfo.InspectNDock(data.api_ndock);
\r
101 _akashiTimer.SetTimer(true);
\r
102 _achievement.InspectBasic(data.api_basic);
\r
103 _battleInfo.InBattle = false;
\r
104 _battleInfo.HasDamagedShip = false;
\r
107 if (url.EndsWith("api_get_member/basic"))
\r
109 _itemInfo.InspectBasic(data);
\r
110 return Update.None;
\r
112 if (url.EndsWith("api_get_member/slot_item"))
\r
114 _itemInfo.InspectSlotItem(data, true);
\r
115 return Update.None;
\r
117 if (url.EndsWith("api_get_member/kdock"))
\r
119 _dockInfo.InspectKDock(data);
\r
120 return Update.Timer;
\r
122 if (url.EndsWith("api_get_member/ndock"))
\r
124 _dockInfo.InspectNDock(data);
\r
125 _akashiTimer.SetTimer();
\r
126 return Update.NDock | Update.Timer | Update.Ship;
\r
128 if (url.EndsWith("api_req_hensei/change"))
\r
130 _shipInfo.InspectChange(request);
\r
131 _akashiTimer.SetTimer();
\r
132 return Update.Ship;
\r
134 if (url.EndsWith("api_get_member/questlist"))
\r
136 _questInfo.Inspect(data);
\r
137 return Update.QuestList;
\r
139 if (url.EndsWith("api_get_member/deck"))
\r
141 _missionInfo.InspectDeck(data);
\r
142 _akashiTimer.SetTimer();
\r
143 return Update.Mission | Update.Timer;
\r
145 if (url.EndsWith("api_get_member/ship2"))
\r
148 _shipInfo.InspectShip(json);
\r
149 _akashiTimer.SetTimer();
\r
150 _battleInfo.InBattle = false;
\r
151 return Update.Item | Update.Ship | Update.Battle;
\r
153 if (url.EndsWith("api_get_member/ship3"))
\r
155 _shipInfo.InspectShip(data);
\r
156 _akashiTimer.SetTimer();
\r
157 return Update.Ship;
\r
159 if (url.EndsWith("api_get_member/material"))
\r
161 _itemInfo.InspectMaterial(data);
\r
162 return Update.Item;
\r
164 if (url.EndsWith("api_req_hokyu/charge"))
\r
166 _shipInfo.InspectCharge(data);
\r
167 return Update.Item | Update.Ship;
\r
169 if (url.EndsWith("api_req_kousyou/createitem"))
\r
171 _itemInfo.InspectCreateItem(data);
\r
172 return Update.Item;
\r
174 if (url.EndsWith("api_req_kousyou/getship"))
\r
176 _itemInfo.InspectGetShip(data);
\r
177 _shipInfo.InspectShip(data);
\r
178 _dockInfo.InspectKDock(data.api_kdock);
\r
179 return Update.Item | Update.Timer;
\r
181 if (url.EndsWith("api_req_kousyou/destroyship"))
\r
183 _shipInfo.InspectDestroyShip(request, data);
\r
184 _akashiTimer.SetTimer();
\r
185 return Update.Item | Update.Ship;
\r
187 if (url.EndsWith("api_req_kousyou/destroyitem2"))
\r
189 _itemInfo.InspectDestroyItem(request, data);
\r
190 return Update.Item;
\r
192 if (url.EndsWith("api_req_kaisou/powerup"))
\r
194 _shipInfo.InspectPowerup(request, data);
\r
195 return Update.Item | Update.Ship;
\r
197 if (url.EndsWith("api_req_nyukyo/start"))
\r
199 _dockInfo.InspectNyukyo(request);
\r
200 _akashiTimer.SetTimer();
\r
201 return Update.Item | Update.Ship;
\r
203 if (url.EndsWith("api_req_nyukyo/speedchange"))
\r
205 _dockInfo.InspectSpeedChange(request);
\r
206 return Update.NDock | Update.Timer | Update.Ship;
\r
208 if (IsNormalBattleAPI(url))
\r
210 _battleInfo.InspectBattle(data);
\r
211 return Update.Battle;
\r
213 if (url.EndsWith("api_req_sortie/battleresult") || url.EndsWith("api_req_practice/battleresult"))
\r
215 _battleInfo.CauseDamage();
\r
216 return Update.Ship;
\r
218 if (url.EndsWith("api_req_mission/result"))
\r
220 _itemInfo.InspectMissionResult(data);
\r
221 return Update.Item;
\r
223 if (IsCombinedBattleAPI(url))
\r
225 _battleInfo.InspectCombinedBattle(data);
\r
226 return Update.Battle;
\r
228 if (url.EndsWith("api_req_combined_battle/battleresult"))
\r
230 _battleInfo.CauseDamageCombined();
\r
231 return Update.Ship;
\r
233 return Update.None;
\r
236 public bool IsBattleAPI(string url)
\r
238 return IsNormalBattleAPI(url) || IsCombinedBattleAPI(url);
\r
241 public bool IsNormalBattleAPI(string url)
\r
243 return url.EndsWith("api_req_sortie/battle") || url.EndsWith("api_req_practice/battle") ||
\r
244 url.EndsWith("api_req_battle_midnight/sp_midnight") ||
\r
245 url.EndsWith("api_req_practice/midnight_battle");
\r
248 public bool IsCombinedBattleAPI(string url)
\r
250 return url.EndsWith("api_req_combined_battle/battle") || url.EndsWith("api_req_combined_battle/airbattle") ||
\r
251 url.EndsWith("api_req_combined_battle/midnight_battle") ||
\r
252 url.EndsWith("api_req_combined_battle/sp_midnight");
\r
255 public NameAndTimer[] NDock
\r
257 get { return _dockInfo.NDock; }
\r
260 public RingTimer[] KDock
\r
262 get { return _dockInfo.KDock; }
\r
265 public ItemInfo Item
\r
267 get { return _itemInfo; }
\r
270 public QuestInfo.NameAndProgress[] Quests
\r
272 get { return _questInfo.Quests; }
\r
275 public NameAndTimer[] Missions
\r
277 get { return _missionInfo.Missions; }
\r
280 public string[] GetConditionTimers(int fleet)
\r
282 return _shipInfo.GetConditionTimers(fleet);
\r
285 public ShipStatus[] GetShipStatuses(int fleet)
\r
287 return _shipInfo.GetShipStatuses(fleet);
\r
290 public ChargeStatus[] ChargeStatuses
\r
292 get { return _shipInfo.ChargeStatuses; }
\r
295 public int GetAirSuperiority(int fleet)
\r
297 return _shipInfo.GetAirSuperiority(fleet);
\r
300 public DamageStatus[] DamagedShipList
\r
302 get { return _shipInfo.GetDamagedShipList(_dockInfo); }
\r
305 public ShipStatus[] ShipList
\r
307 get { return _shipInfo.ShipList; }
\r
310 public AkashiTimer.RepairSpan[] GetAkashiTimers(int fleet)
\r
312 return _akashiTimer.GetTimers(fleet);
\r
315 public string[] GetAkashiTimerNotice()
\r
317 return _akashiTimer.GetNotice();
\r
320 public Achievement Achievement
\r
322 get { return _achievement; }
\r
325 public BattleInfo Battle
\r
327 get { return _battleInfo; }
\r
330 public string LogFile { get; set; }
\r
333 public class NameAndTimer
\r
335 public string Name { get; set; }
\r
336 public RingTimer Timer { get; set; }
\r
338 public NameAndTimer()
\r
340 Timer = new RingTimer();
\r
344 public class RingTimer
\r
346 private DateTime _endTime;
\r
347 private TimeSpan _rest;
\r
348 private readonly TimeSpan _spare;
\r
350 public RingTimer(int spare = 60)
\r
352 _spare = TimeSpan.FromSeconds(spare);
\r
355 public void SetEndTime(double time)
\r
357 SetEndTime((int)time == 0
\r
358 ? DateTime.MinValue
\r
359 : new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time / 1000));
\r
362 public void SetEndTime(DateTime time)
\r
365 if (_endTime == DateTime.MinValue)
\r
366 IsFinished = false;
\r
369 public void Update()
\r
371 if (_endTime == DateTime.MinValue)
\r
373 _rest = TimeSpan.Zero;
\r
376 _rest = _endTime - DateTime.Now;
\r
377 if (_rest < TimeSpan.Zero)
\r
378 _rest = TimeSpan.Zero;
\r
379 if (_rest > _spare || IsFinished)
\r
385 public bool IsFinished { get; private set; }
\r
386 public bool NeedRing { get; set; }
\r
388 public override string ToString()
\r
390 return _rest.Days == 0 ? _rest.ToString(@"hh\:mm\:ss") : _rest.ToString(@"d\.hh\:mm");
\r