import Cocoa
-
-enum GuardEscapeAPI: String {
-
- case goback = "/kcsapi/api_req_combined_battle/goback_port"
-}
-
extension Notification.Name {
static let DidUpdateGuardEscape = Notification.Name("com.masakih.KCD.Notification.DidUpdateGuardEscape")
}
final class GuardShelterCommand: JSONCommand {
-
- override class func canExecuteAPI(_ api: String) -> Bool {
- return GuardEscapeAPI(rawValue: api) != nil ? true : false
+ override class func canExecuteAPI(_ api: API) -> Bool {
+
+ return api.type == .guardEscape
}
override func execute() {
- if let b = BattleAPI(rawValue: api) {
+ switch api.type {
- switch b {
- case .battleResult, .combinedBattleResult:
- registerReserve()
-
- default:
- break
- }
- }
- if let m = MapAPI(rawValue: api), m == .next {
+ case .battleResult:
+ reserveEscapeShips()
+ case .map:
removeInvalidEntry()
- }
- if let _ = GuardEscapeAPI(rawValue: api) {
-
+ case .guardEscape:
ensureGuardShelter()
- }
- if let _ = PortAPI(rawValue: api) {
-
+ case .port:
removeAllEntry()
+ default: return Logger.shared.log("Missing API: \(apiResponse.api)")
}
}
- private func fleetMembers(fleetId: Int) -> [Int]? {
+ private func damagedShipId(damagedPos: Int) -> Int? {
- guard let deck = ServerDataStore.default.deck(by: fleetId)
- else { return nil }
+ let tempStore = TemporaryDataStore.default
+ let firstDeckId = tempStore.sync { tempStore.battle()?.deckId ?? 1 }
- return [deck.ship_0, deck.ship_1, deck.ship_2,
- deck.ship_3, deck.ship_4, deck.ship_5]
- }
-
- private func damagedShipId(damagedPos: Int) -> Int? {
+ let store = ServerDataStore.default
- func fleetAndPos(_ pos: Int) -> ([Int]?, Int) {
- switch pos {
- case 1...6: return (fleetMembers(fleetId: 1), pos - 1)
-
- case 7...12: return (fleetMembers(fleetId: 2), pos - 6 - 1)
-
- default: return (nil, -1)
+ switch firstDeckId {
+ case 1:
+ switch damagedPos {
+ case 1...6: return store.sync { store.deck(by: 1)?.shipId(of: damagedPos - 1) }
+ case 7...12: return store.sync { store.deck(by: 2)?.shipId(of: damagedPos - 6 - 1) }
+ default: return nil
}
+ case 3:
+ return store.sync { store.deck(by: 3)?.shipId(of: damagedPos - 1) }
+ default:
+ return nil
}
-
- let (fleet, pos) = fleetAndPos(damagedPos)
-
- return fleet?[pos]
}
- private func registerReserve() {
+ private func reserveEscapeShips() {
let escape = data["api_escape"]
- guard let guardianPos = escape["api_tow_idx"][0].int
- else { return }
-
- let fixedGuardianPos = guardianPos - 6 - 1
-
- guard case 0..<6 = fixedGuardianPos,
- let guardianId = fleetMembers(fleetId: 2)?[fixedGuardianPos]
- else { return print("guardianPos is wrong") }
-
- guard let escapeIdx = escape["api_escape_idx"][0].int,
- let damagedId = damagedShipId(damagedPos: escapeIdx)
- else { return print("damagedPos is wrong") }
+ guard let escapeIdx = escape["api_escape_idx"][0].int else { return }
+ guard let damagedId = damagedShipId(damagedPos: escapeIdx) else {
+
+ return Logger.shared.log("damagedPos is wrong")
+ }
let store = TemporaryDataStore.oneTimeEditor()
-
- guard let guardian = store.createGuardEscaped()
- else { return print("Can not create GuardEscaped for guardinan") }
-
- guardian.shipID = guardianId
- guardian.ensured = false
-
- guard let damaged = store.createGuardEscaped()
- else { return print("Can not create GuardEscaped for damaged") }
-
- damaged.shipID = damagedId
- damaged.ensured = false
+ store.sync {
+
+ guard let damaged = store.createGuardEscaped() else {
+
+ return Logger.shared.log("Can not create GuardEscaped for damaged")
+ }
+
+ damaged.shipID = damagedId
+ damaged.ensured = false
+
+ // store guardian if needs
+ guard let guardianPos = escape["api_tow_idx"][0].int else { return }
+
+ let fixedGuardianPos = guardianPos - 6 - 1
+
+ let sStore = ServerDataStore.default
+ guard let guardianId = sStore.sync(execute: { sStore.deck(by: 2)?.shipId(of: fixedGuardianPos) }) else {
+
+ return Logger.shared.log("guardianPos is wrong")
+ }
+
+ guard let guardian = store.createGuardEscaped() else {
+
+ return Logger.shared.log("Can not create GuardEscaped for guardinan")
+ }
+
+ guardian.shipID = guardianId
+ guardian.ensured = false
+ }
}
private func removeInvalidEntry() {
let store = TemporaryDataStore.oneTimeEditor()
-
- store.notEnsuredGuardEscaped().forEach { store.delete($0) }
- store.save()
+ store.sync {
+ store.notEnsuredGuardEscaped().forEach(store.delete)
+ store.save(errorHandler: store.presentOnMainThread)
+ }
Thread.sleep(forTimeInterval: 0.1)
notify()
}
private func removeAllEntry() {
let store = TemporaryDataStore.oneTimeEditor()
-
- store.guardEscaped().forEach { store.delete($0) }
- store.save()
+ store.sync {
+ store.guardEscaped().forEach(store.delete)
+ store.save(errorHandler: store.presentOnMainThread)
+ }
Thread.sleep(forTimeInterval: 0.1)
notify()
}
- func ensureGuardShelter() {
+ private func ensureGuardShelter() {
let store = TemporaryDataStore.oneTimeEditor()
-
- store.guardEscaped().forEach { $0.ensured = true }
- store.save()
+ store.sync {
+ store.guardEscaped().forEach { $0.ensured = true }
+ store.save(errorHandler: store.presentOnMainThread)
+ }
Thread.sleep(forTimeInterval: 0.1)
notify()
}
private func notify() {
- NotificationCenter.default.post(name: .DidUpdateGuardEscape, object: self)
+ DispatchQueue.main.async {
+
+ NotificationCenter.default.post(name: .DidUpdateGuardEscape, object: self)
+ }
}
}