OSDN Git Service

varを使用しないように変更
authormasakih <masakih@users.sourceforge.jp>
Mon, 16 Oct 2017 12:57:25 +0000 (21:57 +0900)
committermasakih <masakih@users.sourceforge.jp>
Mon, 16 Oct 2017 12:57:25 +0000 (21:57 +0900)
KCD/DamageCalculator.swift

index bc198af..8ce1f1d 100644 (file)
@@ -258,51 +258,37 @@ extension DamageCalculator {
         }
     }
     
-    private func buildDamagedEntity() {
+    private func buildDamagesOfFleet(fleet: Int, ships: [Ship]) {
         
+        guard case 0...1 = fleet else { return print("fleet must 0 or 1.") }
         guard let battle = store.battle() else { return print("Battle is invalid.") }
         
-        let aStore = ServerDataStore.default
-        var ships: [Any] = []
-        
-        // 第一艦隊
-        let firstFleetShips = aStore.ships(byDeckId: battle.deckId)
-        
-        ships += (firstFleetShips as [Any])
-        
-        while ships.count != 6 {
+        (0..<6).forEach {
             
-            ships.append(0)
-        }
-        
-        // 第二艦隊
-        let secondFleetShips = aStore.ships(byDeckId: 2)
-        
-        ships += (secondFleetShips as [Any])
-        
-        while ships.count != 12 {
-            
-            ships.append(0)
-        }
-        
-        ships.enumerated().forEach {
-            
-            guard let damage = store.createDamage() else {
-                
-                print("Can not create Damage")
-                return
-            }
+            guard let damage = store.createDamage() else { return print("Can not create Damage") }
             
             damage.battle = battle
-            damage.id = $0.offset
+            damage.id = $0 + fleet * 6
             
-            if let ship = $0.element as? Ship {
-                
-                damage.hp = ship.nowhp
-                damage.shipID = ship.id
-            }
+            guard case 0..<ships.count = $0 else { return }
+            
+            damage.hp = ships[$0].nowhp
+            damage.shipID = ships[$0].id
         }
     }
+    
+    private func buildDamagedEntity() {
+        
+        guard let battle = store.battle() else { return print("Battle is invalid.") }
+        
+        // 第一艦隊
+        let firstFleetShips = ServerDataStore.default.ships(byDeckId: battle.deckId)
+        buildDamagesOfFleet(fleet: 0, ships: firstFleetShips)
+        
+        // 第二艦隊
+        let secondFleetShips = ServerDataStore.default.ships(byDeckId: 2)
+        buildDamagesOfFleet(fleet: 1, ships: secondFleetShips)
+    }
 }
 
 // MARK: - Primitive Calclator