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 class MasterShipMapper: JSONMapper {
13     typealias ObjectType = MasterShip
14     
15     let apiResponse: APIResponse
16     let configuration = MappingConfiguration(entity: MasterShip.entity,
17                                              dataKeys: ["api_data", "api_mst_ship"],
18                                              editorStore: ServerDataStore.oneTimeEditor())
19     
20     required init(_ apiResponse: APIResponse) {
21         self.apiResponse = apiResponse
22     }
23     
24     private lazy var masterSTypes: [MasterSType] = {
25         return ServerDataStore.default.sortedMasterSTypesById()
26     }()
27     
28     func handleExtraValue(_ value: JSON, forKey key: String, to masterShip: MasterShip) -> Bool {
29         if key != "api_stype" { return false }
30         
31         guard let sType = value.int
32             else {
33                 print("MasterShipMapper: value is not Int")
34                 return false
35         }
36         setStype(sType, to: masterShip)
37         return true
38     }
39     
40     private func setStype(_ stypeID: Int, to masterShip: MasterShip) {
41         if masterShip.stype.id == stypeID { return }
42         guard let stype = masterSTypes.binarySearch(comparator: { $0.id ==? stypeID })
43             else { return print("MasterShipMapper: Can not find MasterSType") }
44         guard let masterSType = configuration.editorStore.object(with: stype.objectID) as? MasterSType
45             else { return print("MasterShipMapper: Can not convert to current moc object masterSType") }
46         masterShip.stype = masterSType
47     }
48 }