1 // Copyright (C) 2019 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.Xml.Serialization;
\r
19 using KancolleSniffer.Util;
\r
21 namespace KancolleSniffer.Model
\r
23 public class QuestCount
\r
25 public int Id { get; set; }
\r
26 public int Now { get; set; }
\r
27 public int[] NowArray { get; set; }
\r
30 public QuestSpec Spec { get; set; }
\r
32 public bool AdjustCount(int progress)
\r
34 if (!Spec.AdjustCount)
\r
36 if (NowArray != null)
\r
38 if (progress != 100)
\r
40 NowArray = NowArray.Zip(Spec.MaxArray, Math.Max).ToArray();
\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
64 var high = (int)Math.Ceiling(max * next / 100.0);
\r
67 Now = low - Spec.Shift;
\r
72 Now = high - 1 - Spec.Shift;
\r
78 public override string ToString()
\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
87 public QuestCount Clone()
\r
89 var clone = (QuestCount)MemberwiseClone();
\r
90 if (NowArray != null)
\r
91 clone.NowArray = (int[])NowArray.Clone();
\r
95 public bool Equals(QuestCount other)
\r
99 if (NowArray == null)
\r
100 return Now == other.Now;
\r
101 return NowArray.SequenceEqual(other.NowArray);
\r
104 private static string MapString(int map)
\r
110 _ => $"{map / 10}-{map % 10}"
\r
114 public string ToToolTip()
\r
116 if (NowArray == null)
\r
118 if (Spec is QuestSortie spec && spec.Maps != null && spec.MaxArray != null)
\r
120 return string.Join(" ", spec.Maps.Zip(NowArray, (map, n) => $"{MapString(map)}:{n}"));
\r
122 return string.Join(" ", (Id switch
\r
124 426 => new[] {"警備任務", "対潜警戒任務", "海上護衛任務", "強硬偵察任務"},
\r
125 428 => new[] {"対潜警戒任務", "海峡警備行動", "長時間対潜警戒"},
\r
126 436 => new[] {"練習航海", "長距離練習航海", "警備任務", "対潜警戒任務", "強行偵察任務"},
\r
127 688 => new[] {"艦戦", "艦爆", "艦攻", "水偵"},
\r
129 }).Zip(NowArray, (entry, n) => $"{entry}{n}"));
\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
136 public class QuestCounter
\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
143 private bool _boss;
\r
145 private class ResultShipSpecs
\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
154 public class NameChecker
\r
156 private readonly string[] _names;
\r
158 public NameChecker(ShipSpec[] specs)
\r
160 _names = specs.Select(spec => spec.Name).ToArray();
\r
163 public bool Contains(string demand)
\r
165 return _names.Any(name => name.StartsWith(demand));
\r
168 public int Count(params string[] demands)
\r
170 return demands.Sum(demand => _names.Count(name => name.StartsWith(demand)));
\r
174 public ResultShipSpecs(BattleInfo battleInfo)
\r
176 Specs = battleInfo.Result?.Friend.Main.Where(s => s.NowHp > 0).Select(ship => ship.Spec).ToArray() ??
\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
186 public QuestCounter(QuestInfo questInfo, ItemInfo itemInfo, BattleInfo battleInfo)
\r
188 _questInfo = questInfo;
\r
189 _quests = questInfo.QuestDictionary;
\r
190 _itemInfo = itemInfo;
\r
191 _battleInfo = battleInfo;
\r
194 private bool NeedSave
\r
196 set => _questInfo.NeedSave = value;
\r
199 public void InspectMapStart(dynamic json)
\r
201 if (_quests.TryGetValue(214, out var ago)) // あ号
\r
202 ago.Count.NowArray[0]++;
\r
203 InspectMapNext(json);
\r
206 public void InspectMapNext(dynamic json)
\r
208 _map = (int)json.api_maparea_id * 10 + (int)json.api_mapinfo_no;
\r
211 var cell = json.api_no() ? (int)json.api_no : 0;
\r
218 _boss = (int)json.api_event_id == 5;
\r
220 if (_map != 16 || (int)json.api_event_id != 8)
\r
222 foreach (var count in _quests.Values.Select(q => q.Count))
\r
224 if (!(count.Spec is QuestSortie sortie))
\r
226 if (!FleetCheck(count.Id))
\r
228 if (sortie.Count(count, "S", _map, true))
\r
233 public void InspectBattleResult(dynamic json)
\r
235 var rank = json.api_win_rank;
\r
236 foreach (var count in _quests.Values.Select(q => q.Count))
\r
238 switch (count.Spec)
\r
240 case QuestSortie sortie:
\r
241 if (!FleetCheck(count.Id))
\r
243 if (!_boss && count.Id == 216)
\r
248 if (sortie.Count(count, rank, _map, _boss))
\r
251 case QuestEnemyType enemyType:
\r
252 var num = enemyType.CountResult(
\r
253 _battleInfo.Result.Enemy.Main.Concat(_battleInfo.Result.Enemy.Guard));
\r
258 if (count.Id == 214)
\r
259 CountAgo(count, rank);
\r
263 private void CountAgo(QuestCount count, string rank)
\r
265 if (QuestSortie.CompareRank(rank, "S") == 0)
\r
266 IncrementNth(count, 1);
\r
269 IncrementNth(count, 2);
\r
270 if (QuestSortie.CompareRank(rank, "B") <= 0)
\r
271 IncrementNth(count, 3);
\r
274 private bool FleetCheck(int id)
\r
276 var specs = new ResultShipSpecs(_battleInfo);
\r
280 return specs.Names.Count("妙高", "那智", "羽黒") == 3;
\r
282 return specs.FlagshipType == 3 && specs.Types.Count(s => s == 3) <= 3 &&
\r
283 specs.Types.All(s => s == 2 || s == 3);
\r
285 return specs.Types.Count(type => type == 3) > 0 && specs.Classes.Count(c => new[]
\r
291 }.Contains(c)) == 3;
\r
293 return specs.Types.Count(type => type == 2) >= 2 &&
\r
294 specs.Specs.Count(spec => spec.IsAircraftCarrier) >= 2;
\r
296 return specs.FlagshipType == 2 &&
\r
297 specs.Types.OrderBy(x => x).SequenceEqual(new[] {2, 2, 2, 2, 3, 5});
\r
300 return specs.Types.Count(type => type == 1 || type == 2) >= 3 &&
\r
301 specs.Types.Intersect(new[] {3, 4, 7, 21}).Any();
\r
303 return specs.Types.Count(s => s == 10 || s == 22) == 2;
\r
305 return specs.Types.Count(s => s == 3) >= 2 && specs.Types.Count(s => s == 16) >= 1;
\r
307 return specs.Types.Count(type => type == 3) >= 1;
\r
309 return specs.Names.Contains("長波改二") &&
\r
310 specs.Names.Count("朝霜改", "高波改", "沖波改") > 0;
\r
312 return specs.Names.Count("鳥海", "青葉", "衣笠", "加古", "古鷹", "天龍", "夕張") >= 4;
\r
314 return specs.Specs.Any(spec => spec.IsAircraftCarrier);
\r
316 return specs.Flagship.Name.StartsWith("夕張改二") &&
\r
317 (specs.Names.Count("睦月", "如月", "弥生", "卯月", "菊月", "望月") >= 2 || specs.Names.Contains("由良改二"));
\r
319 return specs.Names.Count("綾波改二", "敷波改二") == 2;
\r
321 return specs.Types.Count(type => type == 1) >= 3 && specs.Types.Length <= 5;
\r
323 return specs.Flagship.Name.StartsWith("明石") && specs.Types.Count(type => type == 2) >= 3;
\r
325 return specs.Types.Count(type => type == 5) >= 3 && specs.Types.Count(type => type == 2) >= 1;
\r
327 return specs.Types.Count(type => type == 3) >= 2;
\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
333 return specs.Names.Count("陽炎", "不知火", "霰", "霞") == 4;
\r
335 return specs.Names.Count("磯波", "浦波", "綾波", "敷波") == 4;
\r
341 private int _questFleet;
\r
343 public void StartPractice(string request)
\r
345 var values = HttpUtility.ParseQueryString(request);
\r
346 _questFleet = int.Parse(values["api_deck_id"]) - 1;
\r
349 public void InspectPracticeResult(dynamic json)
\r
351 foreach (var count in _quests.Values.Select(q => q.Count))
\r
353 if (!FleetCheck(count.Id))
\r
355 if (count.Id == 318 && _questFleet != 0)
\r
357 if (!(count.Spec is QuestPractice practice))
\r
359 if (practice.Check(json.api_win_rank))
\r
364 private readonly int[] _missionId = new int[ShipInfo.FleetCount];
\r
366 public void InspectDeck(dynamic json)
\r
368 foreach (var entry in json)
\r
369 _missionId[(int)entry.api_id - 1] = (int)entry.api_mission[1];
\r
372 public void InspectMissionResult(string request, dynamic json)
\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
378 var mid = _missionId[deck - 1];
\r
379 foreach (var count in _quests.Values.Select(q => q.Count))
\r
381 if (!(count.Spec is QuestMission mission))
\r
383 if (mission.Count(count, mid))
\r
388 public void CountNyukyo() => Increment(503);
\r
390 public void CountCharge() => Increment(504);
\r
392 public void InspectCreateItem(string request)
\r
394 var values = HttpUtility.ParseQueryString(request);
\r
395 var count = values["api_multiple_flag"] == "1" ? 3 : 1;
\r
400 public void CountCreateShip()
\r
406 public void InspectDestroyShip(string request)
\r
408 Add(609, HttpUtility.ParseQueryString(request)["api_ship_id"].Split(',').Length);
\r
411 public void CountRemodelSlot() => Increment(619);
\r
413 public void InspectDestroyItem(string request, dynamic json)
\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
421 var count = quest.Count;
\r
422 if (count.Spec is QuestDestroyItem destroy)
\r
424 if (destroy.Count(count, items))
\r
428 if (quest.Id == 680)
\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
437 public void InspectPowerUp(dynamic json)
\r
439 if ((int)json.api_powerup_flag == 0)
\r
441 foreach (var quest in _quests.Values)
\r
443 var count = quest.Count;
\r
444 if (!(count.Spec is QuestPowerUp))
\r
450 public void Increment(QuestCount count)
\r
455 public void Add(QuestCount count, int value)
\r
457 count.Now += value;
\r
461 public void Increment(int id)
\r
466 public void Add(int id, int value)
\r
468 if (!_quests.TryGetValue(id, out var quest))
\r
470 quest.Count.Now += value;
\r
474 public void IncrementNth(QuestCount count, int n)
\r
476 count.NowArray[n]++;
\r