5 // Created by Hori,Masaki on 2017/02/05.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 fileprivate struct LFSeparateLine {
12 static let empty = LFSeparateLine(line: "", empty: true)
15 private let isEmpty: Bool
17 init(line: String, empty: Bool = false) {
21 func append(_ column: String) -> LFSeparateLine {
22 if isEmpty { return LFSeparateLine(line: column) }
23 let newLine = line + "\t" + column
24 return LFSeparateLine(line: newLine)
26 func append(_ dateCol: Date) -> LFSeparateLine {
27 return append("\(dateCol)")
29 func append(_ intCol: Int) -> LFSeparateLine {
30 return append("\(intCol)")
32 func append(_ boolCol: Bool) -> LFSeparateLine {
33 return append("\(boolCol)")
37 // swiftlint:disable type_body_length
39 private let store = LocalDataStore.oneTimeEditor()
41 private var dateFomatter: DateFormatter = {
42 let formatter = DateFormatter()
43 formatter.dateFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'Z"
48 let panel = NSOpenPanel()
49 panel.allowedFileTypes = ["kcdlocaldata"]
51 guard $0 == NSModalResponseOK else { return }
53 panel.urls.forEach { url in
54 guard let fileW = try? FileWrapper(url: url) else { return }
55 fileW.fileWrappers?.forEach {
56 guard let data = $0.value.regularFileContents else { return }
58 case "kaihatu.tsv": self.registerKaihatuHistory(data)
59 case "kenzo.tsv": self.registerKenzoHistory(data)
60 case "kenzoMark.tsv": self.registerKenzoMark(data)
61 case "dropShip.tsv": self.registerDropShipHistory(data)
70 let panel = NSSavePanel()
71 panel.allowedFileTypes = ["kcdlocaldata"]
73 guard $0 == NSModalResponseOK,
76 guard let kaihatuHistory = self.dataOfKaihatuHistory(),
77 let kenzoHistory = self.dataOfKenzoHistory(),
78 let kenzoMark = self.dataOfKenzoMark(),
79 let dropShipHistory = self.dataOfDropShipHistory()
81 let fileW = FileWrapper(directoryWithFileWrappers: [:])
82 fileW.addRegularFile(withContents: kaihatuHistory, preferredFilename: "kaihatu.tsv")
83 fileW.addRegularFile(withContents: kenzoHistory, preferredFilename: "kenzo.tsv")
84 fileW.addRegularFile(withContents: kenzoMark, preferredFilename: "kenzoMark.tsv")
85 fileW.addRegularFile(withContents: dropShipHistory, preferredFilename: "dropShip.tsv")
87 try fileW.write(to: url, originalContentsURL: nil)
88 } catch { print("Error to write") }
92 private func localData<T>(_ entity: Entity<T>, sortBy: String = "date") -> [T] {
93 let sortDesc = NSSortDescriptor(key: sortBy, ascending: true)
94 guard let array = try? store.objects(with: entity, sortDescriptors: [sortDesc])
96 print("Can not get \(entity.name)")
101 private func dataOfKaihatuHistory() -> Data? {
102 return localData(KaihatuHistory.entity)
110 .append($0.kaihatusizai)
112 .append($0.flagShipName)
113 .append($0.flagShipLv)
114 .append($0.commanderLv)
117 .joined(separator: "\n")
120 private func dataOfKenzoHistory() -> Data? {
121 return localData(KenzoHistory.entity)
129 .append($0.kaihatusizai)
132 .append($0.flagShipName)
133 .append($0.flagShipLv)
134 .append($0.commanderLv)
137 .joined(separator: "\n")
140 private func dataOfKenzoMark() -> Data? {
141 return localData(KenzoMark.entity, sortBy: "kDockId")
149 .append($0.kaihatusizai)
150 .append($0.created_ship_id)
152 .append($0.flagShipName)
153 .append($0.flagShipLv)
154 .append($0.commanderLv)
157 .joined(separator: "\n")
160 private func dataOfDropShipHistory() -> Data? {
161 return localData(DropShipHistory.entity)
169 .append($0.mapAreaName)
170 .append($0.mapInfoName)
175 .joined(separator: "\n")
178 private func registerKaihatuHistory(_ data: Data) {
179 let array = String(data: data, encoding: .utf8)?.components(separatedBy: "\n")
180 let store = LocalDataStore.oneTimeEditor()
182 let attr = $0.components(separatedBy: "\t")
183 guard attr.count == 10,
184 let date = dateFomatter.date(from: attr[0]),
185 let fuel = Int(attr[1]),
186 let bull = Int(attr[2]),
187 let steel = Int(attr[3]),
188 let bauxite = Int(attr[4]),
189 let kaihatu = Int(attr[5]),
190 let flagLv = Int(attr[8]),
191 let commandLv = Int(attr[9])
193 let p = NSPredicate(format: "date = %@", argumentArray: [date])
194 guard let oo = try? store.objects(with: KaihatuHistory.entity, predicate: p),
198 guard let obj = store.insertNewObject(for: KaihatuHistory.entity)
205 obj.bauxite = bauxite
206 obj.kaihatusizai = kaihatu
208 obj.flagShipName = attr[7]
209 obj.flagShipLv = flagLv
210 obj.commanderLv = commandLv
213 private func registerKenzoHistory(_ data: Data) {
214 let array = String(data: data, encoding: .utf8)?.components(separatedBy: "\n")
215 let store = LocalDataStore.oneTimeEditor()
217 let attr = $0.components(separatedBy: "\t")
218 guard attr.count == 11,
219 let date = dateFomatter.date(from: attr[0]),
220 let fuel = Int(attr[1]),
221 let bull = Int(attr[2]),
222 let steel = Int(attr[3]),
223 let bauxite = Int(attr[4]),
224 let kaihatu = Int(attr[5]),
225 let sType = Int(attr[7]),
226 let flagLv = Int(attr[9]),
227 let commandLv = Int(attr[10])
229 let p = NSPredicate(format: "date = %@", argumentArray: [date])
230 guard let oo = try? store.objects(with: KenzoHistory.entity, predicate: p),
234 guard let obj = store.insertNewObject(for: KenzoHistory.entity)
241 obj.bauxite = bauxite
242 obj.kaihatusizai = kaihatu
245 obj.flagShipName = attr[8]
246 obj.flagShipLv = flagLv
247 obj.commanderLv = commandLv
250 private func registerKenzoMark( _ data: Data) {
251 let array = String(data: data, encoding: .utf8)?.components(separatedBy: "\n")
252 let store = LocalDataStore.oneTimeEditor()
254 let attr = $0.components(separatedBy: "\t")
255 guard attr.count == 11,
256 let date = dateFomatter.date(from: attr[0]),
257 let fuel = Int(attr[1]),
258 let bull = Int(attr[2]),
259 let steel = Int(attr[3]),
260 let bauxite = Int(attr[4]),
261 let kaihatu = Int(attr[5]),
262 let shiId = Int(attr[6]),
263 let kDock = Int(attr[7]),
264 let flagLv = Int(attr[9]),
265 let commandLv = Int(attr[10])
267 let p = NSPredicate(format: "date = %@", argumentArray: [date])
268 guard let oo = try? store.objects(with: KenzoMark.entity, predicate: p),
272 guard let obj = store.insertNewObject(for: KenzoMark.entity)
279 obj.bauxite = bauxite
280 obj.kaihatusizai = kaihatu
281 obj.created_ship_id = shiId
283 obj.flagShipName = attr[8]
284 obj.flagShipLv = flagLv
285 obj.commanderLv = commandLv
288 private func registerDropShipHistory( _ data: Data) {
289 let array = String(data: data, encoding: .utf8)?.components(separatedBy: "\n")
290 let store = LocalDataStore.oneTimeEditor()
292 let attr = $0.components(separatedBy: "\t")
293 guard attr.count == 9,
294 let date = dateFomatter.date(from: attr[0]),
295 let mapInfo = Int(attr[3]),
296 let mapCell = Int(attr[4]),
297 let mark = Int(attr[7])
299 let p = NSPredicate(format: "date = %@", argumentArray: [date])
300 guard let oo = try? store.objects(with: DropShipHistory.entity, predicate: p),
304 guard let obj = store.insertNewObject(for: DropShipHistory.entity)
308 obj.shipName = attr[1]
309 obj.mapArea = attr[2]
310 obj.mapInfo = mapInfo
311 obj.mapCell = mapCell
312 obj.mapAreaName = attr[5]
313 obj.mapInfoName = attr[6]
315 obj.winRank = attr[8]