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 {
13 case port = "/kcsapi/api_port/port"
14 case kousyouCreateItem = "/kcsapi/api_req_kousyou/createitem"
15 case kousyouDestoroyShip = "/kcsapi/api_req_kousyou/destroyship"
16 case kousyouRemodelSlot = "/kcsapi/api_req_kousyou/remodel_slot"
17 case hokyuCharge = "/kcsapi/api_req_hokyu/charge"
20 fileprivate func dataKeys(_ apiResponse: APIResponse) -> [String] {
21 guard let materialApi = MaterialAPI(rawValue: apiResponse.api)
22 else { return ["api_data"] }
24 case .port: return ["api_data", "api_material"]
25 case .kousyouCreateItem: return ["api_data", "api_material"]
26 case .kousyouDestoroyShip: return ["api_data", "api_material"]
27 case .kousyouRemodelSlot: return ["api_data", "api_after_material"]
28 case .hokyuCharge: return ["api_data", "api_material"]
32 class MaterialMapper: JSONMapper {
33 typealias ObjectType = Material
35 let apiResponse: APIResponse
36 let configuration: MappingConfiguration<Material>
39 "fuel", "bull", "steel", "bauxite",
40 "kousokukenzo", "kousokushuhuku", "kaihatusizai", "screw"
43 required init(_ apiResponse: APIResponse) {
44 self.apiResponse = apiResponse
45 self.configuration = MappingConfiguration(entity: Material.entity,
46 dataKeys: dataKeys(apiResponse),
47 editorStore: ServerDataStore.oneTimeEditor())
51 guard let store = configuration.editorStore as? ServerDataStore,
52 let material = store.material() ?? store.createMaterial()
53 else { return print("Can not create Material") }
55 if let _ = data[0].int {
56 let array = data.arrayValue.flatMap { $0.int }
57 register(material, data: array)
58 } else if let _ = data[0].dictionary {
59 register(material, data: data.arrayValue)
61 print("JSON is unknown type")
65 private func register(_ material: Material, data: [Int]) {
66 data.enumerated().forEach {
67 guard $0.offset < keys.count else { return }
68 material.setValue($0.element as NSNumber, forKey: keys[$0.offset])
71 private func register(_ material: Material, data: [JSON]) {
73 guard let i = $0["api_id"].int,
76 let newValue = $0["api_value"].int
78 material.setValue(newValue as NSNumber, forKey: keys[i - 1])