OSDN Git Service

Squashed commit of the following: Swiftに変換した
[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 data = json[dataKey] as? [String: Any],
19             let getShip = data["api_get_ship"] as? [String: Any],
20             let shipName = getShip["api_ship_name"] as? String,
21             let winRank = data["api_win_rank"] as? String
22             else { return }
23         guard let battle = TemporaryDataStore.default.battle()
24             else { return print("Can not get Battle") }
25         let mapAreaId = battle.mapArea
26         
27         let store = ServerDataStore.default
28         guard let mapInfo = store.mapInfo(area: mapAreaId, no: battle.mapInfo)
29             else { return print("KCMasterMapInfo is not found") }
30         guard let mapArea = store.mapArea(byId: mapAreaId)
31             else { return print("KCMasterMapArea is not found") }
32         
33         let localStore = LocalDataStore.oneTimeEditor()
34         guard let new = localStore.createHiddenDropShipHistory()
35             else { return print("Can not create HiddenDropShipHistory") }
36         new.shipName = shipName
37         new.mapArea = "\(mapAreaId)"
38         new.mapAreaName = mapArea.name
39         new.mapInfo = battle.mapInfo
40         new.mapInfoName = mapInfo.name
41         new.mapCell = battle.no
42         new.winRank = winRank
43         new.date = Date()
44     }
45     
46     private func storeToVisible() {
47         let store = LocalDataStore.oneTimeEditor()
48         store.hiddenDropShipHistories()
49             .forEach {
50                 guard let new = store.createDropShipHistory()
51                     else { return print("Can not create DropShipHistory") }
52                 new.shipName = $0.shipName
53                 new.mapArea = $0.mapArea
54                 new.mapAreaName = $0.mapAreaName
55                 new.mapInfo = $0.mapInfo
56                 new.mapInfoName = $0.mapInfoName
57                 new.mapCell = $0.mapCell
58                 new.winRank = $0.winRank
59                 new.date = $0.date
60                 
61                 store.delete($0)
62         }
63     }
64 }