OSDN Git Service

ReSharperの指摘に従い不要なメソッド等を消す
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Model / ItemInfo.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.Collections.Generic;\r
16 using System.Linq;\r
17 using KancolleSniffer.Util;\r
18 \r
19 namespace KancolleSniffer.Model\r
20 {\r
21     public class ItemInfo\r
22     {\r
23         private readonly ItemMaster _itemMaster;\r
24         private readonly ItemInventry _itemInventry;\r
25         public AlarmCounter Counter { get; }\r
26 \r
27         public ItemInfo(ItemMaster itemMaster, ItemInventry itemInventry)\r
28         {\r
29             _itemMaster = itemMaster;\r
30             _itemInventry = itemInventry;\r
31             Counter = new AlarmCounter(() => _itemInventry.Count) {Margin = 5};\r
32         }\r
33 \r
34         public void InspectBasic(dynamic json)\r
35         {\r
36             Counter.Max = (int)json.api_max_slotitem;\r
37         }\r
38 \r
39         public void InspectMaster(dynamic json)\r
40         {\r
41             _itemMaster.InspectMaster(json);\r
42         }\r
43 \r
44         public void InspectSlotItem(dynamic json, bool full = false)\r
45         {\r
46             if (!json.IsArray)\r
47                 json = new[] {json};\r
48             if (full)\r
49                 _itemInventry.Clear();\r
50             foreach (var entry in json)\r
51             {\r
52                 var id = (int)entry.api_id;\r
53                 _itemInventry[id] = new ItemStatus(id)\r
54                 {\r
55                     Spec = _itemMaster[(int)entry.api_slotitem_id],\r
56                     Level = entry.api_level() ? (int)entry.api_level : 0,\r
57                     Alv = entry.api_alv() ? (int)entry.api_alv : 0\r
58                 };\r
59             }\r
60         }\r
61 \r
62         public void InspectCreateItem(dynamic json)\r
63         {\r
64             if (!json.IsDefined("api_slot_item"))\r
65                 return;\r
66             InspectSlotItem(json.api_slot_item);\r
67         }\r
68 \r
69         public void InspectGetShip(dynamic json)\r
70         {\r
71             if (json.api_slotitem == null) // まるゆにはスロットがない\r
72                 return;\r
73             InspectSlotItem(json.api_slotitem);\r
74         }\r
75 \r
76         public void InspectDestroyItem(string request, dynamic json)\r
77         {\r
78             var values = HttpUtility.ParseQueryString(request);\r
79             DeleteItems(values["api_slotitem_ids"].Split(',').Select(int.Parse).ToArray());\r
80         }\r
81 \r
82         public void InspectRemodelSlot(dynamic json)\r
83         {\r
84             if (json.api_after_slot())\r
85                 InspectSlotItem(json.api_after_slot);\r
86             if (!json.api_use_slot_id())\r
87                 return;\r
88             DeleteItems((int[])json.api_use_slot_id);\r
89         }\r
90 \r
91         private void DeleteItems(IEnumerable<int> ids)\r
92         {\r
93             _itemInventry.Remove(ids);\r
94         }\r
95 \r
96         public ItemSpec GetSpecByItemId(int id) => _itemMaster[id];\r
97 \r
98         public string GetName(int id) => GetStatus(id).Spec.Name;\r
99 \r
100         public ItemStatus GetStatus(int id)\r
101         {\r
102             return _itemInventry[id];\r
103         }\r
104 \r
105         public void ClearHolder()\r
106         {\r
107             foreach (var item in _itemInventry.AllItems)\r
108                 item.Holder = new ShipStatus();\r
109         }\r
110 \r
111         public ItemStatus[] ItemList => _itemInventry.AllItems.ToArray();\r
112 \r
113         public string GetUseItemName(int id) => _itemMaster.GetUseItemName(id);\r
114 \r
115         public void InjectItemSpec(IEnumerable<ItemSpec> specs)\r
116         {\r
117             foreach (var spec in specs)\r
118                 _itemMaster[spec.Id] = spec;\r
119         }\r
120 \r
121         public ItemStatus[] InjectItems(IEnumerable<int> itemIds)\r
122         {\r
123             var id = _itemInventry.MaxId + 1;\r
124             return itemIds.Select(itemId =>\r
125             {\r
126                 var spec = _itemMaster[itemId];\r
127                 if (spec.Empty)\r
128                 {\r
129                     spec = new ItemSpec {Id = itemId};\r
130                     _itemMaster[itemId] = spec;\r
131                 }\r
132                 var item = new ItemStatus {Id = id++, Spec = spec};\r
133                 _itemInventry.Add(item);\r
134                 return item;\r
135             }).ToArray();\r
136         }\r
137     }\r
138 }