2 // ResourceHistoryManager.swift
5 // Created by Hori,Masaki on 2017/01/22.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 final class ResourceHistoryManager: NSObject {
13 private let periodicNotification = PeriodicNotifier(hour: 23, minutes: 3)
20 NotificationCenter.default
21 .addObserver(forName: .Periodic,
22 object: periodicNotification,
27 private var timer: Timer?
29 @objc fileprivate func notifyIfNeeded(_ timer: Timer?) {
31 if timer != nil { saveResources() }
32 if let valid = timer?.isValid, valid { timer?.invalidate() }
35 var nowComp = Calendar.current
36 .dateComponents([.year, .month, .day, .hour, .minute], from: now)
38 nowComp.minute = nowComp.minute
40 .flatMap { ($0 + 2) / 5 }
43 guard let notifyDate = Calendar.current.date(from: nowComp) else {
45 print("ResourceHistoryManager: Can not create notify date")
49 let notifyTime = notifyDate.timeIntervalSinceNow
50 self.timer = Timer.scheduledTimer(timeInterval: notifyTime,
52 selector: #selector(ResourceHistoryManager.notifyIfNeeded(_:)),
57 private func saveResources() {
59 let store = ServerDataStore.default
61 guard let material = store.material() else {
63 return Logger.shared.log("ResourceHistoryManager: Can not get Material")
66 guard let basic = store.basic() else {
68 return Logger.shared.log("ResourceHistoryManager: Can not get Basic")
71 let historyStore = ResourceHistoryDataStore.oneTimeEditor()
73 guard let newHistory = historyStore.createResource() else {
75 return Logger.shared.log("ResourceHistoryManager: Can not create ResourceHIstory")
79 var nowComp = Calendar.current
80 .dateComponents([.year, .month, .day, .hour, .minute], from: now)
81 let minutes = nowComp.minute.map { ($0 + 2) / 5 } ?? 0
82 nowComp.minute = minutes * 5
84 newHistory.date = Calendar.current.date(from: nowComp)!
85 newHistory.minute = (minutes != 60) ? minutes : 0
86 newHistory.fuel = material.fuel
87 newHistory.bull = material.bull
88 newHistory.steel = material.steel
89 newHistory.bauxite = material.bauxite
90 newHistory.kaihatusizai = material.kaihatusizai
91 newHistory.kousokukenzo = material.kousokukenzo
92 newHistory.kousokushuhuku = material.kousokushuhuku
93 newHistory.screw = material.screw
94 newHistory.experience = basic.experience
97 private func reduceResourceByConditions(_ store: ResourceHistoryDataStore, _ target: [Int], _ ago: Date) {
99 store.resources(in: target, older: ago).forEach(store.delete)
102 private func dateOfMonth(_ month: Int) -> Date {
104 return Date(timeIntervalSinceNow: TimeInterval(month * 30 * 24 * 60 * 60))
107 private func reduce(_ notification: Notification) {
109 let queue = DispatchQueue(label: "ResourceHistoryManager")
112 let store = ResourceHistoryDataStore.oneTimeEditor()
115 self.reduceResourceByConditions(store,
116 [5, 10, 20, 25, 35, 40, 50, 55],
117 self.dateOfMonth(-1))
120 self.reduceResourceByConditions(store,
121 [5, 10, 15, 20, 25, 35, 40, 45, 50, 55],
122 self.dateOfMonth(-3))
125 self.reduceResourceByConditions(store,
126 [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55],
127 self.dateOfMonth(-6))