2 // MaterialMapper.swift
5 // Created by Hori,Masaki on 2017/02/24.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
12 fileprivate enum MaterialAPI: String {
14 case port = "/kcsapi/api_port/port"
15 case kousyouCreateItem = "/kcsapi/api_req_kousyou/createitem"
16 case kousyouDestoroyShip = "/kcsapi/api_req_kousyou/destroyship"
17 case kousyouRemodelSlot = "/kcsapi/api_req_kousyou/remodel_slot"
18 case hokyuCharge = "/kcsapi/api_req_hokyu/charge"
21 fileprivate func dataKeys(_ apiResponse: APIResponse) -> [String] {
23 guard let materialApi = MaterialAPI(rawValue: apiResponse.api)
24 else { return ["api_data"] }
27 case .port: return ["api_data", "api_material"]
29 case .kousyouCreateItem: return ["api_data", "api_material"]
31 case .kousyouDestoroyShip: return ["api_data", "api_material"]
33 case .kousyouRemodelSlot: return ["api_data", "api_after_material"]
35 case .hokyuCharge: return ["api_data", "api_material"]
39 final class MaterialMapper: JSONMapper {
41 typealias ObjectType = Material
43 let apiResponse: APIResponse
44 let configuration: MappingConfiguration<Material>
47 "fuel", "bull", "steel", "bauxite",
48 "kousokukenzo", "kousokushuhuku", "kaihatusizai", "screw"
51 required init(_ apiResponse: APIResponse) {
53 self.apiResponse = apiResponse
54 self.configuration = MappingConfiguration(entity: Material.entity,
55 dataKeys: dataKeys(apiResponse),
56 editorStore: ServerDataStore.oneTimeEditor())
61 guard let store = configuration.editorStore as? ServerDataStore,
62 let material = store.material() ?? store.createMaterial()
63 else { return print("Can not create Material") }
65 if let _ = data[0].int {
67 let array = data.arrayValue.flatMap { $0.int }
68 register(material, data: array)
70 } else if let _ = data[0].dictionary {
72 register(material, data: data.arrayValue)
76 print("JSON is unknown type")
81 private func register(_ material: Material, data: [Int]) {
83 data.enumerated().forEach {
85 guard $0.offset < keys.count else { return }
87 material.setValue($0.element as NSNumber, forKey: keys[$0.offset])
91 private func register(_ material: Material, data: [JSON]) {
95 guard let i = $0["api_id"].int,
98 let newValue = $0["api_value"].int
101 material.setValue(newValue as NSNumber, forKey: keys[i - 1])