OSDN Git Service

Doutaku を 1.0 にアップデート
[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.self,
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" {
28             
29             return false
30         }
31         
32         if airbase.planeInfo.count == 0 {
33             
34             if let store = configuration.editorStore as? ServerDataStore {
35                 
36                 let new: [AirBasePlaneInfo] = (0..<4).compactMap { _ in
37                     
38                     store.createAirBasePlaneInfo()
39                     
40                 }
41                 
42                 airbase.planeInfo = NSOrderedSet(array: new)
43             }
44         }
45         
46         guard let planeInfos = value.array else {
47             
48             Logger.shared.log("value is wrong")
49             
50             return false
51         }
52         
53         guard let infos = airbase.planeInfo.array as? [AirBasePlaneInfo] else {
54             
55             Logger.shared.log("airbase is wrong")
56             
57             return false
58         }
59         
60         zip(infos, planeInfos).forEach { (info, dict) in
61             
62             guard let slotid = dict["api_slotid"].int, slotid != 0 else {
63                 
64                 return
65             }
66             guard let state = dict["api_state"].int else {
67                 
68                 return
69             }
70             guard let squadronid = dict["api_squadron_id"].int else {
71                 
72                 return
73             }
74             
75             if state == 2 {
76                 
77                 info.cond = 0
78                 info.count = 0
79                 info.max_count = 0
80                 info.slotid = slotid
81                 info.squadron_id = squadronid
82                 info.state = state
83                 info.airBase = airbase
84                 
85                 return
86             }
87
88             guard let cond = dict["api_cond"].int else {
89                 
90                 Logger.shared.log("api_cond is wrong.")
91                 
92                 return
93             }
94             guard let count = dict["api_count"].int else {
95                 
96                 Logger.shared.log("api_cond is wrong.")
97                 
98                 return
99             }
100             guard let maxCount = dict["api_max_count"].int else {
101                 
102                 Logger.shared.log("api_max_count is wrong")
103                 
104                 return
105             }
106             
107             info.cond = cond
108             info.count = count
109             info.max_count = maxCount
110             info.slotid = slotid
111             info.squadron_id = squadronid
112             info.state = state
113             info.airBase = airbase
114         }
115         
116         return true
117     }
118 }