2 // MaterialMapper.swift
5 // Created by Hori,Masaki on 2017/02/24.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
12 final class MaterialMapper: JSONMapper {
14 typealias ObjectType = Material
16 let apiResponse: APIResponse
17 let configuration: MappingConfiguration<Material>
20 "fuel", "bull", "steel", "bauxite",
21 "kousokukenzo", "kousokushuhuku", "kaihatusizai", "screw"
24 required init(_ apiResponse: APIResponse) {
26 self.apiResponse = apiResponse
27 self.configuration = MappingConfiguration(entity: Material.entity,
28 dataKeys: MaterialMapper.dataKeys(apiResponse),
29 editorStore: ServerDataStore.oneTimeEditor())
33 private enum MaterialAPI: String {
35 case port = "/kcsapi/api_port/port"
36 case kousyouCreateItem = "/kcsapi/api_req_kousyou/createitem"
37 case kousyouDestoroyShip = "/kcsapi/api_req_kousyou/destroyship"
38 case kousyouRemodelSlot = "/kcsapi/api_req_kousyou/remodel_slot"
39 case hokyuCharge = "/kcsapi/api_req_hokyu/charge"
42 private class func dataKeys(_ apiResponse: APIResponse) -> [String] {
44 guard let materialApi = MaterialAPI(rawValue: apiResponse.api) else { return ["api_data"] }
47 case .port: return ["api_data", "api_material"]
49 case .kousyouCreateItem: return ["api_data", "api_material"]
51 case .kousyouDestoroyShip: return ["api_data", "api_material"]
53 case .kousyouRemodelSlot: return ["api_data", "api_after_material"]
55 case .hokyuCharge: return ["api_data", "api_material"]
61 guard let store = configuration.editorStore as? ServerDataStore,
62 let material = store.material() ?? store.createMaterial() else {
64 print("Can not create Material")
68 if let _ = data[0].int {
70 let array = data.arrayValue.flatMap { $0.int }
71 register(material, data: array)
73 } else if let _ = data[0].dictionary {
75 register(material, data: data.arrayValue)
79 print("JSON is unknown type")
84 private func register(_ material: Material, data: [Int]) {
86 data.enumerated().forEach {
88 guard $0.offset < keys.count else { return }
90 material.setValue($0.element as NSNumber, forKey: keys[$0.offset])
94 private func register(_ material: Material, data: [JSON]) {
98 guard let i = $0["api_id"].int, case 1..<keys.count = i else { return }
99 guard let newValue = $0["api_value"].int else { return }
101 material.setValue(newValue as NSNumber, forKey: keys[i - 1])