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 let apiResponse: APIResponse
15 let configuration: MappingConfiguration<Material>
18 "fuel", "bull", "steel", "bauxite",
19 "kousokukenzo", "kousokushuhuku", "kaihatusizai", "screw"
22 required init(_ apiResponse: APIResponse) {
24 self.apiResponse = apiResponse
25 self.configuration = MappingConfiguration(entity: Material.entity,
26 dataKeys: MaterialMapper.dataKeys(apiResponse),
27 editorStore: ServerDataStore.oneTimeEditor())
31 private enum MaterialAPI: String {
33 case port = "/kcsapi/api_port/port"
34 case kousyouCreateItem = "/kcsapi/api_req_kousyou/createitem"
35 case kousyouDestoroyShip = "/kcsapi/api_req_kousyou/destroyship"
36 case kousyouRemodelSlot = "/kcsapi/api_req_kousyou/remodel_slot"
37 case hokyuCharge = "/kcsapi/api_req_hokyu/charge"
40 private class func dataKeys(_ apiResponse: APIResponse) -> [String] {
42 guard let materialApi = MaterialAPI(rawValue: apiResponse.api) else { return ["api_data"] }
45 case .port: return ["api_data", "api_material"]
47 case .kousyouCreateItem: return ["api_data", "api_material"]
49 case .kousyouDestoroyShip: return ["api_data", "api_material"]
51 case .kousyouRemodelSlot: return ["api_data", "api_after_material"]
53 case .hokyuCharge: return ["api_data", "api_material"]
59 guard let store = configuration.editorStore as? ServerDataStore,
60 let material = store.material() ?? store.createMaterial() else {
62 print("Can not create Material")
66 if let _ = data[0].int {
68 let array = data.arrayValue.flatMap { $0.int }
69 register(material, data: array)
71 } else if let _ = data[0].dictionary {
73 register(material, data: data.arrayValue)
77 print("JSON is unknown type")
82 private func register(_ material: Material, data: [Int]) {
84 data.enumerated().forEach {
86 guard $0.offset < keys.count else { return }
88 material.setValue($0.element as NSNumber, forKey: keys[$0.offset])
92 private func register(_ material: Material, data: [JSON]) {
96 guard let i = $0["api_id"].int, case 1..<keys.count = i else { return }
97 guard let newValue = $0["api_value"].int else { return }
99 material.setValue(newValue as NSNumber, forKey: keys[i - 1])