2 // MaterialMapper.swift
5 // Created by Hori,Masaki on 2017/02/24.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 fileprivate enum MaterialAPI: String {
12 case port = "/kcsapi/api_port/port"
13 case kousyouCreateItem = "/kcsapi/api_req_kousyou/createitem"
14 case kousyouDestoroyShip = "/kcsapi/api_req_kousyou/destroyship"
15 case kousyouRemodelSlot = "/kcsapi/api_req_kousyou/remodel_slot"
16 case hokyuCharge = "/kcsapi/api_req_hokyu/charge"
19 fileprivate func dataKey(_ apiResponse: APIResponse) -> String {
20 guard let materialApi = MaterialAPI(rawValue: apiResponse.api)
21 else { return "api_data" }
23 case .port: return "api_data.api_material"
24 case .kousyouCreateItem: return "api_data.api_material"
25 case .kousyouDestoroyShip: return "api_data.api_material"
26 case .kousyouRemodelSlot: return "api_data.api_after_material"
27 case .hokyuCharge: return "api_data.api_material"
31 class MaterialMapper: JSONMapper {
32 typealias ObjectType = Material
34 let apiResponse: APIResponse
35 let configuration: MappingConfiguration<Material>
38 "fuel", "bull", "steel", "bauxite",
39 "kousokukenzo", "kousokushuhuku", "kaihatusizai", "screw"
42 required init(_ apiResponse: APIResponse) {
43 self.apiResponse = apiResponse
44 self.configuration = MappingConfiguration(entity: Material.entity,
45 dataKey: dataKey(apiResponse),
46 editorStore: ServerDataStore.oneTimeEditor())
50 let j = apiResponse.json as NSDictionary
51 guard let data = j.value(forKeyPath: configuration.dataKey)
52 else { return print("JSON is wrong") }
53 guard let store = configuration.editorStore as? ServerDataStore,
54 let material = store.material() ?? store.createMaterial()
55 else { return print("Can not create Material") }
58 case let array as [Int]: register(material, data: array)
59 case let array as [[String: Any]]: register(material, data: array)
60 default: print("JSON is unknown type")
64 private func register(_ material: Material, data: [Int]) {
65 data.enumerated().forEach {
66 guard $0.offset < keys.count
68 material.setValue($0.element as NSNumber, forKey: keys[$0.offset])
71 private func register(_ material: Material, data: [[String: Any]]) {
73 guard let i = $0["api_id"] as? Int,
76 let newValue = $0["api_value"] as? Int
78 material.setValue(newValue as NSNumber, forKey: keys[i - 1])