OSDN Git Service

2708e51690c5527fdca70a9ef9525c29012960db
[kcd/KCD.git] / KCD / LocalDataStore.swift
1 //
2 //  LocalDataStore.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/06.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class LocalDataStore: CoreDataManager {
12     
13     static let core = CoreDataCore(CoreDataConfiguration("LocalData"))
14     
15     static let `default` = LocalDataStore(type: .reader)
16     
17     class func oneTimeEditor() -> LocalDataStore {
18         
19         return LocalDataStore(type: .editor)
20     }
21     
22     required init(type: CoreDataManagerType) {
23         
24         context = LocalDataStore.context(for: type)
25     }
26     
27     deinit {
28         
29         save()
30     }
31     
32     let context: NSManagedObjectContext
33 }
34
35 extension LocalDataStore {
36     
37     func unmarkedDropShipHistories(befor days: Int) -> [DropShipHistory] {
38         
39         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
40         let predicate = NSPredicate
41             .and([
42                 NSPredicate(#keyPath(DropShipHistory.date), lessThan: date),
43                 NSPredicate(#keyPath(DropShipHistory.mark), equal: 0)
44                     .or(.isNil(#keyPath(DropShipHistory.mark))),
45                 NSPredicate(#keyPath(DropShipHistory.mapArea), valuesIn: ["1", "2", "3", "4", "5", "6", "7", "8", "9"])
46                 ])
47         
48         guard let dropHistories = try? objects(of: DropShipHistory.entity, predicate: predicate) else { return [] }
49         
50         return dropHistories
51     }
52     
53     func createDropShipHistory() -> DropShipHistory? {
54         
55         return insertNewObject(for: DropShipHistory.entity)
56     }
57     
58     func kaihatuHistories() -> [KaihatuHistory] {
59         
60         guard let kaihatuHistories = try? objects(of: KaihatuHistory.entity) else { return [] }
61         
62         return kaihatuHistories
63     }
64     
65     func unmarkedKaihatuHistories(befor days: Int) -> [KaihatuHistory] {
66         
67         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
68         let predicate = NSPredicate
69             .and([
70                 NSPredicate(#keyPath(KaihatuHistory.date), lessThan: date),
71                 NSPredicate(#keyPath(KaihatuHistory.mark), equal: 0)
72                     .or(.isNil(#keyPath(KaihatuHistory.mark)))
73                 ])
74         
75         guard let kaihatuHistories = try? objects(of: KaihatuHistory.entity, predicate: predicate) else { return [] }
76         
77         return kaihatuHistories
78     }
79     
80     func createKaihatuHistory() -> KaihatuHistory? {
81         
82         return insertNewObject(for: KaihatuHistory.entity)
83     }
84     
85     func kenzoMark(byDockId dockId: Int) -> KenzoMark? {
86         
87         let predicate = NSPredicate(#keyPath(KenzoMark.kDockId), equal: dockId)
88         
89         guard let kenzoMarks = try? objects(of: KenzoMark.entity, predicate: predicate) else { return nil }
90         
91         return kenzoMarks.first
92     }
93     
94     // swiftlint:disable function_parameter_count
95     func kenzoMark(fuel: Int, bull: Int, steel: Int, bauxite: Int, kaihatusizai: Int, kDockId: Int, shipId: Int) -> KenzoMark? {
96         
97         let predicate = NSPredicate.empty
98             .and(NSPredicate(#keyPath(KenzoMark.fuel), equal: fuel))
99             .and(NSPredicate(#keyPath(KenzoMark.bull), equal: bull))
100             .and(NSPredicate(#keyPath(KenzoMark.steel), equal: steel))
101             .and(NSPredicate(#keyPath(KenzoMark.bauxite), equal: bauxite))
102             .and(NSPredicate(#keyPath(KenzoMark.kaihatusizai), equal: kaihatusizai))
103             .and(NSPredicate(#keyPath(KenzoMark.kDockId), equal: kDockId))
104             .and(NSPredicate(#keyPath(KenzoMark.created_ship_id), equal: shipId))
105         
106         guard let kenzoMarks = try? objects(of: KenzoMark.entity, predicate: predicate) else { return nil }
107         
108         return kenzoMarks.first
109     }
110     
111     func createKenzoMark() -> KenzoMark? {
112         
113         return insertNewObject(for: KenzoMark.entity)
114     }
115     
116     func unmarkedKenzoHistories(befor days: Int) -> [KenzoHistory] {
117         
118         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
119         let predicate = NSPredicate
120             .and([
121                 NSPredicate(#keyPath(KenzoHistory.date), lessThan: date),
122                 NSPredicate(#keyPath(KenzoHistory.mark), equal: 0)
123                     .or(.isNil(#keyPath(KenzoHistory.mark)))
124                 ])
125         
126         guard let kenzoHistories = try? objects(of: KenzoHistory.entity, predicate: predicate) else { return [] }
127         
128         return kenzoHistories
129     }
130     
131     func createKenzoHistory() -> KenzoHistory? {
132         
133         return insertNewObject(for: KenzoHistory.entity)
134     }
135     
136     func hiddenDropShipHistories() -> [HiddenDropShipHistory] {
137         
138         guard let dropShipHistories = try? objects(of: HiddenDropShipHistory.entity) else { return [] }
139         
140         return dropShipHistories
141     }
142     
143     func createHiddenDropShipHistory() -> HiddenDropShipHistory? {
144         
145         return insertNewObject(for: HiddenDropShipHistory.entity)
146     }
147 }