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 print("ResourceHistoryManager: Can not get Material")
67 guard let basic = store.basic() else {
69 print("ResourceHistoryManager: Can not get Basic")
73 let historyStore = ResourceHistoryDataStore.oneTimeEditor()
75 guard let newHistory = historyStore.createResource() else {
77 print("ResourceHistoryManager: Can not create ResourceHIstory")
82 var nowComp = Calendar.current
83 .dateComponents([.year, .month, .day, .hour, .minute], from: now)
84 let minutes = nowComp.minute.map { ($0 + 2) / 5 } ?? 0
85 nowComp.minute = minutes * 5
87 newHistory.date = Calendar.current.date(from: nowComp)!
88 newHistory.minute = (minutes != 60) ? minutes : 0
89 newHistory.fuel = material.fuel
90 newHistory.bull = material.bull
91 newHistory.steel = material.steel
92 newHistory.bauxite = material.bauxite
93 newHistory.kaihatusizai = material.kaihatusizai
94 newHistory.kousokukenzo = material.kousokukenzo
95 newHistory.kousokushuhuku = material.kousokushuhuku
96 newHistory.screw = material.screw
97 newHistory.experience = basic.experience
100 private func reduceResourceByConditions(_ store: ResourceHistoryDataStore, _ target: [Int], _ ago: Date) {
102 store.resources(in: target, older: ago).forEach(store.delete)
105 private func dateOfMonth(_ month: Int) -> Date {
107 return Date(timeIntervalSinceNow: TimeInterval(month * 30 * 24 * 60 * 60))
110 private func reduce(_ notification: Notification) {
112 let queue = DispatchQueue(label: "ResourceHistoryManager")
115 let store = ResourceHistoryDataStore.oneTimeEditor()
118 self.reduceResourceByConditions(store,
119 [5, 10, 20, 25, 35, 40, 50, 55],
120 self.dateOfMonth(-1))
123 self.reduceResourceByConditions(store,
124 [5, 10, 15, 20, 25, 35, 40, 45, 50, 55],
125 self.dateOfMonth(-3))
128 self.reduceResourceByConditions(store,
129 [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55],
130 self.dateOfMonth(-6))