OSDN Git Service

Doutakuを導入
[kcd/KCD.git] / KCD / KenzoMarkCommand.swift
index 23121e6..ac2963a 100644 (file)
@@ -10,74 +10,69 @@ import Cocoa
 
 final class KenzoMarkCommand: JSONCommand {
     
+    struct KenzoDockInfo {
+        let dockId: Int
+        let shipId: Int
+        let fuel: Int
+        let bull: Int
+        let steel: Int
+        let bauxite: Int
+        let kaihatusizai: Int
+    }
+    
     override func execute() {
         
+        let store = LocalDataStore.oneTimeEditor()
+        store.sync {
+            self.executeInContext(localStore: store)
+        }
+    }
+    
+    private func executeInContext(localStore: LocalDataStore) {
+        
         guard let kdockId = parameter["api_kdock_id"].int else {
             
             return Logger.shared.log("api_kdock_id is wrong")
         }
         
         let store = ServerDataStore.default
-        
-        guard let kenzoDock = store.kenzoDock(by: kdockId) else {
+        guard let kenzoDock = store.sync(execute: { store.kenzoDock(by: kdockId) }) else {
             
-            return Logger.shared.log("KenzoDock is not fount")
+            return Logger.shared.log("KenzoDock is not found")
         }
-        
-        let fuel = kenzoDock.item1
-        let bull = kenzoDock.item2
-        let steel = kenzoDock.item3
-        let bauxite = kenzoDock.item4
-        let kaihatu = kenzoDock.item5
-        let shipId = kenzoDock.created_ship_id
-        
-        guard let flagShip = store.masterShip(by: shipId) else {
+        let kenzoDockInfo = store.sync {
+            KenzoDockInfo(dockId: kenzoDock.id,
+                          shipId: kenzoDock.created_ship_id,
+                          fuel: kenzoDock.item1,
+                          bull: kenzoDock.item2,
+                          steel: kenzoDock.item3,
+                          bauxite: kenzoDock.item4,
+                          kaihatusizai: kenzoDock.item5)
+        }
+        guard let flagShip = store.sync(execute: { store.masterShip(by: kenzoDock.created_ship_id) }) else {
             
             return Logger.shared.log("MasterShip is not found")
         }
         
-        let localStore = LocalDataStore.oneTimeEditor()
         guard let new = localStore.createKenzoHistory() else {
             
             return Logger.shared.log("Can not create KenzoHistory")
         }
         
-        new.name = flagShip.name
-        new.sTypeId = flagShip.stype.id
-        new.fuel = fuel
-        new.bull = bull
-        new.steel = steel
-        new.bauxite = bauxite
-        new.kaihatusizai = kaihatu
+        new.name = store.sync { flagShip.name }
+        new.sTypeId = store.sync { flagShip.stype.id }
+        new.fuel = kenzoDockInfo.fuel
+        new.bull = kenzoDockInfo.bull
+        new.steel = kenzoDockInfo.steel
+        new.bauxite = kenzoDockInfo.bauxite
+        new.kaihatusizai = kenzoDockInfo.kaihatusizai
         new.date = Date()
-        (new.flagShipLv, new.flagShipName, new.commanderLv) =
-            markedValues(fuel: fuel,
-                         bull: bull,
-                         steel: steel,
-                         bauxite: bauxite,
-                         kaihatu: kaihatu,
-                         kdockId: kdockId,
-                         shipId: shipId)
+        (new.flagShipLv, new.flagShipName, new.commanderLv) = markedValues(docInfo: kenzoDockInfo, in: localStore)
     }
     
-    // swiftlint:disable function_parameter_count
-    private func markedValues(fuel: Int,
-                              bull: Int,
-                              steel: Int,
-                              bauxite: Int,
-                              kaihatu: Int,
-                              kdockId: Int,
-                              shipId: Int) -> (Int, String, Int) {
-        
-        let store = LocalDataStore.default
+    private func markedValues(docInfo: KenzoDockInfo, in store: LocalDataStore) -> (Int, String, Int) {
         
-        if let kenzoMark = store.kenzoMark(fuel: fuel,
-                                           bull: bull,
-                                           steel: steel,
-                                           bauxite: bauxite,
-                                           kaihatusizai: kaihatu,
-                                           kDockId: kdockId,
-                                           shipId: shipId) {
+        if let kenzoMark = store.kenzoMark(docInfo: docInfo) {
             
             return (kenzoMark.flagShipLv, kenzoMark.flagShipName, kenzoMark.commanderLv)
         }