OSDN Git Service

海域ゲージ情報で7-2の2本目のゲージが3になるのを直す
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Model / MiscTextInfo.cs
1 // Copyright (C) 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.Collections.Generic;\r
16 using System.Linq;\r
17 \r
18 namespace KancolleSniffer.Model\r
19 {\r
20     public class MiscTextInfo\r
21     {\r
22         private readonly ShipInfo _shipInfo;\r
23         private readonly ItemInfo _itemInfo;\r
24         private bool _inSortie;\r
25         private readonly Dictionary<int, int> _gaugeCount = new Dictionary<int, int>();\r
26         private readonly Dictionary<int, string> _furniture = new Dictionary<int, string>();\r
27 \r
28         private const string GuideText =\r
29             "[海域ゲージ情報]\r\n 海域選択画面に進むと表示します。\r\n[演習情報]\r\n 演習相手を選ぶと表示します。\r\n[獲得アイテム]\r\n 帰投したときに表示します。";\r
30 \r
31         public string Text { get; private set; } = GuideText;\r
32 \r
33         public MiscTextInfo(ShipInfo shipInfo, ItemInfo itemInfo)\r
34         {\r
35             _shipInfo = shipInfo;\r
36             _itemInfo = itemInfo;\r
37         }\r
38 \r
39         public void Port()\r
40         {\r
41             if (_inSortie)\r
42             {\r
43                 _inSortie = false;\r
44                 var text = GenerateItemGetText();\r
45                 Text = text == "" ? GuideText : "[獲得アイテム]\r\n" + text;\r
46             }\r
47             _items.Clear();\r
48         }\r
49 \r
50         public void InspectMaster(dynamic json)\r
51         {\r
52             if (json.api_mst_mapinfo())\r
53             {\r
54                 foreach (var entry in json.api_mst_mapinfo)\r
55                 {\r
56                     if (entry.api_required_defeat_count != null)\r
57                         _gaugeCount[(int)entry.api_id] = (int)entry.api_required_defeat_count;\r
58                 }\r
59             }\r
60             if (json.api_mst_furniture())\r
61             {\r
62                 foreach (var entry in json.api_mst_furniture)\r
63                     _furniture[(int)entry.api_id] = (string)entry.api_title;\r
64             }\r
65         }\r
66 \r
67         public void InspectMapInfo(dynamic json)\r
68         {\r
69             Text = "[海域ゲージ]\r\n";\r
70             foreach (var entry in json.api_map_info() ? json.api_map_info : json)\r
71             {\r
72                 var map = (int)entry.api_id;\r
73                 if (entry.api_eventmap())\r
74                 {\r
75                     var eventMap = entry.api_eventmap;\r
76                     Text +=\r
77                         $"{map / 10}-{map % 10} : HP {(int)eventMap.api_now_maphp}/{(int)eventMap.api_max_maphp}\r\n";\r
78                     continue;\r
79                 }\r
80                 if (!entry.api_defeat_count())\r
81                     continue;\r
82                 var max = entry.api_required_defeat_count()\r
83                     ? (int)entry.api_required_defeat_count\r
84                     : _gaugeCount[map];\r
85                 var count = $"{max - (int)entry.api_defeat_count}/{max}";\r
86                 Text += $"{map / 10}-{map % 10} : 残り {count}\r\n";\r
87             }\r
88         }\r
89 \r
90         public void InspectPracticeEnemyInfo(dynamic json)\r
91         {\r
92             Text = $"[演習情報]\r\n敵艦隊名 : {json.api_deckname}\r\n";\r
93             var ships = json.api_deck.api_ships;\r
94             var s1 = (int)ships[0].api_id != -1 ? (int)ships[0].api_level : 1;\r
95             var s2 = (int)ships[1].api_id != -1 ? (int)ships[1].api_level : 1;\r
96             var exp = PracticeExp.GetExp(s1, s2);\r
97             var bonus = PracticeExp.TrainingCruiserBonus(_shipInfo.Fleets[0].Ships);\r
98             Text += $"獲得経験値 : {(int)(exp * bonus)}\r\nS勝利 : {(int)((int)(exp * 1.2) * bonus)}";\r
99         }\r
100 \r
101         public void InspectMapNext(dynamic json)\r
102         {\r
103             if (json.api_airsearch() && (int)json.api_airsearch.api_result != 0)\r
104             {\r
105                 var item = json.api_itemget;\r
106                 AddItemCount((int)item.api_usemst + 100, (int)item.api_id, (int)item.api_getcount);\r
107                 return;\r
108             }\r
109             if (json.api_itemget())\r
110             {\r
111                 foreach (var item in json.api_itemget)\r
112                     AddItemCount((int)item.api_usemst, (int)item.api_id, (int)item.api_getcount);\r
113             }\r
114             if (json.api_itemget_eo_result())\r
115             {\r
116                 var eo = json.api_itemget_eo_result;\r
117                 AddItemCount((int)eo.api_usemst, (int)eo.api_id, (int)eo.api_getcount);\r
118             }\r
119             if (json.api_itemget_eo_comment())\r
120             {\r
121                 var eo = json.api_itemget_eo_comment;\r
122                 AddItemCount((int)eo.api_usemst, (int)eo.api_id, (int)eo.api_getcount);\r
123             }\r
124             if (json.api_eventmap() && json.api_eventmap.api_itemget())\r
125             {\r
126                 foreach (var item in json.api_eventmap.api_itemget)\r
127                 {\r
128                     var type = (int)item.api_type;\r
129                     type = type == 1 ? 5 :\r
130                         type == 5 ? 6 : type;\r
131                     AddItemCount(type, (int)item.api_id, (int)item.api_value);\r
132                 }\r
133             }\r
134         }\r
135 \r
136         public void InspectMapStart(dynamic json)\r
137         {\r
138             _inSortie = true;\r
139             InspectMapNext(json);\r
140         }\r
141 \r
142         public void InspectBattleResult(dynamic json)\r
143         {\r
144             if (json.api_get_eventitem())\r
145             {\r
146                 foreach (var item in json.api_get_eventitem)\r
147                 {\r
148                     var type = (int)item.api_type;\r
149                     type = type == 1 ? 5 :\r
150                         type == 5 ? 6 : type;\r
151                     var id = (int)item.api_id;\r
152                     AddItemCount(type, id, (int)item.api_value);\r
153                 }\r
154             }\r
155             if (json.api_mapcell_incentive() && (int)json.api_mapcell_incentive == 1)\r
156             {\r
157                 foreach (var type in _items.Keys.Where(type => type > 100).ToArray())\r
158                 {\r
159                     foreach (var id in _items[type])\r
160                         AddItemCount(type - 100, id.Key, id.Value);\r
161                 }\r
162             }\r
163         }\r
164 \r
165         private readonly Dictionary<int, SortedDictionary<int, int>> _items =\r
166             new Dictionary<int, SortedDictionary<int, int>>();\r
167 \r
168         private void AddItemCount(int type, int id, int count)\r
169         {\r
170             if (!_items.ContainsKey(type))\r
171                 _items[type] = new SortedDictionary<int, int>();\r
172             var dict = _items[type];\r
173             if (!dict.ContainsKey(id))\r
174                 dict[id] = 0;\r
175             dict[id] += count;\r
176         }\r
177 \r
178         private string GetName(int type, int id)\r
179         {\r
180             switch (type)\r
181             {\r
182                 case 2:\r
183                     return _shipInfo.GetSpec(id).Name;\r
184                 case 3:\r
185                     return _itemInfo.GetSpecByItemId(id).Name;\r
186                 case 4:\r
187                     return new[] {"燃料", "弾薬", "鋼材", "ボーキサイト", "高速建造材", "高速修復材", "開発資材", "改修資材"}[id - 1];\r
188                 case 5:\r
189                     return _itemInfo.GetUseItemName(id);\r
190                 case 6:\r
191                     return _furniture[id];\r
192                 default:\r
193                     return "";\r
194             }\r
195         }\r
196 \r
197         private string GenerateItemGetText()\r
198         {\r
199             return string.Join("\r\n",\r
200                 new[] {4, 5, 3, 6, 2}.Where(_items.ContainsKey).SelectMany(type =>\r
201                     _items[type].Select(pair => GetName(type, pair.Key) + ": " + pair.Value)));\r
202         }\r
203     }\r
204 }