OSDN Git Service

可能な限り#keyPathを使うように変更
[kcd/KCD.git] / KCD / TSVSupport.swift
1 //
2 //  File.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/02/05.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 private struct LFSeparateLine {
12     
13     static let empty = LFSeparateLine(line: "", empty: true)
14     
15     let line: String
16     
17     private let isEmpty: Bool
18     
19     init(line: String, empty: Bool = false) {
20         
21         self.line = line
22         isEmpty = empty
23     }
24     
25     func append(_ column: String) -> LFSeparateLine {
26         
27         if isEmpty { return LFSeparateLine(line: column) }
28         
29         let newLine = line + "\t" + column
30         
31         return LFSeparateLine(line: newLine)
32     }
33     
34     func append(_ dateCol: Date) -> LFSeparateLine {
35         
36         return append("\(dateCol)")
37     }
38     
39     func append(_ intCol: Int) -> LFSeparateLine {
40         
41         return append("\(intCol)")
42     }
43     
44     func append(_ boolCol: Bool) -> LFSeparateLine {
45         
46         return append("\(boolCol)")
47     }
48 }
49
50 // swiftlint:disable type_body_length
51 final class TSVSupport {
52     
53     private let store = LocalDataStore.oneTimeEditor()
54     
55     private var dateFomatter: DateFormatter = {
56         
57         let formatter = DateFormatter()
58         formatter.dateFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'Z"
59         return formatter
60     }()
61     
62     func load() {
63         
64         let panel = NSOpenPanel()
65         panel.allowedFileTypes = ["kcdlocaldata"]
66         panel.begin {
67             
68             guard $0 == .OK else { return }
69             
70             panel.urls.forEach { url in
71                 
72                 guard let fileW = try? FileWrapper(url: url) else { return }
73                 
74                 fileW.fileWrappers?.forEach {
75                     
76                     guard let data = $0.value.regularFileContents else { return }
77                     
78                     switch $0.key {
79                     case "kaihatu.tsv": self.registerKaihatuHistory(data)
80                     case "kenzo.tsv": self.registerKenzoHistory(data)
81                     case "kenzoMark.tsv": self.registerKenzoMark(data)
82                     case "dropShip.tsv": self.registerDropShipHistory(data)
83                     default: break
84                     }
85                 }
86                 
87             }
88         }
89     }
90     
91     func save() {
92         
93         let panel = NSSavePanel()
94         panel.allowedFileTypes = ["kcdlocaldata"]
95         panel.begin {
96             
97             guard $0 == .OK else { return }
98             guard let url = panel.url else { return }
99             guard let kaihatuHistory = self.dataOfKaihatuHistory() else { return }
100             guard let kenzoHistory = self.dataOfKenzoHistory() else { return }
101             guard let kenzoMark = self.dataOfKenzoMark() else { return }
102             guard let dropShipHistory = self.dataOfDropShipHistory() else { return }
103             
104             let fileW = FileWrapper(directoryWithFileWrappers: [:])
105             fileW.addRegularFile(withContents: kaihatuHistory, preferredFilename: "kaihatu.tsv")
106             fileW.addRegularFile(withContents: kenzoHistory, preferredFilename: "kenzo.tsv")
107             fileW.addRegularFile(withContents: kenzoMark, preferredFilename: "kenzoMark.tsv")
108             fileW.addRegularFile(withContents: dropShipHistory, preferredFilename: "dropShip.tsv")
109             do {
110                 
111                 try fileW.write(to: url, originalContentsURL: nil)
112                 
113             } catch {
114                 
115                 print("Error to write")
116             }
117         }
118     }
119     
120     private func localData<T>(_ entity: Entity<T>, sortBy: String = "date") -> [T] {
121         
122         let sortDesc = NSSortDescriptor(key: sortBy, ascending: true)
123         
124         guard let array = try? store.objects(of: entity, sortDescriptors: [sortDesc]) else {
125             
126             print("Can not get \(entity.name)")
127             return []
128         }
129         
130         return array
131     }
132     
133     private func dataOfKaihatuHistory() -> Data? {
134         
135         return localData(KaihatuHistory.entity)
136             .map {
137                 LFSeparateLine.empty
138                     .append($0.date)
139                     .append($0.fuel)
140                     .append($0.bull)
141                     .append($0.steel)
142                     .append($0.bauxite)
143                     .append($0.kaihatusizai)
144                     .append($0.name)
145                     .append($0.flagShipName)
146                     .append($0.flagShipLv)
147                     .append($0.commanderLv)
148                     .line
149             }
150             .joined(separator: "\n")
151             .data(using: .utf8)
152     }
153     
154     private func dataOfKenzoHistory() -> Data? {
155         
156         return localData(KenzoHistory.entity)
157             .map {
158             LFSeparateLine.empty
159                 .append($0.date)
160                 .append($0.fuel)
161                 .append($0.bull)
162                 .append($0.steel)
163                 .append($0.bauxite)
164                 .append($0.kaihatusizai)
165                 .append($0.name)
166                 .append($0.sTypeId)
167                 .append($0.flagShipName)
168                 .append($0.flagShipLv)
169                 .append($0.commanderLv)
170                 .line
171             }
172             .joined(separator: "\n")
173             .data(using: .utf8)
174     }
175     
176     private func dataOfKenzoMark() -> Data? {
177         
178         return localData(KenzoMark.entity, sortBy: "kDockId")
179             .map {
180             LFSeparateLine.empty
181                 .append($0.date)
182                 .append($0.fuel)
183                 .append($0.bull)
184                 .append($0.steel)
185                 .append($0.bauxite)
186                 .append($0.kaihatusizai)
187                 .append($0.created_ship_id)
188                 .append($0.kDockId)
189                 .append($0.flagShipName)
190                 .append($0.flagShipLv)
191                 .append($0.commanderLv)
192                 .line
193             }
194             .joined(separator: "\n")
195             .data(using: .utf8)
196     }
197     
198     private func dataOfDropShipHistory() -> Data? {
199         
200         return localData(DropShipHistory.entity)
201             .map {
202                 LFSeparateLine.empty
203                     .append($0.date)
204                     .append($0.shipName)
205                     .append($0.mapArea)
206                     .append($0.mapInfo)
207                     .append($0.mapCell)
208                     .append($0.mapAreaName)
209                     .append($0.mapInfoName)
210                     .append($0.mark)
211                     .append($0.winRank)
212                     .line
213             }
214             .joined(separator: "\n")
215             .data(using: .utf8)
216     }
217     
218     private func registerKaihatuHistory(_ data: Data) {
219         
220         let array = String(data: data, encoding: .utf8)?.components(separatedBy: "\n")
221         let store = LocalDataStore.oneTimeEditor()
222         array?.forEach {
223             
224             let attr = $0.components(separatedBy: "\t")
225             
226             guard attr.count == 10 else { return }
227             guard let date = dateFomatter.date(from: attr[0]) else { return }
228             guard let fuel = Int(attr[1]) else { return }
229             guard let bull = Int(attr[2]) else { return }
230             guard let steel = Int(attr[3]) else { return }
231             guard let bauxite = Int(attr[4]) else { return }
232             guard let kaihatu = Int(attr[5]) else { return }
233             guard let flagLv = Int(attr[8]) else { return }
234             guard let commandLv = Int(attr[9]) else { return }
235             
236             let p = NSPredicate(#keyPath(KaihatuHistory.date), equal: date)
237             
238             guard let oo = try? store.objects(of: KaihatuHistory.entity, predicate: p) else { return }
239             guard oo.count != 0 else { return }
240             guard let obj = store.insertNewObject(for: KaihatuHistory.entity) else { return }
241             
242             obj.date = date
243             obj.fuel = fuel
244             obj.bull = bull
245             obj.steel = steel
246             obj.bauxite = bauxite
247             obj.kaihatusizai = kaihatu
248             obj.name = attr[6]
249             obj.flagShipName = attr[7]
250             obj.flagShipLv = flagLv
251             obj.commanderLv = commandLv
252         }
253     }
254     
255     private func registerKenzoHistory(_ data: Data) {
256         
257         let array = String(data: data, encoding: .utf8)?.components(separatedBy: "\n")
258         let store = LocalDataStore.oneTimeEditor()
259         
260         array?.forEach {
261             
262             let attr = $0.components(separatedBy: "\t")
263             
264             guard attr.count == 11 else { return }
265             guard let date = dateFomatter.date(from: attr[0]) else { return }
266             guard let fuel = Int(attr[1]) else { return }
267             guard let bull = Int(attr[2]) else { return }
268             guard let steel = Int(attr[3]) else { return }
269             guard let bauxite = Int(attr[4]) else { return }
270             guard let kaihatu = Int(attr[5]) else { return }
271             guard let sType = Int(attr[7]) else { return }
272             guard let flagLv = Int(attr[9]) else { return }
273             guard let commandLv = Int(attr[10]) else { return }
274             
275             let p = NSPredicate(#keyPath(KenzoHistory.date), equal: date)
276             
277             guard let oo = try? store.objects(of: KenzoHistory.entity, predicate: p) else { return }
278             guard oo.count != 0 else { return }
279             guard let obj = store.insertNewObject(for: KenzoHistory.entity) else { return }
280             
281             obj.date = date
282             obj.fuel = fuel
283             obj.bull = bull
284             obj.steel = steel
285             obj.bauxite = bauxite
286             obj.kaihatusizai = kaihatu
287             obj.name = attr[6]
288             obj.sTypeId = sType
289             obj.flagShipName = attr[8]
290             obj.flagShipLv = flagLv
291             obj.commanderLv = commandLv
292         }
293     }
294     
295     private func registerKenzoMark( _ data: Data) {
296         
297         let array = String(data: data, encoding: .utf8)?.components(separatedBy: "\n")
298         let store = LocalDataStore.oneTimeEditor()
299         
300         array?.forEach {
301             
302             let attr = $0.components(separatedBy: "\t")
303             
304             guard attr.count == 11 else { return }
305             guard let date = dateFomatter.date(from: attr[0]) else { return }
306             guard let fuel = Int(attr[1]) else { return }
307             guard let bull = Int(attr[2]) else { return }
308             guard let steel = Int(attr[3]) else { return }
309             guard let bauxite = Int(attr[4]) else { return }
310             guard let kaihatu = Int(attr[5]) else { return }
311             guard let shiId = Int(attr[6]) else { return }
312             guard let kDock = Int(attr[7]) else { return }
313             guard let flagLv = Int(attr[9]) else { return }
314             guard let commandLv = Int(attr[10]) else { return }
315             
316             let p = NSPredicate(#keyPath(KenzoMark.date), equal: date)
317             
318             guard let oo = try? store.objects(of: KenzoMark.entity, predicate: p) else { return }
319             guard oo.count != 0 else { return }
320             guard let obj = store.insertNewObject(for: KenzoMark.entity) else { return }
321             
322             obj.date = date
323             obj.fuel = fuel
324             obj.bull = bull
325             obj.steel = steel
326             obj.bauxite = bauxite
327             obj.kaihatusizai = kaihatu
328             obj.created_ship_id = shiId
329             obj.kDockId = kDock
330             obj.flagShipName = attr[8]
331             obj.flagShipLv = flagLv
332             obj.commanderLv = commandLv
333         }
334     }
335     
336     private func registerDropShipHistory( _ data: Data) {
337         
338         let array = String(data: data, encoding: .utf8)?.components(separatedBy: "\n")
339         let store = LocalDataStore.oneTimeEditor()
340         
341         array?.forEach {
342             
343             let attr = $0.components(separatedBy: "\t")
344             
345             guard attr.count == 9 else { return }
346             guard let date = dateFomatter.date(from: attr[0]) else { return }
347             guard let mapInfo = Int(attr[3]) else { return }
348             guard let mapCell = Int(attr[4]) else { return }
349             guard let mark = Int(attr[7]) else { return }
350             
351             let p = NSPredicate(#keyPath(DropShipHistory.date), equal: date)
352             
353             guard let oo = try? store.objects(of: DropShipHistory.entity, predicate: p) else { return }
354             guard oo.count != 0 else { return }
355             guard let obj = store.insertNewObject(for: DropShipHistory.entity) else { return }
356             
357             obj.date = date
358             obj.shipName = attr[1]
359             obj.mapArea = attr[2]
360             obj.mapInfo = mapInfo
361             obj.mapCell = mapCell
362             obj.mapAreaName = attr[5]
363             obj.mapInfoName = attr[6]
364             obj.mark = mark != 0
365             obj.winRank = attr[8]
366         }
367     }
368 }