OSDN Git Service

UAをVersion/10.0.3 Safari/602.4.8に変更
[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 class DropShipHistoryCommand: JSONCommand {
12     override func execute() {
13         if api == "/kcsapi/api_port/port" || api == "/kcsapi/api_get_member/ship_deck" {
14             storeToVisible()
15         }
16         if !api.hasSuffix("battleresult") { return }
17         
18         guard let shipName = data["api_get_ship"]["api_ship_name"].string,
19             let winRank = data["api_win_rank"].string
20             else { return }
21         guard let battle = TemporaryDataStore.default.battle()
22             else { return print("Can not get Battle") }
23         let mapAreaId = battle.mapArea
24         
25         let store = ServerDataStore.default
26         guard let mapInfo = store.mapInfo(area: mapAreaId, no: battle.mapInfo)
27             else { return print("KCMasterMapInfo is not found") }
28         guard let mapArea = store.mapArea(byId: mapAreaId)
29             else { return print("KCMasterMapArea is not found") }
30         
31         let localStore = LocalDataStore.oneTimeEditor()
32         guard let new = localStore.createHiddenDropShipHistory()
33             else { return print("Can not create HiddenDropShipHistory") }
34         new.shipName = shipName
35         new.mapArea = "\(mapAreaId)"
36         new.mapAreaName = mapArea.name
37         new.mapInfo = battle.mapInfo
38         new.mapInfoName = mapInfo.name
39         new.mapCell = battle.no
40         new.winRank = winRank
41         new.date = Date()
42     }
43     
44     private func storeToVisible() {
45         let store = LocalDataStore.oneTimeEditor()
46         store.hiddenDropShipHistories()
47             .forEach {
48                 guard let new = store.createDropShipHistory()
49                     else { return print("Can not create DropShipHistory") }
50                 new.shipName = $0.shipName
51                 new.mapArea = $0.mapArea
52                 new.mapAreaName = $0.mapAreaName
53                 new.mapInfo = $0.mapInfo
54                 new.mapInfoName = $0.mapInfoName
55                 new.mapCell = $0.mapCell
56                 new.winRank = $0.winRank
57                 new.date = $0.date
58                 
59                 store.delete($0)
60         }
61     }
62 }