OSDN Git Service

staticプロパティをインスタンスプロパティに変更
[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 {
21             
22             return
23         }
24         
25         guard let shipName = data["api_get_ship"]["api_ship_name"].string else {
26             
27             return
28         }
29         guard let winRank = data["api_win_rank"].string else {
30             
31             return
32         }
33         
34         let tempStore = TemporaryDataStore.default
35         guard let battle = tempStore.sync(execute: { tempStore.battle() }) else {
36             
37             Logger.shared.log("Can not get Battle")
38             
39             return
40         }
41         
42         let mapAreaId = tempStore.sync { battle.mapArea }
43         let mapInfoId = tempStore.sync { battle.mapInfo }
44         
45         let store = ServerDataStore.default
46         
47         guard let mapInfoName = store.sync(execute: { store.mapInfo(area: mapAreaId, no: mapInfoId)?.name }) else {
48             
49             Logger.shared.log("KCMasterMapInfo is not found")
50             
51             return
52         }
53         
54         guard let mapAreaName = store.sync(execute: { store.mapArea(by: mapAreaId)?.name }) else {
55             
56             Logger.shared.log("KCMasterMapArea is not found")
57             
58             return
59         }
60         
61         
62         let localStore = LocalDataStore.oneTimeEditor()
63         localStore.sync {
64             
65             guard let new = localStore.createHiddenDropShipHistory() else {
66                 
67                 Logger.shared.log("Can not create HiddenDropShipHistory")
68                 
69                 return
70             }
71             
72             new.shipName = shipName
73             new.mapArea = "\(mapAreaId)"
74             new.mapAreaName = mapAreaName
75             new.mapInfo = tempStore.sync { battle.mapInfo }
76             new.mapInfoName = mapInfoName
77             new.mapCell = tempStore.sync { battle.no }
78             new.winRank = winRank
79             new.date = Date()
80         }
81     }
82     
83     private func storeToVisible() {
84         
85         let store = LocalDataStore.oneTimeEditor()
86         store.sync {
87             
88             let hidden = store.hiddenDropShipHistories()
89             _ = hidden.map(store.createDropShipHistory(from:))
90             hidden.forEach(store.delete)
91         }
92     }
93 }