OSDN Git Service

7隻の艦隊表示を作成
[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 == "/kcsapi/api_port/port" || api == "/kcsapi/api_get_member/ship_deck" {
16             
17             storeToVisible()
18             
19         }
20         
21         if !api.hasSuffix("battleresult") { return }
22         
23         guard let shipName = data["api_get_ship"]["api_ship_name"].string else { return }
24         guard let winRank = data["api_win_rank"].string else { return }
25         
26         guard let battle = TemporaryDataStore.default.battle() else {
27             
28             return Logger.shared.log("Can not get Battle")
29         }
30         
31         let mapAreaId = battle.mapArea
32         
33         let store = ServerDataStore.default
34         
35         guard let mapInfo = store.mapInfo(area: mapAreaId, no: battle.mapInfo) else {
36             
37             return Logger.shared.log("KCMasterMapInfo is not found")
38         }
39         
40         guard let mapArea = store.mapArea(by: mapAreaId) else {
41             
42             return Logger.shared.log("KCMasterMapArea is not found")
43         }
44         
45         
46         let localStore = LocalDataStore.oneTimeEditor()
47         guard let new = localStore.createHiddenDropShipHistory() else {
48             
49             return Logger.shared.log("Can not create HiddenDropShipHistory")
50         }
51         
52         new.shipName = shipName
53         new.mapArea = "\(mapAreaId)"
54         new.mapAreaName = mapArea.name
55         new.mapInfo = battle.mapInfo
56         new.mapInfoName = mapInfo.name
57         new.mapCell = battle.no
58         new.winRank = winRank
59         new.date = Date()
60     }
61     
62     private func storeToVisible() {
63         
64         let store = LocalDataStore.oneTimeEditor()
65         
66         store.hiddenDropShipHistories()
67             .forEach {
68                 
69                 guard let new = store.createDropShipHistory() else {
70                     
71                     return Logger.shared.log("Can not create DropShipHistory")
72                 }
73                 
74                 new.shipName = $0.shipName
75                 new.mapArea = $0.mapArea
76                 new.mapAreaName = $0.mapAreaName
77                 new.mapInfo = $0.mapInfo
78                 new.mapInfoName = $0.mapInfoName
79                 new.mapCell = $0.mapCell
80                 new.winRank = $0.winRank
81                 new.date = $0.date
82                 
83                 store.delete($0)
84         }
85     }
86 }