OSDN Git Service

改修工廠メニューを更新
[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             return Logger.shared.log("ResourceHistoryManager: Can not get Material")
64         }
65         
66         guard let basic = store.basic() else {
67             
68             return Logger.shared.log("ResourceHistoryManager: Can not get Basic")
69         }
70         
71         let historyStore = ResourceHistoryDataStore.oneTimeEditor()
72         
73         guard let newHistory = historyStore.createResource() else {
74             
75             return Logger.shared.log("ResourceHistoryManager: Can not create ResourceHIstory")
76         }
77         
78         let now = Date()
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
83         
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
95     }
96     
97     private func reduceResourceByConditions(_ store: ResourceHistoryDataStore, _ target: [Int], _ ago: Date) {
98         
99         store.resources(in: target, older: ago).forEach(store.delete)
100     }
101     
102     private func dateOfMonth(_ month: Int) -> Date {
103         
104         return Date(timeIntervalSinceNow: TimeInterval(month * 30 * 24 * 60 * 60))
105     }
106     
107     private func reduce(_ notification: Notification) {
108         
109         let queue = DispatchQueue(label: "ResourceHistoryManager")
110         queue.async {
111             
112             let store = ResourceHistoryDataStore.oneTimeEditor()
113             
114             // 1 month
115             self.reduceResourceByConditions(store,
116                                             [5, 10, 20, 25, 35, 40, 50, 55],
117                                             self.dateOfMonth(-1))
118             
119             // 3 months
120             self.reduceResourceByConditions(store,
121                                             [5, 10, 15, 20, 25, 35, 40, 45, 50, 55],
122                                             self.dateOfMonth(-3))
123             
124             // 6 manths
125             self.reduceResourceByConditions(store,
126                                             [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55],
127                                             self.dateOfMonth(-6))
128         }
129     }
130 }