OSDN Git Service

Doutakuを導入
[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                 return Logger.shared.log("SetPlaneCommand: Argument is wrong")
25         }
26         guard let distance = data["api_distance"].int else {
27             
28             return Logger.shared.log("SetPlaneCommand: JSON is wrong")
29         }
30         
31         let planInfo = data["api_plane_info"][0]
32         
33         guard let slotid = planInfo["api_slotid"].int,
34             let state = planInfo["api_state"].int else {
35                 
36                 return Logger.shared.log("api_plane_info is wrong")
37         }
38         
39         let store = ServerDataStore.oneTimeEditor()
40         store.sync {
41             
42             guard let airbase = store.airBase(area: areaId, base: rId) else {
43                 
44                 return Logger.shared.log("AirBase is not found")
45             }
46             
47             let planes = airbase.planeInfo
48             
49             guard planes.count >= squadronId,
50                 let plane = planes[squadronId - 1] as? AirBasePlaneInfo else {
51                     
52                     return Logger.shared.log("AirBase is wrong")
53             }
54             
55             // TODO: state が 2 の時のみ cond, count, max_count がnilであることを許すようにする
56             plane.cond = planInfo["api_cond"].int ?? 0
57             plane.slotid = slotid
58             plane.state = state
59             plane.count = planInfo["api_count"].int ?? 0
60             plane.max_count = planInfo["api_max_count"].int ?? 0
61             
62             airbase.distance = distance
63             
64             self.data["api_after_bauxite"].int.map { store.material()?.bauxite = $0 }
65         }
66     }
67 }