1 // Copyright (C) 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>
\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
23 namespace KancolleSniffer
\r
25 public class AkashiTimer
\r
27 private readonly ShipInfo _shipInfo;
\r
28 private readonly DockInfo _dockInfo;
\r
29 private readonly RepairStatus[] _repairStatuses = new RepairStatus[ShipInfo.FleetCount];
\r
30 private DateTime _start;
\r
31 private DateTime _prev;
\r
33 public class RepairSpan
\r
35 public int Diff { get; set; }
\r
36 public TimeSpan Span { get; set; }
\r
38 public RepairSpan(int diff, TimeSpan span)
\r
45 private class RepairStatus
\r
47 private ShipStatus[] _target = new ShipStatus[0];
\r
48 private int[] _deck = new int[0];
\r
52 set { _deck = value; }
\r
55 public State State { get; set; }
\r
57 public void Invalidate()
\r
59 _target = new ShipStatus[0];
\r
62 public bool IsRepaired(ShipStatus[] target) => _target.Zip(target, (a, b) => a.NowHp < b.NowHp).Any(x => x);
\r
64 public bool DeckChanged(IEnumerable<int> deck) => !_deck.SequenceEqual(deck);
\r
66 public void UpdateTarget(ShipStatus[] target)
\r
71 public RepairSpan[] GetTimers(DateTime start, DateTime now)
\r
73 var spent = TimeSpan.FromSeconds((int)(now - start).TotalSeconds);
\r
74 return _target.Select(s =>
\r
76 var damage = s.MaxHp - s.NowHp;
\r
78 return new RepairSpan(0, TimeSpan.MinValue);
\r
79 if (spent < TimeSpan.FromMinutes(20))
\r
80 return new RepairSpan(0, TimeSpan.FromMinutes(20) - spent);
\r
82 return new RepairSpan(1, TimeSpan.Zero);
\r
83 for (var d = 2; d <= damage; d++)
\r
85 var sec = s.CalcRepairSec(d) + 60;
\r
88 if (TimeSpan.FromSeconds(sec) > spent)
\r
89 return new RepairSpan(d - 1, TimeSpan.FromSeconds(sec) - spent);
\r
91 return new RepairSpan(damage, TimeSpan.Zero);
\r
95 public Notice GetNotice(DateTime start, DateTime prev, DateTime now)
\r
97 var m20 = TimeSpan.FromMinutes(20);
\r
98 var proc = new List<string>();
\r
99 var comp = new List<string>();
\r
100 foreach (var s in _target)
\r
102 var damage = s.MaxHp - s.NowHp;
\r
107 if (prev - start < m20 && now - start >= m20)
\r
111 // スリープで時間が飛んだときに修理完了だけを表示するために、
\r
112 // 完全回復から減らしながら所要時間と経過時間と比較する。
\r
113 for (var d = damage; d >= 2; d--)
\r
115 var sec = s.CalcRepairSec(d) + 60;
\r
116 if (sec <= 20 * 60)
\r
118 if (d == damage && (prev - start < m20 && now - start >= m20))
\r
122 var span = TimeSpan.FromSeconds(sec);
\r
123 if (span <= prev - start || now - start < span)
\r
134 Proceeded = proc.Count == 0 ? "" : string.Join(" ", proc),
\r
135 Completed = comp.Count == 0 ? "" : string.Join(" ", comp)
\r
140 public class Notice
\r
142 public string Proceeded { get; set; }
\r
143 public string Completed { get; set; }
\r
146 public AkashiTimer(ShipInfo ship, DockInfo dock)
\r
150 for (var i = 0; i < _repairStatuses.Length; i++)
\r
151 _repairStatuses[i] = new RepairStatus();
\r
163 var now = DateTime.Now;
\r
164 var reset = _repairStatuses.Any(r => r.State == State.Reset);
\r
165 if (_start == DateTime.MinValue || now - _start > TimeSpan.FromMinutes(20) || reset)
\r
169 public void InspectChange(string request)
\r
172 var values = HttpUtility.ParseQueryString(request);
\r
173 if (int.Parse(values["api_ship_idx"]) == -1)
\r
175 if (_repairStatuses.Any(r => r.State == State.Reset))
\r
176 _start = DateTime.Now;
\r
179 public void CheckFleet()
\r
181 for (var fleet = 0; fleet < ShipInfo.FleetCount; fleet++)
\r
185 private void CheckFleet(int fleet)
\r
187 var deck = _shipInfo.GetDeck(fleet).ToArray();
\r
188 var repair = _repairStatuses[fleet];
\r
189 var fs = _shipInfo.GetStatus(deck[0]);
\r
190 repair.State = State.Continue;
\r
191 if (!fs.Name.StartsWith("明石"))
\r
193 repair.Invalidate();
\r
194 repair.Deck = deck;
\r
197 var cap = fs.Slot.Count(item => item.Spec.Name == "艦艇修理施設") + 2;
\r
199 * 泊地修理の条件を満たさない艦はMaxHp==NowHpのダミーを設定する。
\r
200 * - 入渠中の艦娘は終わったときに回復扱いされないようNowHp=MaxHpに
\r
201 * - 中破以上でNowHp=MaxHpにすると回復扱いされるのでNowHp=MaxHp=0に
\r
203 var target = (from id in deck.Take(cap)
\r
204 let s = _shipInfo.GetStatus(id)
\r
205 let full = new ShipStatus {NowHp = s.MaxHp, MaxHp = s.MaxHp}
\r
206 let zero = new ShipStatus()
\r
207 select _dockInfo.InNDock(id) ? full : s.DamageLevel >= ShipStatus.Damage.Half ? zero : s).ToArray();
\r
208 repair.State = State.Continue;
\r
209 if (repair.DeckChanged(deck) || repair.IsRepaired(target))
\r
210 repair.State = State.Reset;
\r
211 repair.UpdateTarget(target);
\r
212 repair.Deck = deck;
\r
215 public RepairSpan[] GetTimers(int fleet)
\r
216 => _start == DateTime.MinValue ? new RepairSpan[0] : _repairStatuses[fleet].GetTimers(_start, DateTime.Now);
\r
218 public TimeSpan PresetDeckTimer
\r
222 if (_start == DateTime.MinValue)
\r
223 return TimeSpan.MinValue;
\r
224 var r = _start + TimeSpan.FromMinutes(20) - DateTime.Now;
\r
225 return r >= TimeSpan.Zero ? r : TimeSpan.Zero;
\r
229 public Notice[] GetNotice()
\r
231 var now = DateTime.Now;
\r
234 if (prev == DateTime.MinValue || _start == DateTime.MinValue)
\r
235 return new Notice[0];
\r
236 var r = _repairStatuses.Select(repair => repair.GetNotice(_start, prev, now)).ToArray();
\r
237 var m20 = TimeSpan.FromMinutes(20);
\r
238 if (prev - _start < m20 && now - _start >= m20)
\r
239 r[0].Proceeded = "20分経過しました。";
\r