OSDN Git Service

ObjectTypeを明示せず推論させるようにした
[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     
26     private enum SlotItemAPI: String {
27         
28         case getMemberSlotItem = "/kcsapi/api_get_member/slot_item"
29         case kousyouGetShip = "/kcsapi/api_req_kousyou/getship"
30         case getMemberRequireInfo = "/kcsapi/api_get_member/require_info"
31     }
32     
33     private class func dataKeys(_ apiResponse: APIResponse) -> [String] {
34         
35         guard let slotItemApi = SlotItemAPI(rawValue: apiResponse.api) else { return ["api_data"] }
36         
37         switch slotItemApi {
38         case .kousyouGetShip: return ["api_data", "api_slotitem"]
39             
40         case .getMemberRequireInfo: return ["api_data", "api_slot_item"]
41             
42         case .getMemberSlotItem: return ["api_data"]
43         }
44     }
45     
46     private var registerIds: [Int] = []
47     private lazy var masterSlotItems: [MasterSlotItem] = {
48         return ServerDataStore.default.sortedMasterSlotItemsById()
49     }()
50     
51     func beginRegister(_ slotItem: SlotItem) {
52         
53         slotItem.alv = 0
54     }
55     
56     func handleExtraValue(_ value: JSON, forKey key: String, to object: SlotItem) -> Bool {
57         
58         // 取得後破棄した装備のデータを削除するため保有IDを保存
59         if key == "api_id" {
60             
61             guard let id = value.int else { return false }
62             
63             registerIds.append(id)
64             
65             return false
66         }
67         
68         if key == "api_slotitem_id" {
69             
70             guard let id = value.int else { return false }
71             
72             setMaster(id, to: object)
73             
74             return true
75         }
76         
77         return false
78     }
79     
80     func finishOperating() {
81         
82         // getshipの時は取得した艦娘の装備のみのデータのため無視
83         if let slotItemApi = SlotItemAPI(rawValue: apiResponse.api),
84             slotItemApi == .kousyouGetShip {
85             
86             return
87             
88         }
89         
90         guard let store = configuration.editorStore as? ServerDataStore else { return }
91         
92         store.slotItems(exclude: registerIds).forEach { store.delete($0) }
93     }
94     
95     private func setMaster(_ masterId: Int, to slotItem: SlotItem?) {
96         
97         guard let slotItem = slotItem else { return }
98         
99         if slotItem.slotitem_id == masterId { return }
100         
101         guard let mSlotItem = masterSlotItems.binarySearch(comparator: { $0.id ==? masterId }) else {
102             
103             print("Can not find MasterSlotItem")
104             return
105         }
106         
107         guard let moc = slotItem.managedObjectContext,
108             let masterSlotItem = moc.object(with: mSlotItem.objectID) as? MasterSlotItem else {
109                 
110                 print("Can not convert to current moc object")
111                 return
112         }
113         
114         slotItem.master_slotItem = masterSlotItem
115         slotItem.slotitem_id = masterId
116     }
117 }