2 // SetPlaneCommand.swift
5 // Created by Hori,Masaki on 2017/01/14.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 final class SetPlaneCommand: JSONCommand {
13 override class func canExecuteAPI(_ api: API) -> Bool {
15 return api.endpoint == .setPlane
18 override func execute() {
20 guard let areaId = parameter["api_area_id"].int,
21 let rId = parameter["api_base_id"].int,
22 let squadronId = parameter["api_squadron_id"].int else {
24 return Logger.shared.log("SetPlaneCommand: Argument is wrong")
26 guard let distance = data["api_distance"].int else {
28 return Logger.shared.log("SetPlaneCommand: JSON is wrong")
31 let planInfo = data["api_plane_info"][0]
33 guard let slotid = planInfo["api_slotid"].int,
34 let state = planInfo["api_state"].int else {
36 return Logger.shared.log("api_plane_info is wrong")
39 let store = ServerDataStore.oneTimeEditor()
42 guard let airbase = store.airBase(area: areaId, base: rId) else {
44 return Logger.shared.log("AirBase is not found")
47 let planes = airbase.planeInfo
49 guard planes.count >= squadronId,
50 let plane = planes[squadronId - 1] as? AirBasePlaneInfo else {
52 return Logger.shared.log("AirBase is wrong")
55 // TODO: state が 2 の時のみ cond, count, max_count がnilであることを許すようにする
56 plane.cond = planInfo["api_cond"].int ?? 0
59 plane.count = planInfo["api_count"].int ?? 0
60 plane.max_count = planInfo["api_max_count"].int ?? 0
62 airbase.distance = distance
64 self.data["api_after_bauxite"].int.map { store.material()?.bauxite = $0 }