OSDN Git Service

装備のD&Dに対応
authormasakih <masakih@users.sourceforge.jp>
Sat, 31 Oct 2015 12:43:59 +0000 (21:43 +0900)
committermasakih <masakih@users.sourceforge.jp>
Sat, 31 Oct 2015 12:43:59 +0000 (21:43 +0900)
10月30日アップデート

KCD.xcodeproj/project.pbxproj
KCD/HMSlotResetCommand.h [new file with mode: 0644]
KCD/HMSlotResetCommand.m [new file with mode: 0644]

index c1082f0..9851e14 100644 (file)
@@ -7,6 +7,7 @@
        objects = {
 
 /* Begin PBXBuildFile section */
+               F402D5371BE3AFAB008D5881 /* HMSlotResetCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F402D5361BE3AFAB008D5881 /* HMSlotResetCommand.m */; };
                F4131ECF190F5C08004F7F9A /* HMStoreCreateSlotItemHistoryCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4131ECE190F5C08004F7F9A /* HMStoreCreateSlotItemHistoryCommand.m */; };
                F4131ED5190F63D8004F7F9A /* HMUpdateSlotItemCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4131ED4190F63D8004F7F9A /* HMUpdateSlotItemCommand.m */; };
                F4131ED8190FA08D004F7F9A /* HMDestroyItem2Command.m in Sources */ = {isa = PBXBuildFile; fileRef = F4131ED7190FA08D004F7F9A /* HMDestroyItem2Command.m */; };
 /* End PBXContainerItemProxy section */
 
 /* Begin PBXFileReference section */
+               F402D5351BE3AFAB008D5881 /* HMSlotResetCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMSlotResetCommand.h; sourceTree = "<group>"; };
+               F402D5361BE3AFAB008D5881 /* HMSlotResetCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMSlotResetCommand.m; sourceTree = "<group>"; };
                F4131ECD190F5C08004F7F9A /* HMStoreCreateSlotItemHistoryCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMStoreCreateSlotItemHistoryCommand.h; sourceTree = "<group>"; };
                F4131ECE190F5C08004F7F9A /* HMStoreCreateSlotItemHistoryCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMStoreCreateSlotItemHistoryCommand.m; sourceTree = "<group>"; };
                F4131ED3190F63D8004F7F9A /* HMUpdateSlotItemCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMUpdateSlotItemCommand.h; sourceTree = "<group>"; };
                                F43769E01AE7CFF800DE6204 /* HMClearItemGetComand.m */,
                                F43769E21AE7D01D00DE6204 /* HMUpdateQuestListCommand.h */,
                                F43769E31AE7D01D00DE6204 /* HMUpdateQuestListCommand.m */,
+                               F402D5351BE3AFAB008D5881 /* HMSlotResetCommand.h */,
+                               F402D5361BE3AFAB008D5881 /* HMSlotResetCommand.m */,
                        );
                        name = Command;
                        sourceTree = "<group>";
                                F4AA060D1930E69200844688 /* HMMidnightBattleCommand.m in Sources */,
                                F4D6BC551A93903E00742FEB /* HMDropShipHistory.m in Sources */,
                                F4131EE91911445F004F7F9A /* HMSlotItemEquipTypeTransformer.m in Sources */,
+                               F402D5371BE3AFAB008D5881 /* HMSlotResetCommand.m in Sources */,
                                F4BDEB5A187252F30069D0CE /* HMAppDelegate.m in Sources */,
                                F4E7802B18DC3A820011BC4C /* HMLocalDataStore.m in Sources */,
                                F494DC6718C8BA6400AE97E4 /* HMPowerUpSupportViewController.m in Sources */,
diff --git a/KCD/HMSlotResetCommand.h b/KCD/HMSlotResetCommand.h
new file mode 100644 (file)
index 0000000..ffa1297
--- /dev/null
@@ -0,0 +1,13 @@
+//
+//  HMSlotResetCommand.h
+//  KCD
+//
+//  Created by Hori,Masaki on 2015/10/30.
+//  Copyright (c) 2015年 Hori,Masaki. All rights reserved.
+//
+
+#import "HMJSONCommand.h"
+
+@interface HMSlotResetCommand : HMJSONCommand
+
+@end
diff --git a/KCD/HMSlotResetCommand.m b/KCD/HMSlotResetCommand.m
new file mode 100644 (file)
index 0000000..2494dd2
--- /dev/null
@@ -0,0 +1,121 @@
+//
+//  HMSlotResetCommand.m
+//  KCD
+//
+//  Created by Hori,Masaki on 2015/10/30.
+//  Copyright (c) 2015年 Hori,Masaki. All rights reserved.
+//
+
+#import "HMSlotResetCommand.h"
+
+#import "HMServerDataStore.h"
+
+
+@interface HMSlotResetCommand ()
+@property (nonatomic, strong) NSArray *slotItems;
+
+@end
+
+@implementation HMSlotResetCommand
++ (void)load
+{
+       static dispatch_once_t onceToken;
+       dispatch_once(&onceToken, ^{
+               [HMJSONCommand registerClass:self];
+       });
+}
++ (BOOL)canExcuteAPI:(NSString *)api
+{
+       if([api isEqualToString:@"/kcsapi/api_req_kaisou/slot_exchange_index"]) return YES;
+       return NO;
+}
+
+- (void)execute
+{
+       HMServerDataStore *store = [HMServerDataStore oneTimeEditor];
+       
+       NSString *destroyedShipId = [self.arguments objectForKey:@"api_id"];
+       
+       NSError *error = nil;
+       NSArray *ships = [store objectsWithEntityName:@"Ship"
+                                                                                       error:&error
+                                                                 predicateFormat:@"id = %@", @([destroyedShipId integerValue])];
+       if(ships.count == 0) {
+               return;
+       }
+       
+       id ship = ships[0];
+       
+       NSArray *slotItems = [self.json valueForKeyPath:@"api_data.api_slot"];
+       for(NSUInteger i = 0; i < slotItems.count; i++) {
+               NSNumber *slotID = slotItems[i];
+               if([slotID integerValue] == -1) continue;
+               [ship setValue:slotID forKey:[NSString stringWithFormat:@"slot_%ld", i]];
+       }
+       
+       [self addSlotItem:slotItems toObject:ship];
+       
+}
+
+- (void)addSlotItem:(id)slotItems toObject:(NSManagedObject *)object
+{
+       if(!self.slotItems) {
+               NSError *error = nil;
+               NSManagedObjectContext *managedObjectContext = [object managedObjectContext];
+               NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"SlotItem"];
+               NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"id" ascending:YES];
+               [req setSortDescriptors:@[sortDescriptor]];
+               self.slotItems = [managedObjectContext executeFetchRequest:req
+                                                                                                                        error:&error];
+               if(error) {
+                       [self log:@"Fetch error: %@", error];
+                       return;
+               }
+               if([self.slotItems count] == 0) {
+                       return;
+               }
+       }
+       
+       NSMutableArray *newItems = [NSMutableArray new];
+       NSRange range = NSMakeRange(0, self.slotItems.count);
+       for(id value in slotItems) {
+               if([value integerValue] == -1) continue;
+               NSUInteger index = [self.slotItems indexOfObject:value
+                                                                                  inSortedRange:range
+                                                                                                options:NSBinarySearchingFirstEqual
+                                                                                usingComparator:^(id obj1, id obj2) {
+                                                                                        id value1, value2;
+                                                                                        if([obj1 isKindOfClass:[NSNumber class]]) {
+                                                                                                value1 = obj1;
+                                                                                        } else {
+                                                                                                value1 = [obj1 valueForKey:@"id"];
+                                                                                        }
+                                                                                        if([obj2 isKindOfClass:[NSNumber class]]) {
+                                                                                                value2 = obj2;
+                                                                                        } else {
+                                                                                                value2 = [obj2 valueForKey:@"id"];
+                                                                                        }
+                                                                                        return [value1 compare:value2];
+                                                                                }];
+               if(index == NSNotFound) {
+                       id lastItem = [self.slotItems lastObject];
+                       NSInteger lastItemId = [[lastItem valueForKey:@"id"] integerValue];
+                       if(lastItemId < [value integerValue]) {
+#if DEBUG
+                               [self log:@"item is maybe unregistered, so it is new ship's equipment."];
+#endif
+                       } else {
+                               [self log:@"Item %@ is not found.", value];
+                       }
+                       continue;
+               }
+               id item = [self.slotItems objectAtIndex:index];
+               
+               [newItems addObject:item];
+       }
+       
+       NSMutableOrderedSet *orderedSet = [object mutableOrderedSetValueForKey:@"equippedItem"];
+       [orderedSet removeAllObjects];
+       [orderedSet addObjectsFromArray:newItems];
+}
+@end