OSDN Git Service

07b2957611e0e0d7bde4ca67cb66b4b5d6f259d7
[kcd/KCD.git] / KCD / HMAnchorageRepairManager.m
1 //
2 //  HMAnchorageRepairManager.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2016/02/20.
6 //  Copyright © 2016年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMAnchorageRepairManager.h"
10
11 #import "KCD-Swift.h"
12
13 #import "HMFleetManager.h"
14 #import "HMFleet.h"
15
16 #import "HMKCShipObject+Extensions.h"
17 #import "HMKCMasterShipObject.h"
18 #import "HMKCMasterSType.h"
19
20 #import "HMUserDefaults.h"
21 #import "HMAppDelegate.h"
22
23 #import "HMChangeHenseiNotification.h"
24
25
26 @interface HMAnchorageRepairManager ()
27 @property (strong) HMFleetManager *fleetManager;
28
29 @property (strong) NSDate *repairTime;
30
31 @end
32
33 @implementation HMAnchorageRepairManager
34
35 + (instancetype)defaultManager
36 {
37         static HMAnchorageRepairManager *defaultManager = nil;
38         static dispatch_once_t onceToken;
39         dispatch_once(&onceToken, ^{
40                 defaultManager = [HMAnchorageRepairManager new];
41         });
42         return defaultManager;
43 }
44
45 - (instancetype)init
46 {
47         self = [super init];
48         if(self) {
49         HMAppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
50                 _fleetManager = appDelegate.fleetManager;
51                 
52                 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
53                 [[NSNotificationCenter defaultCenter] addObserver:self
54                                                                                                  selector:@selector(henseiDidChangeNotification:)
55                                                                                                          name:HMChangeHenseiNotification
56                                                                                                    object:nil];
57                 [nc addObserver:self
58                            selector:@selector(didReturnToBaseNotification:)
59                                    name:HMPortNotifyCommand.notificationName
60                                  object:nil];
61         }
62         
63         return self;
64 }
65
66 - (NSDate *)repairTime
67 {
68         return HMStandardDefaults.repairTime;;
69 }
70 - (void)setRepairTime:(NSDate *)repairTime
71 {
72         HMStandardDefaults.repairTime = repairTime;
73 }
74
75 - (NSArray<NSNumber *> *)repairShipTypeIds
76 {
77         return @[@(19)];
78 }
79
80 - (void)reset
81 {
82         self.repairTime = [NSDate dateWithTimeIntervalSinceNow:0.0];
83 }
84
85 - (NSNumber *)shipTypeIDWithShipID:(NSNumber *)shipID
86 {
87         HMServerDataStore *store = [HMServerDataStore defaultManager];
88         NSError *error = nil;
89         NSArray<HMKCShipObject *> *array = [store objectsWithEntityName:@"Ship"
90                                                                                                                   predicate:[NSPredicate predicateWithFormat:@"id = %@", shipID]
91                                                                                                                           error:&error];
92         if(array.count == 0) {
93                 NSLog(@"Ship not found.");
94                 return nil;
95         }
96         
97         NSNumber *masterShipID = array[0].master_ship.stype.id;
98         
99         return masterShipID;
100 }
101
102 - (NSNumber *)shipTypeIDWithFleetNumber:(NSNumber *)fleetNumber position:(NSNumber *)position
103 {
104         NSInteger fleetNumberValue = fleetNumber.integerValue;
105         if(fleetNumberValue < 1 || fleetNumberValue > 4) return nil;
106         
107         HMFleet *fleet = self.fleetManager.fleets[fleetNumberValue - 1];
108         HMKCShipObject *ship = fleet[position.integerValue];
109         
110         return ship.master_ship.stype.id;
111 }
112
113 - (BOOL)needsResetWithInfo:(HMChangeHenseiNotificationUserInfo *)userInfo
114 {
115         // 変更のあった艦隊の旗艦は工作艦か?
116         NSNumber *flagShipShipTypeID = [self shipTypeIDWithFleetNumber:userInfo.fleetNumber position:@0];
117         if([self.repairShipTypeIds containsObject:flagShipShipTypeID]) {
118                 return YES;
119         }
120         if(userInfo.type == kHMChangeHenseiReplace) {
121                 flagShipShipTypeID = [self shipTypeIDWithFleetNumber:userInfo.replaceFleetNumber position:@0];
122                 if([self.repairShipTypeIds containsObject:flagShipShipTypeID]) {
123                         return YES;
124                 }
125         }
126         
127         // 変更のあった艦娘は工作艦か?
128         //     旗艦から外れたか?
129         if(userInfo.type == kHMChangeHenseiRemove || userInfo.type == kHMChangeHenseiAppend) {
130                 NSNumber *shipTypeID = [self shipTypeIDWithShipID:userInfo.shipID];
131                 if([self.repairShipTypeIds containsObject:shipTypeID]) {
132                         return [userInfo.position isEqual:@0];
133                 }
134         }
135         if(userInfo.type == kHMChangeHenseiReplace) {
136                 NSNumber *shipTypeID = [self shipTypeIDWithShipID:userInfo.replaceShipID];
137                 if([self.repairShipTypeIds containsObject:shipTypeID]) {
138                         return [userInfo.replacePosition isEqual:@0];
139                 }
140         }
141         
142         return NO;
143 }
144
145 - (void)didReturnToBaseNotification:(NSNotification *)notification
146 {
147         // 時間をチェック
148         NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
149         NSTimeInterval passedTime = [now timeIntervalSinceDate:self.repairTime];
150         if(passedTime < 20 * 60) return;
151         
152         // 修理時間をリセット
153         [self reset];
154 }
155
156 - (void)henseiDidChangeNotification:(NSNotification *)notification
157 {
158         NSDictionary *userInfo = notification.userInfo;
159         if(!userInfo) return;
160         
161         HMChangeHenseiNotificationUserInfo *info = userInfo[HMChangeHenseiUserInfoKey];
162         
163         switch(info.type) {
164                 case kHMChangeHenseiAppend:
165                 case kHMChangeHenseiReplace:
166                 case kHMChangeHenseiRemove:
167                         if([self needsResetWithInfo:info]) {
168                                 [self reset];
169                         }
170                         break;
171                 case kHMChangeHenseiRemoveAllWithoutFlagship:
172                         // do nothing
173                         break;
174                 default:
175                         NSLog(@"Unknown hensei change type");
176                         break;
177         }
178 }
179
180 @end