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];
111 NSManagedObjectContext *moc = resourceStore.managedObjectContext;
113 HMKCResource *newResource = [NSEntityDescription insertNewObjectForEntityForName:@"Resource"
114 inManagedObjectContext:moc];
116 NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
117 NSCalendarUnit unit = NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
118 NSDateComponents *currentHour = [[NSCalendar currentCalendar] components:unit fromDate:now];
120 NSInteger minute = (currentHour.minute + 2) / 5;
122 currentHour.minute = minute;
124 newResource.date = [[NSCalendar currentCalendar] dateFromComponents:currentHour];
125 newResource.minute = @(minute != 60 ? minute : 0);
126 newResource.bauxite = material.bauxite;
127 newResource.bull = material.bull;
128 newResource.fuel = material.fuel;
129 newResource.kaihatusizai = material.kaihatusizai;
130 newResource.kousokukenzo = material.kousokukenzo;
131 newResource.kousokushuhuku = material.kousokushuhuku;
132 newResource.screw = material.screw;
133 newResource.steel = material.steel;
134 newResource.experience = basic.experience;
136 [resourceStore saveAction:nil];
140 void reduceResourceByConditions(HMResourceHistoryDataStore *resourceStore, NSArray *target, NSDate *oneMonthAgo)
142 NSError *error = nil;
143 NSManagedObjectContext *moc = resourceStore.managedObjectContext;
144 NSArray *array = [resourceStore objectsWithEntityName:@"Resource"
146 predicateFormat:@"minute IN %@ AND date < %@",
147 target, oneMonthAgo];
148 for( NSManagedObject *object in array) {
149 [moc deleteObject:object];
153 - (void)reduce:(NSNotification *)notification
155 dispatch_queue_t queue = dispatch_queue_create("HMResourceHistoryManager", DISPATCH_QUEUE_SERIAL);
156 dispatch_async(queue, ^{
157 HMResourceHistoryDataStore *resourceStore = [HMResourceHistoryDataStore oneTimeEditor];
160 NSArray *target = @[@5, @10, @20, @25, @35, @40, @50, @55];
161 NSDate *oneMonthAgo = [NSDate dateWithTimeIntervalSinceNow:-1 * 30 * 24 * 60 * 60];
162 reduceResourceByConditions(resourceStore, target, oneMonthAgo);
165 target = @[@15, @45];
166 NSDate *threeMonthAgo = [NSDate dateWithTimeIntervalSinceNow:-3 * 30 * 24 * 60 * 60];
167 reduceResourceByConditions(resourceStore, target, threeMonthAgo);
171 NSDate *sixMonthAgo = [NSDate dateWithTimeIntervalSinceNow:-6 * 30 * 24 * 60 * 60];
172 reduceResourceByConditions(resourceStore, target, sixMonthAgo);
174 [resourceStore saveAction:nil];