2 // DropShipHistoryCommand.swift
5 // Created by Hori,Masaki on 2017/01/14.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 final class DropShipHistoryCommand: JSONCommand {
13 override func execute() {
15 if api.endpoint == .port || api.endpoint == .shipDeck {
20 if api.type != .battleResult { return }
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 }
25 let tempStore = TemporaryDataStore.default
26 guard let battle = tempStore.sync(execute: { tempStore.battle() }) else {
28 return Logger.shared.log("Can not get Battle")
31 let mapAreaId = tempStore.sync { battle.mapArea }
32 let mapInfoId = tempStore.sync { battle.mapInfo }
34 let store = ServerDataStore.default
36 guard let mapInfoName = store.sync(execute: { store.mapInfo(area: mapAreaId, no: mapInfoId)?.name }) else {
38 return Logger.shared.log("KCMasterMapInfo is not found")
41 guard let mapAreaName = store.sync(execute: { store.mapArea(by: mapAreaId)?.name }) else {
43 return Logger.shared.log("KCMasterMapArea is not found")
47 let localStore = LocalDataStore.oneTimeEditor()
50 guard let new = localStore.createHiddenDropShipHistory() else {
52 return Logger.shared.log("Can not create HiddenDropShipHistory")
55 new.shipName = shipName
56 new.mapArea = "\(mapAreaId)"
57 new.mapAreaName = mapAreaName
58 new.mapInfo = tempStore.sync { battle.mapInfo }
59 new.mapInfoName = mapInfoName
60 new.mapCell = tempStore.sync { battle.no }
66 private func storeToVisible() {
68 let store = LocalDataStore.oneTimeEditor()
70 let hidden = store.hiddenDropShipHistories()
71 _ = hidden.map(store.createDropShipHistory(from:))
72 hidden.forEach(store.delete)