1 // Copyright (C) 2018 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
20 using KancolleSniffer.Model;
\r
22 namespace KancolleSniffer.Log
\r
24 public class LogProcessor
\r
26 private readonly MaterialCount[] _materialCount;
\r
27 private readonly BattleLogProcessor _battleLogProcessor;
\r
29 public LogProcessor(MaterialCount[] materialCount = null, Dictionary<string, string> mapDictionary = null)
\r
31 _materialCount = materialCount ?? new MaterialCount[0];
\r
32 _battleLogProcessor = new BattleLogProcessor(mapDictionary);
\r
35 public IEnumerable<string> Process(IEnumerable<string> lines, string path, DateTime from, DateTime to,
\r
36 bool number, DateTime now = default)
\r
40 var material = false;
\r
41 switch (Path.GetFileNameWithoutExtension(path))
\r
68 foreach (var line in lines)
\r
70 var data = line.Split(',');
\r
71 var date = ParseDateTime(data[0]);
\r
72 if (date == default)
\r
78 data[0] = Logger.FormatDateTime(date);
\r
81 entries = data.Take(fields).ToArray();
\r
83 entries = _battleLogProcessor.Process(data);
\r
84 if (entries.Length != fields)
\r
88 ? delimiter + "[" + JavaScriptTicks(date) + "," + string.Join(",", entries.Skip(1)) + "]"
\r
89 : delimiter + "[\"" + string.Join("\",\"", entries) + "\"]";
\r
91 yield return result;
\r
93 if (material && !number) // 資材の現在値を出力する
\r
94 yield return delimiter + "[\"" + Logger.FormatDateTime(now) + "\",\"" +
\r
95 string.Join("\",\"", _materialCount.Select(c => c.Now)) + "\"]";
\r
98 private DateTime ParseDateTime(string dateTime)
\r
100 if (DateTime.TryParseExact(dateTime, Logger.DateTimeFormat, CultureInfo.InvariantCulture,
\r
101 DateTimeStyles.AssumeLocal, out var date))
\r
105 // システムが和暦に設定されていて和暦が出力されてしまったケースを救う
\r
106 if (dateTime[2] == '-')
\r
108 if (!int.TryParse(dateTime.Substring(0, 2), out var year))
\r
110 dateTime = 1988 + year + dateTime.Substring(2);
\r
111 return DateTime.TryParseExact(dateTime, Logger.DateTimeFormat, CultureInfo.InvariantCulture,
\r
112 DateTimeStyles.AssumeLocal, out date)
\r
116 return DateTime.TryParse(dateTime, CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal, out date)
\r
121 private long JavaScriptTicks(DateTime date) =>
\r
122 (date.ToUniversalTime().Ticks - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks) /
\r
123 TimeSpan.TicksPerMillisecond;
\r