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 Status _status = new Status();
\r
53 _shipInfo = new ShipInfo(_shipMaster, _itemInfo);
\r
54 _dockInfo = new DockInfo(_shipInfo, _itemInfo);
\r
55 _akashiTimer = new AkashiTimer(_shipInfo, _itemInfo, _dockInfo, _missionInfo);
\r
56 _battleInfo = new BattleInfo(_shipMaster, _shipInfo, _itemInfo);
\r
59 public void SaveState()
\r
61 _achievement.SaveState(_status);
\r
62 _itemInfo.SaveState(_status);
\r
66 public void LoadState()
\r
69 _achievement.LoadState(_status);
\r
70 _itemInfo.LoadSate(_status);
\r
73 public Update Sniff(string url, string request, dynamic json)
\r
75 var data = json.IsDefined("api_data") ? json.api_data : new object();
\r
77 if (url.EndsWith("api_start2"))
\r
80 _shipMaster.Inspect(data.api_mst_ship);
\r
81 _missionInfo.InspectMaster(data.api_mst_mission);
\r
82 _itemInfo.InspectMaster(data.api_mst_slotitem);
\r
83 return Update.Start;
\r
87 if (url.EndsWith("api_port/port"))
\r
89 _itemInfo.InspectBasic(data.api_basic);
\r
90 _itemInfo.InspectMaterial(data.api_material);
\r
91 _shipInfo.InspectShip(data);
\r
92 _missionInfo.InspectDeck(data.api_deck_port);
\r
93 _dockInfo.InspectNDock(data.api_ndock);
\r
94 _akashiTimer.SetTimer(true);
\r
95 _achievement.InspectBasic(data.api_basic);
\r
96 _battleInfo.InBattle = false;
\r
97 _battleInfo.HasDamagedShip = false;
\r
100 if (url.EndsWith("api_get_member/basic"))
\r
102 _itemInfo.InspectBasic(data);
\r
103 return Update.None;
\r
105 if (url.EndsWith("api_get_member/slot_item"))
\r
107 _itemInfo.InspectSlotItem(data, true);
\r
108 return Update.None;
\r
110 if (url.EndsWith("api_get_member/kdock"))
\r
112 _dockInfo.InspectKDock(data);
\r
113 return Update.Timer;
\r
115 if (url.EndsWith("api_get_member/ndock"))
\r
117 _dockInfo.InspectNDock(data);
\r
118 _akashiTimer.SetTimer();
\r
119 return Update.NDock | Update.Timer;
\r
121 if (url.EndsWith("api_req_hensei/change"))
\r
123 _shipInfo.InspectChange(request);
\r
124 _akashiTimer.SetTimer();
\r
125 return Update.Ship;
\r
127 if (url.EndsWith("api_get_member/questlist"))
\r
129 _questInfo.Inspect(data);
\r
130 return Update.QuestList;
\r
132 if (url.EndsWith("api_get_member/deck"))
\r
134 _missionInfo.InspectDeck(data);
\r
135 _akashiTimer.SetTimer();
\r
136 return Update.Mission | Update.Timer;
\r
138 if (url.EndsWith("api_get_member/ship2"))
\r
141 _shipInfo.InspectShip(json);
\r
142 _akashiTimer.SetTimer();
\r
143 _battleInfo.InBattle = false;
\r
144 return Update.Item | Update.Ship | Update.Battle;
\r
146 if (url.EndsWith("api_get_member/ship3"))
\r
148 _shipInfo.InspectShip(data);
\r
149 _akashiTimer.SetTimer();
\r
150 return Update.Ship;
\r
152 if (url.EndsWith("api_req_hokyu/charge"))
\r
154 _shipInfo.InspectCharge(data);
\r
155 return Update.Item | Update.Ship;
\r
157 if (url.EndsWith("api_req_kousyou/createitem"))
\r
159 _itemInfo.InspectCreateItem(data);
\r
160 return Update.Item;
\r
162 if (url.EndsWith("api_req_kousyou/getship"))
\r
164 _itemInfo.InspectGetShip(data);
\r
165 _shipInfo.InspectShip(data);
\r
166 _dockInfo.InspectKDock(data.api_kdock);
\r
167 return Update.Item | Update.Timer;
\r
169 if (url.EndsWith("api_req_kousyou/destroyship"))
\r
171 _shipInfo.InspectDestroyShip(request, data);
\r
172 _akashiTimer.SetTimer();
\r
173 return Update.Item | Update.Ship;
\r
175 if (url.EndsWith("api_req_kousyou/destroyitem2"))
\r
177 _itemInfo.InspectDestroyItem(request, data);
\r
178 return Update.Item;
\r
180 if (url.EndsWith("api_req_kaisou/powerup"))
\r
182 _shipInfo.InspectPowerup(request, data);
\r
183 return Update.Item | Update.Ship;
\r
185 if (url.EndsWith("api_req_nyukyo/start"))
\r
187 _shipInfo.InspectNyukyo(request);
\r
188 return Update.Item | Update.Ship;
\r
190 if (url.EndsWith("api_req_sortie/battle") || url.EndsWith("api_req_practice/battle"))
\r
192 _battleInfo.InspectBattle(data);
\r
193 return Update.Battle;
\r
195 if (url.EndsWith("api_req_sortie/battleresult") || url.EndsWith("api_req_practice/battleresult"))
\r
197 return Update.Ship;
\r
199 if (url.EndsWith("api_req_mission/result"))
\r
201 _itemInfo.InspectMissionResult(data);
\r
202 return Update.Item;
\r
204 return Update.None;
\r
207 public NameAndTimer[] NDock
\r
209 get { return _dockInfo.NDock; }
\r
212 public RingTimer[] KDock
\r
214 get { return _dockInfo.KDock; }
\r
217 public ItemInfo Item
\r
219 get { return _itemInfo; }
\r
222 public QuestInfo.NameAndProgress[] Quests
\r
224 get { return _questInfo.Quests; }
\r
227 public NameAndTimer[] Missions
\r
229 get { return _missionInfo.Missions; }
\r
232 public string[] GetConditionTimers(int fleet)
\r
234 return _shipInfo.GetConditionTimers(fleet);
\r
237 public ShipStatus[] GetShipStatuses(int fleet)
\r
239 return _shipInfo.GetShipStatuses(fleet);
\r
242 public ChargeStatus[] ChargeStatuses
\r
244 get { return _shipInfo.ChargeStatuses; }
\r
247 public int GetAirSuperiority(int fleet)
\r
249 return _shipInfo.GetAirSuperiority(fleet);
\r
252 public DateTime GetAkashiStartTime(int fleet)
\r
254 return _akashiTimer[fleet];
\r
257 public Achievement Achievement
\r
259 get { return _achievement; }
\r
262 public BattleInfo Battle
\r
264 get { return _battleInfo; }
\r
268 public class NameAndTimer
\r
270 public string Name { get; set; }
\r
271 public RingTimer Timer { get; set; }
\r
273 public NameAndTimer()
\r
275 Timer = new RingTimer();
\r
279 public class RingTimer
\r
281 private DateTime _endTime;
\r
282 private TimeSpan _rest;
\r
283 private readonly TimeSpan _spare;
\r
285 public RingTimer(int spare = 60)
\r
287 _spare = TimeSpan.FromSeconds(spare);
\r
290 public void SetEndTime(double time)
\r
292 SetEndTime((int)time == 0
\r
293 ? DateTime.MinValue
\r
294 : new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time / 1000));
\r
297 public void SetEndTime(DateTime time)
\r
300 if (_endTime == DateTime.MinValue)
\r
301 IsFinished = false;
\r
304 public void Update()
\r
306 if (_endTime == DateTime.MinValue)
\r
308 _rest = TimeSpan.Zero;
\r
311 _rest = _endTime - DateTime.Now;
\r
312 if (_rest < TimeSpan.Zero)
\r
313 _rest = TimeSpan.Zero;
\r
314 if (_rest > _spare || IsFinished)
\r
320 public bool IsFinished { get; private set; }
\r
321 public bool NeedRing { get; set; }
\r
323 public override string ToString()
\r
325 return _rest.Days == 0 ? _rest.ToString(@"hh\:mm\:ss") : _rest.ToString(@"d\.hh\:mm");
\r