OSDN Git Service

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