OSDN Git Service

UAをVersion/10.0.3 Safari/602.4.8に変更
[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 extension CoreDataConfiguration {
12     static let local = CoreDataConfiguration("LocalData")
13 }
14 extension CoreDataCore {
15     static let local = CoreDataCore(.local)
16 }
17
18
19 class LocalDataStore: CoreDataAccessor, CoreDataManager {
20     static var `default` = LocalDataStore(type: .reader)
21     class func oneTimeEditor() -> LocalDataStore {
22         return LocalDataStore(type: .editor)
23     }
24     
25     required init(type: CoreDataManagerType) {
26         context = (type == .reader ? core.parentContext : core.editorContext())
27     }
28     deinit {
29         save()
30     }
31     
32     let core = CoreDataCore.local
33     let context: NSManagedObjectContext
34 }
35
36 extension LocalDataStore {
37     func unmarkedDropShipHistories(befor days: Int) -> [DropShipHistory] {
38         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
39         let predicate01 = NSPredicate(format: "date < %@", date as NSDate)
40         let predicate02 = NSPredicate(format: "mark = 0 || mark = nil")
41         let predicate03 = NSPredicate(format: "mapArea IN %@", ["1", "2", "3", "4", "5", "6", "7", "8", "9"])
42         let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate01, predicate02, predicate03])
43         guard let dropHistories = try? objects(with: DropShipHistory.entity, predicate: predicate)
44             else { return [] }
45         return dropHistories
46     }
47     
48     func createDropShipHistory() -> DropShipHistory? {
49         return insertNewObject(for: DropShipHistory.entity)
50     }
51     
52     func kaihatuHistories() -> [KaihatuHistory] {
53         guard let kaihatuHistories = try? objects(with: KaihatuHistory.entity)
54             else { return [] }
55         return kaihatuHistories
56     }
57     func unmarkedKaihatuHistories(befor days: Int) -> [KaihatuHistory] {
58         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
59         let predicate01 = NSPredicate(format: "date < %@", date as NSDate)
60         let predicate02 = NSPredicate(format: "mark = 0 || mark = nil")
61         let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate01, predicate02])
62         guard let kaihatuHistories = try? objects(with: KaihatuHistory.entity, predicate: predicate)
63             else { return [] }
64         return kaihatuHistories
65     }
66     func createKaihatuHistory() -> KaihatuHistory? {
67         return insertNewObject(for: KaihatuHistory.entity)
68     }
69     
70     func kenzoMark(byDockId dockId: Int) -> KenzoMark? {
71         let predicate = NSPredicate(format: "kDockId = %ld", dockId)
72         guard let kenzoMarks = try? objects(with: KenzoMark.entity, predicate: predicate)
73             else { return nil }
74         return kenzoMarks.first
75     }
76     // swiftlint:disable function_parameter_count
77     func kenzoMark(fuel: Int,
78                    bull: Int,
79                    steel: Int,
80                    bauxite: Int,
81                    kaihatusizai: Int,
82                    kDockId: Int,
83                    shipId: Int) -> KenzoMark? {
84         // swiftlint:disable:next line_length
85         let predicate = NSPredicate(format: "fuel = %ld AND bull = %ld AND steel = %ld AND bauxite = %ld AND kaihatusizai = %ld AND kDockId = %ld AND created_ship_id = %ld",
86                                     fuel, bull, steel, bauxite, kaihatusizai, kDockId, shipId)
87         guard let kenzoMarks = try? objects(with: KenzoMark.entity, predicate: predicate)
88             else { return nil }
89         return kenzoMarks.first
90     }
91     func createKenzoMark() -> KenzoMark? {
92         return insertNewObject(for: KenzoMark.entity)
93     }
94     
95     func unmarkedKenzoHistories(befor days: Int) -> [KenzoHistory] {
96         let date = Date(timeIntervalSinceNow: TimeInterval(-1 * days * 24 * 60 * 60))
97         let predicate01 = NSPredicate(format: "date < %@", date as NSDate)
98         let predicate02 = NSPredicate(format: "mark = 0 || mark = nil")
99         let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate01, predicate02])
100         guard let kenzoHistories = try? objects(with: KenzoHistory.entity, predicate: predicate)
101             else { return [] }
102         return kenzoHistories
103     }
104     func createKenzoHistory() -> KenzoHistory? {
105         return insertNewObject(for: KenzoHistory.entity)
106     }
107     
108     func hiddenDropShipHistories() -> [HiddenDropShipHistory] {
109         guard let dropShipHistories = try? objects(with: HiddenDropShipHistory.entity)
110             else { return [] }
111         return dropShipHistories
112     }
113     func createHiddenDropShipHistory() -> HiddenDropShipHistory? {
114         return insertNewObject(for: HiddenDropShipHistory.entity)
115     }
116 }