OSDN Git Service

洋上補給の補強増設用のショートネームをつけた
[kcd/KCD.git] / KCD / MasterShipMapper.swift
1 //
2 //  MasterShipMapper.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/02/22.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10 import SwiftyJSON
11
12 final class MasterShipMapper: JSONMapper {
13         
14     let apiResponse: APIResponse
15     let configuration = MappingConfiguration(entity: MasterShip.entity,
16                                              dataKeys: ["api_data", "api_mst_ship"],
17                                              editorStore: ServerDataStore.oneTimeEditor())
18     
19     required init(_ apiResponse: APIResponse) {
20         
21         self.apiResponse = apiResponse
22     }
23     
24     private lazy var masterSTypes: [MasterSType] = {
25         
26         return ServerDataStore.default.sortedMasterSTypesById()
27     }()
28     
29     func handleExtraValue(_ value: JSON, forKey key: String, to masterShip: MasterShip) -> Bool {
30         
31         if key != "api_stype" { return false }
32         
33         guard let sType = value.int else {
34             
35             return Logger.shared.log("MasterShipMapper: value is not Int", value: false)
36         }
37         
38         setStype(sType, to: masterShip)
39         
40         return true
41     }
42     
43     private func setStype(_ stypeID: Int, to masterShip: MasterShip) {
44         
45         if masterShip.stype.id == stypeID { return }
46         
47         guard let stype = masterSTypes.binarySearch(comparator: { $0.id ==? stypeID }) else {
48             
49             return Logger.shared.log("MasterShipMapper: Can not find MasterSType")
50         }
51         
52         guard let masterSType = configuration.editorStore.object(of: MasterSType.entity, with: stype.objectID) else {
53             
54             return Logger.shared.log("MasterShipMapper: Can not convert to current moc object masterSType")
55         }
56         
57         masterShip.stype = masterSType
58     }
59 }