OSDN Git Service

CoreDataのスレッドを使用するように修正
[kcd/KCD.git] / KCD / DropShipHistoryCommand.swift
1 //
2 //  DropShipHistoryCommand.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/14.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class DropShipHistoryCommand: JSONCommand {
12     
13     override func execute() {
14         
15         if api.endpoint == .port || api.endpoint == .shipDeck {
16             
17             storeToVisible()
18         }
19         
20         if api.type != .battleResult { return }
21         
22         guard let shipName = data["api_get_ship"]["api_ship_name"].string else { return }
23         guard let winRank = data["api_win_rank"].string else { return }
24         
25         let tempStore = TemporaryDataStore.default
26         guard let battle = tempStore.sync(execute: { tempStore.battle() }) else {
27             
28             return Logger.shared.log("Can not get Battle")
29         }
30         
31         let mapAreaId = tempStore.sync { battle.mapArea }
32         let mapInfoId = tempStore.sync { battle.mapInfo }
33         
34         let store = ServerDataStore.default
35         
36         guard let mapInfoName = store.sync(execute: { store.mapInfo(area: mapAreaId, no: mapInfoId)?.name }) else {
37             
38             return Logger.shared.log("KCMasterMapInfo is not found")
39         }
40         
41         guard let mapAreaName = store.sync(execute: { store.mapArea(by: mapAreaId)?.name }) else {
42             
43             return Logger.shared.log("KCMasterMapArea is not found")
44         }
45         
46         
47         let localStore = LocalDataStore.oneTimeEditor()
48         localStore.sync {
49             
50             guard let new = localStore.createHiddenDropShipHistory() else {
51                 
52                 return Logger.shared.log("Can not create HiddenDropShipHistory")
53             }
54             
55             new.shipName = shipName
56             new.mapArea = "\(mapAreaId)"
57             new.mapAreaName = mapAreaName
58             new.mapInfo = tempStore.sync { battle.mapInfo }
59             new.mapInfoName = mapInfoName
60             new.mapCell = tempStore.sync { battle.no }
61             new.winRank = winRank
62             new.date = Date()
63         }
64     }
65     
66     private func storeToVisible() {
67         
68         let store = LocalDataStore.oneTimeEditor()
69         store.sync {
70             let hidden = store.hiddenDropShipHistories()
71             _ = hidden.map(store.createDropShipHistory(from:))
72             hidden.forEach(store.delete)
73         }
74     }
75 }