1 // Copyright (C) 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>
\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
7 // http://www.apache.org/licenses/LICENSE-2.0
\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
16 using System.Collections.Generic;
\r
21 namespace KancolleSniffer
\r
30 CreateItem = 1 << 3,
\r
31 CreateShip = 1 << 4,
\r
32 RemodelSlot = 1 << 5,
\r
33 Achivement = 1 << 6,
\r
39 private LogType _logType;
\r
40 private readonly ShipInfo _shipInfo;
\r
41 private readonly ItemInfo _itemInfo;
\r
42 private readonly BattleInfo _battleInfo;
\r
43 private Action<string, string, string> _writer;
\r
44 private Func<DateTime> _nowFunc;
\r
45 public const string DateTimeFormat = @"yyyy\-MM\-dd HH\:mm\:ss";
\r
46 private dynamic _battle;
\r
47 private dynamic _map;
\r
48 private dynamic _basic;
\r
49 private int _kdockId;
\r
50 private DateTime _prevTime;
\r
51 private int[] _currentMaterial = new int[Enum.GetValues(typeof(Material)).Length];
\r
52 private int _materialLogInterval = 10;
\r
53 private bool _start;
\r
54 private int _lastExp = -1;
\r
55 private DateTime _lastDate;
\r
56 private DateTime _endOfMonth;
\r
57 private DateTime _nextDate;
\r
59 public int MaterialLogInterval
\r
61 set => _materialLogInterval = value;
\r
64 public string OutputDir
\r
66 set => _writer = new LogWriter(value).Write;
\r
69 public Logger(ShipInfo ship, ItemInfo item, BattleInfo battle)
\r
73 _battleInfo = battle;
\r
74 _writer = new LogWriter().Write;
\r
75 _nowFunc = () => DateTime.Now;
\r
78 public void EnableLog(LogType type)
\r
83 public void SetWriter(Action<string, string, string> writer, Func<DateTime> nowFunc)
\r
89 public void FlashLog()
\r
91 FlashAchivementLog();
\r
94 public void InspectMissionResult(dynamic json)
\r
96 var r = (int)json.api_clear_result;
\r
97 var rstr = r == 2 ? "大成功" : r == 1 ? "成功" : "失敗";
\r
98 var material = new int[7];
\r
100 ((int[])json.api_get_material).CopyTo(material, 0);
\r
101 foreach (var i in new[] {1, 2})
\r
103 var attr = "api_get_item" + i;
\r
104 if (!json.IsDefined(attr) || json[attr].api_useitem_id != -1)
\r
106 var count = (int)json[attr].api_useitem_count;
\r
107 var flag = ((int[])json.api_useitem_flag)[i - 1];
\r
109 material[(int)Material.Bucket] = count;
\r
110 else if (flag == 2)
\r
111 material[(int)Material.Burner + 2] = count; // 高速建造材と開発資材が反対なのでいつか直す
\r
112 else if (flag == 3)
\r
113 material[(int)Material.Development - 2] = count;
\r
115 if ((_logType & LogType.Mission) != 0)
\r
118 string.Join(",", _nowFunc().ToString(DateTimeFormat),
\r
119 rstr, json.api_quest_name, string.Join(",", material)),
\r
120 "日付,結果,遠征,燃料,弾薬,鋼材,ボーキ,開発資材,高速修復材,高速建造材");
\r
124 public void InspectMapStart(dynamic json)
\r
129 if ((_logType & LogType.Material) != 0)
\r
130 WriteMaterialLog(_nowFunc());
\r
133 public void InspectMapNext(dynamic json)
\r
135 if ((_logType & LogType.Achivement) != 0 && json.api_get_eo_rate() && (int)json.api_get_eo_rate != 0)
\r
138 _nowFunc().ToString(DateTimeFormat) + "," + _lastExp + "," + (int)json.api_get_eo_rate,
\r
144 public void InspectClearItemGet(dynamic json)
\r
146 if ((_logType & LogType.Achivement) == 0)
\r
148 if (json.api_bounus_count == 0)
\r
150 foreach (var entry in json.api_bounus)
\r
152 if (entry.api_type != 18)
\r
155 _nowFunc().ToString(DateTimeFormat) + "," + _lastExp + "," + (int)entry.api_count,
\r
162 public void InspectBattle(dynamic json)
\r
164 if (_battle != null) // 通常の夜戦は無視する
\r
169 public void InspectBattleResult(dynamic result)
\r
171 if ((_logType & LogType.Achivement) != 0 && result.api_get_exmap_rate())
\r
173 var rate = result.api_get_exmap_rate is string
\r
174 ? int.Parse(result.api_get_exmap_rate)
\r
175 : (int)result.api_get_exmap_rate;
\r
178 _writer("戦果", _nowFunc().ToString(DateTimeFormat) + "," + _lastExp + "," + rate,
\r
182 if ((_logType & LogType.Battle) == 0 || _map == null || _battle == null)
\r
184 _map = _battle = null;
\r
187 var fships = GenerateFirendShipList();
\r
188 var eships = GenerateEnemyShipList();
\r
189 var cell = (int)_map.api_no;
\r
193 if (cell == (int)_map.api_bosscell_no || (int)_map.api_event_id == 5)
\r
194 boss = _start ? "出撃&ボス" : "ボス";
\r
195 var dropType = result.api_get_ship() ? result.api_get_ship.api_ship_type : "";
\r
196 if (result.api_get_useitem())
\r
198 if (dropType == "")
\r
201 dropType += "+アイテム";
\r
203 var dropName = result.api_get_ship() ? result.api_get_ship.api_ship_name : "";
\r
204 if (result.api_get_useitem())
\r
206 var itemName = _itemInfo.GetUseItemName((int)result.api_get_useitem.api_useitem_id);
\r
207 if (dropName == "")
\r
208 dropName = itemName;
\r
210 dropName += "+" + itemName;
\r
212 var fp = _shipInfo.GetFighterPower(BattleInfo.DeckId(_battle));
\r
213 var fpower = fp[0] == fp[1] ? fp[0].ToString() : fp[0] + "~" + fp[1];
\r
214 _writer("海戦・ドロップ報告書", string.Join(",", _nowFunc().ToString(DateTimeFormat),
\r
215 result.api_quest_name,
\r
217 result.api_win_rank,
\r
218 BattleFormationName((int)_battle.api_formation[2]),
\r
219 FormationName(_battle.api_formation[0]),
\r
220 FormationName(_battle.api_formation[1]),
\r
221 result.api_enemy_info.api_deck_name,
\r
222 dropType, dropName,
\r
223 string.Join(",", fships),
\r
224 string.Join(",", eships),
\r
225 fpower, _battleInfo.EnemyFighterPower.AirCombat + _battleInfo.EnemyFighterPower.UnknownMark,
\r
226 AirControlLevelName(_battle)),
\r
227 "日付,海域,マス,ボス,ランク,艦隊行動,味方陣形,敵陣形,敵艦隊,ドロップ艦種,ドロップ艦娘," +
\r
228 "味方艦1,味方艦1HP,味方艦2,味方艦2HP,味方艦3,味方艦3HP,味方艦4,味方艦4HP,味方艦5,味方艦5HP,味方艦6,味方艦6HP," +
\r
229 "敵艦1,敵艦1HP,敵艦2,敵艦2HP,敵艦3,敵艦3HP,敵艦4,敵艦4HP,敵艦5,敵艦5HP,敵艦6,敵艦6HP," +
\r
232 _map = _battle = null;
\r
236 private IEnumerable<string> GenerateFirendShipList()
\r
238 int deckId = BattleInfo.DeckId(_battle);
\r
239 if (_battle.api_nowhps_combined() && (int)_battle.api_nowhps_combined[1] != -1)
\r
241 var main = _shipInfo.GetDeck(0);
\r
242 var guard = _shipInfo.GetDeck(1);
\r
243 return main.Zip(guard, (m, g) =>
\r
245 if (m == -1 && g == -1)
\r
251 var sm = _shipInfo.GetStatus(m);
\r
252 name = $"{sm.Name}(Lv{sm.Level})";
\r
253 hp = $"{sm.NowHp}/{sm.MaxHp}";
\r
259 var sg = _shipInfo.GetStatus(g);
\r
260 name += $"{sg.Name}(Lv{sg.Level})";
\r
261 hp += $"{sg.NowHp}/{sg.MaxHp}";
\r
263 return name + "," + hp;
\r
266 var deck = _shipInfo.GetDeck(deckId);
\r
267 return deck.Select(id =>
\r
271 var s = _shipInfo.GetStatus(id);
\r
272 return $"{s.Name}(Lv{s.Level}),{s.NowHp}/{s.MaxHp}";
\r
276 private IEnumerable<string> GenerateEnemyShipList()
\r
278 var result = _battleInfo.EnemyResultStatus;
\r
279 if (result.Length <= 6)
\r
280 return result.Select(s => s.Id == -1 ? "," : $"{s.Name},{s.NowHp}/{s.MaxHp}").ToList();
\r
281 var main = result.Take(6);
\r
282 var guard = result.Skip(6);
\r
283 return main.Zip(guard, (m, g) =>
\r
285 if (m.Id == -1 && g.Id == -1)
\r
291 name = $"{m.Name}";
\r
292 hp = $"{m.NowHp}/{m.MaxHp}";
\r
298 name += $"{g.Name}";
\r
299 hp += $"{g.NowHp}/{g.MaxHp}";
\r
301 return name + "," + hp;
\r
305 private string FormationName(dynamic f)
\r
307 if (f is string) // 連合艦隊のときは文字列
\r
334 private static string BattleFormationName(int f)
\r
351 private string AirControlLevelName(dynamic json)
\r
353 // BattleInfo.AirControlLevelは夜戦で消されているかもしれないので、こちらで改めて調べる。
\r
354 if (!json.api_kouku())
\r
356 var stage1 = json.api_kouku.api_stage1;
\r
357 if (stage1 == null)
\r
359 if (stage1.api_f_count == 0 && stage1.api_e_count == 0)
\r
361 switch ((int)stage1.api_disp_seiku)
\r
378 public void InspectBasic(dynamic json)
\r
381 if ((_logType & LogType.Achivement) == 0)
\r
383 var now = _nowFunc();
\r
384 var exp = (int)json.api_experience;
\r
385 var isNewMonth = _endOfMonth == DateTime.MinValue || now.CompareTo(_endOfMonth) >= 0;
\r
386 var isNewDate = _nextDate == DateTime.MinValue || now.CompareTo(_nextDate) >= 0;
\r
387 if (isNewDate || isNewMonth)
\r
389 if (_lastDate != DateTime.MinValue)
\r
391 _writer("戦果", _lastDate.ToString(DateTimeFormat) + "," + _lastExp + ",0", "日付,経験値,EO");
\r
393 _writer("戦果", now.ToString(DateTimeFormat) + "," + exp + ",0", "日付,経験値,EO");
\r
396 _endOfMonth = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month), 22, 0, 0);
\r
397 if (_endOfMonth.CompareTo(now) <= 0)
\r
399 var days = _endOfMonth.Month == 12
\r
400 ? DateTime.DaysInMonth(_endOfMonth.Year + 1, 1)
\r
401 : DateTime.DaysInMonth(_endOfMonth.Year, _endOfMonth.Month);
\r
402 _endOfMonth = _endOfMonth.AddDays(days);
\r
405 _nextDate = new DateTime(now.Year, now.Month, now.Day, 2, 0, 0);
\r
407 _nextDate = _nextDate.AddDays(1);
\r
408 if (_nextDate.Day == 1)
\r
409 _nextDate = _nextDate.AddDays(1);
\r
415 private void FlashAchivementLog()
\r
417 if ((_logType & LogType.Achivement) == 0)
\r
419 if (_lastDate != DateTime.MinValue)
\r
421 _writer("戦果", _lastDate.ToString(DateTimeFormat) + "," + _lastExp + ",0", "日付,経験値,EO");
\r
425 public void InspectCreateItem(string request, dynamic json)
\r
427 if ((_logType & LogType.CreateItem) == 0)
\r
429 var values = HttpUtility.ParseQueryString(request);
\r
432 if (json.api_slot_item())
\r
434 var spec = _itemInfo.GetSpecByItemId((int)json.api_slot_item.api_slotitem_id);
\r
436 type = spec.TypeName;
\r
439 _nowFunc().ToString(DateTimeFormat) + "," +
\r
440 string.Join(",", name, type,
\r
441 values["api_item1"], values["api_item2"], values["api_item3"], values["api_item4"],
\r
442 Secretary(), _basic.api_level),
\r
443 "日付,開発装備,種別,燃料,弾薬,鋼材,ボーキ,秘書艦,司令部Lv");
\r
446 public void InspectCreateShip(string request)
\r
448 var values = HttpUtility.ParseQueryString(request);
\r
449 _kdockId = int.Parse(values["api_kdock_id"]);
\r
452 public void InspectKDock(dynamic json)
\r
454 if ((_logType & LogType.CreateShip) == 0 || _basic == null || _kdockId == 0)
\r
456 var kdock = ((dynamic[])json).First(e => e.api_id == _kdockId);
\r
457 var material = Enumerable.Range(1, 5).Select(i => (int)kdock["api_item" + i]).ToArray();
\r
458 var ship = _shipInfo.GetSpec((int)kdock.api_created_ship_id);
\r
459 var avail = ((dynamic[])json).Count(e => (int)e.api_state == 0);
\r
461 _nowFunc().ToString(DateTimeFormat) + "," +
\r
462 string.Join(",", material.First() >= 1500 ? "大型艦建造" : "通常艦建造",
\r
463 ship.Name, ship.ShipTypeName, string.Join(",", material), avail, Secretary(), _basic.api_level),
\r
464 "日付,種類,名前,艦種,燃料,弾薬,鋼材,ボーキ,開発資材,空きドック,秘書艦,司令部Lv");
\r
468 private string Secretary()
\r
470 var ship = _shipInfo.GetShipStatuses(0)[0];
\r
471 return ship.Name + "(" + ship.Level + ")";
\r
474 public void InspectMaterial(dynamic json)
\r
476 if ((_logType & LogType.Material) == 0)
\r
478 foreach (var e in json)
\r
479 _currentMaterial[(int)e.api_id - 1] = (int)e.api_value;
\r
480 var now = _nowFunc();
\r
481 if (now - _prevTime < TimeSpan.FromMinutes(_materialLogInterval))
\r
483 WriteMaterialLog(now);
\r
486 public void WriteMaterialLog(DateTime now)
\r
490 now.ToString(DateTimeFormat) + "," +
\r
491 string.Join(",", _currentMaterial),
\r
492 "日付,燃料,弾薬,鋼材,ボーキ,高速建造材,高速修復材,開発資材,改修資材");
\r
495 public void SetCurrentMaterial(int[] material)
\r
497 _currentMaterial = material;
\r
500 public void InspectRemodelSlot(string request, dynamic json)
\r
502 if ((_logType & LogType.RemodelSlot) == 0)
\r
504 var now = _nowFunc();
\r
505 var values = HttpUtility.ParseQueryString(request);
\r
506 var id = int.Parse(values["api_slot_id"]);
\r
507 var name = _itemInfo.GetName(id);
\r
508 var level = _itemInfo.GetStatus(id).Level;
\r
509 var success = (int)json.api_remodel_flag == 1 ? "○" : "×";
\r
510 var certain = int.Parse(values["api_certain_flag"]) == 1 ? "○" : "";
\r
513 if (json.api_use_slot_id())
\r
515 var use = (int[])json.api_use_slot_id;
\r
516 useName = _itemInfo.GetName(use[0]);
\r
517 useNum = use.Length.ToString();
\r
519 var after = (int[])json.api_after_material;
\r
520 var diff = new int[after.Length];
\r
521 for (var i = 0; i < after.Length; i++)
\r
522 diff[i] = _currentMaterial[i] - after[i];
\r
523 var ship1 = Secretary();
\r
525 var ships = _shipInfo.GetShipStatuses(0);
\r
526 if (ships.Length >= 2)
\r
527 ship2 = ships[1].Name + "(" + ships[1].Level + ")";
\r
529 now.ToString(DateTimeFormat) + "," +
\r
530 string.Join(",", name, level, success, certain, useName, useNum,
\r
531 diff[(int)Material.Fuel], diff[(int)Material.Bullet], diff[(int)Material.Steal],
\r
532 diff[(int)Material.Bouxite],
\r
533 diff[(int)Material.Development], diff[(int)Material.Screw],
\r
535 "日付,改修装備,レベル,成功,確実化,消費装備,消費数,燃料,弾薬,鋼材,ボーキ,開発資材,改修資材,秘書艦,二番艦");
\r
539 public class LogWriter
\r
541 private readonly IFile _file;
\r
542 private readonly string _outputDir;
\r
544 public interface IFile
\r
546 string ReadAllText(string path);
\r
547 void AppendAllText(string path, string text);
\r
548 void Delete(string path);
\r
549 bool Exists(string path);
\r
552 private class FileWrapper : IFile
\r
554 // Shift_JISでないとExcelで文字化けする
\r
555 private readonly Encoding _encoding = Encoding.GetEncoding("Shift_JIS");
\r
557 public string ReadAllText(string path) => File.ReadAllText(path, _encoding);
\r
559 public void AppendAllText(string path, string text)
\r
561 File.AppendAllText(path, text, _encoding);
\r
564 public void Delete(string path)
\r
569 public bool Exists(string path) => File.Exists(path);
\r
572 public LogWriter(string outputDir = null, IFile file = null)
\r
574 _outputDir = outputDir ?? AppDomain.CurrentDomain.BaseDirectory;
\r
575 _file = file ?? new FileWrapper();
\r
578 public void Write(string file, string s, string header)
\r
580 var path = Path.Combine(_outputDir, file);
\r
581 var csv = path + ".csv";
\r
582 var tmp = path + ".tmp";
\r
583 if (_file.Exists(tmp))
\r
587 _file.AppendAllText(csv, _file.ReadAllText(tmp));
\r
590 catch (IOException)
\r
594 if (!_file.Exists(csv))
\r
595 s = header + "\r\n" + s;
\r
596 foreach (var f in new[] {csv, tmp})
\r
600 _file.AppendAllText(f, s + "\r\n");
\r
603 catch (IOException e)
\r
606 throw new LogIOException("報告書の出力中にエラーが発生しました。", e);
\r
612 public class LogIOException : Exception
\r
614 public LogIOException()
\r
618 public LogIOException(string message) : base(message)
\r
622 public LogIOException(string message, Exception inner) : base(message, inner)
\r