OSDN Git Service

第三部隊の7隻目にアクセスすると落ちる問題を修正
[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 final class SetPlaneCommand: JSONCommand {
12     
13     override class func canExecuteAPI(_ api: String) -> Bool {
14         
15         if api == "/kcsapi/api_req_air_corps/set_plane" { return true }
16         
17         return false
18     }
19     
20     override func execute() {
21         
22         guard let areaId = parameter["api_area_id"].int,
23             let rId = parameter["api_base_id"].int,
24             let squadronId = parameter["api_squadron_id"].int else {
25                 
26                 return Logger.shared.log("SetPlaneCommand: Argument is wrong")
27         }
28         guard let distance = data["api_distance"].int else {
29             
30             return Logger.shared.log("SetPlaneCommand: JSON is wrong")
31         }
32         
33         let planInfo = data["api_plane_info"][0]
34         
35         guard let slotid = planInfo["api_slotid"].int,
36             let state = planInfo["api_state"].int else {
37                 
38                 return Logger.shared.log("api_plane_info is wrong")
39         }
40         
41         let store = ServerDataStore.oneTimeEditor()
42         
43         guard let airbase = store.airBase(area: areaId, base: rId) else {
44             
45             return Logger.shared.log("AirBase is not found")
46         }
47         
48         let planes = airbase.planeInfo
49         
50         guard planes.count >= squadronId,
51             let plane = planes[squadronId - 1] as? AirBasePlaneInfo else {
52                 
53                 return Logger.shared.log("AirBase is wrong")
54         }
55         
56         // TODO: state が 2 の時のみ cond, count, max_count がnilであることを許すようにする
57         plane.cond = planInfo["api_cond"].int ?? 0
58         plane.slotid = slotid
59         plane.state = state
60         plane.count = planInfo["api_count"].int ?? 0
61         plane.max_count = planInfo["api_max_count"].int ?? 0
62         
63         airbase.distance = distance
64         
65         data["api_after_bauxite"].int.map { store.material()?.bauxite = $0 }
66     }
67 }