1 // Copyright (C) 2013, 2014 Kazuhiro Fujieda <fujieda@users.sourceforge.jp>
\r
3 // This program is part of KancolleSniffer.
\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
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
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
19 using System.Collections.Generic;
\r
22 namespace KancolleSniffer
\r
24 public struct ItemSpec
\r
30 public class ItemInfo
\r
32 private int _nowShips, _nowEquips;
\r
33 private readonly Dictionary<int, ItemSpec> _itemSpecs = new Dictionary<int, ItemSpec>();
\r
34 private readonly Dictionary<int, int> _itemIds = new Dictionary<int, int>();
\r
36 public int MaxShips { get; private set; }
\r
37 public int MarginShips { get; set; }
\r
38 public bool RingShips { get; set; }
\r
39 public int MaxEquips { get; private set; }
\r
40 public int MarginEquips { get; set; }
\r
41 public bool RingEquips { get; set; }
\r
42 public MaterialCount[] MaterialHistory { get; private set; }
\r
46 get { return _nowShips; }
\r
51 var limit = MaxShips - MarginShips;
\r
52 RingShips = _nowShips < limit && value >= limit;
\r
58 public bool TooManyShips
\r
60 get { return MaxShips != 0 && NowShips >= MaxShips - MarginShips; }
\r
63 public int NowEquips
\r
65 get { return _nowEquips; }
\r
70 var limit = MaxEquips - MarginEquips;
\r
71 RingEquips = _nowEquips < limit && value >= limit;
\r
77 public bool TooManyEquips
\r
79 get { return MaxEquips != 0 && NowEquips >= MaxEquips - MarginEquips; }
\r
84 MaterialHistory = new MaterialCount[Enum.GetValues(typeof (Material)).Length];
\r
85 foreach (Material m in Enum.GetValues(typeof (Material)))
\r
86 MaterialHistory[(int)m] = new MaterialCount();
\r
90 public void InspectBasic(dynamic json)
\r
92 MaxShips = (int)json.api_max_chara;
\r
93 MaxEquips = (int)json.api_max_slotitem;
\r
96 public void InspectMaterial(dynamic json)
\r
98 foreach (var entry in json)
\r
99 MaterialHistory[(int)entry.api_id - 1].Now = (int)entry.api_value;
\r
102 public void InspectMaster(dynamic json)
\r
104 foreach (var entry in json)
\r
106 _itemSpecs[(int)entry.api_id] = new ItemSpec
\r
108 Name = (string)entry.api_name,
\r
109 TyKu = (int)entry.api_type[0] == 3 || (int)entry.api_type[2] == 11 ? (int)entry.api_tyku : 0
\r
113 _itemSpecs[-1] = new ItemSpec();
\r
116 public void InspectSlotItem(dynamic json, bool full = false)
\r
119 json = new[] {json};
\r
121 NowEquips = ((object[])json).Length;
\r
122 foreach (var entry in json)
\r
123 _itemIds[(int)entry.api_id] = (int)entry.api_slotitem_id;
\r
127 public void InspectCreateItem(dynamic json)
\r
129 var m = (dynamic[])json.api_material;
\r
130 for (var i = 0; i < m.Length; i++)
\r
131 MaterialHistory[i].Now = (int)m[i];
\r
132 if (!json.IsDefined("api_slot_item"))
\r
134 InspectSlotItem(json.api_slot_item);
\r
138 public void InspectGetShip(dynamic json)
\r
141 if (json.api_slotitem == null) // まるゆにはスロットがない
\r
143 InspectSlotItem(json.api_slotitem);
\r
144 NowEquips += ((object[])json.api_slotitem).Length;
\r
147 public void InspectDestroyItem(string request, dynamic json)
\r
149 var values = HttpUtility.ParseQueryString(request);
\r
150 NowEquips -= values["api_slotitem_ids"].Split(',').Length;
\r
151 var get = (int[])json.api_get_material;
\r
152 for (var i = 0; i < get.Length; i++)
\r
153 MaterialHistory[i].Now += get[i];
\r
156 public void InspectMissionResult(dynamic json)
\r
158 if ((int)json.api_clear_result == 0) // 失敗
\r
160 var get = (int[])json.api_get_material;
\r
161 for (var i = 0; i < get.Length; i++)
\r
162 MaterialHistory[i].Now += get[i];
\r
165 public ItemSpec this[int id]
\r
167 get { return GetSpecById(id); }
\r
170 public ItemSpec GetSpecById(int id)
\r
173 if (!_itemIds.TryGetValue(id, out itemId))
\r
175 return _itemSpecs[itemId];
\r
178 public ItemSpec GetSpecByItemId(int id)
\r
180 return _itemSpecs[id];
\r
183 public void SaveState(Status status)
\r
185 status.MatreialHistory = MaterialHistory;
\r
188 public void LoadSate(Status status)
\r
190 if (status.MatreialHistory != null)
\r
191 status.MatreialHistory.CopyTo(MaterialHistory, 0);
\r
195 public enum Material
\r
207 public class MaterialCount
\r
211 public int BegOfDay { get; set; }
\r
212 public int BegOfWeek { get; set; }
\r
213 public DateTime LastSet { get; set; }
\r
217 get { return _now; }
\r
220 if (!Status.Restoring) // JSONから値を復旧するときは履歴に触らない
\r
223 LastSet = DateTime.Now;
\r
229 public void UpdateHistory()
\r
231 var morning = DateTime.Today.AddHours(5);
\r
232 var dow = (int)morning.DayOfWeek;
\r
233 var monday = morning.AddDays(dow == 0 ? -6 : -dow + 1);
\r
234 if (DateTime.Now >= morning && LastSet < morning)
\r
236 if (DateTime.Now >= monday && LastSet < monday)
\r