store.battles().forEach { store.delete($0) }
}
private func resetDamage() {
- store.damagesWithSortDescriptors(nil)?.forEach { store.delete($0) }
+ store.damages().forEach { store.delete($0) }
}
private func startBattle() {
guard let deckIdString = arguments["api_deck_id"],
ships.append(0)
}
ships.enumerated().forEach {
- guard let damage = store.insertNewObject(forEntityName: "Damage") as? HMKCDamage
+ guard let damage = store.createDamage()
else { return print("Can not create Damage") }
damage.battle = battle
damage.id = $0.offset
//
private var damages: [HMKCDamage] {
- let sortDescriptor = NSSortDescriptor(key: "id", ascending: true)
- let array = store.damagesWithSortDescriptors([sortDescriptor])
- if array == nil || array!.count != 12 {
+ let array = store.sortedDamagesById()
+ if array.count != 12 {
buildDamagedEntity()
- guard let newDamages = store.damagesWithSortDescriptors([sortDescriptor])
+ let newDamages = store.sortedDamagesById()
+ guard newDamages.count == 12
else {
print("ERROR!!!! CAN NOT CREATE DAMAGE OBJECT")
return []
}
return newDamages
}
- return array!
+ return array
}
//
targetArraysArray.count == hougeki1Damages.count
else { return print("api_damage is wrong") }
- let eFlags: [Int]? = data["api_at_eflag"] as? [Int]
+ let eFlags: [Int]? = (data["api_at_eflag"] as? [Int])?.filter { $0 != -1 }
#if DAMAGE_CHECK
print("Start Hougeki \(baseKeyPath)")
let shipOffset = (battleFleet == .second) ? 6 : 0
targetArraysArray.enumerated().forEach { (i, targetArray) in
targetArray.enumerated().forEach { (j, targetPosition) in
- if let e = eFlags, e.count > i {
+ // targetは自軍か?
+ if let e = eFlags, 0..<e.count ~= i {
if e[i] != 1 { return }
}
else {
if battleFleet == .each {
- if targetPosition < 0 || targetPosition > 12 { return }
- }
- else {
- if targetPosition < 0 || targetPosition > 6 { return }
+ guard 1...12 ~= targetPosition else { return }
+ } else {
+ guard 1...6 ~= targetPosition else { return }
}
}
- let damageObject = damages[targetPosition - 1 + shipOffset]
+ let damagePos = targetPosition - 1 + shipOffset
+ guard 0..<damages.count ~= damagePos
+ else { return print("damage pos is larger than damage count") }
+ let damageObject = damages[damagePos]
+ guard 0..<hougeki1Damages[i].count ~= j
+ else { return print("target pos is larger than damages") }
let damage = hougeki1Damages[i][j]
let hp = damageObject.hp
var newHP = (hp as Int) - damage
koukuDamages.enumerated().forEach { (idx, damage) in
if idx == 0 { return }
- let damageObject = damages[ idx - 1 + shipOffset]
+ let damagePos = idx - 1 + shipOffset
+ guard 0..<damages.count ~= damagePos
+ else { return }
+ let damageObject = damages[damagePos]
var newHP = damageObject.hp - damage
if newHP <= 0 {
let shipId = damageObject.shipID
// 連合vs連合(機動) 第1 全体 use kouku nor kouku2
let bf: () -> BattleFleet = {
switch self.battleType {
- case .combinedWater, .combinedAir: return .second
- case .eachCombinedWater, .eachCombinedAir: return .each
+ case .combinedWater, .combinedAir,
+ .eachCombinedWater, .eachCombinedAir:
+ return .second
default: return .first
}
}
switch battleType {
case .combinedWater: return .second
case .eachCombinedWater: return .each
- case .eachCombinedAir: return .second
+// case .eachCombinedAir: return .second
default: return .first
}
}
calculateHogeki(baseKeyPath: "api_data.api_hougeki3") {
switch battleType {
case .combinedWater: return .second
- case .eachCombinedWater: return .second
+// case .eachCombinedWater: return .second
case .eachCombinedAir: return .each
default: return .first
}
private func damageControlIfPossible(nowhp: Int, ship: HMKCShipObject) -> Int {
var nowHp = nowhp
if nowHp < 0 { nowHp = 0 }
- let maxhp = ship.maxhp
+ let maxhp = ship.maxhp
let store = HMServerDataStore.default
var useDamageControl = false
ship.equippedItem.forEach {
//
private func applyDamage() {
- let sortDesc = NSSortDescriptor(key: "id", ascending: true)
- guard let totalDamages = store.damagesWithSortDescriptors([sortDesc]),
- totalDamages.count == 12
+ let totalDamages = store.sortedDamagesById()
+ guard totalDamages.count == 12
else { return print("Damages count is invalid") }
let aStore = HMServerDataStore.oneTimeEditor()
totalDamages.forEach {
return battles().first
}
func battles() -> [HMKCBattle] {
- guard let a = try? self.objects(withEntityName: "Battle"),
+ guard let a = try? self.objects(withEntityName: "Battle"),
let array = a as? [HMKCBattle]
- else { return [] }
- return array
+ else { return [] }
+ return array
}
func resetBattle() {
battles().forEach { delete($0) }
}
- func damagesWithSortDescriptors(_ sortDescriptors: [NSSortDescriptor]?) -> [HMKCDamage]? {
- guard let a = try? objects(withEntityName: "Damage", sortDescriptors: sortDescriptors, predicate: nil),
- let array = a as? [HMKCDamage],
- array.count != 0
- else { return nil }
+ func sortedDamagesById() -> [HMKCDamage] {
+ let sortDescriptor = NSSortDescriptor(key: "id", ascending: true)
+ guard let a = try? objects(withEntityName: "Damage", sortDescriptors: [sortDescriptor]),
+ let array = a as? [HMKCDamage]
+ else { return [] }
return array
}
+ func damages() -> [HMKCDamage] {
+ guard let a = try? objects(withEntityName: "Damage"),
+ let array = a as? [HMKCDamage]
+ else { return [] }
+ return array
+ }
+ func createDamage() -> HMKCDamage? {
+ return insertNewObject(forEntityName: "Damage") as? HMKCDamage
+ }
func guardEscaped() -> [HMKCGuardEscaped] {
guard let e = try? objects(withEntityName: "GuardEscaped"),