OSDN Git Service

対空噴進弾幕の発動率が100%以上だと一覧の対空でエラーになるのを直す
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / View / RepairShipCount.cs
1 // Copyright (C) 2019 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 //\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
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\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
14 \r
15 using System.Collections.Generic;\r
16 using System.Linq;\r
17 using KancolleSniffer.Model;\r
18 \r
19 namespace KancolleSniffer.View\r
20 {\r
21     public class RepairShipCount\r
22     {\r
23         private const int Minor = (int)ShipStatus.Damage.Minor;\r
24         private const int Small = (int)ShipStatus.Damage.Small;\r
25         private const int Half = (int)ShipStatus.Damage.Half;\r
26         private const int Badly = (int)ShipStatus.Damage.Badly;\r
27 \r
28         private readonly int[] _counts = new int[typeof(ShipStatus.Damage).GetEnumValues().Length];\r
29         private readonly List<string> _result = new List<string>();\r
30 \r
31         public RepairShipCount(ShipStatus[] repairList)\r
32         {\r
33             foreach (var ship in repairList)\r
34                 _counts[(int)ship.DamageLevel]++;\r
35         }\r
36 \r
37         public override string ToString()\r
38         {\r
39             if (_counts.All(n => n == 0))\r
40                 return "なし";\r
41             CountTwoLevels(Minor);\r
42             CountTwoLevels(Half);\r
43             return string.Join("\r\n", _result);\r
44         }\r
45 \r
46         private void CountTwoLevels(int level)\r
47         {\r
48             FormatCount(level);\r
49             FormatCount(level + 1);\r
50             if (EitherNonZero(_counts[level], _counts[level + 1]) && ShowSum)\r
51                 _result.Add($" 計 {_counts[level] + _counts[level + 1]}");\r
52         }\r
53 \r
54         private readonly string[] _damageString = {"軽微", "小破", "中破", "大破"};\r
55 \r
56         private void FormatCount(int level)\r
57         {\r
58             if (_counts[level] > 0)\r
59                 _result.Add($"{_damageString[level]} {_counts[level]}");\r
60         }\r
61 \r
62         private bool ShowSum =>\r
63             BothNonZero(_counts[Minor], _counts[Small]) || BothNonZero(_counts[Half], _counts[Badly]);\r
64 \r
65         private static bool BothNonZero(int a, int b) => a * b > 0;\r
66 \r
67         private static bool EitherNonZero(int a, int b) => a + b > 0;\r
68     }\r
69 }