OSDN Git Service

関数名を変更
[kcd/KCD.git] / KCD / AirBaseMapper.swift
1 //
2 //  AirBaseMapper.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/02/25.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10 import SwiftyJSON
11
12 final class AirBaseMapper: JSONMapper {
13         
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())
19     
20     required init(_ apiResponse: APIResponse) {
21         
22         self.apiResponse = apiResponse
23     }
24
25     func handleExtraValue(_ value: JSON, forKey key: String, to airbase: AirBase) -> Bool {
26         
27         if key != "api_plane_info" { return false }
28         
29         if airbase.planeInfo.count == 0 {
30             
31             if let store = configuration.editorStore as? ServerDataStore {
32                 
33                 let new: [AirBasePlaneInfo] = (0..<4).flatMap {_ in
34                     
35                     store.createAirBasePlaneInfo()
36                     
37                 }
38                 
39                 airbase.planeInfo = NSOrderedSet(array: new)
40             }
41         }
42         
43         guard let planeInfos = value.array else {
44             
45             return Logger.shared.log("value is wrong", value: false)
46         }
47         
48         guard let infos = airbase.planeInfo.array as? [AirBasePlaneInfo] else {
49             
50             return Logger.shared.log("airbase is wrong", value: false)
51         }
52         
53         zip(infos, planeInfos).forEach { (info, dict) in
54             
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 }
58             
59             if state == 2 {
60                 
61                 info.cond = 0
62                 info.count = 0
63                 info.max_count = 0
64                 info.slotid = slotid
65                 info.squadron_id = squadronid
66                 info.state = state
67                 info.airBase = airbase
68                 
69                 return
70             }
71
72             guard let cond = dict["api_cond"].int else {
73                 
74                 return Logger.shared.log("api_cond is wrong.")
75             }
76             guard let count = dict["api_count"].int else {
77                 
78                 return Logger.shared.log("api_cond is wrong.")
79             }
80             guard let maxCount = dict["api_max_count"].int else {
81                 
82                 return Logger.shared.log("api_max_count is wrong")
83             }
84             
85             info.cond = cond
86             info.count = count
87             info.max_count = maxCount
88             info.slotid = slotid
89             info.squadron_id = squadronid
90             info.state = state
91             info.airBase = airbase
92         }
93         
94         return true
95     }
96 }