OSDN Git Service

テスト用ログ出力を削除
[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         NSManagedObjectContext *moc = resourceStore.managedObjectContext;
112         
113         HMKCResource *newResource = [NSEntityDescription insertNewObjectForEntityForName:@"Resource"
114                                                                                                                           inManagedObjectContext:moc];
115         
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];
119         
120         NSInteger minute = (currentHour.minute + 2) / 5;
121         minute *= 5;
122         currentHour.minute = minute;
123         
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;
135         
136         [resourceStore saveAction:nil];
137         
138 }
139
140 void reduceResourceByConditions(HMResourceHistoryDataStore *resourceStore, NSArray *target, NSDate *oneMonthAgo)
141 {
142         NSError *error = nil;
143         NSManagedObjectContext *moc = resourceStore.managedObjectContext;
144         NSArray *array = [resourceStore objectsWithEntityName:@"Resource"
145                                                                                                         error:&error
146                                                                                   predicateFormat:@"minute IN %@ AND date < %@",
147                                           target, oneMonthAgo];
148         for( NSManagedObject *object in array) {
149                 [moc deleteObject:object];
150         }
151 }
152
153 - (void)reduce:(NSNotification *)notification
154 {
155         dispatch_queue_t queue = dispatch_queue_create("HMResourceHistoryManager", DISPATCH_QUEUE_SERIAL);
156         dispatch_async(queue, ^{
157                 HMResourceHistoryDataStore *resourceStore = [HMResourceHistoryDataStore oneTimeEditor];
158                 
159                 // 1 month.
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);
163                 
164                 // 3 month.
165                 target = @[@15, @45];
166                 NSDate *threeMonthAgo = [NSDate dateWithTimeIntervalSinceNow:-3 * 30 * 24 * 60 * 60];
167                 reduceResourceByConditions(resourceStore, target, threeMonthAgo);
168                 
169                 // 6 month.
170                 target = @[@30];
171                 NSDate *sixMonthAgo = [NSDate dateWithTimeIntervalSinceNow:-6 * 30 * 24 * 60 * 60];
172                 reduceResourceByConditions(resourceStore, target, sixMonthAgo);
173                 
174                 [resourceStore saveAction:nil];
175         });
176         
177 }
178
179 @end