5 // Created by Hori,Masaki on 2017/02/25.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
12 class AirBaseMapper: JSONMapper {
13 typealias ObjectType = AirBase
15 let apiResponse: APIResponse
16 let configuration = MappingConfiguration(entity: AirBase.entity,
17 dataKeys: ["api_data", "api_air_base"],
18 primaryKeys: ["area_id", "rid"],
19 editorStore: ServerDataStore.oneTimeEditor())
21 required init(_ apiResponse: APIResponse) {
22 self.apiResponse = apiResponse
25 func handleExtraValue(_ value: JSON, forKey key: String, to airbase: AirBase) -> Bool {
26 if key != "api_plane_info" { return false }
28 if airbase.planeInfo.count == 0 {
29 if let store = configuration.editorStore as? ServerDataStore {
30 let new: [AirBasePlaneInfo] = (0..<4).flatMap {_ in
31 store.createAirBasePlaneInfo()
33 airbase.planeInfo = NSOrderedSet(array: new)
37 guard let planeInfos = value.array
39 print("value is wrong")
42 guard let infos = airbase.planeInfo.array as? [AirBasePlaneInfo]
44 print("airbase is wrong")
47 zip(infos, planeInfos).forEach { (info, dict) in
48 guard let slotid = dict["api_slotid"].int,
51 guard let cond = dict["api_cond"].int,
52 let count = dict["api_count"].int,
53 let maxCount = dict["api_max_count"].int,
54 let squadronid = dict["api_squadron_id"].int,
55 let state = dict["api_state"].int
56 else { return print("planeInfos is wrong") }
59 info.max_count = maxCount
61 info.squadron_id = squadronid
63 info.airBase = airbase