OSDN Git Service

MappingConfigurationをジェネリクスにした
[kcd/KCD.git] / KCD / MaterialMapper.swift
1 //
2 //  MaterialMapper.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/02/24.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
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"
17 }
18
19 fileprivate func dataKey(_ apiResponse: APIResponse) -> String {
20     guard let materialApi = MaterialAPI(rawValue: apiResponse.api)
21         else { return "api_data" }
22     switch materialApi {
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"
28     }
29 }
30
31 class MaterialMapper: JSONMapper {
32     typealias ObjectType = Material
33     
34     let apiResponse: APIResponse
35     let configuration: MappingConfiguration<Material>
36     
37     private let keys = [
38         "fuel", "bull", "steel", "bauxite",
39         "kousokukenzo", "kousokushuhuku", "kaihatusizai", "screw"
40     ]
41     
42     required init(_ apiResponse: APIResponse) {
43         self.apiResponse = apiResponse
44         self.configuration = MappingConfiguration(entity: Material.entity,
45                                                   dataKey: dataKey(apiResponse),
46                                                   editorStore: ServerDataStore.oneTimeEditor())
47     }
48     
49     func commit() {
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") }
56         
57         switch data {
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")
61         }
62     }
63     
64     private func register(_ material: Material, data: [Int]) {
65         data.enumerated().forEach {
66             guard $0.offset < keys.count
67                 else { return }
68             material.setValue($0.element as NSNumber, forKey: keys[$0.offset])
69         }
70     }
71     private func register(_ material: Material, data: [[String: Any]]) {
72         data.forEach {
73             guard let i = $0["api_id"] as? Int,
74                 i != 0,
75                 i - 1 < keys.count,
76                 let newValue = $0["api_value"] as? Int
77                 else { return }
78             material.setValue(newValue as NSNumber, forKey: keys[i - 1])
79         }
80     }
81 }