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 import SwiftyJSON
11
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"
18 }
19
20 fileprivate func dataKeys(_ apiResponse: APIResponse) -> [String] {
21     guard let materialApi = MaterialAPI(rawValue: apiResponse.api)
22         else { return ["api_data"] }
23     switch materialApi {
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"]
29     }
30 }
31
32 class MaterialMapper: JSONMapper {
33     typealias ObjectType = Material
34     
35     let apiResponse: APIResponse
36     let configuration: MappingConfiguration<Material>
37     
38     private let keys = [
39         "fuel", "bull", "steel", "bauxite",
40         "kousokukenzo", "kousokushuhuku", "kaihatusizai", "screw"
41     ]
42     
43     required init(_ apiResponse: APIResponse) {
44         self.apiResponse = apiResponse
45         self.configuration = MappingConfiguration(entity: Material.entity,
46                                                   dataKeys: dataKeys(apiResponse),
47                                                   editorStore: ServerDataStore.oneTimeEditor())
48     }
49     
50     func commit() {
51         guard let store = configuration.editorStore as? ServerDataStore,
52             let material = store.material() ?? store.createMaterial()
53             else { return print("Can not create Material") }
54         
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)
60         } else {
61             print("JSON is unknown type")
62         }
63     }
64     
65     private func register(_ material: Material, data: [Int]) {
66         data.enumerated().forEach {
67             guard $0.offset < keys.count
68                 else { return }
69             material.setValue($0.element as NSNumber, forKey: keys[$0.offset])
70         }
71     }
72     private func register(_ material: Material, data: [JSON]) {
73         data.forEach {
74             guard let i = $0["api_id"].int,
75                 i != 0,
76                 i - 1 < keys.count,
77                 let newValue = $0["api_value"].int
78                 else { return }
79             material.setValue(newValue as NSNumber, forKey: keys[i - 1])
80         }
81     }
82 }