OSDN Git Service

コード整形
[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     
14     case port = "/kcsapi/api_port/port"
15     case kousyouCreateItem = "/kcsapi/api_req_kousyou/createitem"
16     case kousyouDestoroyShip = "/kcsapi/api_req_kousyou/destroyship"
17     case kousyouRemodelSlot = "/kcsapi/api_req_kousyou/remodel_slot"
18     case hokyuCharge = "/kcsapi/api_req_hokyu/charge"
19 }
20
21 fileprivate func dataKeys(_ apiResponse: APIResponse) -> [String] {
22     
23     guard let materialApi = MaterialAPI(rawValue: apiResponse.api)
24         else { return ["api_data"] }
25     
26     switch materialApi {
27     case .port: return ["api_data", "api_material"]
28         
29     case .kousyouCreateItem: return ["api_data", "api_material"]
30         
31     case .kousyouDestoroyShip: return ["api_data", "api_material"]
32         
33     case .kousyouRemodelSlot: return ["api_data", "api_after_material"]
34         
35     case .hokyuCharge: return ["api_data", "api_material"]
36     }
37 }
38
39 final class MaterialMapper: JSONMapper {
40     
41     typealias ObjectType = Material
42     
43     let apiResponse: APIResponse
44     let configuration: MappingConfiguration<Material>
45     
46     private let keys = [
47         "fuel", "bull", "steel", "bauxite",
48         "kousokukenzo", "kousokushuhuku", "kaihatusizai", "screw"
49     ]
50     
51     required init(_ apiResponse: APIResponse) {
52         
53         self.apiResponse = apiResponse
54         self.configuration = MappingConfiguration(entity: Material.entity,
55                                                   dataKeys: dataKeys(apiResponse),
56                                                   editorStore: ServerDataStore.oneTimeEditor())
57     }
58     
59     func commit() {
60         
61         guard let store = configuration.editorStore as? ServerDataStore,
62             let material = store.material() ?? store.createMaterial()
63             else { return print("Can not create Material") }
64         
65         if let _ = data[0].int {
66             
67             let array = data.arrayValue.flatMap { $0.int }
68             register(material, data: array)
69             
70         } else if let _ = data[0].dictionary {
71             
72             register(material, data: data.arrayValue)
73             
74         } else {
75             
76             print("JSON is unknown type")
77             
78         }
79     }
80     
81     private func register(_ material: Material, data: [Int]) {
82         
83         data.enumerated().forEach {
84             
85             guard $0.offset < keys.count else { return }
86             
87             material.setValue($0.element as NSNumber, forKey: keys[$0.offset])
88         }
89     }
90     
91     private func register(_ material: Material, data: [JSON]) {
92         
93         data.forEach {
94             
95             guard let i = $0["api_id"].int,
96                 i != 0,
97                 i - 1 < keys.count,
98                 let newValue = $0["api_value"].int
99                 else { return }
100             
101             material.setValue(newValue as NSNumber, forKey: keys[i - 1])
102         }
103     }
104 }