OSDN Git Service

カウンタの実装を変更してShipInfoからItemInfoへの依存をなくす
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Model / ShipInfo.cs
1 // Copyright (C) 2013, 2014, 2015 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 KancolleSniffer.Util;\r
19 using static System.Math;\r
20 \r
21 namespace KancolleSniffer.Model\r
22 {\r
23     public class ShipInfo\r
24     {\r
25         public const int FleetCount = 4;\r
26         public const int MemberCount = 6;\r
27 \r
28         private readonly Fleet[] _fleets;\r
29         private readonly ShipMaster _shipMaster;\r
30         private readonly ShipInventry _shipInventry;\r
31         private readonly ItemInventry _itemInventry;\r
32         private readonly List<int> _escapedShips = new List<int>();\r
33         private ShipStatus[] _battleResult = new ShipStatus[0];\r
34         private readonly NumEquipsChecker _numEquipsChecker = new NumEquipsChecker();\r
35         public AlarmCounter Counter { get; }\r
36         public int HqLevel { get; private set; }\r
37         public ShipStatusPair[] BattleResultDiff { get; private set; } = new ShipStatusPair[0];\r
38         public bool IsBattleResultError => BattleResultDiff.Length > 0;\r
39         public ShipStatus[] BattleStartStatus { get; private set; } = new ShipStatus[0];\r
40         public int DropShipId { private get; set; } = -1;\r
41 \r
42         private class NumEquipsChecker\r
43         {\r
44             public int MaxId { private get; set; } = int.MaxValue;\r
45 \r
46             public void Check(ShipStatus ship)\r
47             {\r
48                 var spec = ship.Spec;\r
49                 if (spec.NumEquips != -1 || ship.Id <= MaxId)\r
50                     return;\r
51                 spec.NumEquips = ship.Slot.Count(item => !item.Empty);\r
52             }\r
53         }\r
54 \r
55         public class ShipStatusPair\r
56         {\r
57             public ShipStatus Assumed { get; set; }\r
58             public ShipStatus Actual { get; set; }\r
59 \r
60             public ShipStatusPair(ShipStatus assumed, ShipStatus actual)\r
61             {\r
62                 Assumed = assumed;\r
63                 Actual = actual;\r
64             }\r
65         }\r
66 \r
67         public ShipInfo(ShipMaster shipMaster, ShipInventry shipInventry, ItemInventry itemInventry)\r
68         {\r
69             _shipMaster = shipMaster;\r
70             _shipInventry = shipInventry;\r
71             _fleets = Enumerable.Range(0, FleetCount).Select((x, i) => new Fleet(this, i)).ToArray();\r
72             _itemInventry = itemInventry;\r
73             Counter = new AlarmCounter(() => _shipInventry.Count){Margin = 4};\r
74         }\r
75 \r
76         public void InspectMaster(dynamic json)\r
77         {\r
78             _shipMaster.Inspect(json);\r
79             _shipInventry.Clear();\r
80         }\r
81 \r
82         public void InspectShip(dynamic json)\r
83         {\r
84             if (json.api_deck_port()) // port\r
85             {\r
86                 _shipInventry.Clear();\r
87                 for (var i = 0; i < FleetCount; i++)\r
88                     _fleets[i].State = FleetState.Port;\r
89                 InspectDeck(json.api_deck_port);\r
90                 InspectShipData(json.api_ship);\r
91                 InspectBasic(json.api_basic);\r
92                 if (json.api_combined_flag())\r
93                     _fleets[0].CombinedType = _fleets[1].CombinedType = (CombinedType)(int)json.api_combined_flag;\r
94                 VerifyBattleResult();\r
95             }\r
96             else if (json.api_data()) // ship2\r
97             {\r
98                 InspectDeck(json.api_data_deck);\r
99                 InspectShipData(json.api_data);\r
100             }\r
101             else if (json.api_ship_data()) // ship3とship_deck\r
102             {\r
103                 InspectDeck(json.api_deck_data);\r
104                 InspectShipData(json.api_ship_data);\r
105                 VerifyBattleResult();\r
106                 // ship_deckでドロップ艦を反映する\r
107                 if (DropShipId != -1)\r
108                 {\r
109                     _shipInventry.InflateCount(1);\r
110                     var num = _shipMaster.GetSpec(DropShipId).NumEquips;\r
111                     if (num > 0)\r
112                         _itemInventry.InflateCount(num);\r
113                 }\r
114             }\r
115             else if (json.api_ship()) // getshipとpowerup\r
116             {\r
117                 InspectShipData(new[] {json.api_ship});\r
118             }\r
119             DropShipId = -1;\r
120         }\r
121 \r
122         public void SaveBattleResult()\r
123         {\r
124             _battleResult = _fleets.Where(fleet =>\r
125                     fleet.State >= FleetState.Sortie && !GetStatus(fleet.Deck[0]).Spec.IsRepairShip)\r
126                 .SelectMany(fleet => fleet.Deck.Select(GetStatus)).ToArray();\r
127         }\r
128 \r
129         private void VerifyBattleResult()\r
130         {\r
131             BattleResultDiff = (from assumed in _battleResult\r
132                 let actual = GetStatus(assumed.Id)\r
133                 where !assumed.Escaped && assumed.NowHp != actual.NowHp\r
134                 select new ShipStatusPair(assumed, actual)).ToArray();\r
135             _battleResult = new ShipStatus[0];\r
136         }\r
137 \r
138         public void SaveBattleStartStatus()\r
139         {\r
140             BattleStartStatus = _fleets.Where(fleet => fleet.State >= FleetState.Sortie)\r
141                 .SelectMany(fleet => fleet.Deck.Select(id => (ShipStatus)GetStatus(id).Clone())).ToArray();\r
142         }\r
143 \r
144         public void InspectDeck(dynamic json)\r
145         {\r
146             foreach (var entry in json)\r
147             {\r
148                 var fleet = (int)entry.api_id - 1;\r
149                 _fleets[fleet].Deck = (int[])entry.api_ship;\r
150                 if ((int)entry.api_mission[0] != 0)\r
151                     _fleets[fleet].State = FleetState.Mission;\r
152             }\r
153         }\r
154 \r
155         private void InspectShipData(dynamic json)\r
156         {\r
157             foreach (var entry in json)\r
158             {\r
159                 var id = (int)entry.api_id;\r
160                 var ship = new ShipStatus\r
161                 {\r
162                     Id = id,\r
163                     Spec = _shipMaster.GetSpec((int)entry.api_ship_id),\r
164                     Level = (int)entry.api_lv,\r
165                     ExpToNext = (int)entry.api_exp[1],\r
166                     MaxHp = (int)entry.api_maxhp,\r
167                     NowHp = (int)entry.api_nowhp,\r
168                     Cond = (int)entry.api_cond,\r
169                     Fuel = (int)entry.api_fuel,\r
170                     Bull = (int)entry.api_bull,\r
171                     OnSlot = (int[])entry.api_onslot,\r
172                     Slot = ((int[])entry.api_slot).Select(item => new ItemStatus(item)).ToArray(),\r
173                     SlotEx = entry.api_slot_ex() ? new ItemStatus((int)entry.api_slot_ex) : new ItemStatus(0),\r
174                     NdockTime = (int)entry.api_ndock_time,\r
175                     NdockItem = (int[])entry.api_ndock_item,\r
176                     LoS = (int)entry.api_sakuteki[0],\r
177                     Firepower = (int)entry.api_karyoku[0],\r
178                     Torpedo = (int)entry.api_raisou[0],\r
179                     AntiSubmarine = (int)entry.api_taisen[0],\r
180                     AntiAir = (int)entry.api_taiku[0],\r
181                     Lucky = (int)entry.api_lucky[0],\r
182                     Locked = entry.api_locked() && entry.api_locked == 1\r
183                 };\r
184                 _shipInventry.Add(ship);\r
185                 _numEquipsChecker.Check(ship);\r
186             }\r
187             _numEquipsChecker.MaxId = _shipInventry.MaxId;\r
188         }\r
189 \r
190         private void InspectBasic(dynamic json)\r
191         {\r
192             HqLevel = (int)json.api_level;\r
193             Counter.Max = (int)json.api_max_chara;\r
194         }\r
195 \r
196         public void InspectCharge(dynamic json)\r
197         {\r
198             foreach (var entry in json.api_ship)\r
199             {\r
200                 var status = _shipInventry[(int)entry.api_id];\r
201                 status.Bull = (int)entry.api_bull;\r
202                 status.Fuel = (int)entry.api_fuel;\r
203                 status.OnSlot = (from num in (dynamic[])entry.api_onslot select (int)num).ToArray();\r
204             }\r
205         }\r
206 \r
207         public void InspectChange(string request)\r
208         {\r
209             var values = HttpUtility.ParseQueryString(request);\r
210             var fleet = _fleets[int.Parse(values["api_id"]) - 1];\r
211             var idx = int.Parse(values["api_ship_idx"]);\r
212             var ship = int.Parse(values["api_ship_id"]);\r
213 \r
214             if (idx == -1)\r
215             {\r
216                 var deck = fleet.Deck;\r
217                 for (var i = 1; i < deck.Length; i++)\r
218                     deck[i] = -1;\r
219                 return;\r
220             }\r
221             if (ship == -1)\r
222             {\r
223                 WithdrowShip(fleet, idx);\r
224                 return;\r
225             }\r
226             var of = FindFleet(ship, out var oi);\r
227             var orig = fleet.Deck[idx];\r
228             fleet.Deck[idx] = ship;\r
229             if (of == null)\r
230                 return;\r
231             // 入れ替えの場合\r
232             if ((of.Deck[oi] = orig) == -1)\r
233                 WithdrowShip(of, oi);\r
234         }\r
235 \r
236         private Fleet FindFleet(int ship, out int idx)\r
237         {\r
238             foreach (var fleet in _fleets)\r
239             {\r
240                 idx = Array.FindIndex(fleet.Deck, id => id == ship);\r
241                 if (idx < 0)\r
242                     continue;\r
243                 return fleet;\r
244             }\r
245             idx = -1;\r
246             return null;\r
247         }\r
248 \r
249         private void WithdrowShip(Fleet fleet, int idx)\r
250         {\r
251             var deck = fleet.Deck;\r
252             var j = idx;\r
253             for (var i = idx + 1; i < deck.Length; i++)\r
254             {\r
255                 if (deck[i] != -1)\r
256                     deck[j++] = deck[i];\r
257             }\r
258             for (; j < deck.Length; j++)\r
259                 deck[j] = -1;\r
260         }\r
261 \r
262         public void InspectPowerup(string request, dynamic json)\r
263         {\r
264             var values = HttpUtility.ParseQueryString(request);\r
265             var ships = values["api_id_items"].Split(',').Select(int.Parse).ToArray();\r
266             if (!_shipInventry.Contains(ships[0])) // 二重に実行された場合\r
267                 return;\r
268             _itemInventry.Remove(ships.SelectMany(id => _shipInventry[id].Slot));\r
269             _shipInventry.Remove(ships);\r
270             InspectDeck(json.api_deck);\r
271             InspectShip(json);\r
272         }\r
273 \r
274         public void InspectSlotExchange(string request, dynamic json)\r
275         {\r
276             var values = HttpUtility.ParseQueryString(request);\r
277             var ship = int.Parse(values["api_id"]);\r
278             _shipInventry[ship].Slot = ((int[])json.api_slot).Select(id => new ItemStatus(id)).ToArray();\r
279         }\r
280 \r
281         public void InspectSlotDeprive(dynamic json)\r
282         {\r
283             InspectShipData(new[] {json.api_ship_data.api_set_ship, json.api_ship_data.api_unset_ship});\r
284         }\r
285 \r
286         public void InspectDestroyShip(string request, dynamic json)\r
287         {\r
288             var values = HttpUtility.ParseQueryString(request);\r
289             var delitem = int.Parse(values["api_slot_dest_flag"] ?? "0") == 1;\r
290             foreach (var id in values["api_ship_id"].Split(',').Select(int.Parse))\r
291             {\r
292                 if (delitem)\r
293                     _itemInventry.Remove(_shipInventry[id].AllSlot);\r
294                 var of = FindFleet(id, out var oi);\r
295                 if (of != null)\r
296                     WithdrowShip(of, oi);\r
297                 _shipInventry.Remove(id);\r
298             }\r
299         }\r
300 \r
301         public void InspectCombined(string request)\r
302         {\r
303             var values = HttpUtility.ParseQueryString(request);\r
304             _fleets[0].CombinedType = _fleets[1].CombinedType = (CombinedType)int.Parse(values["api_combined_type"]);\r
305         }\r
306 \r
307         public void InspectMapStart(string request)\r
308         {\r
309             var values = HttpUtility.ParseQueryString(request);\r
310             var fleet = int.Parse(values["api_deck_id"]) - 1;\r
311             if (_fleets[0].CombinedType == 0 || fleet > 1)\r
312             {\r
313                 _fleets[fleet].State = FleetState.Sortie;\r
314             }\r
315             else\r
316             {\r
317                 _fleets[0].State = _fleets[1].State = FleetState.Sortie;\r
318             }\r
319             SetBadlyDamagedShips();\r
320         }\r
321 \r
322         public void StartPractice(string request)\r
323         {\r
324             var values = HttpUtility.ParseQueryString(request);\r
325             var fleet = int.Parse(values["api_deck_id"]) - 1;\r
326             _fleets[fleet].State = FleetState.Practice;\r
327         }\r
328 \r
329         public void RepairShip(int id)\r
330         {\r
331             var s = _shipInventry[id];\r
332             s.NowHp = s.MaxHp;\r
333             s.Cond = Max(40, s.Cond);\r
334         }\r
335 \r
336         public Fleet[] Fleets => _fleets;\r
337 \r
338         public ShipStatus GetStatus(int id) => FillUp(_shipInventry[id]);\r
339 \r
340         private ShipStatus FillUp(ShipStatus ship)\r
341         {\r
342             if (ship.Empty)\r
343                 return ship;\r
344             ship.Slot = ship.Slot.Select(item => _itemInventry[item.Id]).ToArray();\r
345             ship.SlotEx = _itemInventry[ship.SlotEx.Id];\r
346             ship.Escaped = _escapedShips.Contains(ship.Id);\r
347             ship.Fleet = FindFleet(ship.Id, out var idx);\r
348             ship.DeckIndex = idx;\r
349             return ship;\r
350         }\r
351 \r
352         public void SetItemHolder()\r
353         {\r
354             foreach (var ship in _shipInventry.AllShips)\r
355             {\r
356                 foreach (var item in ship.Slot)\r
357                     _itemInventry[item.Id].Holder = ship;\r
358                 _itemInventry[ship.SlotEx.Id].Holder = ship;\r
359             }\r
360         }\r
361 \r
362         public ShipSpec GetSpec(int id) => _shipMaster.GetSpec(id);\r
363 \r
364         public ShipStatus[] ShipList => _shipInventry.AllShips.Select(FillUp).ToArray();\r
365 \r
366         public ShipStatus[] GetRepairList(DockInfo dockInfo)\r
367             => (from s in ShipList\r
368                 where s.NowHp < s.MaxHp && !dockInfo.InNDock(s.Id) &&\r
369                       (s.Fleet == null || s.Fleet.State != FleetState.Practice)\r
370                 select s).OrderByDescending(s => s.RepairTime).ToArray();\r
371 \r
372         public string[] BadlyDamagedShips { get; private set; } = new string[0];\r
373 \r
374         public void SetBadlyDamagedShips()\r
375         {\r
376             BadlyDamagedShips =\r
377             (from s in _fleets.Where(fleet => fleet.State == FleetState.Sortie)\r
378                     .SelectMany(fleet => fleet.Deck.Where(id => id != -1).Select(GetStatus))\r
379                 where !s.Escaped && s.DamageLevel == ShipStatus.Damage.Badly &&\r
380                       !(s.Fleet.CombinedType != 0 && s.Fleet.Number == 1 && s.DeckIndex == 0) // 第二艦隊の旗艦を除く\r
381                 select s.Name).ToArray();\r
382         }\r
383 \r
384         public void ClearBadlyDamagedShips()\r
385         {\r
386             BadlyDamagedShips = new string[0];\r
387         }\r
388 \r
389         public void SetEscapedShips(List<int> ships)\r
390         {\r
391             _escapedShips.AddRange(ships);\r
392         }\r
393 \r
394         public void ClearEscapedShips()\r
395         {\r
396             _escapedShips.Clear();\r
397         }\r
398     }\r
399 }