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
17 using System.Globalization;
\r
21 using KancolleSniffer.Model;
\r
22 using KancolleSniffer.Util;
\r
24 namespace KancolleSniffer
\r
33 CreateItem = 1 << 3,
\r
34 CreateShip = 1 << 4,
\r
35 RemodelSlot = 1 << 5,
\r
36 Achivement = 1 << 6,
\r
42 private LogType _logType;
\r
43 private readonly ShipInfo _shipInfo;
\r
44 private readonly ItemInfo _itemInfo;
\r
45 private readonly BattleInfo _battleInfo;
\r
46 private Action<string, string, string> _writer;
\r
47 private Func<DateTime> _nowFunc;
\r
48 public const string DateTimeFormat = @"yyyy\-MM\-dd HH\:mm\:ss";
\r
49 private dynamic _battle;
\r
50 private dynamic _map;
\r
51 private dynamic _basic;
\r
52 private int _kdockId;
\r
53 private DateTime _prevTime;
\r
54 private int[] _currentMaterial = new int[Enum.GetValues(typeof(Material)).Length];
\r
55 private int _materialLogInterval = 10;
\r
56 private bool _start;
\r
57 private int _lastExp = -1;
\r
58 private DateTime _lastDate;
\r
59 private DateTime _endOfMonth;
\r
60 private DateTime _nextDate;
\r
62 public int MaterialLogInterval
\r
64 set => _materialLogInterval = value;
\r
67 public string OutputDir
\r
69 set => _writer = new LogWriter(value).Write;
\r
72 public static string FormatDateTime(DateTime date)
\r
74 return date.ToString(DateTimeFormat, CultureInfo.InvariantCulture);
\r
77 public Logger(ShipInfo ship, ItemInfo item, BattleInfo battle)
\r
81 _battleInfo = battle;
\r
82 _writer = new LogWriter().Write;
\r
83 _nowFunc = () => DateTime.Now;
\r
86 public void EnableLog(LogType type)
\r
91 public void SetWriter(Action<string, string, string> writer, Func<DateTime> nowFunc)
\r
97 public void FlashLog()
\r
99 FlashAchivementLog();
\r
102 public void InspectMissionResult(dynamic json)
\r
104 var r = (int)json.api_clear_result;
\r
105 var rstr = r == 2 ? "大成功" : r == 1 ? "成功" : "失敗";
\r
106 var material = new int[7];
\r
108 ((int[])json.api_get_material).CopyTo(material, 0);
\r
109 foreach (var i in new[] {1, 2})
\r
111 var attr = "api_get_item" + i;
\r
112 if (!json.IsDefined(attr) || json[attr].api_useitem_id != -1)
\r
114 var count = (int)json[attr].api_useitem_count;
\r
115 var flag = ((int[])json.api_useitem_flag)[i - 1];
\r
117 material[(int)Material.Bucket] = count;
\r
118 else if (flag == 2)
\r
119 material[(int)Material.Burner + 2] = count; // 高速建造材と開発資材が反対なのでいつか直す
\r
120 else if (flag == 3)
\r
121 material[(int)Material.Development - 2] = count;
\r
123 if ((_logType & LogType.Mission) != 0)
\r
126 string.Join(",", FormatDateTime(_nowFunc()),
\r
127 rstr, json.api_quest_name, string.Join(",", material)),
\r
128 "日付,結果,遠征,燃料,弾薬,鋼材,ボーキ,開発資材,高速修復材,高速建造材");
\r
132 public void InspectMapStart(dynamic json)
\r
137 if ((_logType & LogType.Material) != 0)
\r
138 WriteMaterialLog(_nowFunc());
\r
141 public void InspectMapNext(dynamic json)
\r
143 if ((_logType & LogType.Achivement) != 0 && json.api_get_eo_rate() && (int)json.api_get_eo_rate != 0)
\r
146 FormatDateTime(_nowFunc()) + "," + _lastExp + "," + (int)json.api_get_eo_rate,
\r
152 public void InspectClearItemGet(dynamic json)
\r
154 if ((_logType & LogType.Achivement) == 0)
\r
156 if (!json.api_bounus())
\r
158 foreach (var entry in json.api_bounus)
\r
160 if (entry.api_type != 18)
\r
163 FormatDateTime(_nowFunc()) + "," + _lastExp + "," + (int)entry.api_count,
\r
169 public void InspectBattle(dynamic json)
\r
171 if (_battle != null) // 通常の夜戦は無視する
\r
176 public void InspectBattleResult(dynamic result)
\r
178 if ((_logType & LogType.Achivement) != 0 && result.api_get_exmap_rate())
\r
180 var rate = result.api_get_exmap_rate is string
\r
181 ? int.Parse(result.api_get_exmap_rate)
\r
182 : (int)result.api_get_exmap_rate;
\r
185 _writer("戦果", FormatDateTime(_nowFunc()) + "," + _lastExp + "," + rate,
\r
189 if ((_logType & LogType.Battle) == 0 || _map == null || _battle == null)
\r
191 _map = _battle = null;
\r
194 var fships = GenerateFirendShipList();
\r
195 var eships = GenerateEnemyShipList();
\r
196 var cell = (int)_map.api_no;
\r
200 if (cell == (int)_map.api_bosscell_no || (int)_map.api_event_id == 5)
\r
201 boss = _start ? "出撃&ボス" : "ボス";
\r
202 var dropType = result.api_get_ship() ? result.api_get_ship.api_ship_type : "";
\r
203 if (result.api_get_useitem())
\r
205 if (dropType == "")
\r
208 dropType += "+アイテム";
\r
210 var dropName = result.api_get_ship() ? result.api_get_ship.api_ship_name : "";
\r
211 if (result.api_get_useitem())
\r
213 var itemName = _itemInfo.GetUseItemName((int)result.api_get_useitem.api_useitem_id);
\r
214 if (dropName == "")
\r
215 dropName = itemName;
\r
217 dropName += "+" + itemName;
\r
219 var fp = _shipInfo.Fleets[BattleInfo.DeckId(_battle)].FighterPower;
\r
220 var fpower = fp[0] == fp[1] ? fp[0].ToString() : fp[0] + "~" + fp[1];
\r
221 _writer("海戦・ドロップ報告書", string.Join(",", FormatDateTime(_nowFunc()),
\r
222 result.api_quest_name,
\r
224 result.api_win_rank,
\r
225 BattleFormationName((int)_battle.api_formation[2]),
\r
226 FormationName(_battle.api_formation[0]),
\r
227 FormationName(_battle.api_formation[1]),
\r
228 result.api_enemy_info.api_deck_name,
\r
229 dropType, dropName,
\r
230 string.Join(",", fships),
\r
231 string.Join(",", eships),
\r
232 fpower, _battleInfo.EnemyFighterPower.AirCombat + _battleInfo.EnemyFighterPower.UnknownMark,
\r
233 AirControlLevelName(_battle)),
\r
234 "日付,海域,マス,ボス,ランク,艦隊行動,味方陣形,敵陣形,敵艦隊,ドロップ艦種,ドロップ艦娘," +
\r
235 "味方艦1,味方艦1HP,味方艦2,味方艦2HP,味方艦3,味方艦3HP,味方艦4,味方艦4HP,味方艦5,味方艦5HP,味方艦6,味方艦6HP," +
\r
236 "敵艦1,敵艦1HP,敵艦2,敵艦2HP,敵艦3,敵艦3HP,敵艦4,敵艦4HP,敵艦5,敵艦5HP,敵艦6,敵艦6HP," +
\r
239 _map = _battle = null;
\r
243 private IEnumerable<string> GenerateFirendShipList()
\r
245 int deckId = BattleInfo.DeckId(_battle);
\r
246 if (_battle.api_f_nowhps_combined())
\r
248 var mainShips = _shipInfo.Fleets[0].Ships;
\r
249 var guardShips = _shipInfo.Fleets[1].Ships;
\r
250 return mainShips.Zip(guardShips, (main, guard) =>
\r
252 if (main.Empty && guard.Empty)
\r
258 name = $"{main.Name}(Lv{main.Level})";
\r
259 hp = $"{main.NowHp}/{main.MaxHp}";
\r
265 name += $"{guard.Name}(Lv{guard.Level})";
\r
266 hp += $"{guard.NowHp}/{guard.MaxHp}";
\r
268 return name + "," + hp;
\r
271 var ships = _shipInfo.Fleets[deckId].Ships;
\r
272 if (ships.Count > 6)
\r
274 var result = new List<string>();
\r
275 for (var i = 0; i < 12 - ships.Count; i++)
\r
277 var ship = ships[i];
\r
278 result.Add($"{ship.Name}(Lv{ship.Level}),{ship.NowHp}/{ship.MaxHp}");
\r
280 for (var i = 0; i < ships.Count - 6; i++)
\r
282 var s1 = ships[12 - ships.Count + i];
\r
283 var s2 = ships[6 + i];
\r
285 $"{s1.Name}(Lv{s1.Level})・{s2.Name}(Lv{s2.Level})," +
\r
286 $"{s1.NowHp}/{s1.MaxHp}・{s2.NowHp}/{s2.MaxHp}");
\r
290 return ships.Select(ship =>
\r
294 return $"{ship.Name}(Lv{ship.Level}),{ship.NowHp}/{ship.MaxHp}";
\r
298 private IEnumerable<string> GenerateEnemyShipList()
\r
300 var result = _battleInfo.Result.Enemy.Main.Concat(Enumerable.Repeat(new ShipStatus(), 6)).Take(6);
\r
301 if (_battleInfo.Result.Enemy.Guard.Length == 0)
\r
303 return result.Select(s => s.Empty ? "," : $"{s.Name},{s.NowHp}/{s.MaxHp}").ToList();
\r
305 var mainShips = result;
\r
306 var guardShips = _battleInfo.Result.Enemy.Guard.Concat(Enumerable.Repeat(new ShipStatus(), 6)).Take(6);
\r
307 return mainShips.Zip(guardShips, (main, guard) =>
\r
309 if (main.Empty && guard.Empty)
\r
315 name = $"{main.Name}";
\r
316 hp = $"{main.NowHp}/{main.MaxHp}";
\r
322 name += $"{guard.Name}";
\r
323 hp += $"{guard.NowHp}/{guard.MaxHp}";
\r
325 return name + "," + hp;
\r
329 private string FormationName(dynamic f)
\r
331 if (f is string) // 連合艦隊のときは文字列
\r
360 private static string BattleFormationName(int f)
\r
377 private string AirControlLevelName(dynamic json)
\r
379 // BattleInfo.AirControlLevelは夜戦で消されているかもしれないので、こちらで改めて調べる。
\r
380 if (!json.api_kouku())
\r
382 var stage1 = json.api_kouku.api_stage1;
\r
383 if (stage1 == null)
\r
385 if (stage1.api_f_count == 0 && stage1.api_e_count == 0)
\r
387 switch ((int)stage1.api_disp_seiku)
\r
404 public void InspectBasic(dynamic json)
\r
407 if ((_logType & LogType.Achivement) == 0)
\r
409 var now = _nowFunc();
\r
410 var exp = (int)json.api_experience;
\r
411 var isNewMonth = _endOfMonth == DateTime.MinValue || now.CompareTo(_endOfMonth) >= 0;
\r
412 var isNewDate = _nextDate == DateTime.MinValue || now.CompareTo(_nextDate) >= 0;
\r
413 if (isNewDate || isNewMonth)
\r
415 if (_lastDate != DateTime.MinValue)
\r
417 _writer("戦果", FormatDateTime(_lastDate) + "," + _lastExp + ",0", "日付,経験値,EO");
\r
419 _writer("戦果", FormatDateTime(now) + "," + exp + ",0", "日付,経験値,EO");
\r
422 _endOfMonth = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month),
\r
424 if (_endOfMonth.CompareTo(now) <= 0)
\r
426 var days = _endOfMonth.Month == 12
\r
427 ? DateTime.DaysInMonth(_endOfMonth.Year + 1, 1)
\r
428 : DateTime.DaysInMonth(_endOfMonth.Year, _endOfMonth.Month);
\r
429 _endOfMonth = _endOfMonth.AddDays(days);
\r
432 _nextDate = new DateTime(now.Year, now.Month, now.Day, 2, 0, 0);
\r
434 _nextDate = _nextDate.AddDays(1);
\r
435 if (_nextDate.Day == 1)
\r
436 _nextDate = _nextDate.AddDays(1);
\r
442 private void FlashAchivementLog()
\r
444 if ((_logType & LogType.Achivement) == 0)
\r
446 if (_lastDate != DateTime.MinValue)
\r
448 _writer("戦果", FormatDateTime(_lastDate) + "," + _lastExp + ",0", "日付,経験値,EO");
\r
452 public void InspectCreateItem(string request, dynamic json)
\r
454 if ((_logType & LogType.CreateItem) == 0)
\r
456 var values = HttpUtility.ParseQueryString(request);
\r
459 if (json.api_slot_item())
\r
461 var spec = _itemInfo.GetSpecByItemId((int)json.api_slot_item.api_slotitem_id);
\r
463 type = spec.TypeName;
\r
466 FormatDateTime(_nowFunc()) + "," +
\r
467 string.Join(",", name, type,
\r
468 values["api_item1"], values["api_item2"], values["api_item3"], values["api_item4"],
\r
469 Secretary(), _basic.api_level),
\r
470 "日付,開発装備,種別,燃料,弾薬,鋼材,ボーキ,秘書艦,司令部Lv");
\r
473 public void InspectCreateShip(string request)
\r
475 var values = HttpUtility.ParseQueryString(request);
\r
476 _kdockId = int.Parse(values["api_kdock_id"]);
\r
479 public void InspectKDock(dynamic json)
\r
481 if ((_logType & LogType.CreateShip) == 0 || _basic == null || _kdockId == 0)
\r
483 var kdock = ((dynamic[])json).First(e => e.api_id == _kdockId);
\r
484 var material = Enumerable.Range(1, 5).Select(i => (int)kdock["api_item" + i]).ToArray();
\r
485 var ship = _shipInfo.GetSpec((int)kdock.api_created_ship_id);
\r
486 var avail = ((dynamic[])json).Count(e => (int)e.api_state == 0);
\r
488 FormatDateTime(_nowFunc()) + "," +
\r
489 string.Join(",", material.First() >= 1500 ? "大型艦建造" : "通常艦建造",
\r
490 ship.Name, ship.ShipTypeName, string.Join(",", material), avail, Secretary(), _basic.api_level),
\r
491 "日付,種類,名前,艦種,燃料,弾薬,鋼材,ボーキ,開発資材,空きドック,秘書艦,司令部Lv");
\r
495 private string Secretary()
\r
497 var ship = _shipInfo.Fleets[0].Ships[0];
\r
498 return ship.Name + "(" + ship.Level + ")";
\r
501 public void InspectMaterial(dynamic json)
\r
503 if ((_logType & LogType.Material) == 0)
\r
505 foreach (var e in json)
\r
506 _currentMaterial[(int)e.api_id - 1] = (int)e.api_value;
\r
507 var now = _nowFunc();
\r
508 if (now - _prevTime < TimeSpan.FromMinutes(_materialLogInterval))
\r
510 WriteMaterialLog(now);
\r
513 public void WriteMaterialLog(DateTime now)
\r
517 FormatDateTime(now) + "," +
\r
518 string.Join(",", _currentMaterial),
\r
519 "日付,燃料,弾薬,鋼材,ボーキ,高速建造材,高速修復材,開発資材,改修資材");
\r
522 public void SetCurrentMaterial(int[] material)
\r
524 _currentMaterial = material;
\r
527 public void InspectRemodelSlot(string request, dynamic json)
\r
529 if ((_logType & LogType.RemodelSlot) == 0)
\r
531 var now = _nowFunc();
\r
532 var values = HttpUtility.ParseQueryString(request);
\r
533 var id = int.Parse(values["api_slot_id"]);
\r
534 var name = _itemInfo.GetName(id);
\r
535 var level = _itemInfo.GetStatus(id).Level;
\r
536 var success = (int)json.api_remodel_flag == 1 ? "○" : "×";
\r
537 var certain = int.Parse(values["api_certain_flag"]) == 1 ? "○" : "";
\r
540 if (json.api_use_slot_id())
\r
542 var use = (int[])json.api_use_slot_id;
\r
543 useName = _itemInfo.GetName(use[0]);
\r
544 useNum = use.Length.ToString();
\r
546 var after = (int[])json.api_after_material;
\r
547 var diff = new int[after.Length];
\r
548 for (var i = 0; i < after.Length; i++)
\r
549 diff[i] = _currentMaterial[i] - after[i];
\r
550 var ship1 = Secretary();
\r
552 var ships = _shipInfo.Fleets[0].Ships;
\r
553 if (!ships[1].Empty)
\r
554 ship2 = ships[1].Name + "(" + ships[1].Level + ")";
\r
556 FormatDateTime(now) + "," +
\r
557 string.Join(",", name, level, success, certain, useName, useNum,
\r
558 diff[(int)Material.Fuel], diff[(int)Material.Bullet], diff[(int)Material.Steal],
\r
559 diff[(int)Material.Bouxite],
\r
560 diff[(int)Material.Development], diff[(int)Material.Screw],
\r
562 "日付,改修装備,レベル,成功,確実化,消費装備,消費数,燃料,弾薬,鋼材,ボーキ,開発資材,改修資材,秘書艦,二番艦");
\r
566 public class LogWriter
\r
568 private readonly IFile _file;
\r
569 private readonly string _outputDir;
\r
571 public interface IFile
\r
573 string ReadAllText(string path);
\r
574 void AppendAllText(string path, string text);
\r
575 void Delete(string path);
\r
576 bool Exists(string path);
\r
579 private class FileWrapper : IFile
\r
581 // Shift_JISでないとExcelで文字化けする
\r
582 private readonly Encoding _encoding = Encoding.GetEncoding("Shift_JIS");
\r
584 public string ReadAllText(string path) => File.ReadAllText(path, _encoding);
\r
586 public void AppendAllText(string path, string text)
\r
588 File.AppendAllText(path, text, _encoding);
\r
591 public void Delete(string path)
\r
596 public bool Exists(string path) => File.Exists(path);
\r
599 public LogWriter(string outputDir = null, IFile file = null)
\r
601 _outputDir = outputDir ?? AppDomain.CurrentDomain.BaseDirectory;
\r
602 _file = file ?? new FileWrapper();
\r
605 public void Write(string file, string s, string header)
\r
607 var path = Path.Combine(_outputDir, file);
\r
608 var csv = path + ".csv";
\r
609 var tmp = path + ".tmp";
\r
610 if (_file.Exists(tmp))
\r
614 _file.AppendAllText(csv, _file.ReadAllText(tmp));
\r
617 catch (IOException)
\r
621 if (!_file.Exists(csv))
\r
622 s = header + "\r\n" + s;
\r
623 foreach (var f in new[] {csv, tmp})
\r
627 _file.AppendAllText(f, s + "\r\n");
\r
630 catch (IOException e)
\r
633 throw new LogIOException("報告書の出力中にエラーが発生しました。", e);
\r
639 public class LogIOException : Exception
\r
641 public LogIOException()
\r
645 public LogIOException(string message) : base(message)
\r
649 public LogIOException(string message, Exception inner) : base(message, inner)
\r