1 // Copyright (C) 2014 Kazuhiro Fujieda <fujieda@users.osdn.me>
\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
21 using System.IO.Compression;
\r
23 using Codeplex.Data;
\r
24 using ExpressionToCodeLib;
\r
25 using Microsoft.VisualStudio.TestTools.UnitTesting;
\r
27 namespace KancolleSniffer.Test
\r
30 public class SnifferTest
\r
32 public static StreamReader OpenLogFile(string name)
\r
34 var dir = Path.GetDirectoryName(Path.GetDirectoryName(Environment.CurrentDirectory));
\r
35 // ReSharper disable once AssignNullToNotNullAttribute
\r
36 var path = Path.Combine(dir, Path.Combine("logs", name + ".log.gz"));
\r
37 return new StreamReader(new GZipStream(File.Open(path, FileMode.Open), CompressionMode.Decompress));
\r
40 public static void SniffLogFile(Sniffer sniffer, string name, Action<Sniffer> action = null)
\r
43 using (var stream = OpenLogFile(name))
\r
45 while (!stream.EndOfStream)
\r
47 var triple = new List<string>();
\r
48 foreach (var s in new[] {"url: ", "request: ", "response: "})
\r
50 var line = stream.ReadLine();
\r
53 throw new Exception($"ログのurl, request, responseがそろっていません: {ln:d}行目");
\r
54 if (!line.StartsWith(s))
\r
55 throw new Exception($"ログに不正な行が含まれています: {ln:d}行目");
\r
56 triple.Add(line.Substring(s.Length));
\r
58 var json = DynamicJson.Parse(triple[2]);
\r
59 sniffer.Sniff(triple[0], triple[1], json);
\r
60 action?.Invoke(sniffer);
\r
66 /// 演習で受けたダメージが次の戦闘の結果に影響しない
\r
69 public void DamageInPracticeNotSpillIntoSortie()
\r
71 var expected = new[] {31, 15, 15};
\r
72 var sniffer = new Sniffer();
\r
73 SniffLogFile(sniffer, "battle_001");
\r
74 var result = sniffer.GetShipStatuses(0).Select(s => s.NowHp);
\r
75 PAssert.That(() => (expected.SequenceEqual(result)));
\r
82 public void IgnoreDamagedShipsInPractice()
\r
84 var sniffer = new Sniffer();
\r
85 SniffLogFile(sniffer, "practice_001");
\r
86 PAssert.That(() => !sniffer.Battle.HasDamagedShip);
\r
90 /// 夜戦のダメージを戦闘結果に反映する
\r
93 public void CaptureDamageInNightCombat()
\r
95 var sniffer = new Sniffer();
\r
96 SniffLogFile(sniffer, "battle_002");
\r
97 AssertEqualBattleResult(sniffer, new[] {28, 1, 13});
\r
98 PAssert.That(() => sniffer.Battle.HasDamagedShip);
\r
101 private void AssertEqualBattleResult(Sniffer sniffer, IEnumerable<int> expected, string msg = null)
\r
103 var result = sniffer.GetShipStatuses(0).Select(s => s.NowHp);
\r
104 PAssert.That(() => (expected.SequenceEqual(result)), msg);
\r
108 /// 連合艦隊(水上打撃部隊)による戦闘のダメージを結果に反映する
\r
111 public void CombinedFleetSurface()
\r
113 var sniffer = new Sniffer();
\r
114 SniffLogFile(sniffer, "combined_surface_001");
\r
115 AssertEauqlCombinedResult(sniffer, new[] {40, 77, 77, 33, 51, 47}, new[] {39, 35, 11, 39, 37, 40});
\r
116 PAssert.That(() => !sniffer.Battle.HasDamagedShip);
\r
118 SniffLogFile(sniffer, "combined_surface_002");
\r
119 AssertEauqlCombinedResult(sniffer, new[] {40, 77, 77, 33, 15, 6}, new[] {39, 35, 4, 3, 14, 40});
\r
120 PAssert.That(() => sniffer.Battle.HasDamagedShip);
\r
123 private void AssertEauqlCombinedResult(Sniffer sniffer, IEnumerable<int> expected0, IEnumerable<int> expected1,
\r
126 var result0 = sniffer.GetShipStatuses(0).Select(s => s.NowHp);
\r
127 var result1 = sniffer.GetShipStatuses(1).Select(s => s.NowHp);
\r
128 PAssert.That(() => (expected0.SequenceEqual(result0) && expected1.SequenceEqual(result1)), msg);
\r
132 /// 開幕夜戦のダメージを戦闘結果に反映する
\r
135 public void SpMidnight()
\r
137 var sniffer = new Sniffer();
\r
138 SniffLogFile(sniffer, "sp_midnight_001");
\r
139 AssertEqualBattleResult(sniffer, new[] {1});
\r
140 PAssert.That(() => sniffer.Battle.HasDamagedShip);
\r
144 /// 連合艦隊(空母機動部隊)による戦闘のダメージを結果に反映する
\r
147 public void CombinedFleetAir()
\r
149 var sniffer = new Sniffer();
\r
150 SniffLogFile(sniffer, "combined_air_001");
\r
151 AssertEauqlCombinedResult(sniffer, new[] {40, 98, 90, 66, 78, 86}, new[] {47, 41, 5, 42, 43, 29});
\r
152 PAssert.That(() => sniffer.Battle.HasDamagedShip);
\r
154 SniffLogFile(sniffer, "combined_air_002");
\r
155 AssertEauqlCombinedResult(sniffer, new[] {13, 87, 90, 59, 69, 86}, new[] {47, 41, 5, 20, 43, 29});
\r
162 public void EscapeShip()
\r
164 var sniffer = new Sniffer();
\r
165 SniffLogFile(sniffer, "combined_escape_001");
\r
166 AssertEauqlCombinedResult(sniffer, new[] {37, 105, 106, 90, 66, 10}, new[] {41, 41, 37, 44, 43, 43},
\r
167 "連合艦隊で2戦して大破が出るまで");
\r
168 PAssert.That(() => sniffer.Battle.HasDamagedShip);
\r
169 SniffLogFile(sniffer, "combined_escape_002");
\r
170 PAssert.That(() => sniffer.GetShipStatuses(0)[5].Escaped && sniffer.GetShipStatuses(1)[1].Escaped,
\r
172 PAssert.That(() => !sniffer.Battle.HasDamagedShip);
\r
173 SniffLogFile(sniffer, "combined_escape_003");
\r
174 AssertEauqlCombinedResult(sniffer, new[] {37, 105, 106, 90, 1, 10}, new[] {41, 41, 32, 44, 43, 43},
\r
176 PAssert.That(() => sniffer.Battle.HasDamagedShip);
\r
177 SniffLogFile(sniffer, "combined_escape_004");
\r
178 PAssert.That(() => sniffer.GetShipStatuses(0)[5].Escaped && sniffer.GetShipStatuses(1)[1].Escaped &&
\r
179 sniffer.GetShipStatuses(0)[4].Escaped && sniffer.GetShipStatuses(1)[2].Escaped,
\r
181 PAssert.That(() => !sniffer.Battle.HasDamagedShip);
\r
185 /// 護衛退避した空母を除いた制空値を計算する
\r
188 public void FighterPowerWithoutEscapedShip()
\r
190 var sniffer = new Sniffer();
\r
191 SniffLogFile(sniffer, "combined_escape_001");
\r
192 PAssert.That(() => sniffer.GetFighterPower(0)[0] == 187);
\r
193 SniffLogFile(sniffer, "combined_escape_002");
\r
194 PAssert.That(() => sniffer.GetFighterPower(0)[0] == 65);
\r
198 /// 夜戦の開始時に昼戦の結果を反映する
\r
201 public void ResultOfDayBattleShowInNightBattle()
\r
203 var sniffer = new Sniffer();
\r
204 SniffLogFile(sniffer, "battle_003");
\r
205 AssertEqualBattleResult(sniffer, new[] {28, 2, 13});
\r
206 PAssert.That(() => !sniffer.Battle.HasDamagedShip, "夜戦の開始時は大破警告を出さない");
\r
210 /// 通常艦隊の航空戦のダメージを結果に反映する
\r
213 public void AirBattle()
\r
215 var sniffer = new Sniffer();
\r
216 SniffLogFile(sniffer, "airbattle_001");
\r
217 AssertEqualBattleResult(sniffer, new[] {37, 36, 31, 37, 17, 63}, "夜戦あり");
\r
219 sniffer = new Sniffer();
\r
220 SniffLogFile(sniffer, "airbattle_002");
\r
221 AssertEqualBattleResult(sniffer, new[] {66, 36, 16, 27, 35, 38}, "昼戦のみ");
\r
225 /// 支援射撃による敵の損傷を勝利判定に反映させる
\r
228 public void SupportShellingChangeResultRank()
\r
230 var sniffer = new Sniffer();
\r
231 SniffLogFile(sniffer, "support_001");
\r
232 PAssert.That(() => sniffer.Battle.ResultRank == BattleResultRank.A);
\r
236 /// ダメコンの使用を戦闘結果に反映させる
\r
239 public void DamageControl()
\r
241 var sniffer = new Sniffer();
\r
242 SniffLogFile(sniffer, "damecon_001");
\r
243 AssertEqualBattleResult(sniffer, new[] {30, 1, 3}, "戦闘前");
\r
244 PAssert.That(() => sniffer.GetShipStatuses(0)[1].Slot.Select(item => item.Id).
\r
245 SequenceEqual(new[] {2, 4593, -1, -1, -1}), "ダメコンを二つ装備");
\r
246 PAssert.That(() => sniffer.Battle.ResultRank == BattleResultRank.S, "判定はS勝利");
\r
247 SniffLogFile(sniffer, "damecon_002");
\r
248 AssertEqualBattleResult(sniffer, new[] {30, 1, 3}, "戦闘後");
\r
249 PAssert.That(() => sniffer.GetShipStatuses(0)[1].Slot.Select(item => item.Id).
\r
250 SequenceEqual(new[] {-1, 4593, -1, -1, -1}), "ダメコンを一つ消費");
\r
254 /// 補強装備スロットのダメコンの使用を反映する
\r
257 public void DamgeControlInSlotEx()
\r
259 var sniffer = new Sniffer();
\r
260 SniffLogFile(sniffer, "damecon_003");
\r
261 PAssert.That(() => sniffer.GetShipStatuses(0)[5].SlotEx.Id == 10306, "戦闘前");
\r
262 SniffLogFile(sniffer, "damecon_004");
\r
263 PAssert.That(() => sniffer.GetShipStatuses(0)[5].SlotEx.Id == -1, "戦闘後");
\r
267 /// 連合艦隊(水上打撃部隊)で二回目の砲撃戦がない場合を正しく処理する
\r
270 // ReSharper disable once InconsistentNaming
\r
271 public void CombinedFleetSurfaceWithout2ndShelling()
\r
273 var sniffer = new Sniffer();
\r
274 SniffLogFile(sniffer, "combined_surface_003");
\r
275 PAssert.That(() => sniffer.Battle.ResultRank == BattleResultRank.P);
\r
279 /// 連合艦隊(水上打撃部隊)で開幕雷撃で戦闘が終わる場合を正しく処理する
\r
282 // ReSharper disable once InconsistentNaming
\r
283 public void CombinedFleetSurfaceWithoutAnyShelling()
\r
285 var sniffer = new Sniffer();
\r
286 SniffLogFile(sniffer, "combined_surface_004");
\r
287 PAssert.That(() => sniffer.Battle.ResultRank == BattleResultRank.P);
\r
291 /// ship2に代わるship_deckを処理する
\r
294 public void ShipDeck()
\r
296 var sniffer = new Sniffer();
\r
297 SniffLogFile(sniffer, "ship_deck_001");
\r
298 PAssert.That(() => sniffer.GetShipStatuses(0)[0].Fuel == 36);
\r
302 /// 夜戦かどうかを選択する画面でリロードしても結果を次の戦闘に持ち越さない
\r
305 public void ReloadBeforeBattleResult()
\r
307 var sniffer = new Sniffer();
\r
308 SniffLogFile(sniffer, "reload_001");
\r
309 PAssert.That(() => sniffer.GetShipStatuses(0)[0].NowHp == 41);
\r
313 /// 航空戦のない演習を正しく処理する
\r
316 public void PracticeWithoutAirBattle()
\r
318 var sniffer = new Sniffer();
\r
319 SniffLogFile(sniffer, "practice_002");
\r
320 PAssert.That(() => sniffer.Battle.AirControlLevel == -1);
\r
324 /// 熟練度込みの制空値を正しく計算する
\r
327 public void FighterPowerWithBonus()
\r
329 var sniffer = new Sniffer();
\r
330 SniffLogFile(sniffer, "fighterpower_001");
\r
331 PAssert.That(() => sniffer.GetFighterPower(0).SequenceEqual(new [] {156, 159}));
\r
335 /// 2-5をクリアしたときの特別戦果を反映する
\r
338 public void ExMapBattleResult()
\r
340 var sniffer0 = new Sniffer();
\r
341 sniffer0.ExMap.ClearClearStatus();
\r
342 SniffLogFile(sniffer0, "eo_001");
\r
343 PAssert.That(() => sniffer0.ExMap.Achievement == 100, "ほかのマップの情報なし");
\r
344 var sniffer1 = new Sniffer();
\r
345 SniffLogFile(sniffer1, "eo_001");
\r
346 // すでに3-5をクリアしているので合計で250
\r
347 PAssert.That(() => sniffer1.ExMap.Achievement == 250, "ほかのマップの情報あり");
\r
351 /// 1-6をクリアしたときの特別戦果を反映する
\r
354 public void ExMapMapNext()
\r
356 var sniffer0 = new Sniffer();
\r
357 sniffer0.ExMap.ClearClearStatus();
\r
358 SniffLogFile(sniffer0, "eo_002");
\r
359 PAssert.That(() => sniffer0.ExMap.Achievement == 75, "ほかのマップの情報なし");
\r
360 var sniffer1 = new Sniffer();
\r
361 SniffLogFile(sniffer1, "eo_002");
\r
362 // 5-5以外クリアしているので合計で400
\r
363 PAssert.That(() => sniffer1.ExMap.Achievement == 400, "ほかのマップの情報あり");
\r
367 /// 編成で空き番号を使ったローテートを正しく反映する
\r
370 public void RotateFleetMember()
\r
372 var sniffer = new Sniffer();
\r
373 SniffLogFile(sniffer, "deck_001");
\r
374 var result = sniffer.GetDeck(0);
\r
375 PAssert.That(() => new[] {756, 17204, 6156, 28806, 1069, -1}.SequenceEqual(result));
\r
379 /// 編成で艦隊に配置ずみの艦娘を交換する
\r
382 public void ExchangeFleetMember()
\r
384 var sniffer = new Sniffer();
\r
386 SniffLogFile(sniffer, "deck_002");
\r
387 var result0 = sniffer.GetDeck(0);
\r
388 PAssert.That(() => new[] {1069, 6156, 756, 3223, -1, -1}.SequenceEqual(result0), "編成で艦隊内で艦娘と交換する");
\r
390 SniffLogFile(sniffer, "deck_003");
\r
391 var result10 = sniffer.GetDeck(0);
\r
392 var result11 = sniffer.GetDeck(1);
\r
393 PAssert.That(() => new[] {1069, 6156, 14258, 3223, -1, -1}.SequenceEqual(result10) &&
\r
394 new[] {101, 4487, 756, 14613, 28806, -1}.SequenceEqual(result11), "002に続いて艦隊をまたがって交換する");
\r
396 SniffLogFile(sniffer, "deck_004");
\r
397 var result20 = sniffer.GetDeck(0);
\r
398 var result21 = sniffer.GetDeck(1);
\r
399 PAssert.That(() => new[] {1069, 6156, 14258, 3223, 756, -1}.SequenceEqual(result20) &&
\r
400 new[] {101, 4487, 14613, 28806, -1, -1}.SequenceEqual(result21),
\r
401 "003に続いて空き番号にほかの艦隊の艦娘を配置する");
\r
408 public void PresetSelect()
\r
410 var sniffer = new Sniffer();
\r
411 SniffLogFile(sniffer, "preset_001");
\r
412 var result = sniffer.GetDeck(0);
\r
413 PAssert.That(() => new[] {50510, 632, 39843, 113, 478, 47422}.SequenceEqual(result));
\r
420 public void SlotExchange()
\r
422 var sniffer = new Sniffer();
\r
423 SniffLogFile(sniffer, "slot_exchange_001");
\r
424 var result = sniffer.GetShipStatuses(0)[0].Slot.Select(item => item.Id);
\r
425 PAssert.That(() => new[] {26096, 30571, 77694, 61383, -1}.SequenceEqual(result));
\r
429 /// 改修による資材の減少をすぐに反映する
\r
432 public void ConsumptionByRemodelSlot()
\r
434 var sniffer = new Sniffer();
\r
435 SniffLogFile(sniffer, "remodel_slot_001");
\r
436 PAssert.That(() => sniffer.Material.Current
\r
437 .SequenceEqual(new[] {25292, 25570, 25244, 41113, 1405, 1525, 2137, 8}));
\r
444 public void CountItem()
\r
446 var sniffer = new Sniffer();
\r
447 SniffLogFile(sniffer, "createitem_001");
\r
448 PAssert.That(() => sniffer.Item.NowEquips == 606);
\r
455 public void MaterialVariation()
\r
457 var sniffer1 = new Sniffer();
\r
458 var result1 = new List<int[]>();
\r
459 SniffLogFile(sniffer1, "material_001", sn =>
\r
461 var cur = sn.Material.Current;
\r
462 if (result1.Count == 0)
\r
468 if (!result1.Last().SequenceEqual(cur))
\r
472 var expected1 = new List<int[]>
\r
474 new[] {0, 0, 0, 0, 0, 0, 0, 0},
\r
475 new[] {26178, 26742, 21196, 33750, 1426, 1574, 2185, 10},
\r
476 new[] {26178, 26842, 21226, 33750, 1426, 1574, 2185, 10},
\r
477 new[] {28951, 29493, 24945, 35580, 1426, 1574, 2185, 10},
\r
478 new[] {26074, 26616, 21068, 33700, 1426, 1572, 2183, 10},
\r
479 new[] {26171, 26721, 21175, 33750, 1426, 1574, 2185, 10},
\r
480 new[] {27023, 27829, 28136, 42404, 1404, 1521, 2142, 15},
\r
481 new[] {31208, 29819, 29714, 42345, 1407, 1530, 2155, 13},
\r
482 new[] {24595, 25353, 18900, 32025, 1427, 1576, 2187, 10},
\r
483 new[] {24515, 25353, 18749, 32025, 1427, 1575, 2187, 10},
\r
484 new[] {23463, 24964, 17284, 31765, 1427, 1572, 2187, 10},
\r
485 new[] {23463, 25064, 17314, 31765, 1427, 1572, 2187, 10}
\r
487 PAssert.That(() => SequenceOfSequenceEqual(expected1, result1));
\r
489 var sniffer2 = new Sniffer();
\r
490 var result2 = new List<int[]>();
\r
491 SniffLogFile(sniffer2, "material_002", sn =>
\r
493 var cur = sn.Material.Current;
\r
494 if (result2.Count == 0)
\r
500 if (!result2.Last().SequenceEqual(cur))
\r
504 var expected2 = new List<int[]>
\r
506 new[] {0, 0, 0, 0, 0, 0, 0, 0},
\r
507 new[] {201649, 189713, 261490, 123227, 2743, 2828, 3000, 44},
\r
508 new[] {201649, 189714, 261491, 123227, 2743, 2828, 3000, 44},
\r
509 new[] {201650, 189718, 261500, 123227, 2743, 2828, 3000, 44}
\r
511 PAssert.That(() => SequenceOfSequenceEqual(expected2, result2));
\r
514 private bool SequenceOfSequenceEqual<T>(IEnumerable<IEnumerable<T>> a, IEnumerable<IEnumerable<T>> b)
\r
516 var aa = a.ToArray();
\r
517 var bb = b.ToArray();
\r
518 if (aa.Length != bb.Length)
\r
520 return aa.Zip(bb, (x, y) => x.SequenceEqual(y)).All(x => x);
\r
527 public void MapInfo()
\r
529 var sniffer = new Sniffer();
\r
530 SniffLogFile(sniffer, "mapinfo_001");
\r
531 PAssert.That(() => sniffer.MiscText ==
\r
533 "1-6 : 撃破 3/7\r\n5-5 : 撃破 0/5\r\n6-3 : 撃破 0/4\r\n" +
\r
534 "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
536 SniffLogFile(sniffer, "mapinfo_002");
\r
537 PAssert.That(() => sniffer.MiscText ==
\r
538 "[海域ゲージ情報]\r\n 海域選択画面に進むと表示します。\r\n[演習情報]\r\n 演習相手を選ぶと表示します。",
\r
540 SniffLogFile(sniffer, "mapinfo_003");
\r
541 PAssert.That(() => sniffer.MiscText ==
\r
543 "1-6 : 撃破 4/7\r\n5-5 : 撃破 0/5\r\n6-3 : 撃破 0/4\r\n" +
\r
544 "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
552 public void PractiveEnemyInfo()
\r
554 var sniffer = new Sniffer();
\r
555 SniffLogFile(sniffer, "practice_enemyinfo_001");
\r
556 PAssert.That(() => sniffer.MiscText ==
\r
557 "[演習情報]\r\n敵艦隊名 : 大銀河地球連邦 第7艦隊\r\n獲得経験値 : 570\r\nS勝利 : 684",
\r
559 SniffLogFile(sniffer, "practice_enemyinfo_002");
\r
560 PAssert.That(() => sniffer.MiscText ==
\r
561 "[海域ゲージ情報]\r\n 海域選択画面に進むと表示します。\r\n[演習情報]\r\n 演習相手を選ぶと表示します。",
\r