OSDN Git Service

varを使わないように変更
[kcd/KCD.git] / KCD / ResourceHistoryManager.swift
1 //
2 //  ResourceHistoryManager.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/22.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class ResourceHistoryManager: NSObject {
12     
13     private let periodicNotification = PeriodicNotifier(hour: 23, minutes: 3)
14     
15     override init() {
16         
17         super.init()
18         
19         notifyIfNeeded(nil)
20         NotificationCenter.default
21             .addObserver(forName: .Periodic,
22                          object: periodicNotification,
23                          queue: nil,
24                          using: reduce)
25     }
26     
27     private var timer: Timer?
28     
29     @objc fileprivate func notifyIfNeeded(_ timer: Timer?) {
30         
31         if timer != nil { saveResources() }
32         if let valid = timer?.isValid, valid { timer?.invalidate() }
33         
34         let now = Date()
35         var nowComp = Calendar.current
36             .dateComponents([.year, .month, .day, .hour, .minute], from: now)
37         // 現在時刻を超える最小の5分刻みの分
38         nowComp.minute = nowComp.minute
39             .flatMap { $0 + 5 }
40             .flatMap { ($0 + 2) / 5 }
41             .flatMap { $0 * 5 }
42         
43         guard let notifyDate = Calendar.current.date(from: nowComp) else {
44             
45             print("ResourceHistoryManager: Can not create notify date")
46             return
47         }
48         
49         let notifyTime = notifyDate.timeIntervalSinceNow
50         self.timer = Timer.scheduledTimer(timeInterval: notifyTime,
51                                           target: self,
52                                           selector: #selector(ResourceHistoryManager.notifyIfNeeded(_:)),
53                                           userInfo: nil,
54                                           repeats: false)
55     }
56     
57     private func saveResources() {
58         
59         let store = ServerDataStore.default
60         
61         guard let material = store.material() else {
62             
63             print("ResourceHistoryManager: Can not get Material")
64             return
65         }
66         
67         guard let basic = store.basic() else {
68             
69             print("ResourceHistoryManager: Can not get Basic")
70             return
71         }
72         
73         let historyStore = ResourceHistoryDataStore.oneTimeEditor()
74         
75         guard let newHistory = historyStore.createResource() else {
76             
77             print("ResourceHistoryManager: Can not create ResourceHIstory")
78             return
79         }
80         
81         let now = Date()
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
86         
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
98     }
99     
100     private func reduceResourceByConditions(_ store: ResourceHistoryDataStore, _ target: [Int], _ ago: Date) {
101         
102         store.resources(in: target, older: ago).forEach(store.delete)
103     }
104     
105     private func dateOfMonth(_ month: Int) -> Date {
106         
107         return Date(timeIntervalSinceNow: TimeInterval(month * 30 * 24 * 60 * 60))
108     }
109     
110     private func reduce(_ notification: Notification) {
111         
112         let queue = DispatchQueue(label: "ResourceHistoryManager")
113         queue.async {
114             
115             let store = ResourceHistoryDataStore.oneTimeEditor()
116             
117             // 1 month
118             self.reduceResourceByConditions(store,
119                                             [5, 10, 20, 25, 35, 40, 50, 55],
120                                             self.dateOfMonth(-1))
121             
122             // 3 months
123             self.reduceResourceByConditions(store,
124                                             [5, 10, 15, 20, 25, 35, 40, 45, 50, 55],
125                                             self.dateOfMonth(-3))
126             
127             // 6 manths
128             self.reduceResourceByConditions(store,
129                                             [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55],
130                                             self.dateOfMonth(-6))
131         }
132     }
133 }