2 // HMAnchorageRepairManager.m
5 // Created by Hori,Masaki on 2016/02/20.
6 // Copyright © 2016年 Hori,Masaki. All rights reserved.
9 #import "HMAnchorageRepairManager.h"
13 #import "HMFleetManager.h"
16 #import "HMKCShipObject+Extensions.h"
17 #import "HMKCMasterShipObject.h"
18 #import "HMKCMasterSType.h"
20 #import "HMUserDefaults.h"
21 #import "HMAppDelegate.h"
23 #import "HMChangeHenseiNotification.h"
26 @interface HMAnchorageRepairManager ()
27 @property (strong) HMFleetManager *fleetManager;
29 @property (strong) NSDate *repairTime;
33 @implementation HMAnchorageRepairManager
35 + (instancetype)defaultManager
37 static HMAnchorageRepairManager *defaultManager = nil;
38 static dispatch_once_t onceToken;
39 dispatch_once(&onceToken, ^{
40 defaultManager = [HMAnchorageRepairManager new];
42 return defaultManager;
49 HMAppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
50 _fleetManager = appDelegate.fleetManager;
52 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
53 [[NSNotificationCenter defaultCenter] addObserver:self
54 selector:@selector(henseiDidChangeNotification:)
55 name:HMChangeHenseiNotification
58 selector:@selector(didReturnToBaseNotification:)
59 name:HMPortNotifyCommand.notificationName
66 - (NSDate *)repairTime
68 return HMStandardDefaults.repairTime;;
70 - (void)setRepairTime:(NSDate *)repairTime
72 HMStandardDefaults.repairTime = repairTime;
75 - (NSArray<NSNumber *> *)repairShipTypeIds
82 self.repairTime = [NSDate dateWithTimeIntervalSinceNow:0.0];
85 - (NSNumber *)shipTypeIDWithShipID:(NSNumber *)shipID
87 HMServerDataStore *store = [HMServerDataStore defaultManager];
89 NSArray<HMKCShipObject *> *array = [store objectsWithEntityName:@"Ship"
90 predicate:[NSPredicate predicateWithFormat:@"id = %@", shipID]
92 if(array.count == 0) {
93 NSLog(@"Ship not found.");
97 NSNumber *masterShipID = array[0].master_ship.stype.id;
102 - (NSNumber *)shipTypeIDWithFleetNumber:(NSNumber *)fleetNumber position:(NSNumber *)position
104 NSInteger fleetNumberValue = fleetNumber.integerValue;
105 if(fleetNumberValue < 1 || fleetNumberValue > 4) return nil;
107 HMFleet *fleet = self.fleetManager.fleets[fleetNumberValue - 1];
108 HMKCShipObject *ship = fleet[position.integerValue];
110 return ship.master_ship.stype.id;
113 - (BOOL)needsResetWithInfo:(HMChangeHenseiNotificationUserInfo *)userInfo
116 NSNumber *flagShipShipTypeID = [self shipTypeIDWithFleetNumber:userInfo.fleetNumber position:@0];
117 if([self.repairShipTypeIds containsObject:flagShipShipTypeID]) {
120 if(userInfo.type == kHMChangeHenseiReplace) {
121 flagShipShipTypeID = [self shipTypeIDWithFleetNumber:userInfo.replaceFleetNumber position:@0];
122 if([self.repairShipTypeIds containsObject:flagShipShipTypeID]) {
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];
135 if(userInfo.type == kHMChangeHenseiReplace) {
136 NSNumber *shipTypeID = [self shipTypeIDWithShipID:userInfo.replaceShipID];
137 if([self.repairShipTypeIds containsObject:shipTypeID]) {
138 return [userInfo.replacePosition isEqual:@0];
145 - (void)didReturnToBaseNotification:(NSNotification *)notification
148 NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
149 NSTimeInterval passedTime = [now timeIntervalSinceDate:self.repairTime];
150 if(passedTime < 20 * 60) return;
156 - (void)henseiDidChangeNotification:(NSNotification *)notification
158 NSDictionary *userInfo = notification.userInfo;
159 if(!userInfo) return;
161 HMChangeHenseiNotificationUserInfo *info = userInfo[HMChangeHenseiUserInfoKey];
164 case kHMChangeHenseiAppend:
165 case kHMChangeHenseiReplace:
166 case kHMChangeHenseiRemove:
167 if([self needsResetWithInfo:info]) {
171 case kHMChangeHenseiRemoveAllWithoutFlagship:
175 NSLog(@"Unknown hensei change type");