enum ChangeHenseiType: Int {
case append
+
case replace
+
case remove
+
case removeAllWithoutFlagship
}
static let userInfoKey = "HenseiDidChangeUserInfoKey"
- override class func canExecuteAPI(_ api: String) -> Bool {
-
- if api == "/kcsapi/api_req_hensei/change" { return true }
+ override class func canExecuteAPI(_ api: API) -> Bool {
- return false
+ return api.endpoint == .change
}
// api_ship_id の値
let shipId = parameter["api_ship_id"].int,
let shipIndex = parameter["api_ship_idx"].int else {
- return Logger.shared.log("parameter is wrong")
+ Logger.shared.log("parameter is wrong")
+
+ return
}
if shipId == -1 {
- guard let ship = removeShip(deckNumber: deckNumber, index: shipIndex) else { return }
- notify(type: .remove, fleetNumber: deckNumber, position: shipIndex, shipID: ship.id)
+ guard let shipId = removeShip(deckNumber: deckNumber, index: shipIndex) else {
+
+ return
+ }
+ notify(type: .remove, fleetNumber: deckNumber, position: shipIndex, shipID: shipId)
return
}
return
}
+ guard case 0..<Deck.maxShipCount = shipIndex else {
+
+ return
+ }
+
let store = ServerDataStore.oneTimeEditor()
- guard let deck = store.deck(by: deckNumber),
- case 0..<Deck.maxShipCount = shipIndex
- else { return }
+ guard let deck = store.sync(execute: { store.deck(by: deckNumber) }) else {
+
+ return
+ }
// すでに編成されているか? どこに?
let (shipDeckNumber, shipDeckIndex) = position(of: shipId)
// 配置しようとする位置に今配置されている艦娘
- let replaceShipId = deck[shipIndex]?.id
+ let replaceShipId = store.sync { deck[shipIndex]?.id }
// 艦隊に配備
- deck.setShip(id: shipId, for: shipIndex)
+ store.sync { deck.setShip(id: shipId, for: shipIndex) }
// 入れ替え
if shipDeckNumber != nil {
- let shipDeck = store.deck(by: shipDeckNumber!)
- shipDeck?.setShip(id: replaceShipId ?? -1, for: shipDeckIndex)
- packFleet(store: store, deck: shipDeck)
+ let shipDeck = store.sync { store.deck(by: shipDeckNumber!) }
+ store.sync { shipDeck?.setShip(id: replaceShipId ?? -1, for: shipDeckIndex) }
+ shipDeck.map { packFleet(store: store, deck: $0) }
}
packFleet(store: store, deck: deck)
private func position(of shipId: Int) -> (deckNumber: Int?, shipId: Int) {
- return ServerDataStore.default
- .decksSortedById()
- .lazy
- .enumerated()
- .map { (idx, deck) -> (Int, [Ship]) in (idx + 1, deck[0..<Deck.maxShipCount]) }
- .filter { $0.1.contains { $0.id == shipId } }
- .map { (deck, ships) in (deck, ships.index(where: { $0.id == shipId })!) }
- .first ?? (nil, -1)
+ let store = ServerDataStore.default
+
+ return store.sync {
+
+ store
+ .decksSortedById()
+ .lazy
+ .enumerated()
+ .map { (idx, deck) -> (Int, [Ship]) in (idx + 1, deck[0..<Deck.maxShipCount]) }
+ .filter { $0.1.contains { $0.id == shipId } }
+ .map { (deck, ships) in (deck, ships.index(where: { $0.id == shipId })!) }
+ .first ?? (nil, -1)
+ }
}
- private func removeShip(deckNumber: Int, index: Int) -> Ship? {
+ private func removeShip(deckNumber: Int, index: Int) -> Int? {
let store = ServerDataStore.oneTimeEditor()
- guard let deck = store.deck(by: deckNumber) else { return Logger.shared.log("Deck not found", value: nil) }
+ guard let deck = store.sync(execute: { store.deck(by: deckNumber) }) else {
+
+ Logger.shared.log("Deck not found")
+
+ return nil
+ }
- let shipId = deck[index]?.id ?? -1
- deck.setShip(id: -1, for: index)
+ let shipId = store.sync { deck[index]?.id ?? -1 }
+ store.sync { deck.setShip(id: -1, for: index) }
packFleet(store: store, deck: deck)
- return ServerDataStore.default.ship(by: shipId)
+ return shipId
}
private func excludeShipsWithoutFlagShip(deckNumber: Int) {
let store = ServerDataStore.oneTimeEditor()
-
- guard let deck = store.deck(by: deckNumber) else {
+ store.sync {
+ guard let deck = store.deck(by: deckNumber) else {
+
+ Logger.shared.log("Deck not found")
+
+ return
+ }
- return Logger.shared.log("Deck not found")
+ (1..<Deck.maxShipCount).forEach { deck.setShip(id: -1, for: $0) }
}
-
- (1..<Deck.maxShipCount).forEach { deck.setShip(id: -1, for: $0) }
}
- private func packFleet(store: ServerDataStore, deck: Deck?) {
+ private func packFleet(store: ServerDataStore, deck: Deck) {
- guard let deck = deck else { return }
-
- var ships = deck[0..<Deck.maxShipCount]
-
- (0..<Deck.maxShipCount).forEach {
+ func set(_ ships: [Ship], at index: Int, in deck: Deck) {
+
+ guard index < Deck.maxShipCount else {
+
+ return
+ }
- let shipId = ships.first?.id ?? -1
- deck.setShip(id: shipId, for: $0)
- if !ships.isEmpty { ships.removeFirst() }
+ deck.setShip(id: ships.first?.id ?? -1, for: index)
+
+ let newShips = ships.isEmpty ? [] : Array(ships[1...])
+
+ set(newShips, at: index + 1, in: deck)
}
+
+ store.sync { set(deck[0..<Deck.maxShipCount], at: 0, in: deck) }
}
private func notify(type: ChangeHenseiType,