OSDN Git Service

swiftlint 'line_length'の警告を修正
[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 = arguments["api_area_id"].flatMap({ Int($0) }),
19             let rId = arguments["api_base_id"].flatMap({ Int($0) }),
20             let squadronId = arguments["api_squadron_id"].flatMap({ Int($0) })
21             else { return print("Argument is wrong") }
22         guard let data = json[dataKey] as? [String: Any],
23             let distance = data["api_distance"] as? Int,
24             let bauxite = data["api_after_bauxite"] as? Int,
25             let planInfos = data["api_plane_info"] as? [[String: Any]],
26             let planInfo = planInfos.first
27             else { return print("JSON is wrong") }
28         guard let cond = planInfo["api_cond"] as? Int,
29             let slotid = planInfo["api_slotid"] as? Int,
30             let state = planInfo["api_state"] as? Int,
31             let count = planInfo["api_count"] as? Int,
32             let maxCount = planInfo["api_max_count"] as? Int
33             else { return print("api_plane_info is wrong") }
34         
35         let store = ServerDataStore.oneTimeEditor()
36         guard let airbase = store.airBase(area: areaId, base: rId)
37             else { return print("AirBase is not found") }
38         let planes = airbase.planeInfo
39         guard planes.count >= squadronId,
40             let plane = planes[squadronId - 1] as? AirBasePlaneInfo
41             else { return print("AirBase is wrong") }
42         plane.cond = cond
43         plane.slotid = slotid
44         plane.state = state
45         plane.count = count
46         plane.max_count = maxCount
47         
48         airbase.distance = distance
49         
50         store.material()?.bauxite = bauxite
51     }
52 }