OSDN Git Service

無駄を排除し効率化した
authormasakih <masakih@users.sourceforge.jp>
Sat, 13 Feb 2016 19:25:11 +0000 (04:25 +0900)
committermasakih <masakih@users.sourceforge.jp>
Sat, 13 Feb 2016 19:25:11 +0000 (04:25 +0900)
KCD/HMAnchorageRepairManager.m

index 9633c57..7af84d9 100644 (file)
@@ -17,6 +17,7 @@
 #import "HMKCMasterShipObject.h"
 #import "HMKCSlotItemObject+Extensions.h"
 #import "HMKCMasterSlotItemObject.h"
+#import "HMKCMasterSType.h"
 
 
 @interface HMAnchorageRepairManager ()
@@ -26,7 +27,7 @@
 
 @property (strong) HMKCDeck* fleet;
 @property (strong) NSNumber *deckID;
-@property (strong) NSArray *members;
+@property (strong) NSArray<HMKCShipObject *> *members;
 
 @property (strong) NSObjectController *fleetController;
 @property (strong) NSArrayController *memberController;
@@ -89,13 +90,22 @@ static NSMutableArray *sRepairableDeckIDs;
                
                [self buildMembers];
                
-//             [self resetRepairTime];
-               
                NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
                [nc addObserver:self
                           selector:@selector(didRecivePortAPINotification:)
                                   name:HMPortAPIRecieveNotification
                                 object:nil];
+               
+               
+               _memberController = [[NSArrayController alloc] initWithContent:nil];
+               [_memberController bind:NSContentArrayBinding
+                                          toObject:self
+                                       withKeyPath:@"members"
+                                               options:nil];
+               [self.memberController addObserver:self
+                                                               forKeyPath:@"arrangedObjects.equippedItem"
+                                                                  options:0
+                                                                  context:@"members"];
        }
        return self;
 }
@@ -107,6 +117,9 @@ static NSMutableArray *sRepairableDeckIDs;
                [_fleetController removeObserver:self
                                                          forKeyPath:[NSString stringWithFormat:@"selection.%@", key]];
        }
+       
+       [_memberController removeObserver:self forKeyPath:@"arrangedObjects.equippedItem"];
+       [_memberController unbind:@"members"];
 }
 
 - (NSDate *)repairTime
@@ -141,32 +154,12 @@ static NSMutableArray *sRepairableDeckIDs;
 
 - (void)buildMembers
 {
-       NSString *observeKeyPath = @"arrangedObjects.equippedItem";
-       
-       [self.memberController removeObserver:self forKeyPath:observeKeyPath];
-       
-       NSArray *shipKeys = @[@"ship_0", @"ship_1", @"ship_2", @"ship_3", @"ship_4", @"ship_5"];
-       HMServerDataStore *store = [HMServerDataStore defaultManager];
-       NSMutableArray *array = [NSMutableArray new];
-       [shipKeys enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop) {
-               id shipID = [_fleet valueForKey:key];
-               NSError *error = nil;
-               NSArray *ships = [store objectsWithEntityName:@"Ship"
-                                                                                               error:&error
-                                                                         predicateFormat:@"id = %ld", [shipID integerValue]];
-               if(error) {
-                       NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
-               }
-               if(ships.count != 0) {
-                       [array addObject:ships[0]];
-               }
-       }];
-       _members = array;
-       _memberController = [[NSArrayController alloc] initWithContent:array];
-       [self.memberController addObserver:self
-                                                       forKeyPath:observeKeyPath
-                                                          options:0
-                                                          context:@"members"];
+       NSMutableArray<HMKCShipObject *> *array = [NSMutableArray array];
+       for(NSInteger i = 0; i < 6; i++) {
+               HMKCShipObject *ship = self.fleet[i];
+               if(ship) [array addObject:ship];
+       }
+       self.members = array;
 }
 
 - (void)didRecivePortAPINotification:(NSNotification *)notification
@@ -177,6 +170,12 @@ static NSMutableArray *sRepairableDeckIDs;
        }
 }
 
+- (HMKCShipObject *)flagShip
+{
+       if(self.members.count == 0) return nil;
+       return self.members[0];
+}
+
 - (NSArray *)repairShipIds
 {
        return @[@(19)];
@@ -187,10 +186,10 @@ static NSMutableArray *sRepairableDeckIDs;
 }
 - (BOOL)repairable
 {
-       HMKCShipObject *flagShip = self.fleet[0];
+       HMKCShipObject *flagShip = self.flagShip;
        HMKCMasterShipObject *flagShipMaster = flagShip.master_ship;
-       id stype = flagShipMaster.stype;
-       id stypeId = [stype valueForKey:@"id"];
+       HMKCMasterSType *stype = flagShipMaster.stype;
+       NSNumber *stypeId = stype.id;
        BOOL result = [self.repairShipIds containsObject:stypeId];
        if(!result && [sRepairableDeckIDs containsObject:self.deckID]) {
                [sRepairableDeckIDs removeObject:self.deckID];
@@ -205,7 +204,7 @@ static NSMutableArray *sRepairableDeckIDs;
        if(![self repairable]) return @0;
        
        NSUInteger count = 2;
-       HMKCShipObject *flagShip = self.fleet[0];
+       HMKCShipObject *flagShip = self.flagShip;
        for(HMKCSlotItemObject *item in flagShip.equippedItem) {
                if([self.repairSlotItemIds containsObject:item.master_slotItem.type_2]) {
                        count++;