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" {
32 if airbase.planeInfo.count == 0 {
34 if let store = configuration.editorStore as? ServerDataStore {
36 let new: [AirBasePlaneInfo] = (0..<4).compactMap { _ in
38 store.createAirBasePlaneInfo()
42 airbase.planeInfo = NSOrderedSet(array: new)
46 guard let planeInfos = value.array else {
48 Logger.shared.log("value is wrong")
53 guard let infos = airbase.planeInfo.array as? [AirBasePlaneInfo] else {
55 Logger.shared.log("airbase is wrong")
60 zip(infos, planeInfos).forEach { (info, dict) in
62 guard let slotid = dict["api_slotid"].int, slotid != 0 else {
66 guard let state = dict["api_state"].int else {
70 guard let squadronid = dict["api_squadron_id"].int else {
81 info.squadron_id = squadronid
83 info.airBase = airbase
88 guard let cond = dict["api_cond"].int else {
90 Logger.shared.log("api_cond is wrong.")
94 guard let count = dict["api_count"].int else {
96 Logger.shared.log("api_cond is wrong.")
100 guard let maxCount = dict["api_max_count"].int else {
102 Logger.shared.log("api_max_count is wrong")
109 info.max_count = maxCount
111 info.squadron_id = squadronid
113 info.airBase = airbase