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 _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 _missionInfo.InspectDeck(data);
\r
143 _akashiTimer.SetTimer();
\r
144 return Update.Mission | Update.Timer;
\r
146 if (url.EndsWith("api_get_member/ship2"))
\r
149 _shipInfo.InspectShip(json);
\r
150 _akashiTimer.SetTimer();
\r
151 _battleInfo.InBattle = false;
\r
152 return Update.Item | Update.Ship | Update.Battle;
\r
154 if (url.EndsWith("api_get_member/ship3"))
\r
156 _shipInfo.InspectShip(data);
\r
157 _akashiTimer.SetTimer();
\r
158 return Update.Ship;
\r
160 if (url.EndsWith("api_get_member/material"))
\r
162 _itemInfo.InspectMaterial(data);
\r
163 return Update.Item;
\r
165 if (url.EndsWith("api_req_hokyu/charge"))
\r
167 _shipInfo.InspectCharge(data);
\r
168 return Update.Item | Update.Ship;
\r
170 if (url.EndsWith("api_req_kousyou/createitem"))
\r
172 _itemInfo.InspectCreateItem(data);
\r
173 return Update.Item;
\r
175 if (url.EndsWith("api_req_kousyou/getship"))
\r
177 _itemInfo.InspectGetShip(data);
\r
178 _shipInfo.InspectShip(data);
\r
179 _dockInfo.InspectKDock(data.api_kdock);
\r
180 return Update.Item | Update.Timer;
\r
182 if (url.EndsWith("api_req_kousyou/destroyship"))
\r
184 _shipInfo.InspectDestroyShip(request, data);
\r
185 _akashiTimer.SetTimer();
\r
186 return Update.Item | Update.Ship;
\r
188 if (url.EndsWith("api_req_kousyou/destroyitem2"))
\r
190 _itemInfo.InspectDestroyItem(request, data);
\r
191 return Update.Item;
\r
193 if (url.EndsWith("api_req_kaisou/powerup"))
\r
195 _shipInfo.InspectPowerup(request, data);
\r
196 return Update.Item | Update.Ship;
\r
198 if (url.EndsWith("api_req_nyukyo/start"))
\r
200 _dockInfo.InspectNyukyo(request);
\r
201 _akashiTimer.SetTimer();
\r
202 return Update.Item | Update.Ship;
\r
204 if (url.EndsWith("api_req_nyukyo/speedchange"))
\r
206 _dockInfo.InspectSpeedChange(request);
\r
207 return Update.NDock | Update.Timer | Update.Ship;
\r
209 if (IsNormalBattleAPI(url))
\r
211 _battleInfo.InspectBattle(data);
\r
212 return Update.Battle;
\r
214 if (url.EndsWith("api_req_sortie/battleresult") || url.EndsWith("api_req_practice/battleresult"))
\r
216 _battleInfo.CauseDamage();
\r
217 return Update.Ship;
\r
219 if (url.EndsWith("api_req_mission/result"))
\r
221 _itemInfo.InspectMissionResult(data);
\r
222 return Update.Item;
\r
224 if (IsCombinedBattleAPI(url))
\r
226 _battleInfo.InspectCombinedBattle(data);
\r
227 return Update.Battle;
\r
229 if (url.EndsWith("api_req_combined_battle/battleresult"))
\r
231 _battleInfo.CauseDamageCombined();
\r
232 return Update.Ship;
\r
234 return Update.None;
\r
237 public bool IsBattleAPI(string url)
\r
239 return IsNormalBattleAPI(url) || IsCombinedBattleAPI(url);
\r
242 public bool IsNormalBattleAPI(string url)
\r
244 return url.EndsWith("api_req_sortie/battle") || url.EndsWith("api_req_practice/battle") ||
\r
245 url.EndsWith("api_req_battle_midnight/sp_midnight") ||
\r
246 url.EndsWith("api_req_practice/midnight_battle");
\r
249 public bool IsCombinedBattleAPI(string url)
\r
251 return url.EndsWith("api_req_combined_battle/battle") || url.EndsWith("api_req_combined_battle/airbattle") ||
\r
252 url.EndsWith("api_req_combined_battle/midnight_battle") ||
\r
253 url.EndsWith("api_req_combined_battle/sp_midnight");
\r
256 public NameAndTimer[] NDock
\r
258 get { return _dockInfo.NDock; }
\r
261 public RingTimer[] KDock
\r
263 get { return _dockInfo.KDock; }
\r
266 public ItemInfo Item
\r
268 get { return _itemInfo; }
\r
271 public QuestInfo.NameAndProgress[] Quests
\r
273 get { return _questInfo.Quests; }
\r
276 public NameAndTimer[] Missions
\r
278 get { return _missionInfo.Missions; }
\r
281 public DateTime GetConditionTimer(int fleet)
\r
283 return _shipInfo.GetConditionTiemr(fleet);
\r
286 public int[] GetConditionNotice()
\r
288 return _shipInfo.GetConditionNotice();
\r
291 public ShipStatus[] GetShipStatuses(int fleet)
\r
293 return _shipInfo.GetShipStatuses(fleet);
\r
296 public ChargeStatus[] ChargeStatuses
\r
298 get { return _shipInfo.ChargeStatuses; }
\r
301 public int GetAirSuperiority(int fleet)
\r
303 return _shipInfo.GetAirSuperiority(fleet);
\r
306 public DamageStatus[] DamagedShipList
\r
308 get { return _shipInfo.GetDamagedShipList(_dockInfo); }
\r
311 public ShipStatus[] ShipList
\r
313 get { return _shipInfo.ShipList; }
\r
316 public ShipType[] ShipTypeList
\r
318 get { return _shipMaster.ShipTypeList; }
\r
321 public AkashiTimer.RepairSpan[] GetAkashiTimers(int fleet)
\r
323 return _akashiTimer.GetTimers(fleet);
\r
326 public string[] GetAkashiTimerNotice()
\r
328 return _akashiTimer.GetNotice();
\r
331 public Achievement Achievement
\r
333 get { return _achievement; }
\r
336 public BattleInfo Battle
\r
338 get { return _battleInfo; }
\r
341 public string LogFile { get; set; }
\r
344 public class NameAndTimer
\r
346 public string Name { get; set; }
\r
347 public RingTimer Timer { get; set; }
\r
349 public NameAndTimer()
\r
351 Timer = new RingTimer();
\r
355 public class RingTimer
\r
357 private DateTime _endTime;
\r
358 private TimeSpan _rest;
\r
359 private readonly TimeSpan _spare;
\r
361 public RingTimer(int spare = 60)
\r
363 _spare = TimeSpan.FromSeconds(spare);
\r
366 public void SetEndTime(double time)
\r
368 SetEndTime((int)time == 0
\r
369 ? DateTime.MinValue
\r
370 : new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time / 1000));
\r
373 public void SetEndTime(DateTime time)
\r
376 if (_endTime == DateTime.MinValue)
\r
377 IsFinished = false;
\r
380 public void Update()
\r
382 if (_endTime == DateTime.MinValue)
\r
384 _rest = TimeSpan.Zero;
\r
387 _rest = _endTime - DateTime.Now;
\r
388 if (_rest < TimeSpan.Zero)
\r
389 _rest = TimeSpan.Zero;
\r
390 if (_rest > _spare || IsFinished)
\r
396 public bool IsFinished { get; private set; }
\r
397 public bool NeedRing { get; set; }
\r
399 public override string ToString()
\r
401 return _rest.Days == 0 ? _rest.ToString(@"hh\:mm\:ss") : _rest.ToString(@"d\.hh\:mm");
\r