1 // Copyright (C) 2014, 2015 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
19 using System.Collections.Generic;
\r
22 namespace KancolleSniffer
\r
24 public enum BattleResultRank
\r
35 public class BattleInfo
\r
37 private readonly ShipMaster _shipMaster;
\r
38 private readonly ShipInfo _shipInfo;
\r
39 private readonly ItemInfo _itemInfo;
\r
41 private int[] _friendHp;
\r
42 private int[] _friendStartHp;
\r
43 private int[] _guardHp;
\r
44 private int[] _guardStartHp;
\r
45 private int[] _enemyHp;
\r
46 private int[] _enemyStartHp;
\r
47 private readonly List<int> _escapingShips = new List<int>();
\r
49 public bool InBattle { get; set; }
\r
50 public string Formation { get; private set; }
\r
51 public int EnemyAirSuperiority { get; private set; }
\r
52 public bool HasDamagedShip { get; set; }
\r
53 public string[] DamagedShipNames { get; private set; }
\r
54 public int AirControlLevel { get; private set; }
\r
55 public BattleResultRank ResultRank { get; private set; }
\r
57 public BattleInfo(ShipMaster shipMaster, ShipInfo shipInfo, ItemInfo itemInfo)
\r
59 _shipMaster = shipMaster;
\r
60 _shipInfo = shipInfo;
\r
61 _itemInfo = itemInfo;
\r
64 public void InspectBattle(dynamic json)
\r
67 Formation = FormationName(json);
\r
68 EnemyAirSuperiority = CalcEnemyAirSuperiority(json);
\r
69 AirControlLevel = CheckAirControlLevel(json);
\r
70 _fleet = (int)DeckId(json);
\r
71 ShowResult(false); // 昼戦の結果を夜戦のときに表示する
\r
73 if (IsNightBattle(json))
\r
74 CalcHougekiDamage(json.api_hougeki, _friendHp, _enemyHp);
\r
77 ClearOverKill(_friendHp);
\r
78 ClearOverKill(_enemyHp);
\r
79 ResultRank = CalcResultRank();
\r
82 private int DeckId(dynamic json)
\r
84 if (json.api_dock_id()) // 昼戦はtypoしている
\r
85 return (int)json.api_dock_id - 1;
\r
86 if (json.api_deck_id is string) // 通常の夜戦では文字列
\r
87 return int.Parse(json.api_deck_id) - 1;
\r
88 return (int)json.api_deck_id - 1;
\r
91 private string FormationName(dynamic json)
\r
93 if (!json.api_formation()) // 演習の夜戦
\r
95 switch ((int)json.api_formation[2])
\r
109 private void SetupHp(dynamic json)
\r
111 if (_friendHp != null)
\r
113 var nowhps = (int[])json.api_nowhps;
\r
114 _friendHp = nowhps.Skip(1).Take(6).TakeWhile(hp => hp != -1).ToArray();
\r
115 _friendStartHp = (int[])_friendHp.Clone();
\r
116 _enemyHp = nowhps.Skip(7).TakeWhile(hp => hp != -1).ToArray();
\r
117 _enemyStartHp = (int[])_enemyHp.Clone();
\r
118 if (json.api_nowhps_combined())
\r
120 _guardHp = ((int[])json.api_nowhps_combined).Skip(1).ToArray();
\r
121 _guardStartHp = (int[])_guardHp.Clone();
\r
125 _guardHp = _guardStartHp = new int[0];
\r
129 private void CleanupHp()
\r
134 private int CheckAirControlLevel(dynamic json)
\r
136 if (!json.api_kouku())
\r
138 var stage1 = json.api_kouku.api_stage1;
\r
139 if (stage1.api_f_count == 0 && stage1.api_e_count == 0)
\r
141 return (int)stage1.api_disp_seiku;
\r
144 private int CalcEnemyAirSuperiority(dynamic json)
\r
146 var maxEq = ((int[])json.api_ship_ke).Skip(1).SelectMany(id => _shipMaster[id].MaxEq);
\r
147 var equips = ((int[][])json.api_eSlot).SelectMany(x => x);
\r
148 return (from slot in equips.Zip(maxEq, (id, max) => new {id, max})
\r
149 let spec = _itemInfo.GetSpecByItemId(slot.id)
\r
150 where spec.CanAirCombat
\r
151 select (int)Math.Floor(spec.AntiAir * Math.Sqrt(slot.max))).DefaultIfEmpty().Sum();
\r
154 private void CalcDamage(dynamic json)
\r
156 if (json.api_kouku.api_stage3 != null)
\r
157 CalcSimpleDamage(json.api_kouku.api_stage3, _friendHp, _enemyHp);
\r
158 if (json.api_kouku2() && json.api_kouku2.api_stage3 != null) // 航空戦2回目
\r
159 CalcSimpleDamage(json.api_kouku2.api_stage3, _friendHp, _enemyHp);
\r
160 if (!json.api_opening_atack()) // 航空戦のみ
\r
162 if (json.api_opening_atack != null)
\r
163 CalcSimpleDamage(json.api_opening_atack, _friendHp, _enemyHp);
\r
164 if (json.api_hougeki1 != null)
\r
165 CalcHougekiDamage(json.api_hougeki1, _friendHp, _enemyHp);
\r
166 if (json.api_hougeki2 != null)
\r
167 CalcHougekiDamage(json.api_hougeki2, _friendHp, _enemyHp);
\r
168 if (json.api_raigeki != null)
\r
169 CalcSimpleDamage(json.api_raigeki, _friendHp, _enemyHp);
\r
172 private bool IsNightBattle(dynamic json)
\r
174 return json.api_hougeki();
\r
177 private void CalcSimpleDamage(dynamic json, int[] friend, int[] enemy)
\r
179 CalcSimpleDamage(json.api_fdam, friend);
\r
180 CalcSimpleDamage(json.api_edam, enemy);
\r
183 private void CalcSimpleDamage(dynamic rawDamage, int[] result)
\r
185 var damage = (int[])rawDamage;
\r
186 for (var i = 0; i < result.Length; i++)
\r
187 result[i] -= damage[i + 1];
\r
190 private void CalcHougekiDamage(dynamic hougeki, int[] friend, int[] enemy)
\r
192 var targets = ((dynamic[])hougeki.api_df_list).Skip(1).SelectMany(x => (int[])x);
\r
193 var damages = ((dynamic[])hougeki.api_damage).Skip(1).SelectMany(x => (double[])x);
\r
194 foreach (var hit in targets.Zip(damages, (t, d) => new {t, d}))
\r
199 friend[hit.t - 1] -= (int)hit.d;
\r
201 enemy[(hit.t - 1) % 6] -= (int)hit.d;
\r
205 private void ClearOverKill(int[] result)
\r
207 for (var i = 0; i < result.Length; i++)
\r
212 public void InspectBattleResult(dynamic json)
\r
218 public void InspectPracticeResult(dynamic json)
\r
224 private void ShowResult(bool warnDamagedShip = true)
\r
226 if (_friendHp == null)
\r
228 var ships = _shipInfo.GetShipStatuses(_fleet);
\r
229 foreach (var e in ships.Zip(_friendHp, (ship, now) => new {ship, now}))
\r
230 e.ship.NowHp = e.now;
\r
231 if (warnDamagedShip)
\r
233 ConsumeDamageControlItem(ships);
\r
234 UpdateDamgedShipNames(ships);
\r
239 // HPが0の艦娘にダメコンか女神があったら消費する。
\r
240 // 両方ある場合には前のスロットから消費する。
\r
242 private void ConsumeDamageControlItem(IEnumerable<ShipStatus> ships)
\r
244 foreach (var s in ships.Where(s => s.NowHp == 0))
\r
246 for (var i = 0; i < s.Slot.Length; i++)
\r
248 if (_itemInfo[s.Slot[i]].Type != 23)
\r
256 private void UpdateDamgedShipNames(IEnumerable<ShipStatus> ships)
\r
259 (from ship in ships where ship.DamageLevel == ShipStatus.Damage.Badly select ship.Name).ToArray();
\r
260 HasDamagedShip = DamagedShipNames.Any();
\r
263 public void InspectCombinedBattle(dynamic json, bool surfaceFleet)
\r
266 Formation = FormationName(json);
\r
267 EnemyAirSuperiority = CalcEnemyAirSuperiority(json);
\r
268 AirControlLevel = CheckAirControlLevel(json);
\r
270 ShowResultCombined(false);
\r
272 if (IsNightBattle(json))
\r
274 CalcHougekiDamage(json.api_hougeki, _guardHp, _enemyHp);
\r
279 CalcDamageCombinedFleetSurface(json);
\r
281 CalcDamageCombinedFleetAir(json);
\r
283 ClearOverKill(_friendHp);
\r
284 ClearOverKill(_guardHp);
\r
285 ClearOverKill(_enemyHp);
\r
286 ResultRank = CalcResultRank();
\r
289 public void InspectCombinedBattleResult(dynamic json)
\r
291 _escapingShips.Clear();
\r
292 ShowResultCombined();
\r
294 if ((int)json.api_escape_flag == 0)
\r
296 var damaged = (int)json.api_escape.api_escape_idx[0] - 1;
\r
297 _escapingShips.Add(_shipInfo.GetDeck(damaged / 6)[damaged % 6]);
\r
298 var escort = (int)json.api_escape.api_tow_idx[0] - 1;
\r
299 _escapingShips.Add(_shipInfo.GetDeck(escort / 6)[escort % 6]);
\r
302 private void ShowResultCombined(bool warnDamagedShip = true)
\r
304 if (_friendHp == null)
\r
306 var ships = _shipInfo.GetShipStatuses(0).Concat(_shipInfo.GetShipStatuses(1)).ToArray();
\r
307 foreach (var e in ships.Zip(_friendHp.Concat(_guardHp), (ship, now) => new {ship, now}))
\r
308 e.ship.NowHp = e.now;
\r
309 if (warnDamagedShip)
\r
311 ConsumeDamageControlItem(ships);
\r
312 UpdateDamgedShipNames(ships);
\r
316 public void CauseCombinedBattleEscape()
\r
318 _shipInfo.SetEscapedShips(_escapingShips);
\r
319 UpdateDamgedShipNames(_shipInfo.GetShipStatuses(0).Concat(_shipInfo.GetShipStatuses(1)));
\r
322 private void CalcDamageCombinedFleetAir(dynamic json)
\r
324 var kouku = json.api_kouku;
\r
325 if (kouku.api_stage3 != null)
\r
326 CalcSimpleDamage(kouku.api_stage3, _friendHp, _enemyHp);
\r
327 if (kouku.api_stage3_combined != null)
\r
328 CalcSimpleDamage(kouku.api_stage3_combined.api_fdam, _guardHp);
\r
329 if (json.api_kouku2()) // 航空戦2回目
\r
331 kouku = json.api_kouku2;
\r
332 if (kouku.api_stage3 != null)
\r
333 CalcSimpleDamage(kouku.api_stage3, _friendHp, _enemyHp);
\r
334 if (kouku.api_stage3_combined != null)
\r
335 CalcSimpleDamage(kouku.api_stage3_combined.api_fdam, _guardHp);
\r
337 if (!json.api_opening_atack()) // 航空戦のみ
\r
339 if (json.api_opening_atack != null)
\r
340 CalcSimpleDamage(json.api_opening_atack.api_fdam, _guardHp);
\r
341 if (json.api_hougeki1 != null)
\r
342 CalcHougekiDamage(json.api_hougeki1, _guardHp, _enemyHp);
\r
343 if (json.api_hougeki2() && json.api_hougeki2 != null)
\r
344 CalcHougekiDamage(json.api_hougeki2, _friendHp, _enemyHp);
\r
345 if (json.api_hougeki3() && json.api_hougeki3 != null)
\r
346 CalcHougekiDamage(json.api_hougeki3, _friendHp, _enemyHp);
\r
347 if (json.api_raigeki() && json.api_raigeki != null)
\r
348 CalcSimpleDamage(json.api_raigeki, _guardHp, _enemyHp);
\r
351 private void CalcDamageCombinedFleetSurface(dynamic json)
\r
353 var kouku = json.api_kouku;
\r
354 if (kouku.api_stage3 != null)
\r
355 CalcSimpleDamage(kouku.api_stage3, _friendHp, _enemyHp);
\r
356 if (kouku.api_stage3_combined != null)
\r
357 CalcSimpleDamage(kouku.api_stage3_combined.api_fdam, _guardHp);
\r
358 if (json.api_opening_atack != null)
\r
359 CalcSimpleDamage(json.api_opening_atack, _guardHp, _enemyHp);
\r
360 if (json.api_hougeki1 != null)
\r
361 CalcHougekiDamage(json.api_hougeki1, _friendHp, _enemyHp);
\r
362 if (json.api_hougeki2() && json.api_hougeki2 != null)
\r
363 CalcHougekiDamage(json.api_hougeki2, _friendHp, _enemyHp);
\r
364 if (json.api_hougeki3() && json.api_hougeki3 != null)
\r
365 CalcHougekiDamage(json.api_hougeki3, _guardHp, _enemyHp);
\r
366 if (json.api_raigeki() && json.api_raigeki != null)
\r
367 CalcSimpleDamage(json.api_raigeki, _guardHp, _enemyHp);
\r
370 // 以下のコードは航海日誌拡張版の以下のファイルのcalcResultRankを移植したもの
\r
371 // https://github.com/nekopanda/logbook/blob/94ceca4be6d4ce79a8759d1ee747fb9827c08edc/main/logbook/dto/BattleExDto.java
\r
373 // The MIT License (MIT)
\r
375 // Copyright (c) 2013-2014 sanae_hirotaka
\r
377 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
378 // of this software and associated documentation files (the "Software"), to deal
\r
379 // in the Software without restriction, including without limitation the rights
\r
380 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
381 // copies of the Software, and to permit persons to whom the Software is
\r
382 // furnished to do so, subject to the following conditions:
\r
384 // The above copyright notice and this permission notice shall be included in
\r
385 // all copies or substantial portions of the Software.
\r
387 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
388 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
389 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
390 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
391 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
392 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
395 private BattleResultRank CalcResultRank()
\r
397 var combinedHp = _friendHp.Concat(_guardHp).ToArray();
\r
398 var combinedStartHp = _friendStartHp.Concat(_guardStartHp).ToArray();
\r
400 var friendNowShips = combinedHp.Count(hp => hp > 0);
\r
401 var enemyNowShips = _enemyHp.Count(hp => hp > 0);
\r
403 var friendGauge = combinedStartHp.Sum() - combinedHp.Sum();
\r
404 var enemyGauge = _enemyStartHp.Sum() - _enemyHp.Sum();
\r
406 var friendSunk = combinedHp.Count(hp => hp == 0);
\r
407 var enemySunk = _enemyHp.Count(hp => hp == 0);
\r
409 var friendGaugeRate = Math.Floor((double)friendGauge / combinedStartHp.Sum() * 100);
\r
410 var enemyGaugeRate = Math.Floor((double)enemyGauge / _enemyStartHp.Sum() * 100);
\r
411 var equalOrMore = enemyGaugeRate > (0.9 * friendGaugeRate);
\r
412 var superior = enemyGaugeRate > 0 && enemyGaugeRate > (2.5 * friendGaugeRate);
\r
414 if (friendSunk == 0)
\r
416 if (enemyNowShips == 0)
\r
418 if (friendGauge == 0)
\r
419 return BattleResultRank.P;
\r
420 return BattleResultRank.S;
\r
422 if (_enemyHp.Length == 6)
\r
424 if (enemySunk >= 4)
\r
425 return BattleResultRank.A;
\r
427 else if (enemySunk * 2 >= _enemyHp.Length)
\r
429 return BattleResultRank.A;
\r
431 if (_enemyHp[0] == 0)
\r
432 return BattleResultRank.B;
\r
434 return BattleResultRank.B;
\r
438 if (enemyNowShips == 0)
\r
439 return BattleResultRank.B;
\r
440 if (_enemyHp[0] == 0 && friendSunk < enemySunk)
\r
441 return BattleResultRank.B;
\r
443 return BattleResultRank.B;
\r
444 if (_enemyHp[0] == 0)
\r
445 return BattleResultRank.C;
\r
447 if (enemyGauge > 0 && equalOrMore)
\r
448 return BattleResultRank.C;
\r
449 if (friendSunk > 0 && friendNowShips == 1)
\r
450 return BattleResultRank.E;
\r
451 return BattleResultRank.D;
\r