OSDN Git Service

f1f8871e981d3063d7d5854903afb791316de113
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Model / QuestCounter.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;\r
16 using System.Collections.Generic;\r
17 using System.Linq;\r
18 using System.Xml.Serialization;\r
19 using KancolleSniffer.Util;\r
20 \r
21 namespace KancolleSniffer.Model\r
22 {\r
23     public class QuestCount\r
24     {\r
25         public int Id { get; set; }\r
26         public int Now { get; set; }\r
27         public int[] NowArray { get; set; }\r
28 \r
29         [XmlIgnore]\r
30         public QuestSpec Spec { get; set; }\r
31 \r
32         public bool AdjustCount(int progress)\r
33         {\r
34             if (!Spec.AdjustCount)\r
35                 return false;\r
36             if (NowArray != null)\r
37             {\r
38                 if (progress != 100)\r
39                     return false;\r
40                 NowArray = NowArray.Zip(Spec.MaxArray, Math.Max).ToArray();\r
41                 return true;\r
42             }\r
43             var next = 0;\r
44             switch (progress)\r
45             {\r
46                 case 0:\r
47                     next = 50;\r
48                     break;\r
49                 case 50:\r
50                     next = 80;\r
51                     break;\r
52                 case 80:\r
53                     next = 100;\r
54                     break;\r
55                 case 100:\r
56                     next = 100000;\r
57                     break;\r
58             }\r
59             var now = Now + Spec.Shift;\r
60             var max = Spec.Max + Spec.Shift;\r
61             var low = (int)Math.Ceiling(max * progress / 100.0);\r
62             if (low >= max && progress != 100)\r
63                 low = max - 1;\r
64             var high = (int)Math.Ceiling(max * next / 100.0);\r
65             if (now < low)\r
66             {\r
67                 Now = low - Spec.Shift;\r
68                 return true;\r
69             }\r
70             if (now >= high)\r
71             {\r
72                 Now = high - 1 - Spec.Shift;\r
73                 return true;\r
74             }\r
75             return false;\r
76         }\r
77 \r
78         public override string ToString()\r
79         {\r
80             return Spec.MaxArray != null && Spec.MaxArray.All(x => x == 1)\r
81                 ? string.Join("\u200a", NowArray.Select(n => (n % 10).ToString()))\r
82                 : Spec.MaxArray != null\r
83                     ? string.Join(" ", NowArray.Zip(Spec.MaxArray, (n, m) => $"{n}/{m}"))\r
84                     : $"{Now}/{Spec.Max}";\r
85         }\r
86 \r
87         public QuestCount Clone()\r
88         {\r
89             var clone = (QuestCount)MemberwiseClone();\r
90             if (NowArray != null)\r
91                 clone.NowArray = (int[])NowArray.Clone();\r
92             return clone;\r
93         }\r
94 \r
95         public bool Equals(QuestCount other)\r
96         {\r
97             if (Id != other.Id)\r
98                 return false;\r
99             if (NowArray == null)\r
100                 return Now == other.Now;\r
101             return NowArray.SequenceEqual(other.NowArray);\r
102         }\r
103 \r
104         private static string MapString(int map)\r
105         {\r
106             return map switch\r
107             {\r
108                 721 => "7-2G",\r
109                 722 => "7-2M",\r
110                 _ => $"{map / 10}-{map % 10}"\r
111             };\r
112         }\r
113 \r
114         public string ToToolTip()\r
115         {\r
116             if (NowArray == null)\r
117                 return "";\r
118             if (Spec is QuestSortie spec && spec.Maps != null && spec.MaxArray != null)\r
119             {\r
120                 return string.Join(" ", spec.Maps.Zip(NowArray, (map, n) => $"{MapString(map)}:{n}"));\r
121             }\r
122             return string.Join(" ", (Id switch\r
123             {\r
124                 426 => new[] {"警備任務", "対潜警戒任務", "海上護衛任務", "強硬偵察任務"},\r
125                 428 => new[] {"対潜警戒任務", "海峡警備行動", "長時間対潜警戒"},\r
126                 436 => new[] {"練習航海", "長距離練習航海", "警備任務", "対潜警戒任務", "強行偵察任務"},\r
127                 688 => new[] {"艦戦", "艦爆", "艦攻", "水偵"},\r
128                 _ => new string[0]\r
129             }).Zip(NowArray, (entry, n) => $"{entry}{n}"));\r
130         }\r
131 \r
132         public bool Cleared => NowArray?.Zip(Spec.MaxArray, (n, m) => n >= m).All(x => x) ??\r
133                                Spec.Max != 0 && Now >= Spec.Max;\r
134     }\r
135 \r
136     public class QuestCounter\r
137     {\r
138         private readonly QuestInfo _questInfo;\r
139         private readonly ItemInfo _itemInfo;\r
140         private readonly BattleInfo _battleInfo;\r
141         private readonly SortedDictionary<int, QuestStatus> _quests;\r
142         private int _map;\r
143         private bool _boss;\r
144 \r
145         private class ResultShipSpecs\r
146         {\r
147             public ShipSpec[] Specs { get; }\r
148             public NameChecker Names { get; }\r
149             public int[] Types { get; }\r
150             public int[] Classes { get; }\r
151             public ShipSpec Flagship { get; }\r
152             public int FlagshipType { get; }\r
153 \r
154             public class NameChecker\r
155             {\r
156                 private readonly string[] _names;\r
157 \r
158                 public NameChecker(ShipSpec[] specs)\r
159                 {\r
160                     _names = specs.Select(spec => spec.Name).ToArray();\r
161                 }\r
162 \r
163                 public bool Contains(string demand)\r
164                 {\r
165                     return _names.Any(name => name.StartsWith(demand));\r
166                 }\r
167 \r
168                 public int Count(params string[] demands)\r
169                 {\r
170                     return demands.Sum(demand => _names.Count(name => name.StartsWith(demand)));\r
171                 }\r
172             }\r
173 \r
174             public ResultShipSpecs(BattleInfo battleInfo)\r
175             {\r
176                 Specs = battleInfo.Result?.Friend.Main.Where(s => s.NowHp > 0).Select(ship => ship.Spec).ToArray() ??\r
177                         new ShipSpec[0];\r
178                 Names = new NameChecker(Specs);\r
179                 Types = Specs.Select(spec => spec.ShipType).ToArray();\r
180                 Classes = Specs.Select(spec => spec.ShipClass).ToArray();\r
181                 Flagship = Specs.FirstOrDefault();\r
182                 FlagshipType = Types.FirstOrDefault();\r
183             }\r
184         }\r
185 \r
186         public QuestCounter(QuestInfo questInfo, ItemInfo itemInfo, BattleInfo battleInfo)\r
187         {\r
188             _questInfo = questInfo;\r
189             _quests = questInfo.QuestDictionary;\r
190             _itemInfo = itemInfo;\r
191             _battleInfo = battleInfo;\r
192         }\r
193 \r
194         private bool NeedSave\r
195         {\r
196             set => _questInfo.NeedSave = value;\r
197         }\r
198 \r
199         public void InspectMapStart(dynamic json)\r
200         {\r
201             if (_quests.TryGetValue(214, out var ago)) // あ号\r
202                 ago.Count.NowArray[0]++;\r
203             InspectMapNext(json);\r
204         }\r
205 \r
206         public void InspectMapNext(dynamic json)\r
207         {\r
208             _map = (int)json.api_maparea_id * 10 + (int)json.api_mapinfo_no;\r
209             if (_map == 72)\r
210             {\r
211                 var cell = json.api_no() ? (int)json.api_no : 0;\r
212                 _map *= 10;\r
213                 if (cell == 7)\r
214                     _map++;\r
215                 if (cell == 15)\r
216                     _map += 2;\r
217             }\r
218             _boss = (int)json.api_event_id == 5;\r
219 \r
220             if (_map != 16 || (int)json.api_event_id != 8)\r
221                 return;\r
222             foreach (var count in _quests.Values.Select(q => q.Count))\r
223             {\r
224                 if (!(count.Spec is QuestSortie sortie))\r
225                     continue;\r
226                 if (!FleetCheck(count.Id))\r
227                     continue;\r
228                 if (sortie.Count(count, "S", _map, true))\r
229                     NeedSave = true;\r
230             }\r
231         }\r
232 \r
233         public void InspectBattleResult(dynamic json)\r
234         {\r
235             var rank = json.api_win_rank;\r
236             foreach (var count in _quests.Values.Select(q => q.Count))\r
237             {\r
238                 switch (count.Spec)\r
239                 {\r
240                     case QuestSortie sortie:\r
241                         if (!FleetCheck(count.Id))\r
242                             continue;\r
243                         if (!_boss && count.Id == 216)\r
244                         {\r
245                             Increment(count);\r
246                             continue;\r
247                         }\r
248                         if (sortie.Count(count, rank, _map, _boss))\r
249                             NeedSave = true;\r
250                         continue;\r
251                     case QuestEnemyType enemyType:\r
252                         var num = enemyType.CountResult(\r
253                             _battleInfo.Result.Enemy.Main.Concat(_battleInfo.Result.Enemy.Guard));\r
254                         if (num > 0)\r
255                             Add(count, num);\r
256                         continue;\r
257                 }\r
258                 if (count.Id == 214)\r
259                     CountAgo(count, rank);\r
260             }\r
261         }\r
262 \r
263         private void CountAgo(QuestCount count, string rank)\r
264         {\r
265             if (QuestSortie.CompareRank(rank, "S") == 0)\r
266                 IncrementNth(count, 1);\r
267             if (!_boss)\r
268                 return;\r
269             IncrementNth(count, 2);\r
270             if (QuestSortie.CompareRank(rank, "B") <= 0)\r
271                 IncrementNth(count, 3);\r
272         }\r
273 \r
274         private bool FleetCheck(int id)\r
275         {\r
276             var specs = new ResultShipSpecs(_battleInfo);\r
277             switch (id)\r
278             {\r
279                 case 249:\r
280                     return specs.Names.Count("妙高", "那智", "羽黒") == 3;\r
281                 case 257:\r
282                     return specs.FlagshipType == 3 && specs.Types.Count(s => s == 3) <= 3 &&\r
283                            specs.Types.All(s => s == 2 || s == 3);\r
284                 case 259:\r
285                     return specs.Types.Count(type => type == 3) > 0 && specs.Classes.Count(c => new[]\r
286                     {\r
287                         2, // 伊勢型\r
288                         19, // 長門型\r
289                         26, // 扶桑型\r
290                         37 // 大和型\r
291                     }.Contains(c)) == 3;\r
292                 case 264:\r
293                     return specs.Types.Count(type => type == 2) >= 2 &&\r
294                            specs.Specs.Count(spec => spec.IsAircraftCarrier) >= 2;\r
295                 case 266:\r
296                     return specs.FlagshipType == 2 &&\r
297                            specs.Types.OrderBy(x => x).SequenceEqual(new[] {2, 2, 2, 2, 3, 5});\r
298                 case 280:\r
299                 case 284:\r
300                     return specs.Types.Count(type => type == 1 || type == 2) >= 3 &&\r
301                            specs.Types.Intersect(new[] {3, 4, 7, 21}).Any();\r
302                 case 861:\r
303                     return specs.Types.Count(s => s == 10 || s == 22) == 2;\r
304                 case 862:\r
305                     return specs.Types.Count(s => s == 3) >= 2 && specs.Types.Count(s => s == 16) >= 1;\r
306                 case 873:\r
307                     return specs.Types.Count(type => type == 3) >= 1;\r
308                 case 875:\r
309                     return specs.Names.Contains("長波改二") &&\r
310                            specs.Names.Count("朝霜改", "高波改", "沖波改") > 0;\r
311                 case 888:\r
312                     return specs.Names.Count("鳥海", "青葉", "衣笠", "加古", "古鷹", "天龍", "夕張") >= 4;\r
313                 case 894:\r
314                     return specs.Specs.Any(spec => spec.IsAircraftCarrier);\r
315                 case 903:\r
316                     return specs.Flagship.Name.StartsWith("夕張改二") &&\r
317                            (specs.Names.Count("睦月", "如月", "弥生", "卯月", "菊月", "望月") >= 2 || specs.Names.Contains("由良改二"));\r
318                 case 904:\r
319                     return specs.Names.Count("綾波改二", "敷波改二") == 2;\r
320                 case 905:\r
321                     return specs.Types.Count(type => type == 1) >= 3 && specs.Types.Length <= 5;\r
322                 case 912:\r
323                     return specs.Flagship.Name.StartsWith("明石") && specs.Types.Count(type => type == 2) >= 3;\r
324                 case 914:\r
325                     return specs.Types.Count(type => type == 5) >= 3 && specs.Types.Count(type => type == 2) >= 1;\r
326                 case 318:\r
327                     return specs.Types.Count(type => type == 3) >= 2;\r
328                 case 330:\r
329                     return specs.Flagship.IsAircraftCarrier &&\r
330                            specs.Specs.Count(spec => spec.IsAircraftCarrier) >= 2 &&\r
331                            specs.Types.Count(type => type == 2) >= 2;\r
332                 case 337:\r
333                     return specs.Names.Count("陽炎", "不知火", "霰", "霞") == 4;\r
334                 case 339:\r
335                     return specs.Names.Count("磯波", "浦波", "綾波", "敷波") == 4;\r
336                 default:\r
337                     return true;\r
338             }\r
339         }\r
340 \r
341         private int _questFleet;\r
342 \r
343         public void StartPractice(string request)\r
344         {\r
345             var values = HttpUtility.ParseQueryString(request);\r
346             _questFleet = int.Parse(values["api_deck_id"]) - 1;\r
347         }\r
348 \r
349         public void InspectPracticeResult(dynamic json)\r
350         {\r
351             foreach (var count in _quests.Values.Select(q => q.Count))\r
352             {\r
353                 if (!FleetCheck(count.Id))\r
354                     continue;\r
355                 if (count.Id == 318 && _questFleet != 0)\r
356                     continue;\r
357                 if (!(count.Spec is QuestPractice practice))\r
358                     continue;\r
359                 if (practice.Check(json.api_win_rank))\r
360                     Increment(count);\r
361             }\r
362         }\r
363 \r
364         private readonly int[] _missionId = new int[ShipInfo.FleetCount];\r
365 \r
366         public void InspectDeck(dynamic json)\r
367         {\r
368             foreach (var entry in json)\r
369                 _missionId[(int)entry.api_id - 1] = (int)entry.api_mission[1];\r
370         }\r
371 \r
372         public void InspectMissionResult(string request, dynamic json)\r
373         {\r
374             var values = HttpUtility.ParseQueryString(request);\r
375             var deck = int.Parse(values["api_deck_id"]);\r
376             if ((int)json.api_clear_result == 0)\r
377                 return;\r
378             var mid = _missionId[deck - 1];\r
379             foreach (var count in _quests.Values.Select(q => q.Count))\r
380             {\r
381                 if (!(count.Spec is QuestMission mission))\r
382                     continue;\r
383                 if (mission.Count(count, mid))\r
384                     NeedSave = true;\r
385             }\r
386         }\r
387 \r
388         public void CountNyukyo() => Increment(503);\r
389 \r
390         public void CountCharge() => Increment(504);\r
391 \r
392         public void InspectCreateItem(string request)\r
393         {\r
394             var values = HttpUtility.ParseQueryString(request);\r
395             var count = values["api_multiple_flag"] == "1" ? 3 : 1;\r
396             Add(605, count);\r
397             Add(607, count);\r
398         }\r
399 \r
400         public void CountCreateShip()\r
401         {\r
402             Increment(606);\r
403             Increment(608);\r
404         }\r
405 \r
406         public void InspectDestroyShip(string request)\r
407         {\r
408             Add(609, HttpUtility.ParseQueryString(request)["api_ship_id"].Split(',').Length);\r
409         }\r
410 \r
411         public void CountRemodelSlot() => Increment(619);\r
412 \r
413         public void InspectDestroyItem(string request, dynamic json)\r
414         {\r
415             var values = HttpUtility.ParseQueryString(request);\r
416             var items = values["api_slotitem_ids"].Split(',')\r
417                 .Select(id => _itemInfo.GetStatus(int.Parse(id)).Spec).ToArray();\r
418             Increment(613); // 613: 資源の再利用\r
419             foreach (var quest in _quests.Values)\r
420             {\r
421                 var count = quest.Count;\r
422                 if (count.Spec is QuestDestroyItem destroy)\r
423                 {\r
424                     if (destroy.Count(count, items))\r
425                         NeedSave = true;\r
426                     continue;\r
427                 }\r
428                 if (quest.Id == 680)\r
429                 {\r
430                     count.NowArray[0] += items.Count(spec => spec.Type == 21);\r
431                     count.NowArray[1] += items.Count(spec => spec.Type == 12 || spec.Type == 13);\r
432                     NeedSave = true;\r
433                 }\r
434             }\r
435         }\r
436 \r
437         public void InspectPowerUp(dynamic json)\r
438         {\r
439             if ((int)json.api_powerup_flag == 0)\r
440                 return;\r
441             foreach (var quest in _quests.Values)\r
442             {\r
443                 var count = quest.Count;\r
444                 if (!(count.Spec is QuestPowerUp))\r
445                     continue;\r
446                 Increment(count);\r
447             }\r
448         }\r
449 \r
450         public void Increment(QuestCount count)\r
451         {\r
452             Add(count, 1);\r
453         }\r
454 \r
455         public void Add(QuestCount count, int value)\r
456         {\r
457             count.Now += value;\r
458             NeedSave = true;\r
459         }\r
460 \r
461         public void Increment(int id)\r
462         {\r
463             Add(id, 1);\r
464         }\r
465 \r
466         public void Add(int id, int value)\r
467         {\r
468             if (!_quests.TryGetValue(id, out var quest))\r
469                 return;\r
470             quest.Count.Now += value;\r
471             NeedSave = true;\r
472         }\r
473 \r
474         public void IncrementNth(QuestCount count, int n)\r
475         {\r
476             count.NowArray[n]++;\r
477             NeedSave = true;\r
478         }\r
479     }\r
480 }