5 // Created by Hori,Masaki on 2017/02/23.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
12 final class ShipMapper: JSONMapper {
14 private static let ignoreKeys = ["api_gomes", "api_gomes2", "api_broken", "api_powup",
15 "api_voicef", "api_afterlv", "api_aftershipid", "api_backs",
16 "api_slotnum", "api_stype", "api_name", "api_yomi",
17 "api_raig", "api_luck", "api_saku", "api_raim", "api_baku",
18 "api_taik", "api_houg", "api_houm", "api_tyku",
19 "api_ndock_item", "api_star",
20 "api_ndock_time_str", "api_member_id",
21 "api_fuel_max", "api_bull_max"]
23 let apiResponse: APIResponse
24 let configuration: MappingConfiguration<Ship>
26 required init(_ apiResponse: APIResponse) {
28 self.apiResponse = apiResponse
29 self.configuration = MappingConfiguration(entity: Ship.entity,
30 dataKeys: ShipMapper.dataKeys(apiResponse),
31 editorStore: ServerDataStore.oneTimeEditor(),
32 ignoreKeys: ShipMapper.ignoreKeys)
35 // slotDepriveの時に2種類のデータが来るため
36 init(forSlotDepriveUnset apiResponse: APIResponse) {
38 self.apiResponse = apiResponse
39 self.configuration = MappingConfiguration(entity: Ship.entity,
40 dataKeys: ["api_data", "api_ship_data", "api_unset_ship"],
41 editorStore: ServerDataStore.oneTimeEditor(),
42 ignoreKeys: ShipMapper.ignoreKeys)
45 private class func dataKeys(_ apiResponse: APIResponse) -> [String] {
47 switch apiResponse.api.endpoint {
49 case .port, .getShip, .powerup: return ["api_data", "api_ship"]
51 case .ship3, .shipDeck: return ["api_data", "api_ship_data"]
53 case .slotDeprive: return ["api_data", "api_ship_data", "api_set_ship"]
55 case .ship, .ship2: return ["api_data"]
57 default: return Logger.shared.log("Missing API: \(apiResponse.api)", value: ["api_data"])
61 private var registerIds: [Int] = []
62 private lazy var masterShips: [MasterShip] = {
64 return ServerDataStore.default.sortedMasterShipsById()
66 private lazy var slotItems: [SlotItem] = {
68 return ServerDataStore.default.sortedSlotItemsById()
70 private var needsDeleteUnregisteredShip: Bool {
72 switch apiResponse.api.endpoint {
73 case .ship3, .getShip, .shipDeck, .powerup, .slotDeprive:
81 private var store: ServerDataStore? {
83 return configuration.editorStore as? ServerDataStore
86 func beginRegister(_ ship: Ship) {
91 func handleExtraValue(_ value: JSON, forKey key: String, to ship: Ship) -> Bool {
93 // 取得後破棄した装備のデータを削除するため保有IDを保存
96 guard let id = value.int else { return false }
98 registerIds.append(id)
103 if key == "api_ship_id" {
105 guard let masterId = value.int else { return false }
107 if ship.ship_id == masterId { return true }
109 setMaster(masterId, to: ship)
114 if key == "api_exp" {
116 guard let exp = value[0].int else { return false }
118 if ship.exp == exp { return true }
125 if key == "api_slot" {
127 setSlot(value, to: ship)
132 if key == "api_slot_ex" {
134 guard let ex = value.int else { return false }
136 if ship.slot_ex == ex { return true }
138 setExtraSlot(ex, to: ship)
146 func finishOperating() {
148 if !needsDeleteUnregisteredShip { return }
150 store?.ships(exclude: registerIds).forEach { store?.delete($0) }
153 private func setMaster(_ masterId: Int, to ship: Ship) {
155 guard let mShip = masterShips.binarySearch(comparator: { $0.id ==? masterId }),
156 let masterShip = store?.object(of: MasterShip.entity, with: mShip.objectID) else {
158 return Logger.shared.log("Can not convert to current moc object masterShip")
161 ship.master_ship = masterShip
162 ship.ship_id = masterId
165 private func setSlot(_ slotItems: JSON, to ship: Ship) {
167 guard let convertedSlotItems = slotItems.arrayObject as? [Int] else { return }
168 guard let store = store else { return }
170 let newItems: [SlotItem] = convertedSlotItems
171 .filter { $0 != 0 && $0 != -1 }
174 guard let found = self.slotItems.binarySearch(comparator: { $0.id ==? item }),
175 let slotItem = store.object(of: SlotItem.entity, with: found.objectID) else {
177 let maxV = convertedSlotItems.last
178 if maxV != nil, maxV! < item {
180 Debug.print("item is maybe unregistered, so it is new ship's equipment.")
183 Logger.shared.log("Can not convert to current moc object slotItem")
190 ship.equippedItem = NSOrderedSet(array: newItems)
193 private func setExtraSlot(_ exSlotItem: Int, to ship: Ship) {
195 guard exSlotItem != -1, exSlotItem != 0 else { return }
196 guard let found = slotItems.binarySearch(comparator: { $0.id ==? exSlotItem }),
197 let ex = store?.object(of: SlotItem.entity, with: found.objectID) else {
199 return Logger.shared.log("Can not convert to current moc object")
203 ship.slot_ex = exSlotItem