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, bool surfaceFleet = false)
\r
156 var combined = json.api_nowhps_combined();
\r
157 if (json.api_kouku.api_stage3 != null)
\r
158 CalcSimpleDamage(json.api_kouku.api_stage3, _friendHp, _enemyHp);
\r
159 if (json.api_kouku.api_stage3_combined() && json.api_kouku.api_stage3_combined != null)
\r
160 CalcSimpleDamage(json.api_kouku.api_stage3_combined.api_fdam, _guardHp);
\r
161 if (json.api_kouku2()) // 航空戦2回目
\r
163 if (json.api_kouku2.api_stage3 != null)
\r
164 CalcSimpleDamage(json.api_kouku2.api_stage3, _friendHp, _enemyHp);
\r
165 if (json.api_kouku2.api_stage3_combined() && json.api_kouku2.api_stage3_combined != null)
\r
166 CalcSimpleDamage(json.api_kouku2.api_stage3_combined.api_fdam, _guardHp);
\r
168 if (!json.api_opening_atack()) // 航空戦のみ
\r
170 if (json.api_support_info() && json.api_support_info != null)
\r
171 CalcSupportDamage(json.api_support_info);
\r
172 if (json.api_opening_atack != null)
\r
174 var friend = combined ? _guardHp : _friendHp; // 雷撃の対象は護衛
\r
175 CalcSimpleDamage(json.api_opening_atack, friend, _enemyHp);
\r
177 if (json.api_hougeki1 != null)
\r
179 var friend = combined && !surfaceFleet ? _guardHp : _friendHp; // 空母機動部隊は一巡目が護衛
\r
180 CalcHougekiDamage(json.api_hougeki1, friend, _enemyHp);
\r
182 if (json.api_hougeki2 != null)
\r
184 CalcHougekiDamage(json.api_hougeki2, _friendHp, _enemyHp);
\r
186 if (json.api_hougeki3() && json.api_hougeki3 != null)
\r
188 var friend = combined && surfaceFleet ? _guardHp : _friendHp; // 水上打撃部隊は三順目が護衛
\r
189 CalcHougekiDamage(json.api_hougeki3, friend, _enemyHp);
\r
191 if (json.api_raigeki() && json.api_raigeki != null)
\r
193 var friend = combined ? _guardHp : _friendHp;
\r
194 CalcSimpleDamage(json.api_raigeki, friend, _enemyHp);
\r
198 private void CalcSupportDamage(dynamic json)
\r
200 if (json.api_support_hourai != null)
\r
201 CalcSimpleDamage(json.api_support_hourai.api_damage, _enemyHp);
\r
202 else if (json.api_support_airatack != null)
\r
204 var stage3 = json.api_support_airatack.api_stage3;
\r
205 if (stage3 != null)
\r
206 CalcSimpleDamage(stage3.api_edam, _enemyHp);
\r
210 private bool IsNightBattle(dynamic json)
\r
212 return json.api_hougeki();
\r
215 private void CalcSimpleDamage(dynamic json, int[] friend, int[] enemy)
\r
217 CalcSimpleDamage(json.api_fdam, friend);
\r
218 CalcSimpleDamage(json.api_edam, enemy);
\r
221 private void CalcSimpleDamage(dynamic rawDamage, int[] result)
\r
223 var damage = (int[])rawDamage;
\r
224 for (var i = 0; i < result.Length; i++)
\r
225 result[i] -= damage[i + 1];
\r
228 private void CalcHougekiDamage(dynamic hougeki, int[] friend, int[] enemy)
\r
230 var targets = ((dynamic[])hougeki.api_df_list).Skip(1).SelectMany(x => (int[])x);
\r
231 var damages = ((dynamic[])hougeki.api_damage).Skip(1).SelectMany(x => (double[])x);
\r
232 foreach (var hit in targets.Zip(damages, (t, d) => new {t, d}))
\r
237 friend[hit.t - 1] -= (int)hit.d;
\r
239 enemy[(hit.t - 1) % 6] -= (int)hit.d;
\r
243 private void ClearOverKill(int[] result)
\r
245 for (var i = 0; i < result.Length; i++)
\r
250 public void InspectBattleResult(dynamic json)
\r
256 public void InspectPracticeResult(dynamic json)
\r
262 private void ShowResult(bool warnDamagedShip = true)
\r
264 if (_friendHp == null)
\r
266 var ships = _shipInfo.GetShipStatuses(_fleet);
\r
267 foreach (var e in ships.Zip(_friendHp, (ship, now) => new {ship, now}))
\r
268 e.ship.NowHp = e.now;
\r
269 if (warnDamagedShip)
\r
271 ConsumeDamageControlItem(ships);
\r
272 UpdateDamgedShipNames(ships);
\r
277 // HPが0の艦娘にダメコンか女神があったら消費する。
\r
278 // 両方ある場合には前のスロットから消費する。
\r
280 private void ConsumeDamageControlItem(IEnumerable<ShipStatus> ships)
\r
282 foreach (var s in ships.Where(s => s.NowHp == 0))
\r
284 for (var i = 0; i < s.Slot.Length; i++)
\r
286 if (_itemInfo[s.Slot[i]].Type != 23)
\r
288 _itemInfo.DeleteItems(new[] {s.Slot[i]});
\r
295 private void UpdateDamgedShipNames(IEnumerable<ShipStatus> ships)
\r
298 (from ship in ships where ship.DamageLevel == ShipStatus.Damage.Badly select ship.Name).ToArray();
\r
299 HasDamagedShip = DamagedShipNames.Any();
\r
302 public void InspectCombinedBattle(dynamic json, bool surfaceFleet)
\r
305 Formation = FormationName(json);
\r
306 EnemyAirSuperiority = CalcEnemyAirSuperiority(json);
\r
307 AirControlLevel = CheckAirControlLevel(json);
\r
309 ShowResultCombined(false);
\r
311 if (IsNightBattle(json))
\r
312 CalcHougekiDamage(json.api_hougeki, _guardHp, _enemyHp);
\r
314 CalcDamage(json, surfaceFleet);
\r
315 ClearOverKill(_friendHp);
\r
316 ClearOverKill(_guardHp);
\r
317 ClearOverKill(_enemyHp);
\r
318 ResultRank = CalcResultRank();
\r
321 public void InspectCombinedBattleResult(dynamic json)
\r
323 _escapingShips.Clear();
\r
324 ShowResultCombined();
\r
326 if ((int)json.api_escape_flag == 0)
\r
328 var damaged = (int)json.api_escape.api_escape_idx[0] - 1;
\r
329 _escapingShips.Add(_shipInfo.GetDeck(damaged / 6)[damaged % 6]);
\r
330 var escort = (int)json.api_escape.api_tow_idx[0] - 1;
\r
331 _escapingShips.Add(_shipInfo.GetDeck(escort / 6)[escort % 6]);
\r
334 private void ShowResultCombined(bool warnDamagedShip = true)
\r
336 if (_friendHp == null)
\r
338 var ships = _shipInfo.GetShipStatuses(0).Concat(_shipInfo.GetShipStatuses(1)).ToArray();
\r
339 foreach (var e in ships.Zip(_friendHp.Concat(_guardHp), (ship, now) => new {ship, now}))
\r
340 e.ship.NowHp = e.now;
\r
341 if (warnDamagedShip)
\r
343 ConsumeDamageControlItem(ships);
\r
344 UpdateDamgedShipNames(ships);
\r
348 public void CauseCombinedBattleEscape()
\r
350 _shipInfo.SetEscapedShips(_escapingShips);
\r
351 UpdateDamgedShipNames(_shipInfo.GetShipStatuses(0).Concat(_shipInfo.GetShipStatuses(1)));
\r
354 // 以下のコードは航海日誌拡張版の以下のファイルのcalcResultRankを移植したもの
\r
355 // https://github.com/nekopanda/logbook/blob/94ceca4be6d4ce79a8759d1ee747fb9827c08edc/main/logbook/dto/BattleExDto.java
\r
357 // The MIT License (MIT)
\r
359 // Copyright (c) 2013-2014 sanae_hirotaka
\r
361 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
362 // of this software and associated documentation files (the "Software"), to deal
\r
363 // in the Software without restriction, including without limitation the rights
\r
364 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
365 // copies of the Software, and to permit persons to whom the Software is
\r
366 // furnished to do so, subject to the following conditions:
\r
368 // The above copyright notice and this permission notice shall be included in
\r
369 // all copies or substantial portions of the Software.
\r
371 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
372 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
373 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
374 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
375 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
376 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
379 private BattleResultRank CalcResultRank()
\r
381 var combinedHp = _friendHp.Concat(_guardHp).ToArray();
\r
382 var combinedStartHp = _friendStartHp.Concat(_guardStartHp).ToArray();
\r
384 var friendNowShips = combinedHp.Count(hp => hp > 0);
\r
385 var enemyNowShips = _enemyHp.Count(hp => hp > 0);
\r
387 var friendGauge = combinedStartHp.Sum() - combinedHp.Sum();
\r
388 var enemyGauge = _enemyStartHp.Sum() - _enemyHp.Sum();
\r
390 var friendSunk = combinedHp.Count(hp => hp == 0);
\r
391 var enemySunk = _enemyHp.Count(hp => hp == 0);
\r
393 var friendGaugeRate = Math.Floor((double)friendGauge / combinedStartHp.Sum() * 100);
\r
394 var enemyGaugeRate = Math.Floor((double)enemyGauge / _enemyStartHp.Sum() * 100);
\r
395 var equalOrMore = enemyGaugeRate > (0.9 * friendGaugeRate);
\r
396 var superior = enemyGaugeRate > 0 && enemyGaugeRate > (2.5 * friendGaugeRate);
\r
398 if (friendSunk == 0)
\r
400 if (enemyNowShips == 0)
\r
402 if (friendGauge == 0)
\r
403 return BattleResultRank.P;
\r
404 return BattleResultRank.S;
\r
406 if (_enemyHp.Length == 6)
\r
408 if (enemySunk >= 4)
\r
409 return BattleResultRank.A;
\r
411 else if (enemySunk * 2 >= _enemyHp.Length)
\r
413 return BattleResultRank.A;
\r
415 if (_enemyHp[0] == 0)
\r
416 return BattleResultRank.B;
\r
418 return BattleResultRank.B;
\r
422 if (enemyNowShips == 0)
\r
423 return BattleResultRank.B;
\r
424 if (_enemyHp[0] == 0 && friendSunk < enemySunk)
\r
425 return BattleResultRank.B;
\r
427 return BattleResultRank.B;
\r
428 if (_enemyHp[0] == 0)
\r
429 return BattleResultRank.C;
\r
431 if (enemyGauge > 0 && equalOrMore)
\r
432 return BattleResultRank.C;
\r
433 if (friendSunk > 0 && friendNowShips == 1)
\r
434 return BattleResultRank.E;
\r
435 return BattleResultRank.D;
\r