OSDN Git Service

明石タイマーを実装する
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / ItemInfo.cs
1 // Copyright (C) 2013 Kazuhiro Fujieda <fujieda@users.sourceforge.jp>\r
2 // \r
3 // This program is part of KancolleSniffer.\r
4 //\r
5 // KancolleSniffer is free software: you can redistribute it and/or modify\r
6 // it under the terms of the GNU General Public License as published by\r
7 // the Free Software Foundation, either version 3 of the License, or\r
8 // (at your option) any later version.\r
9 //\r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 //\r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, see <http://www.gnu.org/licenses/>.\r
17 \r
18 using System.Collections.Generic;\r
19 using System.Web;\r
20 \r
21 namespace KancolleSniffer\r
22 {\r
23     public struct ItemSpec\r
24     {\r
25         public string Name;\r
26         public int TyKu;\r
27     }\r
28 \r
29     public class ItemInfo\r
30     {\r
31         private int _nowShips;\r
32         private readonly Dictionary<int, ItemSpec> _itemSpecs = new Dictionary<int, ItemSpec>();\r
33         private readonly Dictionary<int, int> _itemIds = new Dictionary<int, int>();\r
34 \r
35         public int MaxShips { get; private set; }\r
36         public int MarginShips { get; set; }\r
37         public bool NeedRing { get; set; }\r
38         public int NowItems { get; set; }\r
39         public int MaxItems { get; private set; }\r
40         public int NumBuckets { get; set; }\r
41 \r
42         public int NowShips\r
43         {\r
44             get { return _nowShips; }\r
45             set\r
46             {\r
47                 if (MaxShips != 0)\r
48                 {\r
49                     var limit = MaxShips - MarginShips;\r
50                     NeedRing = _nowShips < limit && value >= limit;\r
51                 }\r
52                 _nowShips = value;\r
53             }\r
54         }\r
55 \r
56         public bool TooManyShips\r
57         {\r
58             get { return MaxShips != 0 && NowShips >= MaxShips - MarginShips; }\r
59         }\r
60 \r
61         public ItemInfo()\r
62         {\r
63             MarginShips = 4;\r
64         }\r
65 \r
66         public void InspectBasic(dynamic json)\r
67         {\r
68             MaxShips = (int)json.api_max_chara;\r
69             MaxItems = (int)json.api_max_slotitem;\r
70         }\r
71 \r
72         public void InspectMaterial(dynamic json)\r
73         {\r
74             foreach (var entry in json)\r
75             {\r
76                 if ((int)entry.api_id != 6)\r
77                     continue;\r
78                 NumBuckets = (int)entry.api_value;\r
79             }\r
80         }\r
81 \r
82         public void InspectMaster(dynamic json)\r
83         {\r
84             foreach (var entry in json)\r
85             {\r
86                 _itemSpecs[(int)entry.api_id] = new ItemSpec\r
87                 {\r
88                     Name = (string)entry.api_name,\r
89                     TyKu = (int)entry.api_type[0] == 3 ? (int)entry.api_tyku : 0 // 艦載機のみ\r
90                 };\r
91             }\r
92         }\r
93 \r
94         public void InspectSlotItem(dynamic json, bool full = false)\r
95         {\r
96             if (!json.IsArray)\r
97                 json = new[] {json};\r
98             if (full)\r
99                 NowItems = ((object[])json).Length;\r
100             foreach (var entry in json)\r
101                 _itemIds[(int)entry.api_id] = (int)entry.api_slotitem_id;\r
102         }\r
103 \r
104         public void InspectCreateItem(dynamic json)\r
105         {\r
106             if (!json.IsDefined("api_slot_item"))\r
107                 return;\r
108             InspectSlotItem(json.api_slot_item);\r
109             NowItems++;\r
110         }\r
111 \r
112         public void InspectGetShip(dynamic json)\r
113         {\r
114             InspectSlotItem(json.api_slotitem);\r
115             NowItems += ((object[])json.api_slotitem).Length;\r
116             NowShips += 1;\r
117         }\r
118 \r
119         public void InspectDestroyItem(string request)\r
120         {\r
121             var values = HttpUtility.ParseQueryString(request);\r
122             NowItems -= values["api_slotitem_ids"].Split(',').Length;\r
123         }\r
124 \r
125         public ItemSpec this[int id]\r
126         {\r
127             get\r
128             {\r
129                 int itemId;\r
130                 ItemSpec spec;\r
131                 if (_itemIds.TryGetValue(id, out itemId) && _itemSpecs.TryGetValue(itemId, out spec))\r
132                     return spec;\r
133                 return new ItemSpec();\r
134             }\r
135         }\r
136     }\r
137 }