5 // Created by Hori,Masaki on 2017/02/25.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
12 final class AirBaseMapper: JSONMapper {
14 let apiResponse: APIResponse
15 let configuration = MappingConfiguration(entity: AirBase.entity,
16 dataKeys: ["api_data", "api_air_base"],
17 primaryKeys: ["area_id", "rid"],
18 editorStore: ServerDataStore.oneTimeEditor())
20 required init(_ apiResponse: APIResponse) {
22 self.apiResponse = apiResponse
25 func handleExtraValue(_ value: JSON, forKey key: String, to airbase: AirBase) -> Bool {
27 if key != "api_plane_info" { return false }
29 if airbase.planeInfo.count == 0 {
31 if let store = configuration.editorStore as? ServerDataStore {
33 let new: [AirBasePlaneInfo] = (0..<4).flatMap {_ in
35 store.createAirBasePlaneInfo()
39 airbase.planeInfo = NSOrderedSet(array: new)
43 guard let planeInfos = value.array else {
45 return Logger.shared.log("value is wrong", value: false)
48 guard let infos = airbase.planeInfo.array as? [AirBasePlaneInfo] else {
50 return Logger.shared.log("airbase is wrong", value: false)
53 zip(infos, planeInfos).forEach { (info, dict) in
55 guard let slotid = dict["api_slotid"].int, slotid != 0 else { return }
56 guard let state = dict["api_state"].int else { return }
57 guard let squadronid = dict["api_squadron_id"].int else { return }
65 info.squadron_id = squadronid
67 info.airBase = airbase
72 guard let cond = dict["api_cond"].int else {
74 return Logger.shared.log("api_cond is wrong.")
76 guard let count = dict["api_count"].int else {
78 return Logger.shared.log("api_cond is wrong.")
80 guard let maxCount = dict["api_max_count"].int else {
82 return Logger.shared.log("api_max_count is wrong")
87 info.max_count = maxCount
89 info.squadron_id = squadronid
91 info.airBase = airbase