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 final class SetPlaneCommand: JSONCommand {
12     
13     override class func canExecuteAPI(_ api: API) -> Bool {
14         
15         return api.endpoint == .setPlane
16     }
17     
18     override func execute() {
19         
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 {
23                 
24                 Logger.shared.log("SetPlaneCommand: Argument is wrong")
25                 
26                 return
27         }
28         guard let distance = data["api_distance"].int else {
29             
30             Logger.shared.log("SetPlaneCommand: JSON is wrong")
31             
32             return
33         }
34         
35         let planInfo = data["api_plane_info"][0]
36         
37         guard let slotid = planInfo["api_slotid"].int,
38             let state = planInfo["api_state"].int else {
39                 
40                 Logger.shared.log("api_plane_info is wrong")
41                 
42                 return
43         }
44         
45         let store = ServerDataStore.oneTimeEditor()
46         store.sync {
47             
48             guard let airbase = store.airBase(area: areaId, base: rId) else {
49                 
50                 Logger.shared.log("AirBase is not found")
51                 
52                 return
53             }
54             
55             let planes = airbase.planeInfo
56             
57             guard planes.count >= squadronId,
58                 let plane = planes[squadronId - 1] as? AirBasePlaneInfo else {
59                     
60                     Logger.shared.log("AirBase is wrong")
61                     
62                     return
63             }
64             
65             // TODO: state が 2 の時のみ cond, count, max_count がnilであることを許すようにする
66             plane.cond = planInfo["api_cond"].int ?? 0
67             plane.slotid = slotid
68             plane.state = state
69             plane.count = planInfo["api_count"].int ?? 0
70             plane.max_count = planInfo["api_max_count"].int ?? 0
71             
72             airbase.distance = distance
73             
74             self.data["api_after_bauxite"].int.map { store.material()?.bauxite = $0 }
75         }
76     }
77 }