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 688 => new[] {"艦戦", "艦爆", "艦攻", "水偵"},
\r
128 }).Zip(NowArray, (entry, n) => $"{entry}{n}"));
\r
131 public bool Cleared => NowArray?.Zip(Spec.MaxArray, (n, m) => n >= m).All(x => x) ??
\r
132 Spec.Max != 0 && Now >= Spec.Max;
\r
135 public class QuestCounter
\r
137 private readonly QuestInfo _questInfo;
\r
138 private readonly ItemInfo _itemInfo;
\r
139 private readonly BattleInfo _battleInfo;
\r
140 private readonly SortedDictionary<int, QuestStatus> _quests;
\r
142 private bool _boss;
\r
144 private class ResultShipSpecs
\r
146 public ShipSpec[] Specs { get; }
\r
147 public NameChecker Names { get; }
\r
148 public int[] Types { get; }
\r
149 public int[] Classes { get; }
\r
150 public ShipSpec Flagship { get; }
\r
151 public int FlagshipType { get; }
\r
153 public class NameChecker
\r
155 private readonly string[] _names;
\r
157 public NameChecker(ShipSpec[] specs)
\r
159 _names = specs.Select(spec => spec.Name).ToArray();
\r
162 public bool Contains(string demand)
\r
164 return _names.Any(name => name.StartsWith(demand));
\r
167 public int Count(params string[] demands)
\r
169 return demands.Sum(demand => _names.Count(name => name.StartsWith(demand)));
\r
173 public ResultShipSpecs(BattleInfo battleInfo)
\r
175 Specs = battleInfo.Result?.Friend.Main.Where(s => s.NowHp > 0).Select(ship => ship.Spec).ToArray() ??
\r
177 Names = new NameChecker(Specs);
\r
178 Types = Specs.Select(spec => spec.ShipType).ToArray();
\r
179 Classes = Specs.Select(spec => spec.ShipClass).ToArray();
\r
180 Flagship = Specs.FirstOrDefault();
\r
181 FlagshipType = Types.FirstOrDefault();
\r
185 public QuestCounter(QuestInfo questInfo, ItemInfo itemInfo, BattleInfo battleInfo)
\r
187 _questInfo = questInfo;
\r
188 _quests = questInfo.QuestDictionary;
\r
189 _itemInfo = itemInfo;
\r
190 _battleInfo = battleInfo;
\r
193 private bool NeedSave
\r
195 set => _questInfo.NeedSave = value;
\r
198 public void InspectMapStart(dynamic json)
\r
200 if (_quests.TryGetValue(214, out var ago)) // あ号
\r
201 ago.Count.NowArray[0]++;
\r
202 InspectMapNext(json);
\r
205 public void InspectMapNext(dynamic json)
\r
207 _map = (int)json.api_maparea_id * 10 + (int)json.api_mapinfo_no;
\r
210 var cell = json.api_no() ? (int)json.api_no : 0;
\r
217 _boss = (int)json.api_event_id == 5;
\r
219 if (_quests.TryGetValue(861, out var q861) && _map == 16 && (int)json.api_event_id == 8)
\r
221 if (new ResultShipSpecs(_battleInfo).Types.Count(s => s == 10 || s == 22) == 2)
\r
222 Increment(q861.Count);
\r
226 public void InspectBattleResult(dynamic json)
\r
228 var rank = json.api_win_rank;
\r
229 foreach (var count in _quests.Values.Select(q => q.Count))
\r
231 switch (count.Spec)
\r
233 case QuestSortie sortie:
\r
234 if (!FleetCheck(count.Id))
\r
236 if (!_boss && count.Id == 216)
\r
241 if (sortie.Count(count, rank, _map, _boss))
\r
244 case QuestEnemyType enemyType:
\r
245 var num = enemyType.CountResult(
\r
246 _battleInfo.Result.Enemy.Main.Concat(_battleInfo.Result.Enemy.Guard));
\r
251 if (count.Id == 214)
\r
252 CountAgo(count, rank);
\r
256 private void CountAgo(QuestCount count, string rank)
\r
258 if (QuestSortie.CompareRank(rank, "S") == 0)
\r
259 IncrementNth(count, 1);
\r
262 IncrementNth(count, 2);
\r
263 if (QuestSortie.CompareRank(rank, "B") <= 0)
\r
264 IncrementNth(count, 3);
\r
267 private bool FleetCheck(int id)
\r
269 var specs = new ResultShipSpecs(_battleInfo);
\r
273 return specs.Names.Count("妙高", "那智", "羽黒") == 3;
\r
275 return specs.FlagshipType == 3 && specs.Types.Count(s => s == 3) <= 3 &&
\r
276 specs.Types.All(s => s == 2 || s == 3);
\r
278 return specs.Types.Count(type => type == 3) > 0 && specs.Classes.Count(c => new[]
\r
284 }.Contains(c)) == 3;
\r
286 return specs.Types.Count(type => type == 2) >= 2 &&
\r
287 specs.Specs.Count(spec => spec.IsAircraftCarrier) >= 2;
\r
289 return specs.FlagshipType == 2 &&
\r
290 specs.Types.OrderBy(x => x).SequenceEqual(new[] {2, 2, 2, 2, 3, 5});
\r
293 return specs.Types.Count(type => type == 1 || type == 2) >= 3 &&
\r
294 specs.Types.Intersect(new[] {3, 4, 7, 21}).Any();
\r
296 return specs.Types.Count(s => s == 3) >= 2 && specs.Types.Count(s => s == 16) >= 1;
\r
298 return specs.Types.Count(type => type == 3) >= 1;
\r
300 return specs.Names.Contains("長波改二") &&
\r
301 specs.Names.Count("朝霜改", "高波改", "沖波改") > 0;
\r
303 return specs.Names.Count("鳥海", "青葉", "衣笠", "加古", "天竜", "夕張") >= 4;
\r
305 return specs.Specs.Any(spec => spec.IsAircraftCarrier);
\r
307 return specs.Types.Count(type => type == 3) >= 2;
\r
309 return specs.Flagship.IsAircraftCarrier &&
\r
310 specs.Specs.Count(spec => spec.IsAircraftCarrier) >= 2 &&
\r
311 specs.Types.Count(type => type == 2) >= 2;
\r
313 return specs.Names.Count("陽炎", "不知火", "霰", "霞") == 4;
\r
319 private int _questFleet;
\r
321 public void StartPractice(string request)
\r
323 var values = HttpUtility.ParseQueryString(request);
\r
324 _questFleet = int.Parse(values["api_deck_id"]) - 1;
\r
327 public void InspectPracticeResult(dynamic json)
\r
329 foreach (var count in _quests.Values.Select(q => q.Count))
\r
331 if (!FleetCheck(count.Id))
\r
333 if (count.Id == 318 && _questFleet != 0)
\r
335 if (!(count.Spec is QuestPractice practice))
\r
337 if (practice.Check(json.api_win_rank))
\r
342 private readonly int[] _missionId = new int[ShipInfo.FleetCount];
\r
344 public void InspectDeck(dynamic json)
\r
346 foreach (var entry in json)
\r
347 _missionId[(int)entry.api_id - 1] = (int)entry.api_mission[1];
\r
350 public void InspectMissionResult(string request, dynamic json)
\r
352 var values = HttpUtility.ParseQueryString(request);
\r
353 var deck = int.Parse(values["api_deck_id"]);
\r
354 if ((int)json.api_clear_result == 0)
\r
356 var mid = _missionId[deck - 1];
\r
357 foreach (var count in _quests.Values.Select(q => q.Count))
\r
359 if (!(count.Spec is QuestMission mission))
\r
361 if (mission.Count(count, mid))
\r
366 public void CountNyukyo() => Increment(503);
\r
368 public void CountCharge() => Increment(504);
\r
370 public void InspectCreateItem(string request)
\r
372 var values = HttpUtility.ParseQueryString(request);
\r
373 var count = values["api_multiple_flag"] == "1" ? 3 : 1;
\r
378 public void CountCreateShip()
\r
384 public void InspectDestroyShip(string request)
\r
386 Add(609, HttpUtility.ParseQueryString(request)["api_ship_id"].Split(',').Length);
\r
389 public void CountRemodelSlot() => Increment(619);
\r
391 public void InspectDestroyItem(string request, dynamic json)
\r
393 var values = HttpUtility.ParseQueryString(request);
\r
394 var items = values["api_slotitem_ids"].Split(',')
\r
395 .Select(id => _itemInfo.GetStatus(int.Parse(id)).Spec).ToArray();
\r
396 Increment(613); // 613: 資源の再利用
\r
397 foreach (var quest in _quests.Values)
\r
399 var count = quest.Count;
\r
400 if (count.Spec is QuestDestroyItem destroy)
\r
402 if (destroy.Count(count, items))
\r
406 if (quest.Id == 680)
\r
408 count.NowArray[0] += items.Count(spec => spec.Type == 21);
\r
409 count.NowArray[1] += items.Count(spec => spec.Type == 12 || spec.Type == 13);
\r
415 public void InspectPowerUp(dynamic json)
\r
417 if ((int)json.api_powerup_flag == 0)
\r
419 foreach (var quest in _quests.Values)
\r
421 var count = quest.Count;
\r
422 if (!(count.Spec is QuestPowerUp))
\r
428 public void Increment(QuestCount count)
\r
433 public void Add(QuestCount count, int value)
\r
435 count.Now += value;
\r
439 public void Increment(int id)
\r
444 public void Add(int id, int value)
\r
446 if (!_quests.TryGetValue(id, out var quest))
\r
448 quest.Count.Now += value;
\r
452 public void IncrementNth(QuestCount count, int n)
\r
454 count.NowArray[n]++;
\r