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 private func notifyIfNeeded(_ timer: Timer?) {
39 var nowComp = Calendar.current
40 .dateComponents([.year, .month, .day, .hour, .minute], from: now)
42 nowComp.minute = nowComp.minute
44 .flatMap { ($0 + 2) / 5 }
47 guard let notifyDate = Calendar.current.date(from: nowComp) else {
49 print("ResourceHistoryManager: Can not create notify date")
54 self.timer = Timer.scheduledTimer(timeInterval: notifyDate.timeIntervalSinceNow,
56 selector: #selector(ResourceHistoryManager.notifyIfNeeded(_:)),
61 private func saveResources() {
63 let store = ServerDataStore.default
65 guard let material = store.material() else {
67 Logger.shared.log("ResourceHistoryManager: Can not get Material")
72 guard let experience = store.sync(execute: { store.basic()?.experience }) else {
74 Logger.shared.log("ResourceHistoryManager: Can not get Basic")
79 let historyStore = ResourceHistoryDataStore.oneTimeEditor()
81 guard let newHistory = historyStore.createResource() else {
83 Logger.shared.log("ResourceHistoryManager: Can not create ResourceHIstory")
89 var nowComp = Calendar.current
90 .dateComponents([.year, .month, .day, .hour, .minute], from: now)
91 let minutes = nowComp.minute.map { ($0 + 2) / 5 } ?? 0
92 nowComp.minute = minutes * 5
94 newHistory.date = Calendar.current.date(from: nowComp)!
95 newHistory.minute = (minutes != 60) ? minutes : 0
96 newHistory.fuel = store.sync { material.fuel }
97 newHistory.bull = store.sync { material.bull }
98 newHistory.steel = store.sync { material.steel }
99 newHistory.bauxite = store.sync { material.bauxite }
100 newHistory.kaihatusizai = store.sync { material.kaihatusizai }
101 newHistory.kousokukenzo = store.sync { material.kousokukenzo }
102 newHistory.kousokushuhuku = store.sync { material.kousokushuhuku }
103 newHistory.screw = store.sync { material.screw }
104 newHistory.experience = experience
108 private func reduceResourceByConditions(_ store: ResourceHistoryDataStore, _ target: [Int], _ ago: Date) {
112 store.resources(in: target, older: ago).forEach(store.delete)
116 private func dateOfMonth(_ month: Int) -> Date {
118 return Date(timeIntervalSinceNow: TimeInterval(month * 30 * 24 * 60 * 60))
121 private func reduce(_ notification: Notification) {
123 let queue = DispatchQueue(label: "ResourceHistoryManager")
126 let store = ResourceHistoryDataStore.oneTimeEditor()
129 self.reduceResourceByConditions(store,
130 [5, 10, 20, 25, 35, 40, 50, 55],
131 self.dateOfMonth(-1))
134 self.reduceResourceByConditions(store,
135 [5, 10, 15, 20, 25, 35, 40, 45, 50, 55],
136 self.dateOfMonth(-3))
139 self.reduceResourceByConditions(store,
140 [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55],
141 self.dateOfMonth(-6))