OSDN Git Service

分岐係数込みの判定式(33)の値が誤っていたので修正
[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 private func notifyIfNeeded(_ timer: Timer?) {
30         
31         if timer != nil {
32             
33             saveResources()
34         }
35         
36         timer?.invalidate()
37         
38         let now = Date()
39         var nowComp = Calendar.current
40             .dateComponents([.year, .month, .day, .hour, .minute], from: now)
41         // 現在時刻を超える最小の5分刻みの分
42         nowComp.minute = nowComp.minute
43             .flatMap { $0 + 5 }
44             .flatMap { ($0 + 2) / 5 }
45             .flatMap { $0 * 5 }
46         
47         guard let notifyDate = Calendar.current.date(from: nowComp) else {
48             
49             print("ResourceHistoryManager: Can not create notify date")
50             
51             return
52         }
53         
54         self.timer = Timer.scheduledTimer(timeInterval: notifyDate.timeIntervalSinceNow,
55                                           target: self,
56                                           selector: #selector(ResourceHistoryManager.notifyIfNeeded(_:)),
57                                           userInfo: nil,
58                                           repeats: false)
59     }
60     
61     private func saveResources() {
62         
63         let store = ServerDataStore.default
64         
65         guard let material = store.material() else {
66             
67             Logger.shared.log("ResourceHistoryManager: Can not get Material")
68             
69             return
70         }
71         
72         guard let experience = store.sync(execute: { store.basic()?.experience }) else {
73             
74             Logger.shared.log("ResourceHistoryManager: Can not get Basic")
75             
76             return
77         }
78         
79         let historyStore = ResourceHistoryDataStore.oneTimeEditor()
80         historyStore.sync {
81             guard let newHistory = historyStore.createResource() else {
82                 
83                 Logger.shared.log("ResourceHistoryManager: Can not create ResourceHIstory")
84                 
85                 return
86             }
87             
88             let now = Date()
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
93             
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
105         }
106     }
107     
108     private func reduceResourceByConditions(_ store: ResourceHistoryDataStore, _ target: [Int], _ ago: Date) {
109         
110         store.sync {
111             
112             store.resources(in: target, older: ago).forEach(store.delete)
113         }
114     }
115     
116     private func dateOfMonth(_ month: Int) -> Date {
117         
118         return Date(timeIntervalSinceNow: TimeInterval(month * 30 * 24 * 60 * 60))
119     }
120     
121     private func reduce(_ notification: Notification) {
122         
123         let queue = DispatchQueue(label: "ResourceHistoryManager")
124         queue.async {
125             
126             let store = ResourceHistoryDataStore.oneTimeEditor()
127             
128             // 1 month
129             self.reduceResourceByConditions(store,
130                                             [5, 10, 20, 25, 35, 40, 50, 55],
131                                             self.dateOfMonth(-1))
132             
133             // 3 months
134             self.reduceResourceByConditions(store,
135                                             [5, 10, 15, 20, 25, 35, 40, 45, 50, 55],
136                                             self.dateOfMonth(-3))
137             
138             // 6 manths
139             self.reduceResourceByConditions(store,
140                                             [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55],
141                                             self.dateOfMonth(-6))
142         }
143     }
144 }