OSDN Git Service

a1188b1b1603678fc1874429b955ae51de0f74f2
[kcd/KCD.git] / KCD / AirCorpsSupplyCommand.swift
1 //
2 //  AirCorpsSupplyCommand.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/08.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class AirCorpsSupplyCommand: JSONCommand {
12     
13     override class func canExecuteAPI(_ api: String) -> Bool {
14         
15         if api == "/kcsapi/api_req_air_corps/supply" { return true }
16         
17         return false
18     }
19     
20     override func execute() {
21         
22         let store = ServerDataStore.oneTimeEditor()
23         
24         guard let areaId = parameter["api_area_id"].int else { return }
25         guard let rId = parameter["api_base_id"].int else { return }
26         guard let squadronIdsString = parameter["api_squadron_id"].string else { return }
27         guard let airBase = store.airBase(area: areaId, base: rId) else { return }
28         
29         let planeInfos = data["api_plane_info"]
30         let planes = airBase.planeInfo
31         let squadronIds = squadronIdsString
32             .components(separatedBy: ",")
33             .flatMap { Int($0) }
34         
35         squadronIds.enumerated().forEach {
36             
37             guard planes.count >= $0.element else { return }
38             guard planeInfos.count > $0.offset else { return }
39             guard let plane = planes[$0.element - 1] as? AirBasePlaneInfo else { return }
40             
41             let planeInfo = planeInfos[$0.offset]
42             
43             if let v = planeInfo["api_cond"].int { plane.cond = v }
44             if let v = planeInfo["api_slotid"].int { plane.slotid = v }
45             if let v = planeInfo["api_state"].int { plane.state = v }
46             if let v = planeInfo["api_count"].int { plane.count = v }
47             if let v = planeInfo["api_max_count"].int { plane.max_count = v }
48         }
49         
50         if let v = data["api_distance"].int { airBase.distance = v }
51         
52         guard let material = store.material() else { return }
53         
54         if let v = data["api_after_bauxite"].int { material.bauxite = v }
55         if let v = data["api_after_fuel"].int { material.fuel = v }
56     }
57 }