コミットメタ情報

リビジョンf6a846d3a20007bc4ad5768d71d0eacede7dda76 (tree)
日時2018-07-30 21:29:32
作者Kazuhiro Fujieda <fujieda@user...>
コミッターKazuhiro Fujieda

ログメッセージ

ShipInfoとItemInfoから辞書を分離する

変更サマリ

差分

--- a/KancolleSniffer.Test/BattleTest.cs
+++ b/KancolleSniffer.Test/BattleTest.cs
@@ -36,8 +36,8 @@ namespace KancolleSniffer.Test
3636 [TestInitialize]
3737 public void Initialize()
3838 {
39- _itemInfo = new ItemInfo(new ItemMaster());
40- _shipInfo = new ShipInfo(new ShipMaster(), _itemInfo);
39+ _itemInfo = new ItemInfo(new ItemMaster(), new ItemInventry());
40+ _shipInfo = new ShipInfo(new ShipMaster(), new ShipInventry(), _itemInfo);
4141 _battleInfo = new BattleInfo(_shipInfo, _itemInfo);
4242 }
4343
--- a/KancolleSniffer.Test/QuestInfoTest.cs
+++ b/KancolleSniffer.Test/QuestInfoTest.cs
@@ -1087,7 +1087,7 @@ namespace KancolleSniffer.Test
10871087 [TestMethod]
10881088 public void DestroyItem_613_638_643_645_663_673_674_675_676_677_678()
10891089 {
1090- var itemInfo = new ItemInfo(new ItemMaster());
1090+ var itemInfo = new ItemInfo(new ItemMaster(), new ItemInventry());
10911091 var questInfo = new QuestInfo(itemInfo, null, () => new DateTime(2015, 1, 1)) {AcceptMax = 12};
10921092
10931093 itemInfo.InjectItemSpec(new[]
--- a/KancolleSniffer/KancolleSniffer.csproj
+++ b/KancolleSniffer/KancolleSniffer.csproj
@@ -58,6 +58,7 @@
5858 <Compile Include="Model\AkashiTimer.cs" />
5959 <Compile Include="Model\AlarmTimer.cs" />
6060 <Compile Include="Model\ItemMaster.cs" />
61+ <Compile Include="Model\Inventry.cs" />
6162 <Compile Include="Model\ItemSpec.cs" />
6263 <Compile Include="Model\ItemStatus.cs" />
6364 <Compile Include="Model\ShipSpec.cs" />
--- /dev/null
+++ b/KancolleSniffer/Model/Inventry.cs
@@ -0,0 +1,115 @@
1+// Copyright (C) 2018 Kazuhiro Fujieda <fujieda@users.osdn.me>
2+//
3+// Licensed under the Apache License, Version 2.0 (the "License");
4+// you may not use this file except in compliance with the License.
5+// You may obtain a copy of the License at
6+//
7+// http://www.apache.org/licenses/LICENSE-2.0
8+//
9+// Unless required by applicable law or agreed to in writing, software
10+// distributed under the License is distributed on an "AS IS" BASIS,
11+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+// See the License for the specific language governing permissions and
13+// limitations under the License.
14+
15+using System;
16+using System.Collections.Generic;
17+using System.Linq;
18+
19+namespace KancolleSniffer.Model
20+{
21+ public class ShipInventry : Inventry<ShipStatus>
22+ {
23+ public ShipInventry() : base(new ShipStatus())
24+ {
25+ }
26+
27+ protected override ShipStatus CreateDummy(int id) => new ShipStatus();
28+
29+ protected override int GetId(ShipStatus ship) => ship.Id;
30+
31+ public IEnumerable<ShipStatus> AllShips => AllItems;
32+ }
33+
34+ public class ItemInventry : Inventry<ItemStatus>
35+ {
36+ public ItemInventry() : base(new ItemStatus())
37+ {
38+ }
39+
40+ protected override ItemStatus CreateDummy(int id) => new ItemStatus(id);
41+
42+ protected override int GetId(ItemStatus item) => item.Id;
43+ }
44+
45+ public abstract class Inventry<T>
46+ {
47+ private readonly Dictionary<int, T> _dict = new Dictionary<int, T>();
48+
49+ protected abstract T CreateDummy(int id);
50+
51+ protected Inventry(T dummy)
52+ {
53+ _dict[-1] = dummy;
54+ }
55+
56+ public void Clear()
57+ {
58+ _dict.Clear();
59+ _dict[-1] = CreateDummy(-1);
60+ }
61+
62+ public virtual T this[int id]
63+ {
64+ get => _dict.TryGetValue(id, out var item) ? item : CreateDummy(id);
65+ set => _dict[id] = value;
66+ }
67+
68+ protected abstract int GetId(T item);
69+
70+ public void Add(T item)
71+ {
72+ _dict[GetId(item)] = item;
73+ }
74+
75+ public void Add(IEnumerable<T> items)
76+ {
77+ foreach (var item in items)
78+ Add(item);
79+ }
80+
81+ public void Remove(int id)
82+ {
83+ if (id != -1)
84+ _dict.Remove(id);
85+ }
86+
87+ public void Remove(IEnumerable<int> ids)
88+ {
89+ foreach (var id in ids)
90+ Remove(id);
91+ }
92+
93+ public void Remove(T item)
94+ {
95+ Remove(GetId(item));
96+ }
97+
98+ public void Remove(IEnumerable<T> items)
99+ {
100+ foreach (var item in items)
101+ Remove(item);
102+ }
103+
104+ public bool Contains(int id) => _dict.ContainsKey(id);
105+
106+ public bool Contains(T item) => Contains(GetId(item));
107+
108+ public IEnumerable<T> AllItems =>
109+ from kv in _dict where kv.Key != -1 select kv.Value;
110+
111+ public int Count => _dict.Count - 1;
112+
113+ public int MaxId => Math.Max(_dict.Keys.Max(), 0);
114+ }
115+}
\ No newline at end of file
--- a/KancolleSniffer/Model/ItemInfo.cs
+++ b/KancolleSniffer/Model/ItemInfo.cs
@@ -22,7 +22,7 @@ namespace KancolleSniffer.Model
2222 {
2323 private int _nowShips, _nowEquips;
2424 private readonly ItemMaster _itemMaster;
25- private readonly Dictionary<int, ItemStatus> _itemInfo = new Dictionary<int, ItemStatus>();
25+ private readonly ItemInventry _itemInventry;
2626 public int MaxShips { get; private set; }
2727 public int MarginShips { get; set; }
2828 public bool AlarmShips { get; set; }
@@ -62,9 +62,10 @@ namespace KancolleSniffer.Model
6262
6363 public bool TooManyEquips => MaxEquips != 0 && NowEquips >= MaxEquips - MarginEquips;
6464
65- public ItemInfo(ItemMaster itemMaster)
65+ public ItemInfo(ItemMaster itemMaster, ItemInventry itemInventry)
6666 {
6767 _itemMaster = itemMaster;
68+ _itemInventry = itemInventry;
6869 MarginShips = 4;
6970 MarginEquips = 10;
7071 }
@@ -88,21 +89,18 @@ namespace KancolleSniffer.Model
8889 if (!json.IsArray)
8990 json = new[] {json};
9091 if (full)
91- {
92- _itemInfo.Clear();
93- _itemInfo[-1] = new ItemStatus();
94- }
92+ _itemInventry.Clear();
9593 foreach (var entry in json)
9694 {
9795 var id = (int)entry.api_id;
98- _itemInfo[id] = new ItemStatus(id)
96+ _itemInventry[id] = new ItemStatus(id)
9997 {
10098 Spec = _itemMaster[(int)entry.api_slotitem_id],
10199 Level = entry.api_level() ? (int)entry.api_level : 0,
102100 Alv = entry.api_alv() ? (int)entry.api_alv : 0
103101 };
104102 }
105- NowEquips = _itemInfo.Count - 1;
103+ NowEquips = _itemInventry.Count;
106104 }
107105
108106 public void InspectCreateItem(dynamic json)
@@ -142,11 +140,8 @@ namespace KancolleSniffer.Model
142140
143141 private void DeleteItems(IEnumerable<int> ids)
144142 {
145- foreach (var id in ids.Where(id => id != -1))
146- {
147- _itemInfo.Remove(id);
148- NowEquips--;
149- }
143+ _itemInventry.Remove(ids);
144+ NowEquips = _itemInventry.Count;
150145 }
151146
152147 public ItemSpec GetSpecByItemId(int id) => _itemMaster[id];
@@ -155,16 +150,16 @@ namespace KancolleSniffer.Model
155150
156151 public ItemStatus GetStatus(int id)
157152 {
158- return _itemInfo.TryGetValue(id, out var item) ? item : new ItemStatus(id);
153+ return _itemInventry[id];
159154 }
160155
161156 public void ClearHolder()
162157 {
163- foreach (var item in _itemInfo.Values)
158+ foreach (var item in _itemInventry.AllItems)
164159 item.Holder = new ShipStatus();
165160 }
166161
167- public ItemStatus[] ItemList => (from e in _itemInfo where e.Key != -1 select e.Value).ToArray();
162+ public ItemStatus[] ItemList => _itemInventry.AllItems.ToArray();
168163
169164 public string GetUseItemName(int id) => _itemMaster.GetUseItemName(id);
170165
@@ -176,7 +171,7 @@ namespace KancolleSniffer.Model
176171
177172 public ItemStatus[] InjectItems(IEnumerable<int> itemIds)
178173 {
179- var id = _itemInfo.Keys.Count + 1;
174+ var id = _itemInventry.MaxId + 1;
180175 return itemIds.Select(itemId =>
181176 {
182177 var spec = _itemMaster[itemId];
@@ -186,7 +181,7 @@ namespace KancolleSniffer.Model
186181 _itemMaster[itemId] = spec;
187182 }
188183 var item = new ItemStatus {Id = id++, Spec = spec};
189- _itemInfo.Add(item.Id, item);
184+ _itemInventry.Add(item);
190185 return item;
191186 }).ToArray();
192187 }
--- a/KancolleSniffer/Model/ShipInfo.cs
+++ b/KancolleSniffer/Model/ShipInfo.cs
@@ -26,8 +26,8 @@ namespace KancolleSniffer.Model
2626 public const int MemberCount = 6;
2727
2828 private readonly Fleet[] _fleets;
29- private readonly Dictionary<int, ShipStatus> _shipInfo = new Dictionary<int, ShipStatus>();
3029 private readonly ShipMaster _shipMaster;
30+ private readonly ShipInventry _shipInventry;
3131 private readonly ItemInfo _itemInfo;
3232 private readonly List<int> _escapedShips = new List<int>();
3333 private ShipStatus[] _battleResult = new ShipStatus[0];
@@ -63,25 +63,25 @@ namespace KancolleSniffer.Model
6363 }
6464 }
6565
66- public ShipInfo(ShipMaster shipMaster, ItemInfo itemInfo)
66+ public ShipInfo(ShipMaster shipMaster, ShipInventry shipInventry, ItemInfo itemInfo)
6767 {
6868 _shipMaster = shipMaster;
69+ _shipInventry = shipInventry;
6970 _fleets = Enumerable.Range(0, FleetCount).Select((x, i) => new Fleet(this, i)).ToArray();
7071 _itemInfo = itemInfo;
71- ClearShipInfo();
7272 }
7373
7474 public void InspectMaster(dynamic json)
7575 {
7676 _shipMaster.Inspect(json);
77- ClearShipInfo();
77+ _shipInventry.Clear();
7878 }
7979
8080 public void InspectShip(dynamic json)
8181 {
8282 if (json.api_deck_port()) // port
8383 {
84- ClearShipInfo();
84+ _shipInventry.Clear();
8585 for (var i = 0; i < FleetCount; i++)
8686 _fleets[i].State = FleetState.Port;
8787 InspectDeck(json.api_deck_port);
@@ -140,12 +140,6 @@ namespace KancolleSniffer.Model
140140 .SelectMany(fleet => fleet.Deck.Select(id => (ShipStatus)GetStatus(id).Clone())).ToArray();
141141 }
142142
143- private void ClearShipInfo()
144- {
145- _shipInfo.Clear();
146- _shipInfo[-1] = new ShipStatus();
147- }
148-
149143 public void InspectDeck(dynamic json)
150144 {
151145 foreach (var entry in json)
@@ -186,10 +180,10 @@ namespace KancolleSniffer.Model
186180 Lucky = (int)entry.api_lucky[0],
187181 Locked = entry.api_locked() && entry.api_locked == 1
188182 };
189- _shipInfo[id] = ship;
183+ _shipInventry.Add(ship);
190184 _numEquipsChecker.Check(ship);
191185 }
192- _numEquipsChecker.MaxId = _shipInfo.Keys.Max();
186+ _numEquipsChecker.MaxId = _shipInventry.MaxId;
193187 }
194188
195189 private void InspectBasic(dynamic json)
@@ -201,7 +195,7 @@ namespace KancolleSniffer.Model
201195 {
202196 foreach (var entry in json.api_ship)
203197 {
204- var status = _shipInfo[(int)entry.api_id];
198+ var status = _shipInventry[(int)entry.api_id];
205199 status.Bull = (int)entry.api_bull;
206200 status.Fuel = (int)entry.api_fuel;
207201 status.OnSlot = (from num in (dynamic[])entry.api_onslot select (int)num).ToArray();
@@ -267,12 +261,11 @@ namespace KancolleSniffer.Model
267261 {
268262 var values = HttpUtility.ParseQueryString(request);
269263 var ships = values["api_id_items"].Split(',').Select(int.Parse).ToArray();
270- if (!_shipInfo.ContainsKey(ships[0])) // 二重に実行された場合
264+ if (!_shipInventry.Contains(ships[0])) // 二重に実行された場合
271265 return;
272266 _itemInfo.NowShips -= ships.Length;
273- _itemInfo.DeleteItems(ships.SelectMany(id => _shipInfo[id].Slot).ToArray());
274- foreach (var id in ships)
275- _shipInfo.Remove(id);
267+ _itemInfo.DeleteItems(ships.SelectMany(id => _shipInventry[id].Slot).ToArray());
268+ _shipInventry.Remove(ships);
276269 InspectDeck(json.api_deck);
277270 InspectShip(json);
278271 }
@@ -281,7 +274,7 @@ namespace KancolleSniffer.Model
281274 {
282275 var values = HttpUtility.ParseQueryString(request);
283276 var ship = int.Parse(values["api_id"]);
284- _shipInfo[ship].Slot = ((int[])json.api_slot).Select(id => new ItemStatus(id)).ToArray();
277+ _shipInventry[ship].Slot = ((int[])json.api_slot).Select(id => new ItemStatus(id)).ToArray();
285278 }
286279
287280 public void InspectSlotDeprive(dynamic json)
@@ -293,15 +286,15 @@ namespace KancolleSniffer.Model
293286 {
294287 var values = HttpUtility.ParseQueryString(request);
295288 var delitem = int.Parse(values["api_slot_dest_flag"] ?? "0") == 1;
296- foreach (var ship in values["api_ship_id"].Split(',').Select(int.Parse))
289+ foreach (var id in values["api_ship_id"].Split(',').Select(int.Parse))
297290 {
298291 _itemInfo.NowShips--;
299292 if (delitem)
300- _itemInfo.DeleteItems(_shipInfo[ship].AllSlot);
301- var of = FindFleet(ship, out var oi);
293+ _itemInfo.DeleteItems(_shipInventry[id].AllSlot);
294+ var of = FindFleet(id, out var oi);
302295 if (of != null)
303296 WithdrowShip(of, oi);
304- _shipInfo.Remove(ship);
297+ _shipInventry.Remove(id);
305298 }
306299 }
307300
@@ -335,28 +328,30 @@ namespace KancolleSniffer.Model
335328
336329 public void RepairShip(int id)
337330 {
338- var s = _shipInfo[id];
331+ var s = _shipInventry[id];
339332 s.NowHp = s.MaxHp;
340333 s.Cond = Max(40, s.Cond);
341334 }
342335
343336 public Fleet[] Fleets => _fleets;
344337
345- public ShipStatus GetStatus(int id)
338+ public ShipStatus GetStatus(int id) => FillUp(_shipInventry[id]);
339+
340+ private ShipStatus FillUp(ShipStatus ship)
346341 {
347- if (!_shipInfo.TryGetValue(id, out var s))
348- return new ShipStatus();
349- s.Slot = s.Slot.Select(item => _itemInfo.GetStatus(item.Id)).ToArray();
350- s.SlotEx = _itemInfo.GetStatus(s.SlotEx.Id);
351- s.Escaped = _escapedShips.Contains(id);
352- s.Fleet = FindFleet(s.Id, out var idx);
353- s.DeckIndex = idx;
354- return s;
342+ if (ship.Empty)
343+ return ship;
344+ ship.Slot = ship.Slot.Select(item => _itemInfo.GetStatus(item.Id)).ToArray();
345+ ship.SlotEx = _itemInfo.GetStatus(ship.SlotEx.Id);
346+ ship.Escaped = _escapedShips.Contains(ship.Id);
347+ ship.Fleet = FindFleet(ship.Id, out var idx);
348+ ship.DeckIndex = idx;
349+ return ship;
355350 }
356351
357352 public void SetItemHolder()
358353 {
359- foreach (var ship in _shipInfo.Values)
354+ foreach (var ship in _shipInventry.AllShips)
360355 {
361356 foreach (var item in ship.Slot)
362357 _itemInfo.GetStatus(item.Id).Holder = ship;
@@ -366,7 +361,7 @@ namespace KancolleSniffer.Model
366361
367362 public ShipSpec GetSpec(int id) => _shipMaster.GetSpec(id);
368363
369- public ShipStatus[] ShipList => _shipInfo.Keys.Where(id => id != -1).Select(GetStatus).ToArray();
364+ public ShipStatus[] ShipList => _shipInventry.AllShips.Select(FillUp).ToArray();
370365
371366 public ShipStatus[] GetRepairList(DockInfo dockInfo)
372367 => (from s in ShipList
@@ -422,12 +417,11 @@ namespace KancolleSniffer.Model
422417
423418 private void InjectShips(int deck, int[] nowhps, int[] maxhps, int[][] slots)
424419 {
425- var id = _shipInfo.Keys.Count + 1;
420+ var id = _shipInventry.MaxId + 1;
426421 var ships = nowhps.Zip(maxhps,
427422 (now, max) => new ShipStatus {Id = id++, NowHp = now, MaxHp = max}).ToArray();
428423 _fleets[deck].Deck = (from ship in ships select ship.Id).ToArray();
429- foreach (var ship in ships)
430- _shipInfo[ship.Id] = ship;
424+ _shipInventry.Add(ships);
431425 foreach (var entry in ships.Zip(slots, (ship, slot) => new {ship, slot}))
432426 {
433427 entry.ship.Slot = _itemInfo.InjectItems(entry.slot.Take(5));
--- a/KancolleSniffer/Sniffer.cs
+++ b/KancolleSniffer/Sniffer.cs
@@ -25,12 +25,14 @@ namespace KancolleSniffer
2525 {
2626 private bool _start;
2727 private readonly ItemMaster _itemMaster = new ItemMaster();
28+ private readonly ItemInventry _itemInventry = new ItemInventry();
2829 private readonly ItemInfo _itemInfo;
30+ private readonly ShipMaster _shipMaster = new ShipMaster();
31+ private readonly ShipInventry _shipInventry = new ShipInventry();
32+ private readonly ShipInfo _shipInfo;
2933 private readonly MaterialInfo _materialInfo = new MaterialInfo();
3034 private readonly QuestInfo _questInfo;
3135 private readonly MissionInfo _missionInfo = new MissionInfo();
32- private readonly ShipMaster _shipMaster = new ShipMaster();
33- private readonly ShipInfo _shipInfo;
3436 private readonly ConditionTimer _conditionTimer;
3537 private readonly DockInfo _dockInfo;
3638 private readonly AkashiTimer _akashiTimer;
@@ -77,8 +79,8 @@ namespace KancolleSniffer
7779 public Sniffer(bool start = false)
7880 {
7981 _start = start;
80- _itemInfo = new ItemInfo(_itemMaster);
81- _shipInfo = new ShipInfo(_shipMaster, _itemInfo);
82+ _itemInfo = new ItemInfo(_itemMaster, _itemInventry);
83+ _shipInfo = new ShipInfo(_shipMaster, _shipInventry, _itemInfo);
8284 _conditionTimer = new ConditionTimer(_shipInfo);
8385 _dockInfo = new DockInfo(_shipInfo, _materialInfo);
8486 _akashiTimer = new AkashiTimer(_shipInfo, _dockInfo, _presetDeck);
旧リポジトリブラウザで表示