class CalculateDamageCommand: JSONCommand {
override func execute() {
- if api == "/kcsapi/api_req_map/start" {
- resetBattle()
- startBattle()
- return
- }
- if api == "/kcsapi/api_req_map/next" {
- nextCell()
- updateBattleCell()
- return
- }
- if api == "/kcsapi/api_req_sortie/battle" ||
- api == "/kcsapi/api_req_sortie/airbattle" ||
- api == "/kcsapi/api_req_sortie/ld_airbattle" {
+ guard let battleApi = BattleAPI(rawValue: api)
+ else { return }
+
+ switch battleApi {
+ case .battle, .airBattle, .ldAirBattle:
calculateBattle()
- return
- }
- if api == "/kcsapi/api_req_combined_battle/ec_battle" {
+ case .combinedEcBattle:
battleType = .enemyCombined
calcEnemyCombinedBattle()
- return
- }
- if api == "/kcsapi/api_req_combined_battle/battle" ||
- api == "/kcsapi/api_req_combined_battle/airbattle" {
+ case .combinedBattle, .combinedAirBattle:
battleType = .combinedAir
calcCombinedBattleAir()
- return
- }
- if api == "/kcsapi/api_req_combined_battle/battle_water" ||
- api == "/kcsapi/api_req_combined_battle/ld_airbattle" {
+ case .combinedBattleWater, .combinedLdAirBattle:
battleType = .combinedWater
calculateBattle()
- return
- }
- if api == "/kcsapi/api_req_combined_battle/each_battle" {
+ case .combinedEachBattle:
battleType = .eachCombinedAir
calcEachBattleAir()
- return
- }
- if api == "/kcsapi/api_req_combined_battle/each_battle_water" {
+ case .combinedEachBattleWater:
battleType = .eachCombinedWater
calculateBattle()
- return
- }
-
- if api == "/kcsapi/api_req_battle_midnight/battle" {
- calculateMidnightBattle()
- return
- }
- if api == "/kcsapi/api_req_battle_midnight/sp_midnight" {
+ case .midnightBattle, .midnightSpMidnight, .combinedMidnightBattle, .combinedSpMidnight:
calculateMidnightBattle()
- return
- }
- if api == "/kcsapi/api_req_combined_battle/midnight_battle" {
- calculateMidnightBattle()
- return
- }
- if api == "/kcsapi/api_req_combined_battle/sp_midnight" {
- calculateMidnightBattle()
- return
- }
- if api == "/kcsapi/api_req_combined_battle/ec_midnight_battle" {
+ case .combinedEcMidnightBattle:
battleType = .eachCombinedAir
calculateMidnightBattle()
- return
- }
-
- if api == "/kcsapi/api_req_sortie/battleresult" ||
- api == "/kcsapi/api_req_combined_battle/battleresult" {
+ case .battleResult, .combinedBattleResult:
applyDamage()
resetDamage()
- return
}
-
- print("undefined battle type")
}
private var battleType: BattleType = .normal
- private lazy var store: TemporaryDataStore = {
- return TemporaryDataStore.oneTimeEditor()
- }()
+ private let store = TemporaryDataStore.oneTimeEditor()
- private func resetBattle() {
- store.battles().forEach { store.delete($0) }
- }
private func resetDamage() {
store.damages().forEach { store.delete($0) }
}
- private func startBattle() {
- guard let deckId = arguments["api_deck_id"].flatMap({ Int($0) }),
- let mapArea = arguments["api_maparea_id"].flatMap({ Int($0) }),
- let mapInfo = arguments["api_mapinfo_no"].flatMap({ Int($0) })
- else { return print("startBattle Arguments is wrong") }
-
- guard let data = json["api_data"] as? [String:Any],
- let no = data["api_no"] as? Int
- else { return print("startBattle JSON is wrong") }
- guard let battle = store.insertNewObject(forEntityName: "Battle") as? KCBattle
- else { return print("Can not create Battle") }
- battle.deckId = deckId
- battle.mapArea = mapArea
- battle.mapInfo = mapInfo
- battle.no = no
- }
+
private func updateBattleCell() {
guard let battle = store.battle()
else { return print("Battle is invalid.") }
- var battleCell: NSNumber? = battle.no as NSNumber
- if battleCell == 0 { battleCell = nil }
- if api.hasSuffix("next") { battleCell = nil }
- battle.battleCell = battleCell
- }
- private func nextCell() {
- guard let data = json[dataKey] as? [String:Any],
- let cellNumber = data["api_no"] as? Int,
- let eventId = data["api_event_id"] as? Int
- else { return print("updateBattleCell JSON is wrong") }
- guard let battle = store.battle()
- else { return print("Battle is invalid.") }
- battle.no = cellNumber
- battle.isBossCell = (eventId == 5)
+ battle.battleCell = (battle.no == 0 ? nil : battle.no as NSNumber)
}
private func buildDamagedEntity() {
guard let battle = store.battle()
}
private func calculateHogeki(baseKeyPath: String, battleFleet: BattleFleet = .first) {
let j = json as NSDictionary
- guard let data = j.value(forKeyPath: baseKeyPath) as? [String:Any],
+ guard let data = j.value(forKeyPath: baseKeyPath) as? [String: Any],
let tt = data["api_df_list"] as? [Any],
let dd = data["api_damage"] as? [Any]
else { return }
}
private func calculateFDam(baseKeyPath: String, battleFleet: BattleFleet = .first) {
let j = json as NSDictionary
- guard let data = j.value(forKeyPath: baseKeyPath) as? [String:Any],
+ guard let data = j.value(forKeyPath: baseKeyPath) as? [String: Any],
let koukuDamages = data["api_fdam"] as? [Int]
else { return }
//
private func calculateMidnightBattle() {
- updateBattleCell()
-
// 艦隊 戦闘艦隊
// 連合vs通常(水上) 第2
// 連合vs通常(機動) 第2
import Cocoa
+enum MapAPI: String {
+ case start = "/kcsapi/api_req_map/start"
+ case next = "/kcsapi/api_req_map/next"
+}
+
class MapStartCommand: JSONCommand {
override class func canExecuteAPI(_ api: String) -> Bool {
- if api == "/kcsapi/api_req_map/start" { return true }
- if api == "/kcsapi/api_req_map/next" { return true }
- return false
+ return MapAPI(rawValue: api) != nil ? true : false
}
override func execute() {
- CalculateDamageCommand(apiResponse: apiResponse).execute()
+ MapAPI(rawValue: api).map {
+ switch $0 {
+ case .start: startBattle()
+ case .next:
+ nextCell()
+ updateBattleCell()
+ }
+ }
GuardShelterCommand(apiResponse: apiResponse).execute()
}
+
+ private let store = TemporaryDataStore.oneTimeEditor()
+ private func startBattle() {
+ store.battles().forEach { store.delete($0) }
+
+ guard let deckId = arguments["api_deck_id"].flatMap({ Int($0) }),
+ let mapArea = arguments["api_maparea_id"].flatMap({ Int($0) }),
+ let mapInfo = arguments["api_mapinfo_no"].flatMap({ Int($0) })
+ else { return print("startBattle Arguments is wrong") }
+
+ guard let data = json["api_data"] as? [String: Any],
+ let no = data["api_no"] as? Int
+ else { return print("startBattle JSON is wrong") }
+ guard let battle = store.insertNewObject(forEntityName: "Battle") as? KCBattle
+ else { return print("Can not create Battle") }
+ battle.deckId = deckId
+ battle.mapArea = mapArea
+ battle.mapInfo = mapInfo
+ battle.no = no
+ }
+ private func nextCell() {
+ guard let data = json[dataKey] as? [String: Any],
+ let cellNumber = data["api_no"] as? Int,
+ let eventId = data["api_event_id"] as? Int
+ else { return print("updateBattleCell JSON is wrong") }
+ guard let battle = store.battle()
+ else { return print("Battle is invalid.") }
+ battle.no = cellNumber
+ battle.isBossCell = (eventId == 5)
+ }
+ private func updateBattleCell() {
+ store.battle().map { $0.battleCell = nil }
+ }
}