OSDN Git Service

Doutaku を 1.0 にアップデート
[kcd/KCD.git] / KCD / MaterialMapper.swift
index 511b429..f0ff9a5 100644 (file)
@@ -9,62 +9,60 @@
 import Cocoa
 import SwiftyJSON
 
-fileprivate enum MaterialAPI: String {
-    
-    case port = "/kcsapi/api_port/port"
-    case kousyouCreateItem = "/kcsapi/api_req_kousyou/createitem"
-    case kousyouDestoroyShip = "/kcsapi/api_req_kousyou/destroyship"
-    case kousyouRemodelSlot = "/kcsapi/api_req_kousyou/remodel_slot"
-    case hokyuCharge = "/kcsapi/api_req_hokyu/charge"
-}
-
-fileprivate func dataKeys(_ apiResponse: APIResponse) -> [String] {
-    
-    guard let materialApi = MaterialAPI(rawValue: apiResponse.api)
-        else { return ["api_data"] }
-    
-    switch materialApi {
-    case .port: return ["api_data", "api_material"]
-        
-    case .kousyouCreateItem: return ["api_data", "api_material"]
-        
-    case .kousyouDestoroyShip: return ["api_data", "api_material"]
-        
-    case .kousyouRemodelSlot: return ["api_data", "api_after_material"]
-        
-    case .hokyuCharge: return ["api_data", "api_material"]
-    }
-}
-
 final class MaterialMapper: JSONMapper {
-    
-    typealias ObjectType = Material
-    
+        
     let apiResponse: APIResponse
     let configuration: MappingConfiguration<Material>
     
     private let keys = [
-        "fuel", "bull", "steel", "bauxite",
-        "kousokukenzo", "kousokushuhuku", "kaihatusizai", "screw"
+        #keyPath(Material.fuel), #keyPath(Material.bull), #keyPath(Material.steel), #keyPath(Material.bauxite),
+        #keyPath(Material.kousokukenzo), #keyPath(Material.kousokushuhuku), #keyPath(Material.kaihatusizai), #keyPath(Material.screw)
     ]
     
     required init(_ apiResponse: APIResponse) {
         
         self.apiResponse = apiResponse
-        self.configuration = MappingConfiguration(entity: Material.entity,
-                                                  dataKeys: dataKeys(apiResponse),
+        self.configuration = MappingConfiguration(entity: Material.self,
+                                                  dataKeys: MaterialMapper.dataKeys(apiResponse),
                                                   editorStore: ServerDataStore.oneTimeEditor())
     }
     
+    private class func dataKeys(_ apiResponse: APIResponse) -> [String] {
+                
+        switch apiResponse.api.endpoint {
+            
+        case .material: return ["api_data"]
+            
+        case .port, .createItem, .destroyShip, .charge: return ["api_data", "api_material"]
+            
+        case .remodelSlot: return ["api_data", "api_after_material"]
+            
+        default:
+            
+            Logger.shared.log("Missing API: \(apiResponse.api)")
+            
+            return ["api_data"]
+        }
+    }
+    
     func commit() {
         
+        configuration.editorStore.sync(execute: commintInContext)
+    }
+    
+    private func commintInContext() {
+        
         guard let store = configuration.editorStore as? ServerDataStore,
-            let material = store.material() ?? store.createMaterial()
-            else { return print("Can not create Material") }
+            let material = store.material() ?? store.createMaterial() else {
+                
+                Logger.shared.log("Can not create Material")
+                
+                return
+        }
         
         if let _ = data[0].int {
             
-            let array = data.arrayValue.flatMap { $0.int }
+            let array = data.arrayValue.compactMap { $0.int }
             register(material, data: array)
             
         } else if let _ = data[0].dictionary {
@@ -80,11 +78,9 @@ final class MaterialMapper: JSONMapper {
     
     private func register(_ material: Material, data: [Int]) {
         
-        data.enumerated().forEach {
-            
-            guard $0.offset < keys.count else { return }
+        zip(data, keys).forEach {
             
-            material.setValue($0.element as NSNumber, forKey: keys[$0.offset])
+            material.setValue($0, forKey: $1)
         }
     }
     
@@ -92,11 +88,14 @@ final class MaterialMapper: JSONMapper {
         
         data.forEach {
             
-            guard let i = $0["api_id"].int,
-                i != 0,
-                i - 1 < keys.count,
-                let newValue = $0["api_value"].int
-                else { return }
+            guard let i = $0["api_id"].int, case 1...keys.count = i else {
+                
+                return
+            }
+            guard let newValue = $0["api_value"].int else {
+                
+                return
+            }
             
             material.setValue(newValue as NSNumber, forKey: keys[i - 1])
         }