2 // SlotResetCommand.swift
5 // Created by Hori,Masaki on 2017/01/09.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 class SlotResetCommand: JSONCommand {
12 override class func canExecuteAPI(_ api: String) -> Bool {
13 if api == "/kcsapi/api_req_kaisou/slot_exchange_index" { return true }
17 override func execute() {
18 let store = ServerDataStore.oneTimeEditor()
19 guard let ship = parameter["api_id"]
21 .flatMap({ store.ship(by: $0) })
22 else { return print("api_id is wrong") }
23 guard let slotItems = data["api_slot"].arrayObject as? [Int]
24 else { return print("Can not parse api_data.api_slot") }
26 slotItems.enumerated().forEach { ship.setItem($0.element, for: $0.offset) }
28 let storedSlotItems = store.sortedSlotItemsById()
29 let newSet = slotItems
30 .flatMap { slotItem -> SlotItem? in
31 guard slotItem > 0 else { return nil }
32 let found = storedSlotItems.binarySearch { $0.id ==? slotItem }
33 if let item = found { return item }
34 print("Item \(slotItem) is not found")
38 ship.equippedItem = NSOrderedSet(array: newSet)