OSDN Git Service

deleteObject:をHMCoreDataManagerで行うように変更
[kcd/KCD.git] / KCD / HMResourceHistoryManager.m
1 //
2 //  HMResourceHistoryManager.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2015/08/04.
6 //  Copyright (c) 2015年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMResourceHistoryManager.h"
10
11 #import "HMServerDataStore.h"
12 #import "HMResourceHistoryDataStore.h"
13
14 #import "HMPeriodicNotifier.h"
15
16 #import "HMKCMaterial.h"
17 #import "HMKCBasic.h"
18 #import "HMKCResource.h"
19
20 static HMResourceHistoryManager *sInstance;
21
22
23 @interface HMResourceHistoryManager ()
24 @property (strong) NSTimer *timer;
25 @property (strong) HMPeriodicNotifier *periodicNotification;
26
27 @end
28 @implementation HMResourceHistoryManager
29 + (void)load
30 {
31         static dispatch_once_t onceToken;
32         dispatch_once(&onceToken, ^{
33                 
34                 sInstance = [self new];
35                 [sInstance run];
36         });
37 }
38
39 - (void)run
40 {
41         self.periodicNotification = [HMPeriodicNotifier periodicNotifierWithHour:23 minutes:3];
42         [[NSNotificationCenter defaultCenter] addObserver:self
43                                                                                          selector:@selector(reduce:)
44                                                                                                  name:HMPeriodicNotification
45                                                                                            object:self.periodicNotification];
46         [self notifyIfNeeded:nil];
47         
48 }
49
50 - (void)notifyIfNeeded:(id)timer
51 {
52         NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
53         NSCalendarUnit unit = NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
54         NSDateComponents *currentHour = [[NSCalendar currentCalendar] components:unit fromDate:now];
55         
56         if(self.timer) {
57                 [self saveResources];
58         }
59         
60         if(self.timer.valid) {
61                 [self.timer invalidate];
62         }
63         
64         currentHour.minute += 5;
65         NSInteger minute = (currentHour.minute + 2) / 5;
66         minute *= 5;
67         currentHour.minute = minute;
68         NSDate *nextNotifyDate = [[NSCalendar currentCalendar] dateFromComponents:currentHour];
69         NSTimeInterval nextNotifyTime = [nextNotifyDate timeIntervalSinceNow];
70         
71         self.timer = [NSTimer scheduledTimerWithTimeInterval:nextNotifyTime
72                                                                                                   target:self
73                                                                                                 selector:_cmd
74                                                                                                 userInfo:nil
75                                                                                                  repeats:NO];
76 }
77
78 - (void)saveResources
79 {
80         HMServerDataStore *store = [HMServerDataStore defaultManager];
81         
82         NSError *error = nil;
83         NSArray *materials = [store objectsWithEntityName:@"Material"
84                                                                                         predicate:nil
85                                                                                                 error:&error];
86         if(error) {
87                 NSLog(@"Fetch error -> %@", error);
88                 return;
89         }
90         if([materials count] == 0) {
91                 NSLog(@"Material data is invalid.");
92                 return;
93         }
94         HMKCMaterial *material = materials[0];
95         
96         
97         NSArray *basics = [store objectsWithEntityName:@"Basic"
98                                                                                         predicate:nil
99                                                                                                 error:&error];
100         if(error) {
101                 NSLog(@"Fetch error -> %@", error);
102                 return;
103         }
104         if([basics count] == 0) {
105                 NSLog(@"Basic data is invalid.");
106                 return;
107         }
108         HMKCBasic *basic = basics[0];
109         
110         HMResourceHistoryDataStore *resourceStore = [HMResourceHistoryDataStore oneTimeEditor];
111         
112         HMKCResource *newResource = [resourceStore insertNewObjectForEntityForName:@"Resource"];
113         
114         NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
115         NSCalendarUnit unit = NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
116         NSDateComponents *currentHour = [[NSCalendar currentCalendar] components:unit fromDate:now];
117         
118         NSInteger minute = (currentHour.minute + 2) / 5;
119         minute *= 5;
120         currentHour.minute = minute;
121         
122         newResource.date = [[NSCalendar currentCalendar] dateFromComponents:currentHour];
123         newResource.minute = @(minute != 60 ? minute : 0);
124         newResource.bauxite = material.bauxite;
125         newResource.bull = material.bull;
126         newResource.fuel = material.fuel;
127         newResource.kaihatusizai = material.kaihatusizai;
128         newResource.kousokukenzo = material.kousokukenzo;
129         newResource.kousokushuhuku = material.kousokushuhuku;
130         newResource.screw = material.screw;
131         newResource.steel = material.steel;
132         newResource.experience = basic.experience;
133         
134         [resourceStore saveAction:nil];
135         
136 }
137
138 void reduceResourceByConditions(HMResourceHistoryDataStore *resourceStore, NSArray *target, NSDate *oneMonthAgo)
139 {
140         NSError *error = nil;
141         NSArray *array = [resourceStore objectsWithEntityName:@"Resource"
142                                                                                                         error:&error
143                                                                                   predicateFormat:@"minute IN %@ AND date < %@",
144                                           target, oneMonthAgo];
145         for( NSManagedObject *object in array) {
146                 [resourceStore deleteObject:object];
147         }
148 }
149
150 - (void)reduce:(NSNotification *)notification
151 {
152         dispatch_queue_t queue = dispatch_queue_create("HMResourceHistoryManager", DISPATCH_QUEUE_SERIAL);
153         dispatch_async(queue, ^{
154                 HMResourceHistoryDataStore *resourceStore = [HMResourceHistoryDataStore oneTimeEditor];
155                 
156                 // 1 month.
157                 NSArray *target = @[@5, @10, @20, @25, @35, @40, @50, @55];
158                 NSDate *oneMonthAgo = [NSDate dateWithTimeIntervalSinceNow:-1 * 30 * 24 * 60 * 60];
159                 reduceResourceByConditions(resourceStore, target, oneMonthAgo);
160                 
161                 // 3 month.
162                 target = @[@15, @45];
163                 NSDate *threeMonthAgo = [NSDate dateWithTimeIntervalSinceNow:-3 * 30 * 24 * 60 * 60];
164                 reduceResourceByConditions(resourceStore, target, threeMonthAgo);
165                 
166                 // 6 month.
167                 target = @[@30];
168                 NSDate *sixMonthAgo = [NSDate dateWithTimeIntervalSinceNow:-6 * 30 * 24 * 60 * 60];
169                 reduceResourceByConditions(resourceStore, target, sixMonthAgo);
170                 
171                 [resourceStore saveAction:nil];
172         });
173         
174 }
175
176 @end