1 // Copyright (C) 2014 Kazuhiro Fujieda <fujieda@users.osdn.me>
\r
3 // Licensed under the Apache License, Version 2.0 (the "License");
\r
4 // you may not use this file except in compliance with the License.
\r
5 // You may obtain a copy of the License at
\r
7 // http://www.apache.org/licenses/LICENSE-2.0
\r
9 // Unless required by applicable law or agreed to in writing, software
\r
10 // distributed under the License is distributed on an "AS IS" BASIS,
\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
12 // See the License for the specific language governing permissions and
\r
13 // limitations under the License.
\r
16 using System.Collections.Generic;
\r
18 using System.IO.Compression;
\r
20 using Codeplex.Data;
\r
21 using ExpressionToCodeLib;
\r
22 using Microsoft.VisualStudio.TestTools.UnitTesting;
\r
24 namespace KancolleSniffer.Test
\r
27 public class SnifferTest
\r
29 public static StreamReader OpenLogFile(string name)
\r
31 var dir = Path.GetDirectoryName(Path.GetDirectoryName(Environment.CurrentDirectory));
\r
32 // ReSharper disable once AssignNullToNotNullAttribute
\r
33 var path = Path.Combine(dir, Path.Combine("logs", name + ".log.gz"));
\r
34 return new StreamReader(new GZipStream(File.Open(path, FileMode.Open), CompressionMode.Decompress));
\r
37 public static void SniffLogFile(Sniffer sniffer, string name, Action<Sniffer> action = null)
\r
40 using (var stream = OpenLogFile(name))
\r
42 while (!stream.EndOfStream)
\r
44 var triple = new List<string>();
\r
45 foreach (var s in new[] {"url: ", "request: ", "response: "})
\r
47 var line = stream.ReadLine();
\r
50 throw new Exception($"ログのurl, request, responseがそろっていません: {ln:d}行目");
\r
51 if (!line.StartsWith(s))
\r
52 throw new Exception($"ログに不正な行が含まれています: {ln:d}行目");
\r
53 triple.Add(line.Substring(s.Length));
\r
55 var json = DynamicJson.Parse(triple[2]);
\r
56 sniffer.Sniff(triple[0], triple[1], json);
\r
57 action?.Invoke(sniffer);
\r
63 /// 演習で受けたダメージが次の戦闘の結果に影響しない
\r
66 public void DamageInPracticeNotSpillIntoSortie()
\r
68 var expected = new[] {31, 15, 15};
\r
69 var sniffer = new Sniffer();
\r
70 SniffLogFile(sniffer, "battle_001");
\r
71 var result = sniffer.GetShipStatuses(0).Select(s => s.NowHp);
\r
72 PAssert.That(() => (expected.SequenceEqual(result)));
\r
79 public void IgnoreDamagedShipsInPractice()
\r
81 var sniffer = new Sniffer();
\r
82 SniffLogFile(sniffer, "practice_001");
\r
83 PAssert.That(() => !sniffer.BadlyDamagedShips.Any());
\r
87 /// 夜戦のダメージを戦闘結果に反映する
\r
90 public void CaptureDamageInNightCombat()
\r
92 var sniffer = new Sniffer();
\r
93 SniffLogFile(sniffer, "battle_002");
\r
94 AssertEqualBattleResult(sniffer, new[] {28, 1, 13});
\r
95 PAssert.That(() => sniffer.BadlyDamagedShips.Any());
\r
98 private void AssertEqualBattleResult(Sniffer sniffer, IEnumerable<int> expected, string msg = null)
\r
100 var result = sniffer.GetShipStatuses(0).Select(s => s.NowHp);
\r
101 PAssert.That(() => (expected.SequenceEqual(result)), msg);
\r
105 /// 連合艦隊(水上打撃部隊)による戦闘のダメージを結果に反映する
\r
108 public void CombinedFleetSurface()
\r
110 var sniffer = new Sniffer();
\r
111 SniffLogFile(sniffer, "combined_surface_001");
\r
112 AssertEauqlCombinedResult(sniffer, new[] {40, 77, 77, 33, 51, 47}, new[] {39, 35, 11, 39, 37, 40});
\r
113 PAssert.That(() => !sniffer.BadlyDamagedShips.Any());
\r
115 SniffLogFile(sniffer, "combined_surface_002");
\r
116 AssertEauqlCombinedResult(sniffer, new[] {40, 77, 77, 33, 15, 6}, new[] {39, 35, 4, 3, 14, 40});
\r
117 PAssert.That(() => sniffer.BadlyDamagedShips.Any());
\r
120 private void AssertEauqlCombinedResult(Sniffer sniffer, IEnumerable<int> expected0, IEnumerable<int> expected1,
\r
123 var result0 = sniffer.GetShipStatuses(0).Select(s => s.NowHp);
\r
124 var result1 = sniffer.GetShipStatuses(1).Select(s => s.NowHp);
\r
125 PAssert.That(() => (expected0.SequenceEqual(result0) && expected1.SequenceEqual(result1)), msg);
\r
129 /// 開幕夜戦のダメージを戦闘結果に反映する
\r
132 public void SpMidnight()
\r
134 var sniffer = new Sniffer();
\r
135 SniffLogFile(sniffer, "sp_midnight_001");
\r
136 AssertEqualBattleResult(sniffer, new[] {1});
\r
137 PAssert.That(() => sniffer.BadlyDamagedShips.Any());
\r
141 /// 連合艦隊(空母機動部隊)による戦闘のダメージを結果に反映する
\r
144 public void CombinedFleetAir()
\r
146 var sniffer = new Sniffer();
\r
147 SniffLogFile(sniffer, "combined_air_001");
\r
148 AssertEauqlCombinedResult(sniffer, new[] {40, 98, 90, 66, 78, 86}, new[] {47, 41, 5, 42, 43, 29});
\r
149 PAssert.That(() => sniffer.BadlyDamagedShips.Any());
\r
151 SniffLogFile(sniffer, "combined_air_002");
\r
152 AssertEauqlCombinedResult(sniffer, new[] {13, 87, 90, 59, 69, 86}, new[] {47, 41, 5, 20, 43, 29});
\r
159 public void EscapeShip()
\r
161 var sniffer = new Sniffer();
\r
162 SniffLogFile(sniffer, "combined_escape_001");
\r
163 AssertEauqlCombinedResult(sniffer, new[] {37, 105, 106, 90, 66, 10}, new[] {41, 41, 37, 44, 43, 43},
\r
164 "連合艦隊で2戦して大破が出るまで");
\r
165 PAssert.That(() => sniffer.BadlyDamagedShips.Any());
\r
166 SniffLogFile(sniffer, "combined_escape_002");
\r
167 PAssert.That(() => sniffer.GetShipStatuses(0)[5].Escaped && sniffer.GetShipStatuses(1)[1].Escaped,
\r
169 PAssert.That(() => !sniffer.BadlyDamagedShips.Any());
\r
170 SniffLogFile(sniffer, "combined_escape_003");
\r
171 AssertEauqlCombinedResult(sniffer, new[] {37, 105, 106, 90, 1, 10}, new[] {41, 41, 32, 44, 43, 43},
\r
173 PAssert.That(() => sniffer.BadlyDamagedShips.Any());
\r
174 SniffLogFile(sniffer, "combined_escape_004");
\r
175 PAssert.That(() => sniffer.GetShipStatuses(0)[5].Escaped && sniffer.GetShipStatuses(1)[1].Escaped &&
\r
176 sniffer.GetShipStatuses(0)[4].Escaped && sniffer.GetShipStatuses(1)[2].Escaped,
\r
178 PAssert.That(() => !sniffer.BadlyDamagedShips.Any());
\r
182 /// 護衛退避した空母を除いた制空値を計算する
\r
185 public void FighterPowerWithoutEscapedShip()
\r
187 var sniffer = new Sniffer();
\r
188 SniffLogFile(sniffer, "combined_escape_001");
\r
189 PAssert.That(() => sniffer.GetFighterPower(0)[0] == 187);
\r
190 SniffLogFile(sniffer, "combined_escape_002");
\r
191 PAssert.That(() => sniffer.GetFighterPower(0)[0] == 65);
\r
195 /// 夜戦の開始時に昼戦の結果を反映する
\r
198 public void ResultOfDayBattleShowInNightBattle()
\r
200 var sniffer = new Sniffer();
\r
201 SniffLogFile(sniffer, "battle_003");
\r
202 AssertEqualBattleResult(sniffer, new[] {28, 2, 13});
\r
203 PAssert.That(() => !sniffer.BadlyDamagedShips.Any(), "夜戦の開始時は大破警告を出さない");
\r
207 /// 通常艦隊の航空戦のダメージを結果に反映する
\r
210 public void AirBattle()
\r
212 var sniffer = new Sniffer();
\r
213 SniffLogFile(sniffer, "airbattle_001");
\r
214 AssertEqualBattleResult(sniffer, new[] {37, 36, 31, 37, 17, 63}, "夜戦あり");
\r
216 sniffer = new Sniffer();
\r
217 SniffLogFile(sniffer, "airbattle_002");
\r
218 AssertEqualBattleResult(sniffer, new[] {66, 36, 16, 27, 35, 38}, "昼戦のみ");
\r
222 /// 支援射撃による敵の損傷を勝利判定に反映させる
\r
225 public void SupportShellingChangeResultRank()
\r
227 var sniffer = new Sniffer();
\r
228 SniffLogFile(sniffer, "support_001");
\r
229 PAssert.That(() => sniffer.Battle.ResultRank == BattleResultRank.A);
\r
233 /// ダメコンの使用を戦闘結果に反映させる
\r
236 public void DamageControl()
\r
238 var sniffer = new Sniffer();
\r
239 SniffLogFile(sniffer, "damecon_001");
\r
240 AssertEqualBattleResult(sniffer, new[] {30, 1, 3}, "戦闘前");
\r
241 PAssert.That(() => sniffer.GetShipStatuses(0)[1].Slot.Select(item => item.Id).
\r
242 SequenceEqual(new[] {2, 4593, -1, -1, -1}), "ダメコンを二つ装備");
\r
243 PAssert.That(() => sniffer.Battle.ResultRank == BattleResultRank.S, "判定はS勝利");
\r
244 SniffLogFile(sniffer, "damecon_002");
\r
245 AssertEqualBattleResult(sniffer, new[] {30, 1, 3}, "戦闘後");
\r
246 PAssert.That(() => sniffer.GetShipStatuses(0)[1].Slot.Select(item => item.Id).
\r
247 SequenceEqual(new[] {-1, 4593, -1, -1, -1}), "ダメコンを一つ消費");
\r
251 /// 補強装備スロットのダメコンの使用を反映する
\r
254 public void DamgeControlInSlotEx()
\r
256 var sniffer = new Sniffer();
\r
257 SniffLogFile(sniffer, "damecon_003");
\r
258 PAssert.That(() => sniffer.GetShipStatuses(0)[5].SlotEx.Id == 10306, "戦闘前");
\r
259 SniffLogFile(sniffer, "damecon_004");
\r
260 PAssert.That(() => sniffer.GetShipStatuses(0)[5].SlotEx.Id == -1, "戦闘後");
\r
264 /// 連合艦隊(水上打撃部隊)で二回目の砲撃戦がない場合を正しく処理する
\r
267 // ReSharper disable once InconsistentNaming
\r
268 public void CombinedFleetSurfaceWithout2ndShelling()
\r
270 var sniffer = new Sniffer();
\r
271 SniffLogFile(sniffer, "combined_surface_003");
\r
272 PAssert.That(() => sniffer.Battle.ResultRank == BattleResultRank.P);
\r
276 /// 連合艦隊(水上打撃部隊)で開幕雷撃で戦闘が終わる場合を正しく処理する
\r
279 // ReSharper disable once InconsistentNaming
\r
280 public void CombinedFleetSurfaceWithoutAnyShelling()
\r
282 var sniffer = new Sniffer();
\r
283 SniffLogFile(sniffer, "combined_surface_004");
\r
284 PAssert.That(() => sniffer.Battle.ResultRank == BattleResultRank.P);
\r
288 /// ship2に代わるship_deckを処理する
\r
291 public void ShipDeck()
\r
293 var sniffer = new Sniffer();
\r
294 SniffLogFile(sniffer, "ship_deck_001");
\r
295 PAssert.That(() => sniffer.GetShipStatuses(0)[0].Fuel == 36);
\r
299 /// 夜戦かどうかを選択する画面でリロードしても結果を次の戦闘に持ち越さない
\r
302 public void ReloadBeforeBattleResult()
\r
304 var sniffer = new Sniffer();
\r
305 SniffLogFile(sniffer, "reload_001");
\r
306 PAssert.That(() => sniffer.GetShipStatuses(0)[0].NowHp == 41);
\r
310 /// 航空戦のない演習を正しく処理する
\r
313 public void PracticeWithoutAirBattle()
\r
315 var sniffer = new Sniffer();
\r
316 SniffLogFile(sniffer, "practice_002");
\r
317 PAssert.That(() => sniffer.Battle.AirControlLevel == -1);
\r
321 /// 出撃時に大破している艦娘がいたら警告する
\r
324 public void DamagedShipWarningOnMapStart()
\r
326 var sniffer = new Sniffer();
\r
327 SniffLogFile(sniffer, "mapstart_001");
\r
328 PAssert.That(() => sniffer.BadlyDamagedShips.SequenceEqual(new[] {"大潮"}));
\r
332 /// 熟練度込みの制空値を正しく計算する
\r
335 public void FighterPowerWithBonus()
\r
337 var sniffer = new Sniffer();
\r
338 SniffLogFile(sniffer, "fighterpower_001");
\r
339 PAssert.That(() => sniffer.GetFighterPower(0).SequenceEqual(new [] {156, 159}));
\r
343 /// 2-5をクリアしたときの特別戦果を反映する
\r
346 public void ExMapBattleResult()
\r
348 var sniffer0 = new Sniffer();
\r
349 sniffer0.ExMap.ClearClearStatus();
\r
350 SniffLogFile(sniffer0, "eo_001");
\r
351 PAssert.That(() => sniffer0.ExMap.Achievement == 100, "ほかのマップの情報なし");
\r
352 var sniffer1 = new Sniffer();
\r
353 SniffLogFile(sniffer1, "eo_001");
\r
354 // すでに3-5をクリアしているので合計で250
\r
355 PAssert.That(() => sniffer1.ExMap.Achievement == 250, "ほかのマップの情報あり");
\r
359 /// 1-6をクリアしたときの特別戦果を反映する
\r
362 public void ExMapMapNext()
\r
364 var sniffer0 = new Sniffer();
\r
365 sniffer0.ExMap.ClearClearStatus();
\r
366 SniffLogFile(sniffer0, "eo_002");
\r
367 PAssert.That(() => sniffer0.ExMap.Achievement == 75, "ほかのマップの情報なし");
\r
368 var sniffer1 = new Sniffer();
\r
369 SniffLogFile(sniffer1, "eo_002");
\r
370 // 5-5以外クリアしているので合計で400
\r
371 PAssert.That(() => sniffer1.ExMap.Achievement == 400, "ほかのマップの情報あり");
\r
375 /// 編成で空き番号を使ったローテートを正しく反映する
\r
378 public void RotateFleetMember()
\r
380 var sniffer = new Sniffer();
\r
381 SniffLogFile(sniffer, "deck_001");
\r
382 var result = sniffer.GetDeck(0);
\r
383 PAssert.That(() => new[] {756, 17204, 6156, 28806, 1069, -1}.SequenceEqual(result));
\r
387 /// 編成で艦隊に配置ずみの艦娘を交換する
\r
390 public void ExchangeFleetMember()
\r
392 var sniffer = new Sniffer();
\r
394 SniffLogFile(sniffer, "deck_002");
\r
395 var result0 = sniffer.GetDeck(0);
\r
396 PAssert.That(() => new[] {1069, 6156, 756, 3223, -1, -1}.SequenceEqual(result0), "編成で艦隊内で艦娘と交換する");
\r
398 SniffLogFile(sniffer, "deck_003");
\r
399 var result10 = sniffer.GetDeck(0);
\r
400 var result11 = sniffer.GetDeck(1);
\r
401 PAssert.That(() => new[] {1069, 6156, 14258, 3223, -1, -1}.SequenceEqual(result10) &&
\r
402 new[] {101, 4487, 756, 14613, 28806, -1}.SequenceEqual(result11), "002に続いて艦隊をまたがって交換する");
\r
404 SniffLogFile(sniffer, "deck_004");
\r
405 var result20 = sniffer.GetDeck(0);
\r
406 var result21 = sniffer.GetDeck(1);
\r
407 PAssert.That(() => new[] {1069, 6156, 14258, 3223, 756, -1}.SequenceEqual(result20) &&
\r
408 new[] {101, 4487, 14613, 28806, -1, -1}.SequenceEqual(result21),
\r
409 "003に続いて空き番号にほかの艦隊の艦娘を配置する");
\r
416 public void PresetSelect()
\r
418 var sniffer = new Sniffer();
\r
419 SniffLogFile(sniffer, "preset_001");
\r
420 var result = sniffer.GetDeck(0);
\r
421 PAssert.That(() => new[] {50510, 632, 39843, 113, 478, 47422}.SequenceEqual(result));
\r
428 public void SlotExchange()
\r
430 var sniffer = new Sniffer();
\r
431 SniffLogFile(sniffer, "slot_exchange_001");
\r
432 var result = sniffer.GetShipStatuses(0)[0].Slot.Select(item => item.Id);
\r
433 PAssert.That(() => new[] {26096, 30571, 77694, 61383, -1}.SequenceEqual(result));
\r
437 /// 改修による資材の減少をすぐに反映する
\r
440 public void ConsumptionByRemodelSlot()
\r
442 var sniffer = new Sniffer();
\r
443 SniffLogFile(sniffer, "remodel_slot_001");
\r
444 PAssert.That(() => sniffer.Material.Current
\r
445 .SequenceEqual(new[] {25292, 25570, 25244, 41113, 1405, 1525, 2137, 8}));
\r
452 public void CountItem()
\r
454 var sniffer = new Sniffer();
\r
455 SniffLogFile(sniffer, "createitem_001");
\r
456 PAssert.That(() => sniffer.Item.NowEquips == 606);
\r
463 public void MaterialVariation()
\r
465 var sniffer1 = new Sniffer();
\r
466 var result1 = new List<int[]>();
\r
467 SniffLogFile(sniffer1, "material_001", sn =>
\r
469 var cur = sn.Material.Current;
\r
470 if (result1.Count == 0)
\r
476 if (!result1.Last().SequenceEqual(cur))
\r
480 var expected1 = new List<int[]>
\r
482 new[] {0, 0, 0, 0, 0, 0, 0, 0},
\r
483 new[] {26178, 26742, 21196, 33750, 1426, 1574, 2185, 10},
\r
484 new[] {26178, 26842, 21226, 33750, 1426, 1574, 2185, 10},
\r
485 new[] {28951, 29493, 24945, 35580, 1426, 1574, 2185, 10},
\r
486 new[] {26074, 26616, 21068, 33700, 1426, 1572, 2183, 10},
\r
487 new[] {26171, 26721, 21175, 33750, 1426, 1574, 2185, 10},
\r
488 new[] {27023, 27829, 28136, 42404, 1404, 1521, 2142, 15},
\r
489 new[] {31208, 29819, 29714, 42345, 1407, 1530, 2155, 13},
\r
490 new[] {24595, 25353, 18900, 32025, 1427, 1576, 2187, 10},
\r
491 new[] {24515, 25353, 18749, 32025, 1427, 1575, 2187, 10},
\r
492 new[] {23463, 24964, 17284, 31765, 1427, 1572, 2187, 10},
\r
493 new[] {23463, 25064, 17314, 31765, 1427, 1572, 2187, 10}
\r
495 PAssert.That(() => SequenceOfSequenceEqual(expected1, result1));
\r
497 var sniffer2 = new Sniffer();
\r
498 var result2 = new List<int[]>();
\r
499 SniffLogFile(sniffer2, "material_002", sn =>
\r
501 var cur = sn.Material.Current;
\r
502 if (result2.Count == 0)
\r
508 if (!result2.Last().SequenceEqual(cur))
\r
512 var expected2 = new List<int[]>
\r
514 new[] {0, 0, 0, 0, 0, 0, 0, 0},
\r
515 new[] {201649, 189713, 261490, 123227, 2743, 2828, 3000, 44},
\r
516 new[] {201649, 189714, 261491, 123227, 2743, 2828, 3000, 44},
\r
517 new[] {201650, 189718, 261500, 123227, 2743, 2828, 3000, 44}
\r
519 PAssert.That(() => SequenceOfSequenceEqual(expected2, result2));
\r
522 private bool SequenceOfSequenceEqual<T>(IEnumerable<IEnumerable<T>> a, IEnumerable<IEnumerable<T>> b)
\r
524 var aa = a.ToArray();
\r
525 var bb = b.ToArray();
\r
526 if (aa.Length != bb.Length)
\r
528 return aa.Zip(bb, (x, y) => x.SequenceEqual(y)).All(x => x);
\r
535 public void MapInfo()
\r
537 var sniffer = new Sniffer();
\r
538 SniffLogFile(sniffer, "mapinfo_001");
\r
539 PAssert.That(() => sniffer.MiscText ==
\r
541 "1-6 : 撃破 3/7\r\n5-5 : 撃破 0/5\r\n6-3 : 撃破 0/4\r\n" +
\r
542 "31-1 : HP 0/750\r\n31-2 : HP 0/1050\r\n31-3 : HP 0/2100\r\n31-4 : HP 0/3500\r\n31-5 : HP 1/2450\r\n",
\r
544 SniffLogFile(sniffer, "mapinfo_002");
\r
545 PAssert.That(() => sniffer.MiscText ==
\r
546 "[海域ゲージ情報]\r\n 海域選択画面に進むと表示します。\r\n[演習情報]\r\n 演習相手を選ぶと表示します。",
\r
548 SniffLogFile(sniffer, "mapinfo_003");
\r
549 PAssert.That(() => sniffer.MiscText ==
\r
551 "1-6 : 撃破 4/7\r\n5-5 : 撃破 0/5\r\n6-3 : 撃破 0/4\r\n" +
\r
552 "31-1 : HP 0/750\r\n31-2 : HP 0/1050\r\n31-3 : HP 0/2100\r\n31-4 : HP 0/3500\r\n31-5 : HP 1/2450\r\n",
\r
560 public void PractiveEnemyInfo()
\r
562 var sniffer = new Sniffer();
\r
563 SniffLogFile(sniffer, "practice_enemyinfo_001");
\r
564 PAssert.That(() => sniffer.MiscText ==
\r
565 "[演習情報]\r\n敵艦隊名 : 大銀河地球連邦 第7艦隊\r\n獲得経験値 : 570\r\nS勝利 : 684",
\r
567 SniffLogFile(sniffer, "practice_enemyinfo_002");
\r
568 PAssert.That(() => sniffer.MiscText ==
\r
569 "[海域ゲージ情報]\r\n 海域選択画面に進むと表示します。\r\n[演習情報]\r\n 演習相手を選ぶと表示します。",
\r