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     private class func dataKeys(_ apiResponse: APIResponse) -> [String] {
31                 
32         switch apiResponse.api.endpoint {
33             
34         case .material: return ["api_data"]
35             
36         case .port, .createItem, .destroyShip, .charge: return ["api_data", "api_material"]
37             
38         case .remodelSlot: return ["api_data", "api_after_material"]
39             
40         default: return Logger.shared.log("Missing API: \(apiResponse.api)", value: ["api_data"])
41         }
42     }
43     
44     func commit() {
45         
46         guard let store = configuration.editorStore as? ServerDataStore,
47             let material = store.material() ?? store.createMaterial() else {
48                 
49                 return Logger.shared.log("Can not create Material")
50         }
51         
52         if let _ = data[0].int {
53             
54             let array = data.arrayValue.flatMap { $0.int }
55             register(material, data: array)
56             
57         } else if let _ = data[0].dictionary {
58             
59             register(material, data: data.arrayValue)
60             
61         } else {
62             
63             print("JSON is unknown type")
64             
65         }
66     }
67     
68     private func register(_ material: Material, data: [Int]) {
69         
70         zip(data, keys).forEach {
71             
72             material.setValue($0, forKey: $1)
73         }
74     }
75     
76     private func register(_ material: Material, data: [JSON]) {
77         
78         data.forEach {
79             
80             guard let i = $0["api_id"].int, case 1...keys.count = i else { return }
81             guard let newValue = $0["api_value"].int else { return }
82             
83             material.setValue(newValue as NSNumber, forKey: keys[i - 1])
84         }
85     }
86 }