OSDN Git Service

Equatableを自動実装させるようにした
[kcd/KCD.git] / KCD / KenzoMarkCommand.swift
index 66ad95e..9fea3e5 100644 (file)
@@ -8,65 +8,83 @@
 
 import Cocoa
 
-class KenzoMarkCommand: JSONCommand {
+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() {
-        guard let kdockId = arguments["api_kdock_id"].int
-            else { return print("api_kdock_id is wrong") }
         
-        let store = ServerDataStore.default
-        guard let kenzoDock = store.kenzoDock(byDockId: kdockId)
-            else { return print("KenzoDock is not fount") }
-        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
+        let store = LocalDataStore.oneTimeEditor()
+        store.sync {
+            self.executeInContext(localStore: store)
+        }
+    }
+    
+    private func executeInContext(localStore: LocalDataStore) {
         
-        guard let flagShip = store.masterShip(byId: shipId)
-            else { return print("MasterShip is not found") }
+        guard let kdockId = parameter["api_kdock_id"].int else {
+            
+            Logger.shared.log("api_kdock_id is wrong")
+            
+            return
+        }
         
-        let localStore = LocalDataStore.oneTimeEditor()
-        guard let new = localStore.createKenzoHistory()
-            else { return print("Can not create KenzoHistory") }
+        let store = ServerDataStore.default
+        guard let kenzoDock = store.sync(execute: { store.kenzoDock(by: kdockId) }) else {
+            
+            Logger.shared.log("KenzoDock is not found")
+            
+            return
+        }
+        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 {
+            
+            Logger.shared.log("MasterShip is not found")
+            
+            return
+        }
+        
+        guard let new = localStore.createKenzoHistory() else {
+            
+            Logger.shared.log("Can not create KenzoHistory")
+            
+            return
+        }
         
-        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
-        if let kenzoMark = store.kenzoMark(fuel: fuel,
-                                           bull: bull,
-                                           steel: steel,
-                                           bauxite: bauxite,
-                                           kaihatusizai: kaihatu,
-                                           kDockId: kdockId,
-                                           shipId: shipId) {
-            return (kenzoMark.flagShipLv,
-                    kenzoMark.flagShipName,
-                    kenzoMark.commanderLv)
+    
+    private func markedValues(docInfo: KenzoDockInfo, in store: LocalDataStore) -> (Int, String, Int) {
+        
+        if let kenzoMark = store.kenzoMark(docInfo: docInfo) {
+            
+            return (kenzoMark.flagShipLv, kenzoMark.flagShipName, kenzoMark.commanderLv)
         }
+        
         return (-1, "", -1)
     }
 }