OSDN Git Service

洋上補給の補強増設用のショートネームをつけた
[kcd/KCD.git] / KCD / SlotItemMapper.swift
1 //
2 //  SlotItemMapper.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 SlotItemMapper: JSONMapper {
13         
14     let apiResponse: APIResponse
15     let configuration: MappingConfiguration<SlotItem>
16     
17     required init(_ apiResponse: APIResponse) {
18         
19         self.apiResponse = apiResponse
20         self.configuration = MappingConfiguration(entity: SlotItem.entity,
21                                                   dataKeys: SlotItemMapper.dataKeys(apiResponse),
22                                                   editorStore: ServerDataStore.oneTimeEditor())
23     }
24     
25     private class func dataKeys(_ apiResponse: APIResponse) -> [String] {
26         
27         switch apiResponse.api.endpoint {
28             
29         case .getShip: return ["api_data", "api_slotitem"]
30             
31         case .requireInfo: return ["api_data", "api_slot_item"]
32             
33         case .slotItem: return ["api_data"]
34             
35         default: return Logger.shared.log("Missing API: \(apiResponse.api)", value: ["api_data"])
36         }
37     }
38     
39     private var registerIds: [Int] = []
40     private lazy var masterSlotItems: [MasterSlotItem] = {
41         return ServerDataStore.default.sortedMasterSlotItemsById()
42     }()
43     
44     func beginRegister(_ slotItem: SlotItem) {
45         
46         slotItem.alv = 0
47     }
48     
49     func handleExtraValue(_ value: JSON, forKey key: String, to object: SlotItem) -> Bool {
50         
51         // 取得後破棄した装備のデータを削除するため保有IDを保存
52         if key == "api_id" {
53             
54             guard let id = value.int else { return false }
55             
56             registerIds.append(id)
57             
58             return false
59         }
60         
61         if key == "api_slotitem_id" {
62             
63             guard let id = value.int else { return false }
64             
65             setMaster(id, to: object)
66             
67             return true
68         }
69         
70         return false
71     }
72     
73     func finishOperating() {
74         
75         // getshipの時は取得した艦娘の装備のみのデータのため無視
76         if apiResponse.api.endpoint == .getShip {
77             
78             return
79         }
80         
81         guard let store = configuration.editorStore as? ServerDataStore else { return }
82         
83         store.slotItems(exclude: registerIds).forEach(store.delete)
84     }
85     
86     private func setMaster(_ masterId: Int, to slotItem: SlotItem?) {
87         
88         guard let slotItem = slotItem else { return }
89         
90         if slotItem.slotitem_id == masterId { return }
91         
92         guard let mSlotItem = masterSlotItems.binarySearch(comparator: { $0.id ==? masterId }) else {
93             
94             return Logger.shared.log("Can not find MasterSlotItem")
95         }
96         
97         guard let masterSlotItem = configuration.editorStore.object(of: MasterSlotItem.entity, with: mSlotItem.objectID) else {
98                 
99                 return Logger.shared.log("Can not convert to current moc object")
100         }
101         
102         slotItem.master_slotItem = masterSlotItem
103         slotItem.slotitem_id = masterId
104     }
105 }