OSDN Git Service

歴戦「第十方面艦隊」、全力出撃!のカウンターに編成チェックがないのを直す
[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             switch (Spec)\r
119             {\r
120                 case QuestSortie sortie when sortie.Maps != null && sortie.MaxArray != null:\r
121                     return string.Join(" ", sortie.Maps.Zip(NowArray, (map, n) => $"{MapString(map)}:{n}"));\r
122                 case QuestMission mission when mission.Ids != null:\r
123                     return string.Join(" ", mission.Names.Zip(NowArray, (name, n) => $"{name}{n}"));\r
124                 default:\r
125                     return string.Join(" ", (Id switch\r
126                     {\r
127                         688 => new[] {"艦戦", "艦爆", "艦攻", "水偵"},\r
128                         _ => new string[0]\r
129                     }).Zip(NowArray, (entry, n) => $"{entry}{n}"));\r
130             }\r
131         }\r
132 \r
133         public bool Cleared => NowArray?.Zip(Spec.MaxArray, (n, m) => n >= m).All(x => x) ??\r
134                                Spec.Max != 0 && Now >= Spec.Max;\r
135     }\r
136 \r
137     public class QuestCounter\r
138     {\r
139         private readonly QuestInfo _questInfo;\r
140         private readonly ItemInventory _itemInventory;\r
141         private readonly ShipInventory _shipInventory;\r
142         private readonly BattleInfo _battleInfo;\r
143         private readonly SortedDictionary<int, QuestStatus> _quests;\r
144         private int _map;\r
145         private bool _boss;\r
146 \r
147         private class ResultShipSpecs\r
148         {\r
149             public ShipSpec[] Specs { get; }\r
150             public NameChecker Names { get; }\r
151             public int[] Types { get; }\r
152             public int[] Classes { get; }\r
153             public ShipSpec Flagship { get; }\r
154             public int FlagshipType { get; }\r
155 \r
156             public class NameChecker\r
157             {\r
158                 private readonly string[] _names;\r
159 \r
160                 public NameChecker(ShipSpec[] specs)\r
161                 {\r
162                     _names = specs.Select(spec => spec.Name).ToArray();\r
163                 }\r
164 \r
165                 public bool Contains(string demand)\r
166                 {\r
167                     return _names.Any(name => name.StartsWith(demand));\r
168                 }\r
169 \r
170                 public int Count(params string[] demands)\r
171                 {\r
172                     return demands.Sum(demand => _names.Count(name => name.StartsWith(demand)));\r
173                 }\r
174             }\r
175 \r
176             public ResultShipSpecs(BattleInfo battleInfo)\r
177             {\r
178                 Specs = battleInfo.Result?.Friend.Main.Where(s => s.NowHp > 0).Select(ship => ship.Spec).ToArray() ??\r
179                         new ShipSpec[0];\r
180                 Names = new NameChecker(Specs);\r
181                 Types = Specs.Select(spec => spec.ShipType).ToArray();\r
182                 Classes = Specs.Select(spec => spec.ShipClass).ToArray();\r
183                 Flagship = Specs.FirstOrDefault();\r
184                 FlagshipType = Types.FirstOrDefault();\r
185             }\r
186         }\r
187 \r
188         public QuestCounter(QuestInfo questInfo, ItemInventory itemInventory, ShipInventory shipInventory, BattleInfo battleInfo)\r
189         {\r
190             _questInfo = questInfo;\r
191             _quests = questInfo.QuestDictionary;\r
192             _itemInventory = itemInventory;\r
193             _shipInventory = shipInventory;\r
194             _battleInfo = battleInfo;\r
195         }\r
196 \r
197         private bool NeedSave\r
198         {\r
199             set => _questInfo.NeedSave = value;\r
200         }\r
201 \r
202         public void InspectMapStart(dynamic json)\r
203         {\r
204             if (_quests.TryGetValue(214, out var ago)) // あ号\r
205                 ago.Count.NowArray[0]++;\r
206             InspectMapNext(json);\r
207         }\r
208 \r
209         public void InspectMapNext(dynamic json)\r
210         {\r
211             _map = (int)json.api_maparea_id * 10 + (int)json.api_mapinfo_no;\r
212             var cell = json.api_no() ? (int)json.api_no : 0;\r
213             if (_map == 72)\r
214             {\r
215                 if (cell == 7)\r
216                     _map = 721;\r
217                 else if (cell == 15)\r
218                     _map = 722;\r
219             }\r
220             if (_map == 73)\r
221             {\r
222                 if (cell == 5)\r
223                     _map = 731;\r
224                 else if (cell == 18)\r
225                     _map = 732;\r
226             }\r
227             _boss = (int)json.api_event_id == 5;\r
228 \r
229             if (_map != 16 || (int)json.api_event_id != 8)\r
230                 return;\r
231             foreach (var count in _quests.Values.Select(q => q.Count))\r
232             {\r
233                 if (!(count.Spec is QuestSortie sortie) || sortie.Maps == null)\r
234                     continue;\r
235                 if (!FleetCheck(count.Id))\r
236                     continue;\r
237                 if (sortie.Count(count, "S", _map, true))\r
238                     NeedSave = true;\r
239             }\r
240         }\r
241 \r
242         public void InspectBattleResult(dynamic json)\r
243         {\r
244             var rank = json.api_win_rank;\r
245             foreach (var count in _quests.Values.Select(q => q.Count))\r
246             {\r
247                 switch (count.Spec)\r
248                 {\r
249                     case QuestSortie sortie:\r
250                         if (!FleetCheck(count.Id))\r
251                             continue;\r
252                         if (!_boss && count.Id == 216)\r
253                         {\r
254                             Increment(count);\r
255                             continue;\r
256                         }\r
257                         if (sortie.Count(count, rank, _map, _boss))\r
258                             NeedSave = true;\r
259                         continue;\r
260                     case QuestEnemyType enemyType:\r
261                         var num = enemyType.CountResult(\r
262                             _battleInfo.Result.Enemy.Main.Concat(_battleInfo.Result.Enemy.Guard));\r
263                         if (num > 0)\r
264                             Add(count, num);\r
265                         continue;\r
266                 }\r
267                 if (count.Id == 214)\r
268                     CountAgo(count, rank);\r
269             }\r
270         }\r
271 \r
272         private void CountAgo(QuestCount count, string rank)\r
273         {\r
274             if (QuestSortie.CompareRank(rank, "S") == 0)\r
275                 IncrementNth(count, 1);\r
276             if (!_boss)\r
277                 return;\r
278             IncrementNth(count, 2);\r
279             if (QuestSortie.CompareRank(rank, "B") <= 0)\r
280                 IncrementNth(count, 3);\r
281         }\r
282 \r
283         private bool FleetCheck(int id)\r
284         {\r
285             var specs = new ResultShipSpecs(_battleInfo);\r
286             switch (id)\r
287             {\r
288                 case 249:\r
289                     return specs.Names.Count("妙高", "那智", "羽黒") == 3;\r
290                 case 257:\r
291                     return specs.FlagshipType == 3 && specs.Types.Count(s => s == 3) <= 3 &&\r
292                            specs.Types.All(s => s == 2 || s == 3);\r
293                 case 259:\r
294                     return specs.Types.Count(type => type == 3) > 0 && specs.Classes.Count(c => new[]\r
295                     {\r
296                         2, // 伊勢型\r
297                         19, // 長門型\r
298                         26, // 扶桑型\r
299                         37 // 大和型\r
300                     }.Contains(c)) == 3;\r
301                 case 264:\r
302                     return specs.Types.Count(type => type == 2) >= 2 &&\r
303                            specs.Specs.Count(spec => spec.IsAircraftCarrier) >= 2;\r
304                 case 266:\r
305                     return specs.FlagshipType == 2 &&\r
306                            specs.Types.OrderBy(x => x).SequenceEqual(new[] {2, 2, 2, 2, 3, 5});\r
307                 case 280:\r
308                 case 284:\r
309                     return specs.Types.Count(type => type == 1 || type == 2) >= 3 &&\r
310                            specs.Types.Intersect(new[] {3, 4, 7, 21}).Any();\r
311                 case 840:\r
312                     return new[] {3, 4, 7, 21}.Contains(specs.FlagshipType) &&\r
313                            specs.Types.Count(type => type == 2 || type == 1) >= 3;\r
314                 case 861:\r
315                     return specs.Types.Count(s => s == 10 || s == 22) == 2;\r
316                 case 862:\r
317                     return specs.Types.Count(s => s == 3) >= 2 && specs.Types.Count(s => s == 16) >= 1;\r
318                 case 873:\r
319                     return specs.Types.Count(type => type == 3) >= 1;\r
320                 case 875:\r
321                     return specs.Names.Contains("長波改二") &&\r
322                            specs.Names.Count("朝霜改", "高波改", "沖波改") > 0;\r
323                 case 888:\r
324                     return specs.Names.Count("鳥海", "青葉", "衣笠", "加古", "古鷹", "天龍", "夕張") >= 4;\r
325                 case 894:\r
326                     return specs.Specs.Any(spec => spec.IsAircraftCarrier);\r
327                 case 903:\r
328                     return specs.Flagship.Name.StartsWith("夕張改二") &&\r
329                            (specs.Names.Count("睦月", "如月", "弥生", "卯月", "菊月", "望月") >= 2 || specs.Names.Contains("由良改二"));\r
330                 case 904:\r
331                     return specs.Names.Count("綾波改二", "敷波改二") == 2;\r
332                 case 905:\r
333                     return specs.Types.Count(type => type == 1) >= 3 && specs.Types.Length <= 5;\r
334                 case 912:\r
335                     return specs.Flagship.Name.StartsWith("明石") && specs.Types.Count(type => type == 2) >= 3;\r
336                 case 914:\r
337                     return specs.Types.Count(type => type == 5) >= 3 && specs.Types.Count(type => type == 2) >= 1;\r
338                 case 928:\r
339                     return specs.Names.Count("羽黒", "足柄", "妙高", "高雄", "神風") >= 2;\r
340                 case 318:\r
341                     return specs.Types.Count(type => type == 3) >= 2;\r
342                 case 329:\r
343                     return specs.Types.Count(type => type == 2 || type == 1) >= 2;\r
344                 case 330:\r
345                     return specs.Flagship.IsAircraftCarrier &&\r
346                            specs.Specs.Count(spec => spec.IsAircraftCarrier) >= 2 &&\r
347                            specs.Types.Count(type => type == 2) >= 2;\r
348                 case 337:\r
349                     return specs.Names.Count("陽炎", "不知火", "霰", "霞") == 4;\r
350                 case 339:\r
351                     return specs.Names.Count("磯波", "浦波", "綾波", "敷波") == 4;\r
352                 case 342:\r
353                     var t12 = specs.Types.Count(type => type == 1 || type == 2);\r
354                     return t12 >= 4 || t12 >= 3 && specs.Types.Intersect(new[] {3, 4, 7, 21}).Any();\r
355                 case 345:\r
356                     return specs.Names.Count("Warspite", "金剛", "Ark Royal", "Nelson", "Jervis", "Janus") >= 4;\r
357                 case 346:\r
358                     return specs.Names.Count("夕雲改二", "巻雲改二", "風雲改二", "秋雲改二") == 4;\r
359                 default:\r
360                     return true;\r
361             }\r
362         }\r
363 \r
364         private int _questFleet;\r
365 \r
366         public void StartPractice(string request)\r
367         {\r
368             var values = HttpUtility.ParseQueryString(request);\r
369             _questFleet = int.Parse(values["api_deck_id"]) - 1;\r
370         }\r
371 \r
372         public void InspectPracticeResult(dynamic json)\r
373         {\r
374             foreach (var count in _quests.Values.Select(q => q.Count))\r
375             {\r
376                 if (!FleetCheck(count.Id))\r
377                     continue;\r
378                 if (count.Id == 318 && _questFleet != 0)\r
379                     continue;\r
380                 if (!(count.Spec is QuestPractice practice))\r
381                     continue;\r
382                 if (practice.Check(json.api_win_rank))\r
383                     Increment(count);\r
384             }\r
385         }\r
386 \r
387         private readonly int[] _missionId = new int[ShipInfo.FleetCount];\r
388 \r
389         public void InspectDeck(dynamic json)\r
390         {\r
391             foreach (var entry in json)\r
392                 _missionId[(int)entry.api_id - 1] = (int)entry.api_mission[1];\r
393         }\r
394 \r
395         public void InspectMissionResult(string request, dynamic json)\r
396         {\r
397             var values = HttpUtility.ParseQueryString(request);\r
398             var deck = int.Parse(values["api_deck_id"]);\r
399             if ((int)json.api_clear_result == 0)\r
400                 return;\r
401             var mid = _missionId[deck - 1];\r
402             foreach (var count in _quests.Values.Select(q => q.Count))\r
403             {\r
404                 if (!(count.Spec is QuestMission mission))\r
405                     continue;\r
406                 if (mission.Count(count, mid))\r
407                     NeedSave = true;\r
408             }\r
409         }\r
410 \r
411         public void CountNyukyo() => Increment(503);\r
412 \r
413         public void CountCharge() => Increment(504);\r
414 \r
415         public void InspectCreateItem(string request)\r
416         {\r
417             var values = HttpUtility.ParseQueryString(request);\r
418             var count = values["api_multiple_flag"] == "1" ? 3 : 1;\r
419             Add(605, count);\r
420             Add(607, count);\r
421         }\r
422 \r
423         public void CountCreateShip()\r
424         {\r
425             Increment(606);\r
426             Increment(608);\r
427         }\r
428 \r
429         public void InspectDestroyShip(string request)\r
430         {\r
431             Add(609, HttpUtility.ParseQueryString(request)["api_ship_id"].Split(',').Length);\r
432         }\r
433 \r
434         public void CountRemodelSlot() => Increment(619);\r
435 \r
436         public void InspectDestroyItem(string request)\r
437         {\r
438             var values = HttpUtility.ParseQueryString(request);\r
439             var items = values["api_slotitem_ids"].Split(',')\r
440                 .Select(id => _itemInventory[int.Parse(id)].Spec).ToArray();\r
441             Increment(613); // 613: 資源の再利用\r
442             foreach (var quest in _quests.Values)\r
443             {\r
444                 var count = quest.Count;\r
445                 if (count.Spec is QuestDestroyItem destroy)\r
446                 {\r
447                     if (destroy.Count(count, items))\r
448                         NeedSave = true;\r
449                     continue;\r
450                 }\r
451                 if (quest.Id == 680)\r
452                 {\r
453                     count.NowArray[0] += items.Count(spec => spec.Type == 21);\r
454                     count.NowArray[1] += items.Count(spec => spec.Type == 12 || spec.Type == 13);\r
455                     NeedSave = true;\r
456                 }\r
457             }\r
458         }\r
459 \r
460         public void InspectPowerUp(string request, dynamic json)\r
461         {\r
462             if ((int)json.api_powerup_flag == 0)\r
463                 return;\r
464             foreach (var quest in _quests.Values)\r
465             {\r
466                 var count = quest.Count;\r
467                 if (!(count.Spec is QuestPowerUp))\r
468                     continue;\r
469                 if (quest.Id == 714 || quest.Id == 715)\r
470                 {\r
471                     var values = HttpUtility.ParseQueryString(request);\r
472                     if (_shipInventory[int.Parse(values["api_id"])].Spec.ShipType != 2)\r
473                         return;\r
474                     var ships = values["api_id_items"].Split(',').Select(id => _shipInventory[int.Parse(id)]).ToArray();\r
475                     var type = quest.Id == 714 ? 2 : quest.Id == 715 ? 3 : -1;\r
476                     if (ships.Count(s => s.Spec.ShipType == type) < 3)\r
477                         return;\r
478                 }\r
479                 Increment(count);\r
480             }\r
481         }\r
482 \r
483         private void Increment(QuestCount count)\r
484         {\r
485             Add(count, 1);\r
486         }\r
487 \r
488         private void Add(QuestCount count, int value)\r
489         {\r
490             count.Now += value;\r
491             NeedSave = true;\r
492         }\r
493 \r
494         private void Increment(int id)\r
495         {\r
496             Add(id, 1);\r
497         }\r
498 \r
499         private void Add(int id, int value)\r
500         {\r
501             if (!_quests.TryGetValue(id, out var quest))\r
502                 return;\r
503             quest.Count.Now += value;\r
504             NeedSave = true;\r
505         }\r
506 \r
507         private void IncrementNth(QuestCount count, int n)\r
508         {\r
509             count.NowArray[n]++;\r
510             NeedSave = true;\r
511         }\r
512     }\r
513 }