OSDN Git Service

処理を簡素にわかりやすくした
[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         guard let battle = TemporaryDataStore.default.battle() else {
26             
27             return Logger.shared.log("Can not get Battle")
28         }
29         
30         let mapAreaId = battle.mapArea
31         
32         let store = ServerDataStore.default
33         
34         guard let mapInfo = store.mapInfo(area: mapAreaId, no: battle.mapInfo) else {
35             
36             return Logger.shared.log("KCMasterMapInfo is not found")
37         }
38         
39         guard let mapArea = store.mapArea(by: mapAreaId) else {
40             
41             return Logger.shared.log("KCMasterMapArea is not found")
42         }
43         
44         
45         let localStore = LocalDataStore.oneTimeEditor()
46         guard let new = localStore.createHiddenDropShipHistory() else {
47             
48             return Logger.shared.log("Can not create HiddenDropShipHistory")
49         }
50         
51         new.shipName = shipName
52         new.mapArea = "\(mapAreaId)"
53         new.mapAreaName = mapArea.name
54         new.mapInfo = battle.mapInfo
55         new.mapInfoName = mapInfo.name
56         new.mapCell = battle.no
57         new.winRank = winRank
58         new.date = Date()
59     }
60     
61     private func storeToVisible() {
62         
63         let store = LocalDataStore.oneTimeEditor()
64         
65         store.hiddenDropShipHistories()
66             .forEach {
67                 
68                 guard let new = store.createDropShipHistory() else {
69                     
70                     return Logger.shared.log("Can not create DropShipHistory")
71                 }
72                 
73                 new.shipName = $0.shipName
74                 new.mapArea = $0.mapArea
75                 new.mapAreaName = $0.mapAreaName
76                 new.mapInfo = $0.mapInfo
77                 new.mapInfoName = $0.mapInfoName
78                 new.mapCell = $0.mapCell
79                 new.winRank = $0.winRank
80                 new.date = $0.date
81                 
82                 store.delete($0)
83         }
84     }
85 }