import Cocoa
-
-enum GuardEscapeAPI: String {
-
- case goback = "/kcsapi/api_req_combined_battle/goback_port"
- case gobakAlone = "/kcsapi/api_req_sortie/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 {
+ override class func canExecuteAPI(_ api: API) -> Bool {
- return GuardEscapeAPI(rawValue: api) != nil
+ 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 damagedShipId(damagedPos: Int) -> Int? {
- let firstDeckId = TemporaryDataStore.default.battle()?.deckId ?? 1
+ let tempStore = TemporaryDataStore.default
+ let firstDeckId = tempStore.sync { tempStore.battle()?.deckId ?? 1 }
let store = ServerDataStore.default
switch firstDeckId {
case 1:
switch damagedPos {
- case 1...6: return store.deck(by: 1)?.shipId(of: damagedPos - 1)
- case 7...12: return store.deck(by: 2)?.shipId(of: damagedPos - 6 - 1)
+ 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.deck(by: 3)?.shipId(of: damagedPos - 1)
+ return store.sync { store.deck(by: 3)?.shipId(of: damagedPos - 1) }
default:
return nil
}
}
- private func registerReserve() {
+ private func reserveEscapeShips() {
let escape = data["api_escape"]
}
let store = TemporaryDataStore.oneTimeEditor()
-
- guard let damaged = store.createGuardEscaped() else {
+ store.sync {
- 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
-
- guard let guardianId = ServerDataStore.default.deck(by: 2)?.shipId(of: fixedGuardianPos) else {
+ guard let damaged = store.createGuardEscaped() else {
+
+ return Logger.shared.log("Can not create GuardEscaped for damaged")
+ }
- return Logger.shared.log("guardianPos is wrong")
- }
-
- guard let guardian = store.createGuardEscaped() else {
+ damaged.shipID = damagedId
+ damaged.ensured = false
+
+ // store guardian if needs
+ guard let guardianPos = escape["api_tow_idx"][0].int else { return }
- return Logger.shared.log("Can not create GuardEscaped for guardinan")
+ 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
}
-
- guardian.shipID = guardianId
- guardian.ensured = false
}
private func removeInvalidEntry() {
let store = TemporaryDataStore.oneTimeEditor()
-
- store.notEnsuredGuardEscaped().forEach(store.delete)
- store.save(errorHandler: store.presentOnMainThread)
+ 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)
- store.save(errorHandler: store.presentOnMainThread)
+ store.sync {
+ store.guardEscaped().forEach(store.delete)
+ store.save(errorHandler: store.presentOnMainThread)
+ }
Thread.sleep(forTimeInterval: 0.1)
notify()
}
private func ensureGuardShelter() {
let store = TemporaryDataStore.oneTimeEditor()
-
- store.guardEscaped().forEach { $0.ensured = true }
- store.save(errorHandler: store.presentOnMainThread)
+ store.sync {
+ store.guardEscaped().forEach { $0.ensured = true }
+ store.save(errorHandler: store.presentOnMainThread)
+ }
Thread.sleep(forTimeInterval: 0.1)
notify()
}