OSDN Git Service

09a163cdfc099547a673760dee889a3ad922682c
[kcd/KCD.git] / KCD / LocalDataStoreAccessor.swift
1 //
2 //  LocalDataStoreAccessor.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/10/25.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 extension LocalDataStore {
10     
11     func unmarkedDropShipHistories(befor days: Int) -> [DropShipHistory] {
12         
13         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
14         let predicate = NSPredicate
15             .and([
16                 NSPredicate(#keyPath(DropShipHistory.date), lessThan: date),
17                 NSPredicate(#keyPath(DropShipHistory.mark), equal: 0)
18                     .or(.isNil(#keyPath(DropShipHistory.mark))),
19                 NSPredicate(#keyPath(DropShipHistory.mapArea), valuesIn: ["1", "2", "3", "4", "5", "6", "7", "8", "9"])
20                 ])
21         
22         guard let dropHistories = try? objects(of: DropShipHistory.entity, predicate: predicate) else { return [] }
23         
24         return dropHistories
25     }
26     
27     func createDropShipHistory() -> DropShipHistory? {
28         
29         return insertNewObject(for: DropShipHistory.entity)
30     }
31     
32     func kaihatuHistories() -> [KaihatuHistory] {
33         
34         guard let kaihatuHistories = try? objects(of: KaihatuHistory.entity) else { return [] }
35         
36         return kaihatuHistories
37     }
38     
39     func unmarkedKaihatuHistories(befor days: Int) -> [KaihatuHistory] {
40         
41         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
42         let predicate = NSPredicate
43             .and([
44                 NSPredicate(#keyPath(KaihatuHistory.date), lessThan: date),
45                 NSPredicate(#keyPath(KaihatuHistory.mark), equal: 0)
46                     .or(.isNil(#keyPath(KaihatuHistory.mark)))
47                 ])
48         
49         guard let kaihatuHistories = try? objects(of: KaihatuHistory.entity, predicate: predicate) else { return [] }
50         
51         return kaihatuHistories
52     }
53     
54     func createKaihatuHistory() -> KaihatuHistory? {
55         
56         return insertNewObject(for: KaihatuHistory.entity)
57     }
58     
59     func kenzoMark(byDockId dockId: Int) -> KenzoMark? {
60         
61         let predicate = NSPredicate(#keyPath(KenzoMark.kDockId), equal: dockId)
62         
63         guard let kenzoMarks = try? objects(of: KenzoMark.entity, predicate: predicate) else { return nil }
64         
65         return kenzoMarks.first
66     }
67     
68     // swiftlint:disable function_parameter_count
69     func kenzoMark(fuel: Int, bull: Int, steel: Int, bauxite: Int, kaihatusizai: Int, kDockId: Int, shipId: Int) -> KenzoMark? {
70         
71         let predicate = NSPredicate.empty
72             .and(NSPredicate(#keyPath(KenzoMark.fuel), equal: fuel))
73             .and(NSPredicate(#keyPath(KenzoMark.bull), equal: bull))
74             .and(NSPredicate(#keyPath(KenzoMark.steel), equal: steel))
75             .and(NSPredicate(#keyPath(KenzoMark.bauxite), equal: bauxite))
76             .and(NSPredicate(#keyPath(KenzoMark.kaihatusizai), equal: kaihatusizai))
77             .and(NSPredicate(#keyPath(KenzoMark.kDockId), equal: kDockId))
78             .and(NSPredicate(#keyPath(KenzoMark.created_ship_id), equal: shipId))
79         
80         guard let kenzoMarks = try? objects(of: KenzoMark.entity, predicate: predicate) else { return nil }
81         
82         return kenzoMarks.first
83     }
84     
85     func createKenzoMark() -> KenzoMark? {
86         
87         return insertNewObject(for: KenzoMark.entity)
88     }
89     
90     func unmarkedKenzoHistories(befor days: Int) -> [KenzoHistory] {
91         
92         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
93         let predicate = NSPredicate
94             .and([
95                 NSPredicate(#keyPath(KenzoHistory.date), lessThan: date),
96                 NSPredicate(#keyPath(KenzoHistory.mark), equal: 0)
97                     .or(.isNil(#keyPath(KenzoHistory.mark)))
98                 ])
99         
100         guard let kenzoHistories = try? objects(of: KenzoHistory.entity, predicate: predicate) else { return [] }
101         
102         return kenzoHistories
103     }
104     
105     func createKenzoHistory() -> KenzoHistory? {
106         
107         return insertNewObject(for: KenzoHistory.entity)
108     }
109     
110     func hiddenDropShipHistories() -> [HiddenDropShipHistory] {
111         
112         guard let dropShipHistories = try? objects(of: HiddenDropShipHistory.entity) else { return [] }
113         
114         return dropShipHistories
115     }
116     
117     func createHiddenDropShipHistory() -> HiddenDropShipHistory? {
118         
119         return insertNewObject(for: HiddenDropShipHistory.entity)
120     }
121 }