OSDN Git Service

guard の書き方を統一した
[kcd/KCD.git] / KCD / MaterialMapper.swift
1 //
2 //  MaterialMapper.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/02/24.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10 import SwiftyJSON
11
12 final class MaterialMapper: JSONMapper {
13     
14     typealias ObjectType = Material
15     
16     let apiResponse: APIResponse
17     let configuration: MappingConfiguration<Material>
18     
19     private let keys = [
20         "fuel", "bull", "steel", "bauxite",
21         "kousokukenzo", "kousokushuhuku", "kaihatusizai", "screw"
22     ]
23     
24     required init(_ apiResponse: APIResponse) {
25         
26         self.apiResponse = apiResponse
27         self.configuration = MappingConfiguration(entity: Material.entity,
28                                                   dataKeys: MaterialMapper.dataKeys(apiResponse),
29                                                   editorStore: ServerDataStore.oneTimeEditor())
30     }
31     
32     
33     private enum MaterialAPI: String {
34         
35         case port = "/kcsapi/api_port/port"
36         case kousyouCreateItem = "/kcsapi/api_req_kousyou/createitem"
37         case kousyouDestoroyShip = "/kcsapi/api_req_kousyou/destroyship"
38         case kousyouRemodelSlot = "/kcsapi/api_req_kousyou/remodel_slot"
39         case hokyuCharge = "/kcsapi/api_req_hokyu/charge"
40     }
41     
42     private class func dataKeys(_ apiResponse: APIResponse) -> [String] {
43         
44         guard let materialApi = MaterialAPI(rawValue: apiResponse.api) else { return ["api_data"] }
45         
46         switch materialApi {
47         case .port: return ["api_data", "api_material"]
48             
49         case .kousyouCreateItem: return ["api_data", "api_material"]
50             
51         case .kousyouDestoroyShip: return ["api_data", "api_material"]
52             
53         case .kousyouRemodelSlot: return ["api_data", "api_after_material"]
54             
55         case .hokyuCharge: return ["api_data", "api_material"]
56         }
57     }
58     
59     func commit() {
60         
61         guard let store = configuration.editorStore as? ServerDataStore,
62             let material = store.material() ?? store.createMaterial() else {
63                 
64                 print("Can not create Material")
65                 return
66         }
67         
68         if let _ = data[0].int {
69             
70             let array = data.arrayValue.flatMap { $0.int }
71             register(material, data: array)
72             
73         } else if let _ = data[0].dictionary {
74             
75             register(material, data: data.arrayValue)
76             
77         } else {
78             
79             print("JSON is unknown type")
80             
81         }
82     }
83     
84     private func register(_ material: Material, data: [Int]) {
85         
86         data.enumerated().forEach {
87             
88             guard $0.offset < keys.count else { return }
89             
90             material.setValue($0.element as NSNumber, forKey: keys[$0.offset])
91         }
92     }
93     
94     private func register(_ material: Material, data: [JSON]) {
95         
96         data.forEach {
97             
98             guard let i = $0["api_id"].int, case 1..<keys.count = i else { return }
99             guard let newValue = $0["api_value"].int else { return }
100             
101             material.setValue(newValue as NSNumber, forKey: keys[i - 1])
102         }
103     }
104 }