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 final class MaterialMapper: JSONMapper {
13         
14     let apiResponse: APIResponse
15     let configuration: MappingConfiguration<Material>
16     
17     private let keys = [
18         #keyPath(Material.fuel), #keyPath(Material.bull), #keyPath(Material.steel), #keyPath(Material.bauxite),
19         #keyPath(Material.kousokukenzo), #keyPath(Material.kousokushuhuku), #keyPath(Material.kaihatusizai), #keyPath(Material.screw)
20     ]
21     
22     required init(_ apiResponse: APIResponse) {
23         
24         self.apiResponse = apiResponse
25         self.configuration = MappingConfiguration(entity: Material.entity,
26                                                   dataKeys: MaterialMapper.dataKeys(apiResponse),
27                                                   editorStore: ServerDataStore.oneTimeEditor())
28     }
29     
30     
31     private enum MaterialAPI: String {
32         
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"
38     }
39     
40     private class func dataKeys(_ apiResponse: APIResponse) -> [String] {
41         
42         guard let materialApi = MaterialAPI(rawValue: apiResponse.api) else { return ["api_data"] }
43         
44         switch materialApi {
45         case .port: return ["api_data", "api_material"]
46             
47         case .kousyouCreateItem: return ["api_data", "api_material"]
48             
49         case .kousyouDestoroyShip: return ["api_data", "api_material"]
50             
51         case .kousyouRemodelSlot: return ["api_data", "api_after_material"]
52             
53         case .hokyuCharge: return ["api_data", "api_material"]
54         }
55     }
56     
57     func commit() {
58         
59         guard let store = configuration.editorStore as? ServerDataStore,
60             let material = store.material() ?? store.createMaterial() else {
61                 
62                 return Logger.shared.log("Can not create Material")
63         }
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, case 1...keys.count = i else { return }
96             guard let newValue = $0["api_value"].int else { return }
97             
98             material.setValue(newValue as NSNumber, forKey: keys[i - 1])
99         }
100     }
101 }