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);
\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 _shipMaster.InspectStype(data.api_mst_stype);
\r
89 _missionInfo.InspectMaster(data.api_mst_mission);
\r
90 _itemInfo.InspectMaster(data.api_mst_slotitem);
\r
91 return Update.Start;
\r
95 if (url.EndsWith("api_port/port"))
\r
97 _itemInfo.InspectBasic(data.api_basic);
\r
98 _itemInfo.InspectMaterial(data.api_material);
\r
99 _shipInfo.InspectShip(data);
\r
100 _missionInfo.InspectDeck(data.api_deck_port);
\r
101 _dockInfo.InspectNDock(data.api_ndock);
\r
102 _akashiTimer.SetTimer(true);
\r
103 _achievement.InspectBasic(data.api_basic);
\r
104 _battleInfo.InBattle = false;
\r
105 _battleInfo.HasDamagedShip = false;
\r
108 if (url.EndsWith("api_get_member/basic"))
\r
110 _itemInfo.InspectBasic(data);
\r
111 return Update.None;
\r
113 if (url.EndsWith("api_get_member/slot_item"))
\r
115 _itemInfo.InspectSlotItem(data, true);
\r
116 return Update.None;
\r
118 if (url.EndsWith("api_get_member/kdock"))
\r
120 _dockInfo.InspectKDock(data);
\r
121 return Update.Timer;
\r
123 if (url.EndsWith("api_get_member/ndock"))
\r
125 _dockInfo.InspectNDock(data);
\r
126 _akashiTimer.SetTimer();
\r
127 return Update.NDock | Update.Timer | Update.Ship;
\r
129 if (url.EndsWith("api_req_hensei/change"))
\r
131 _shipInfo.InspectChange(request);
\r
132 _akashiTimer.SetTimer();
\r
133 return Update.Ship;
\r
135 if (url.EndsWith("api_get_member/questlist"))
\r
137 _questInfo.Inspect(data);
\r
138 return Update.QuestList;
\r
140 if (url.EndsWith("api_get_member/deck"))
\r
142 _shipInfo.InspectDeck(data);
\r
143 _missionInfo.InspectDeck(data);
\r
144 _akashiTimer.SetTimer();
\r
145 return Update.Mission | Update.Timer;
\r
147 if (url.EndsWith("api_get_member/ship2"))
\r
150 _shipInfo.InspectShip(json);
\r
151 _akashiTimer.SetTimer();
\r
152 _battleInfo.InBattle = false;
\r
153 return Update.Item | Update.Ship | Update.Battle;
\r
155 if (url.EndsWith("api_get_member/ship3"))
\r
157 _shipInfo.InspectShip(data);
\r
158 _akashiTimer.SetTimer();
\r
159 return Update.Ship;
\r
161 if (url.EndsWith("api_get_member/material"))
\r
163 _itemInfo.InspectMaterial(data);
\r
164 return Update.Item;
\r
166 if (url.EndsWith("api_req_hokyu/charge"))
\r
168 _shipInfo.InspectCharge(data);
\r
169 return Update.Item | Update.Ship;
\r
171 if (url.EndsWith("api_req_kousyou/createitem"))
\r
173 _itemInfo.InspectCreateItem(data);
\r
174 return Update.Item;
\r
176 if (url.EndsWith("api_req_kousyou/getship"))
\r
178 _itemInfo.InspectGetShip(data);
\r
179 _shipInfo.InspectShip(data);
\r
180 _dockInfo.InspectKDock(data.api_kdock);
\r
181 return Update.Item | Update.Timer;
\r
183 if (url.EndsWith("api_req_kousyou/destroyship"))
\r
185 _shipInfo.InspectDestroyShip(request, data);
\r
186 _akashiTimer.SetTimer();
\r
187 return Update.Item | Update.Ship;
\r
189 if (url.EndsWith("api_req_kousyou/destroyitem2"))
\r
191 _itemInfo.InspectDestroyItem(request, data);
\r
192 return Update.Item;
\r
194 if (url.EndsWith("api_req_kaisou/powerup"))
\r
196 _shipInfo.InspectPowerup(request, data);
\r
197 return Update.Item | Update.Ship;
\r
199 if (url.EndsWith("api_req_nyukyo/start"))
\r
201 _dockInfo.InspectNyukyo(request);
\r
202 _akashiTimer.SetTimer();
\r
203 return Update.Item | Update.Ship;
\r
205 if (url.EndsWith("api_req_nyukyo/speedchange"))
\r
207 _dockInfo.InspectSpeedChange(request);
\r
208 return Update.NDock | Update.Timer | Update.Ship;
\r
210 if (IsNormalBattleAPI(url))
\r
212 _battleInfo.InspectBattle(data);
\r
213 if (!url.EndsWith("api_req_practice/battle"))
\r
214 return Update.Battle;
\r
215 _shipInfo.StartSortie(request);
\r
216 return Update.Battle | Update.Timer;
\r
218 if (url.EndsWith("api_req_sortie/battleresult") || url.EndsWith("api_req_practice/battleresult"))
\r
220 _battleInfo.CauseDamage();
\r
221 return Update.Ship;
\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 if (url.EndsWith("api_req_map/start"))
\r
235 _shipInfo.StartSortie(request);
\r
236 return Update.Timer;
\r
238 if (url.EndsWith("api_req_mission/result"))
\r
240 _itemInfo.InspectMissionResult(data);
\r
241 return Update.Item;
\r
243 return Update.None;
\r
246 public bool IsBattleAPI(string url)
\r
248 return IsNormalBattleAPI(url) || IsCombinedBattleAPI(url);
\r
251 public bool IsNormalBattleAPI(string url)
\r
253 return url.EndsWith("api_req_sortie/battle") || url.EndsWith("api_req_practice/battle") ||
\r
254 url.EndsWith("api_req_battle_midnight/sp_midnight") ||
\r
255 url.EndsWith("api_req_practice/midnight_battle");
\r
258 public bool IsCombinedBattleAPI(string url)
\r
260 return url.EndsWith("api_req_combined_battle/battle") || url.EndsWith("api_req_combined_battle/airbattle") ||
\r
261 url.EndsWith("api_req_combined_battle/midnight_battle") ||
\r
262 url.EndsWith("api_req_combined_battle/sp_midnight");
\r
265 public NameAndTimer[] NDock
\r
267 get { return _dockInfo.NDock; }
\r
270 public RingTimer[] KDock
\r
272 get { return _dockInfo.KDock; }
\r
275 public ItemInfo Item
\r
277 get { return _itemInfo; }
\r
280 public QuestInfo.NameAndProgress[] Quests
\r
282 get { return _questInfo.Quests; }
\r
285 public NameAndTimer[] Missions
\r
287 get { return _missionInfo.Missions; }
\r
290 public DateTime GetConditionTimer(int fleet)
\r
292 return _shipInfo.GetConditionTiemr(fleet);
\r
295 public int[] GetConditionNotice()
\r
297 return _shipInfo.GetConditionNotice();
\r
300 public ShipStatus[] GetShipStatuses(int fleet)
\r
302 return _shipInfo.GetShipStatuses(fleet);
\r
305 public ChargeStatus[] ChargeStatuses
\r
307 get { return _shipInfo.ChargeStatuses; }
\r
310 public int GetAirSuperiority(int fleet)
\r
312 return _shipInfo.GetAirSuperiority(fleet);
\r
315 public DamageStatus[] DamagedShipList
\r
317 get { return _shipInfo.GetDamagedShipList(_dockInfo); }
\r
320 public ShipStatus[] ShipList
\r
322 get { return _shipInfo.ShipList; }
\r
325 public ShipType[] ShipTypeList
\r
327 get { return _shipMaster.ShipTypeList; }
\r
330 public AkashiTimer.RepairSpan[] GetAkashiTimers(int fleet)
\r
332 return _akashiTimer.GetTimers(fleet);
\r
335 public string[] GetAkashiTimerNotice()
\r
337 return _akashiTimer.GetNotice();
\r
340 public Achievement Achievement
\r
342 get { return _achievement; }
\r
345 public BattleInfo Battle
\r
347 get { return _battleInfo; }
\r
350 public string LogFile { get; set; }
\r
353 public class NameAndTimer
\r
355 public string Name { get; set; }
\r
356 public RingTimer Timer { get; set; }
\r
358 public NameAndTimer()
\r
360 Timer = new RingTimer();
\r
364 public class RingTimer
\r
366 private DateTime _endTime;
\r
367 private TimeSpan _rest;
\r
368 private readonly TimeSpan _spare;
\r
370 public RingTimer(int spare = 60)
\r
372 _spare = TimeSpan.FromSeconds(spare);
\r
375 public void SetEndTime(double time)
\r
377 SetEndTime((int)time == 0
\r
378 ? DateTime.MinValue
\r
379 : new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time / 1000));
\r
382 public void SetEndTime(DateTime time)
\r
385 if (_endTime == DateTime.MinValue)
\r
386 IsFinished = false;
\r
389 public void Update()
\r
391 if (_endTime == DateTime.MinValue)
\r
393 _rest = TimeSpan.Zero;
\r
396 _rest = _endTime - DateTime.Now;
\r
397 if (_rest < TimeSpan.Zero)
\r
398 _rest = TimeSpan.Zero;
\r
399 if (_rest > _spare || IsFinished)
\r
405 public bool IsFinished { get; private set; }
\r
406 public bool NeedRing { get; set; }
\r
408 public override string ToString()
\r
410 return _rest.Days == 0 ? _rest.ToString(@"hh\:mm\:ss") : _rest.ToString(@"d\.hh\:mm");
\r