2 // HMResourceHistoryManager.m
5 // Created by Hori,Masaki on 2015/08/04.
6 // Copyright (c) 2015年 Hori,Masaki. All rights reserved.
9 #import "HMResourceHistoryManager.h"
11 #import "HMServerDataStore.h"
12 #import "HMResourceHistoryDataStore.h"
14 #import "HMPeriodicNotifier.h"
16 #import "HMKCMaterial.h"
18 #import "HMKCResource.h"
20 static HMResourceHistoryManager *sInstance;
23 @interface HMResourceHistoryManager ()
24 @property (strong) NSTimer *timer;
25 @property (strong) HMPeriodicNotifier *periodicNotification;
28 @implementation HMResourceHistoryManager
31 static dispatch_once_t onceToken;
32 dispatch_once(&onceToken, ^{
34 sInstance = [self new];
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];
50 - (void)notifyIfNeeded:(id)timer
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];
60 if(self.timer.valid) {
61 [self.timer invalidate];
64 currentHour.minute += 5;
65 NSInteger minute = (currentHour.minute + 2) / 5;
67 currentHour.minute = minute;
68 NSDate *nextNotifyDate = [[NSCalendar currentCalendar] dateFromComponents:currentHour];
69 NSTimeInterval nextNotifyTime = [nextNotifyDate timeIntervalSinceNow];
71 self.timer = [NSTimer scheduledTimerWithTimeInterval:nextNotifyTime
80 HMServerDataStore *store = [HMServerDataStore defaultManager];
83 NSArray *materials = [store objectsWithEntityName:@"Material"
87 NSLog(@"Fetch error -> %@", error);
90 if([materials count] == 0) {
91 NSLog(@"Material data is invalid.");
94 HMKCMaterial *material = materials[0];
97 NSArray *basics = [store objectsWithEntityName:@"Basic"
101 NSLog(@"Fetch error -> %@", error);
104 if([basics count] == 0) {
105 NSLog(@"Basic data is invalid.");
108 HMKCBasic *basic = basics[0];
110 HMResourceHistoryDataStore *resourceStore = [HMResourceHistoryDataStore oneTimeEditor];
112 HMKCResource *newResource = [resourceStore insertNewObjectForEntityForName:@"Resource"];
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];
118 NSInteger minute = (currentHour.minute + 2) / 5;
120 currentHour.minute = minute;
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;
134 [resourceStore saveAction:nil];
138 void reduceResourceByConditions(HMResourceHistoryDataStore *resourceStore, NSArray *target, NSDate *oneMonthAgo)
140 NSError *error = nil;
141 NSArray *array = [resourceStore objectsWithEntityName:@"Resource"
143 predicateFormat:@"minute IN %@ AND date < %@",
144 target, oneMonthAgo];
145 for( NSManagedObject *object in array) {
146 [resourceStore deleteObject:object];
150 - (void)reduce:(NSNotification *)notification
152 dispatch_queue_t queue = dispatch_queue_create("HMResourceHistoryManager", DISPATCH_QUEUE_SERIAL);
153 dispatch_async(queue, ^{
154 HMResourceHistoryDataStore *resourceStore = [HMResourceHistoryDataStore oneTimeEditor];
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);
162 target = @[@15, @45];
163 NSDate *threeMonthAgo = [NSDate dateWithTimeIntervalSinceNow:-3 * 30 * 24 * 60 * 60];
164 reduceResourceByConditions(resourceStore, target, threeMonthAgo);
168 NSDate *sixMonthAgo = [NSDate dateWithTimeIntervalSinceNow:-6 * 30 * 24 * 60 * 60];
169 reduceResourceByConditions(resourceStore, target, sixMonthAgo);
171 [resourceStore saveAction:nil];