OSDN Git Service

staticプロパティをインスタンスプロパティに変更
[kcd/KCD.git] / KCD / DestroyItem2Command.swift
index 49744b7..1fce73b 100644 (file)
@@ -10,41 +10,37 @@ import Cocoa
 
 final class DestroyItem2Command: JSONCommand {
     
-    override class func canExecuteAPI(_ api: String) -> Bool {
+    override class func canExecuteAPI(_ api: API) -> Bool {
         
-        if api == "/kcsapi/api_req_kousyou/destroyitem2" { return true }
-        
-        return false
+        return api.endpoint == .destroyItem2
     }
     
     override func execute() {
         
-        guard let itemIds = parameter["api_slotitem_ids"]
-            .string?
-            .components(separatedBy: ",")
-            .flatMap({ Int($0) })
-            else { return print("api_slotitem_ids is wrong") }
-        
         let store = ServerDataStore.oneTimeEditor()
         
-        store.slotItems(in: itemIds)
-            .forEach { store.delete($0) }
-        
-        guard let material = store.material()
-            else { return print("Material is not found") }
+        store.sync { store.slotItems(in: self.parameter["api_slotitem_ids"].integerArray).forEach(store.delete) }
         
-        guard let gm = data["api_get_material"].arrayObject as? [Int]
-            else { return print("api_get_material is wrong") }
-        
-        let resouces = ["fuel", "bull", "steel", "bauxite"]
-        
-        zip(gm, resouces).forEach {
+        guard let material = store.sync(execute: { store.material() }) else {
+            
+            Logger.shared.log("Material is not found")
             
-            // TODO: use filter
-            if let current = material.value(forKey: $0.1) as? Int {
+            return
+        }
+        guard let getMaterials = data["api_get_material"].arrayObject as? [Int],
+            getMaterials.count >= 4 else {
+                
+                Logger.shared.log("api_get_material is wrong")
                 
-                material.setValue((current + $0.0) as NSNumber, forKey: $0.1)
-            }
+                return
+        }
+        
+        store.sync {
+            
+            material.fuel += getMaterials[0]
+            material.bull += getMaterials[1]
+            material.steel += getMaterials[2]
+            material.bauxite += getMaterials[3]
         }
     }
 }