2 // ResourceHistoryManager.swift
5 // Created by Hori,Masaki on 2017/01/22.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 private extension Selector {
13 static let notifyIfNeeded = #selector(ResourceHistoryManager.notifyIfNeeded(_:))
16 final class ResourceHistoryManager: NSObject {
18 private let periodicNotification: PeriodicNotifier
22 periodicNotification = PeriodicNotifier(hour: 23, minutes: 3)
27 NotificationCenter.default
28 .addObserver(forName: .Periodic,
29 object: periodicNotification,
34 private var timer: Timer?
36 @objc fileprivate func notifyIfNeeded(_ timer: Timer?) {
38 if timer != nil { saveResources() }
39 if let valid = timer?.isValid, valid { timer?.invalidate() }
42 var nowComp = Calendar.current
43 .dateComponents([.year, .month, .day, .hour, .minute], from: now)
45 nowComp.minute = nowComp.minute
47 .flatMap { ($0 + 2) / 5 }
50 guard let notifyDate = Calendar.current.date(from: nowComp) else {
52 print("ResourceHistoryManager: Can not create notify date")
56 let notifyTime = notifyDate.timeIntervalSinceNow
57 self.timer = Timer.scheduledTimer(timeInterval: notifyTime,
59 selector: .notifyIfNeeded,
64 private func saveResources() {
66 let store = ServerDataStore.default
68 guard let material = store.material() else {
70 print("ResourceHistoryManager: Can not get Material")
74 guard let basic = store.basic() else {
76 print("ResourceHistoryManager: Can not get Basic")
80 let historyStore = ResourceHistoryDataStore.oneTimeEditor()
82 guard let newHistory = historyStore.cerateResource() else {
84 print("ResourceHistoryManager: Can not create ResourceHIstory")
89 var nowComp = Calendar.current
90 .dateComponents([.year, .month, .day, .hour, .minute], from: now)
91 var minutes = nowComp.minute.map { ($0 + 2) / 5 } ?? 0
93 nowComp.minute = minutes
95 newHistory.date = Calendar.current.date(from: nowComp)!
96 newHistory.minute = (minutes != 60) ? minutes : 0
97 newHistory.fuel = material.fuel
98 newHistory.bull = material.bull
99 newHistory.steel = material.steel
100 newHistory.bauxite = material.bauxite
101 newHistory.kaihatusizai = material.kaihatusizai
102 newHistory.kousokukenzo = material.kousokukenzo
103 newHistory.kousokushuhuku = material.kousokushuhuku
104 newHistory.screw = material.screw
105 newHistory.experience = basic.experience
108 private func reduceResourceByConditions(_ store: ResourceHistoryDataStore, _ target: [Int], _ ago: Date) {
110 store.resources(in: target, older: ago).forEach { store.delete($0) }
113 private func dateOfMonth(_ month: Int) -> Date {
115 return Date(timeIntervalSinceNow: TimeInterval(month * 30 * 24 * 60 * 60))
118 private func reduce(_ notification: Notification) {
120 let queue = DispatchQueue(label: "ResourceHistoryManager")
123 let store = ResourceHistoryDataStore.oneTimeEditor()
126 self.reduceResourceByConditions(store,
127 [5, 10, 20, 25, 35, 40, 50, 55],
128 self.dateOfMonth(-1))
131 self.reduceResourceByConditions(store,
132 [5, 10, 15, 20, 25, 35, 40, 45, 50, 55],
133 self.dateOfMonth(-3))
136 self.reduceResourceByConditions(store,
137 [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55],
138 self.dateOfMonth(-6))