OSDN Git Service

Doutaku を 1.0 にアップデート
[kcd/KCD.git] / KCD / SetPlaneCommand.swift
index 6ded100..45f68a6 100644 (file)
@@ -10,11 +10,9 @@ import Cocoa
 
 final class SetPlaneCommand: JSONCommand {
     
-    override class func canExecuteAPI(_ api: String) -> Bool {
+    override class func canExecuteAPI(_ api: API) -> Bool {
         
-        if api == "/kcsapi/api_req_air_corps/set_plane" { return true }
-        
-        return false
+        return api.endpoint == .setPlane
     }
     
     override func execute() {
@@ -23,11 +21,15 @@ final class SetPlaneCommand: JSONCommand {
             let rId = parameter["api_base_id"].int,
             let squadronId = parameter["api_squadron_id"].int else {
                 
-                return Logger.shared.log("SetPlaneCommand: Argument is wrong")
+                Logger.shared.log("SetPlaneCommand: Argument is wrong")
+                
+                return
         }
         guard let distance = data["api_distance"].int else {
             
-            return Logger.shared.log("SetPlaneCommand: JSON is wrong")
+            Logger.shared.log("SetPlaneCommand: JSON is wrong")
+            
+            return
         }
         
         let planInfo = data["api_plane_info"][0]
@@ -35,33 +37,41 @@ final class SetPlaneCommand: JSONCommand {
         guard let slotid = planInfo["api_slotid"].int,
             let state = planInfo["api_state"].int else {
                 
-                return Logger.shared.log("api_plane_info is wrong")
+                Logger.shared.log("api_plane_info is wrong")
+                
+                return
         }
         
         let store = ServerDataStore.oneTimeEditor()
-        
-        guard let airbase = store.airBase(area: areaId, base: rId) else {
+        store.sync {
             
-            return Logger.shared.log("AirBase is not found")
-        }
-        
-        let planes = airbase.planeInfo
-        
-        guard planes.count >= squadronId,
-            let plane = planes[squadronId - 1] as? AirBasePlaneInfo else {
+            guard let airbase = store.airBase(area: areaId, base: rId) else {
+                
+                Logger.shared.log("AirBase is not found")
                 
-                return Logger.shared.log("AirBase is wrong")
+                return
+            }
+            
+            let planes = airbase.planeInfo
+            
+            guard planes.count >= squadronId,
+                let plane = planes[squadronId - 1] as? AirBasePlaneInfo else {
+                    
+                    Logger.shared.log("AirBase is wrong")
+                    
+                    return
+            }
+            
+            // TODO: state が 2 の時のみ cond, count, max_count がnilであることを許すようにする
+            plane.cond = planInfo["api_cond"].int ?? 0
+            plane.slotid = slotid
+            plane.state = state
+            plane.count = planInfo["api_count"].int ?? 0
+            plane.max_count = planInfo["api_max_count"].int ?? 0
+            
+            airbase.distance = distance
+            
+            self.data["api_after_bauxite"].int.map { store.material()?.bauxite = $0 }
         }
-        
-        // TODO: state が 2 の時のみ cond, count, max_count がnilであることを許すようにする
-        plane.cond = planInfo["api_cond"].int ?? 0
-        plane.slotid = slotid
-        plane.state = state
-        plane.count = planInfo["api_count"].int ?? 0
-        plane.max_count = planInfo["api_max_count"].int ?? 0
-        
-        airbase.distance = distance
-        
-        data["api_after_bauxite"].int.map { store.material()?.bauxite = $0 }
     }
 }