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: 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 isDeleteNotExist: Bool {
72 switch apiResponse.api.endpoint {
73 case .ship3, .getShip, .shipDeck,
74 .powerup, .slotDeprive:
82 private var store: ServerDataStore? {
84 return configuration.editorStore as? ServerDataStore
87 func beginRegister(_ ship: Ship) {
92 func handleExtraValue(_ value: JSON, forKey key: String, to ship: Ship) -> Bool {
94 // 取得後破棄した装備のデータを削除するため保有IDを保存
97 guard let id = value.int else { return false }
99 registerIds.append(id)
104 if key == "api_ship_id" {
106 guard let masterId = value.int else { return false }
108 setMaster(masterId, to: ship)
113 if key == "api_exp" {
115 guard let exp = value[0].int else { return false }
122 if key == "api_slot" {
124 setSlot(value, to: ship)
129 if key == "api_slot_ex" {
131 guard let ex = value.int else { return false }
133 setExtraSlot(ex, to: ship)
141 func finishOperating() {
143 if !isDeleteNotExist { return }
145 store?.ships(exclude: registerIds).forEach { store?.delete($0) }
148 private func setMaster(_ masterId: Int, to ship: Ship) {
150 if ship.ship_id == masterId { return }
152 guard let mShip = masterShips.binarySearch(comparator: { $0.id ==? masterId }),
153 let masterShip = store?.object(of: MasterShip.entity, with: mShip.objectID) else {
155 return Logger.shared.log("Can not convert to current moc object masterShip")
158 ship.master_ship = masterShip
159 ship.ship_id = masterId
162 private func setSlot(_ slotItems: JSON, to ship: Ship) {
164 guard let converSlotItems = slotItems.arrayObject as? [Int] else { return }
165 guard let store = store else { return }
167 let newItems: [SlotItem] =
168 converSlotItems.flatMap { item in
170 if item == 0 || item == -1 { return nil }
172 guard let found = self.slotItems.binarySearch(comparator: { $0.id ==? item }),
173 let slotItem = store.object(of: SlotItem.entity, with: found.objectID) else {
175 let maxV = converSlotItems.last
176 if maxV != nil, maxV! < item {
178 Debug.print("item is maybe unregistered, so it is new ship's equipment.")
181 Logger.shared.log("Can not convert to current moc object slotItem")
188 ship.equippedItem = NSOrderedSet(array: newItems)
191 private func setExtraSlot(_ exSlotItem: Int, to ship: Ship) {
193 guard exSlotItem != -1, exSlotItem != 0 else { return }
194 guard let found = slotItems.binarySearch(comparator: { $0.id ==? exSlotItem }),
195 let ex = store?.object(of: SlotItem.entity, with: found.objectID) else {
197 return Logger.shared.log("Can not convert to current moc object")