OSDN Git Service

43d1868bfe0bccf9320b5cbdc4226472198b2786
[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.Linq;\r
16 using static System.Math;\r
17 \r
18 namespace KancolleSniffer\r
19 {\r
20     public class BaseAirCoprs\r
21     {\r
22         private readonly ItemInfo _itemInfo;\r
23 \r
24         public BaseAirCoprs(ItemInfo item)\r
25         {\r
26             _itemInfo = item;\r
27         }\r
28 \r
29         public AirCorpsInfo[] AirCorps { get; set; }\r
30 \r
31         public class AirCorpsInfo\r
32         {\r
33             public int Distance { get; set; }\r
34             public int Action { get; set; }\r
35             public PlaneInfo[] Planes { get; set; }\r
36 \r
37             public string ActionName\r
38             {\r
39                 get\r
40                 {\r
41                     switch (Action)\r
42                     {\r
43                         case 0:\r
44                             return "待機";\r
45                         case 1:\r
46                             return "出撃";\r
47                         case 2:\r
48                             return "防空";\r
49                         case 3:\r
50                             return "退避";\r
51                         case 4:\r
52                             return "休息";\r
53                         default:\r
54                             return "";\r
55                     }\r
56                 }\r
57             }\r
58 \r
59             public int[] FighterPower => Planes.Aggregate(new[] {0, 0}, (prev, plane) =>\r
60             {\r
61                 if (plane.State != 1)\r
62                     return prev;\r
63                 var slot = plane.Slot;\r
64                 var unskilled = (slot.Spec.AntiAir + slot.Spec.Interception * 1.5 + slot.FighterPowerLevelBonus) *\r
65                                 Sqrt(plane.Count);\r
66                 return new[]\r
67                 {\r
68                     prev[0] + (int)(unskilled + slot.AlvBonus[0]),\r
69                     prev[1] + (int)(unskilled + slot.AlvBonus[1])\r
70                 };\r
71             });\r
72         }\r
73 \r
74         public class PlaneInfo\r
75         {\r
76             public int State { get; set; }\r
77             public ItemStatus Slot { get; set; }\r
78             public int Count { get; set; }\r
79             public int MaxCount { get; set; }\r
80 \r
81             public string StateName\r
82             {\r
83                 get\r
84                 {\r
85                     switch (State)\r
86                     {\r
87                         case 0:\r
88                             return "未配備";\r
89                         case 1:\r
90                             return "配備中";\r
91                         case 2:\r
92                             return "配置転換中";\r
93                         default:\r
94                             return "";\r
95                     }\r
96                 }\r
97             }\r
98         }\r
99 \r
100         public void Inspect(dynamic json)\r
101         {\r
102             AirCorps = (from entry in (dynamic[])json\r
103                 select\r
104                     new AirCorpsInfo\r
105                     {\r
106                         Distance = (int)entry.api_distance,\r
107                         Action = (int)entry.api_action_kind,\r
108                         Planes = (from plane in (dynamic[])entry.api_plane_info\r
109                             select new PlaneInfo\r
110                             {\r
111                                 Slot = _itemInfo.GetStatus((int)plane.api_slotid),\r
112                                 State = (int)plane.api_state,\r
113                                 Count = plane.api_count() ? (int)plane.api_count : 0,\r
114                                 MaxCount = plane.api_max_count() ? (int)plane.api_max_count : 0,\r
115                             }).ToArray()\r
116                     }).ToArray();\r
117         }\r
118 \r
119         public void InspectSetPlane(string request, dynamic json)\r
120         {\r
121             if (AirCorps == null)\r
122                 return;\r
123             var values = HttpUtility.ParseQueryString(request);\r
124             var planeInfo = json.api_plane_info[0];\r
125             var airCorps = AirCorps[int.Parse(values["api_base_id"]) - 1];\r
126             airCorps.Distance = (int)json.api_distance;\r
127             airCorps.Planes[(int)planeInfo.api_squadron_id - 1] = new PlaneInfo\r
128             {\r
129                 Slot = _itemInfo.GetStatus((int)planeInfo.api_slotid),\r
130                 State = (int)planeInfo.api_state,\r
131                 Count = planeInfo.api_count() ? (int)planeInfo.api_count : 0,\r
132                 MaxCount = planeInfo.api_max_count() ? (int)planeInfo.api_max_count : 0,\r
133             };\r
134         }\r
135 \r
136         public void InspectSetAction(string request)\r
137         {\r
138             if (AirCorps == null)\r
139                 return;\r
140             var values = HttpUtility.ParseQueryString(request);\r
141             foreach (var entry in\r
142                 values["api_base_id"].Split(',')\r
143                     .Zip(values["api_action_kind"].Split(','), (b, a) => new {baseId = b, action = a}))\r
144             {\r
145                 AirCorps[int.Parse(entry.baseId) - 1].Action = int.Parse(entry.action);\r
146             }\r
147         }\r
148     }\r
149 }