OSDN Git Service

基地航空隊から配備を外した時に正しく反映されない問題を修正
[kcd/KCD.git] / KCD / SetPlaneCommand.swift
1 //
2 //  SetPlaneCommand.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/14.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 class SetPlaneCommand: JSONCommand {
12     override class func canExecuteAPI(_ api: String) -> Bool {
13         if api == "/kcsapi/api_req_air_corps/set_plane" { return true }
14         return false
15     }
16     
17     override func execute() {
18         guard let areaId = parameter["api_area_id"].int,
19             let rId = parameter["api_base_id"].int,
20             let squadronId = parameter["api_squadron_id"].int
21             else { return print("SetPlaneCommand: Argument is wrong") }
22         guard let distance = data["api_distance"].int
23             else { return print("SetPlaneCommand: JSON is wrong") }
24         let planInfo = data["api_plane_info"][0]
25         guard let slotid = planInfo["api_slotid"].int,
26             let state = planInfo["api_state"].int
27             else { return print("api_plane_info is wrong") }
28         
29         let store = ServerDataStore.oneTimeEditor()
30         guard let airbase = store.airBase(area: areaId, base: rId)
31             else { return print("AirBase is not found") }
32         let planes = airbase.planeInfo
33         guard planes.count >= squadronId,
34             let plane = planes[squadronId - 1] as? AirBasePlaneInfo
35             else { return print("AirBase is wrong") }
36         
37         // TODO: state が 2 の時のみ cond, count, max_count がnilであることを許すようにする
38         plane.cond = planInfo["api_cond"].int ?? 0
39         plane.slotid = slotid
40         plane.state = state
41         plane.count = planInfo["api_count"].int ?? 0
42         plane.max_count = planInfo["api_max_count"].int ?? 0
43         
44         airbase.distance = distance
45         
46         data["api_after_bauxite"].int.map { store.material()?.bauxite = $0 }
47     }
48 }