OSDN Git Service

4382d0f98d5476e07bd9fad70c411de14212d034
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / BaseAirCoprs.cs
1 // Copyright (C) 2016 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.Collections.Generic;\r
16 using System.Linq;\r
17 using static System.Math;\r
18 \r
19 namespace KancolleSniffer\r
20 {\r
21     public class BaseAirCoprs\r
22     {\r
23         private readonly ItemInfo _itemInfo;\r
24         private List<int> _relocationgPlanes;\r
25 \r
26         public BaseAirCoprs(ItemInfo item)\r
27         {\r
28             _itemInfo = item;\r
29         }\r
30 \r
31         public AirCorpsInfo[] AirCorps { get; set; }\r
32 \r
33         public class AirCorpsInfo\r
34         {\r
35             public int Distance { get; set; }\r
36             public int Action { get; set; }\r
37             public PlaneInfo[] Planes { get; set; }\r
38 \r
39             public string ActionName\r
40             {\r
41                 get\r
42                 {\r
43                     switch (Action)\r
44                     {\r
45                         case 0:\r
46                             return "待機";\r
47                         case 1:\r
48                             return "出撃";\r
49                         case 2:\r
50                             return "防空";\r
51                         case 3:\r
52                             return "退避";\r
53                         case 4:\r
54                             return "休息";\r
55                         default:\r
56                             return "";\r
57                     }\r
58                 }\r
59             }\r
60 \r
61             public int[] FighterPower => Planes.Aggregate(new[] {0, 0}, (prev, plane) =>\r
62             {\r
63                 if (plane.State != 1)\r
64                     return prev;\r
65                 var slot = plane.Slot;\r
66                 var unskilled = (slot.Spec.AntiAir + slot.Spec.Interception * 1.5 + slot.FighterPowerLevelBonus) *\r
67                                 Sqrt(plane.Count);\r
68                 return new[]\r
69                 {\r
70                     prev[0] + (int)(unskilled + slot.AlvBonus[0]),\r
71                     prev[1] + (int)(unskilled + slot.AlvBonus[1])\r
72                 };\r
73             });\r
74         }\r
75 \r
76         public class PlaneInfo\r
77         {\r
78             public int State { get; set; }\r
79             public ItemStatus Slot { get; set; }\r
80             public int Count { get; set; }\r
81             public int MaxCount { get; set; }\r
82 \r
83             public string StateName\r
84             {\r
85                 get\r
86                 {\r
87                     switch (State)\r
88                     {\r
89                         case 0:\r
90                             return "未配備";\r
91                         case 1:\r
92                             return "配備中";\r
93                         case 2:\r
94                             return "配置転換中";\r
95                         default:\r
96                             return "";\r
97                     }\r
98                 }\r
99             }\r
100         }\r
101 \r
102         public void Inspect(dynamic json)\r
103         {\r
104             AirCorps = (from entry in (dynamic[])json\r
105                 select\r
106                     new AirCorpsInfo\r
107                     {\r
108                         Distance = (int)entry.api_distance,\r
109                         Action = (int)entry.api_action_kind,\r
110                         Planes = (from plane in (dynamic[])entry.api_plane_info\r
111                             select new PlaneInfo\r
112                             {\r
113                                 Slot = _itemInfo.GetStatus((int)plane.api_slotid),\r
114                                 State = (int)plane.api_state,\r
115                                 Count = plane.api_count() ? (int)plane.api_count : 0,\r
116                                 MaxCount = plane.api_max_count() ? (int)plane.api_max_count : 0,\r
117                             }).ToArray()\r
118                     }).ToArray();\r
119         }\r
120 \r
121         public void InspectSetPlane(string request, dynamic json)\r
122         {\r
123             if (AirCorps == null)\r
124                 return;\r
125             var values = HttpUtility.ParseQueryString(request);\r
126             var airCorps = AirCorps[int.Parse(values["api_base_id"]) - 1];\r
127             if (json.api_distance()) // 2016春イベにはない\r
128                 airCorps.Distance = (int)json.api_distance;\r
129             foreach (var planeInfo in json.api_plane_info)\r
130             {\r
131                 var planeId = (int)planeInfo.api_squadron_id - 1;\r
132                 var prev = airCorps.Planes[planeId];\r
133                 if (prev.Slot.Id != -1)\r
134                     _relocationgPlanes.Add(prev.Slot.Id);\r
135                 airCorps.Planes[planeId] = new PlaneInfo\r
136                 {\r
137                     Slot = _itemInfo.GetStatus((int)planeInfo.api_slotid),\r
138                     State = (int)planeInfo.api_state,\r
139                     Count = planeInfo.api_count() ? (int)planeInfo.api_count : 0,\r
140                     MaxCount = planeInfo.api_max_count() ? (int)planeInfo.api_max_count : 0,\r
141                 };\r
142             }\r
143         }\r
144 \r
145         public void InspectSupply(string request, dynamic json)\r
146         {\r
147             InspectSetPlane(request, json);\r
148         }\r
149 \r
150         public void InspectSetAction(string request)\r
151         {\r
152             if (AirCorps == null)\r
153                 return;\r
154             var values = HttpUtility.ParseQueryString(request);\r
155             foreach (var entry in\r
156                 values["api_base_id"].Split(',')\r
157                     .Zip(values["api_action_kind"].Split(','), (b, a) => new {baseId = b, action = a}))\r
158             {\r
159                 AirCorps[int.Parse(entry.baseId) - 1].Action = int.Parse(entry.action);\r
160             }\r
161         }\r
162 \r
163         public void InspectEventObject(dynamic json)\r
164         {\r
165             _relocationgPlanes = json.api_base_convert_slot()\r
166                 ? new List<int>((int[])json.api_base_convert_slot)\r
167                 : new List<int>();\r
168         }\r
169 \r
170         public void SetItemHolder()\r
171         {\r
172             if (AirCorps == null)\r
173                 return;\r
174             var name = new[] {"第一", "第二", "第三"};\r
175             var i = 0;\r
176             foreach (var airCorps in AirCorps)\r
177             {\r
178                 if (i >= name.Length)\r
179                     break;\r
180                 var ship = new ShipStatus {Id = 1000 + i, Spec = new ShipSpec {Name = name[i++] + "基地航空隊"}};\r
181                 foreach (var plane in airCorps.Planes)\r
182                 {\r
183                     if (plane.State != 1)\r
184                         continue;\r
185                     _itemInfo.GetStatus(plane.Slot.Id).Holder = ship;\r
186                 }\r
187             }\r
188             var relocating = new ShipStatus {Id = 1500, Spec = new ShipSpec {Name = "配置転換中"}};\r
189             foreach (var id in _relocationgPlanes)\r
190                 _itemInfo.GetStatus(id).Holder = relocating;\r
191         }\r
192     }\r
193 }