OSDN Git Service

HMSlotResetCommandをSwiftに変換した
authormasakih <masakih@users.sourceforge.jp>
Mon, 9 Jan 2017 01:55:44 +0000 (10:55 +0900)
committermasakih <masakih@users.sourceforge.jp>
Mon, 9 Jan 2017 01:55:44 +0000 (10:55 +0900)
KCD.xcodeproj/project.pbxproj
KCD/HMSlotResetCommand.h [deleted file]
KCD/HMSlotResetCommand.m [deleted file]
KCD/HMSlotResetCommand.swift [new file with mode: 0644]
KCD/KCD-Bridging-Header.h

index 37a4f75..2d974e4 100644 (file)
@@ -7,7 +7,6 @@
        objects = {
 
 /* Begin PBXBuildFile section */
-               F402D5371BE3AFAB008D5881 /* HMSlotResetCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F402D5361BE3AFAB008D5881 /* HMSlotResetCommand.m */; };
                F403893E1CAEA711002A264B /* HMMemberRequireInfoCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F403893D1CAEA711002A264B /* HMMemberRequireInfoCommand.m */; };
                F4048A9D1E18D75E008ACFD6 /* HMDamageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4048A9C1E18D75E008ACFD6 /* HMDamageView.swift */; };
                F4048A9F1E19308A008ACFD6 /* HMStrokeTextFieldCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4048A9E1E19308A008ACFD6 /* HMStrokeTextFieldCell.swift */; };
@@ -65,6 +64,7 @@
                F44BC6E11E2304E3004644E3 /* HMSlotDepriveUnsetCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = F44BC6E01E2304E3004644E3 /* HMSlotDepriveUnsetCommand.swift */; };
                F44BC6E31E230577004644E3 /* HMSlotDepriveCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = F44BC6E21E230577004644E3 /* HMSlotDepriveCommand.swift */; };
                F44BC6E51E23082B004644E3 /* HMCombinedCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = F44BC6E41E23082B004644E3 /* HMCombinedCommand.swift */; };
+               F44BC6E71E231534004644E3 /* HMSlotResetCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = F44BC6E61E231534004644E3 /* HMSlotResetCommand.swift */; };
                F4513EDD18B619B600073552 /* HMCoreDataManager.m in Sources */ = {isa = PBXBuildFile; fileRef = F4513EDC18B619B600073552 /* HMCoreDataManager.m */; };
                F4513EE818B77A5300073552 /* HMMemberKDockCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4513EE718B77A5300073552 /* HMMemberKDockCommand.m */; };
                F4513EEB18B7900A00073552 /* HMMemberDeckCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4513EEA18B7900A00073552 /* HMMemberDeckCommand.m */; };
 /* End PBXCopyFilesBuildPhase 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>"; };
                F402D5381BEF14EA008D5881 /* Bookmark 2.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "Bookmark 2.xcdatamodel"; sourceTree = "<group>"; };
                F403893C1CAEA711002A264B /* HMMemberRequireInfoCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMMemberRequireInfoCommand.h; sourceTree = "<group>"; };
                F403893D1CAEA711002A264B /* HMMemberRequireInfoCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMMemberRequireInfoCommand.m; sourceTree = "<group>"; };
                F44BC6E01E2304E3004644E3 /* HMSlotDepriveUnsetCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMSlotDepriveUnsetCommand.swift; sourceTree = "<group>"; };
                F44BC6E21E230577004644E3 /* HMSlotDepriveCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMSlotDepriveCommand.swift; sourceTree = "<group>"; };
                F44BC6E41E23082B004644E3 /* HMCombinedCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMCombinedCommand.swift; sourceTree = "<group>"; };
+               F44BC6E61E231534004644E3 /* HMSlotResetCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMSlotResetCommand.swift; sourceTree = "<group>"; };
                F4513EDB18B619B600073552 /* HMCoreDataManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMCoreDataManager.h; sourceTree = "<group>"; };
                F4513EDC18B619B600073552 /* HMCoreDataManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMCoreDataManager.m; sourceTree = "<group>"; };
                F4513EE618B77A5300073552 /* HMMemberKDockCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMMemberKDockCommand.h; sourceTree = "<group>"; };
                                F4268030194D1B1A00B2C27F /* HMChangeHenseiCommand.m */,
                                F44AB23319E805D100F217AF /* HMKaisouLockCommand.h */,
                                F44AB23419E805D100F217AF /* HMKaisouLockCommand.m */,
-                               F402D5351BE3AFAB008D5881 /* HMSlotResetCommand.h */,
-                               F402D5361BE3AFAB008D5881 /* HMSlotResetCommand.m */,
+                               F44BC6E61E231534004644E3 /* HMSlotResetCommand.swift */,
                                F44BC6E41E23082B004644E3 /* HMCombinedCommand.swift */,
                                F44BC6E21E230577004644E3 /* HMSlotDepriveCommand.swift */,
                                F44BC6E01E2304E3004644E3 /* HMSlotDepriveUnsetCommand.swift */,
                                F4A2AF5B1A0F9B490024BD9E /* HMScreenshotInformation.m in Sources */,
                                F43769DE1AE1248800DE6204 /* HMAirBattleCommand.m in Sources */,
                                F4AA593E1E1E0BFB001667AF /* HMDamageValueTransformer.swift in Sources */,
+                               F44BC6E71E231534004644E3 /* HMSlotResetCommand.swift in Sources */,
                                F4AA59321E1D211C001667AF /* HMSlotitemNameTransformer.swift in Sources */,
                                F48A705F1DE58A38005DFC0B /* HMKCAirBasePlaneInfo+CoreDataProperties.m in Sources */,
                                F426802E194D0FFD00B2C27F /* HMMemberShip3Command.m in Sources */,
                                F4E5FF0A1E15CE3D0026868C /* HMBridgeViewController.swift in Sources */,
                                F4AA060D1930E69200844688 /* HMMidnightBattleCommand.m in Sources */,
                                F4D6BC551A93903E00742FEB /* HMDropShipHistory.m in Sources */,
-                               F402D5371BE3AFAB008D5881 /* HMSlotResetCommand.m in Sources */,
                                F4BDEB5A187252F30069D0CE /* HMAppDelegate.m in Sources */,
                                F4AA59461E1E33ED001667AF /* HMAreaNameTransformer.swift in Sources */,
                                F4AA59301E1D1FAB001667AF /* HMLengTransformer.swift in Sources */,
diff --git a/KCD/HMSlotResetCommand.h b/KCD/HMSlotResetCommand.h
deleted file mode 100644 (file)
index ffa1297..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-//
-//  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
deleted file mode 100644 (file)
index 094a20b..0000000
+++ /dev/null
@@ -1,123 +0,0 @@
-//
-//  HMSlotResetCommand.m
-//  KCD
-//
-//  Created by Hori,Masaki on 2015/10/30.
-//  Copyright (c) 2015年 Hori,Masaki. All rights reserved.
-//
-
-#import "HMSlotResetCommand.h"
-
-#import "KCD-Swift.h"
-
-#import "HMKCShipObject+Extensions.h"
-
-
-@interface HMSlotResetCommand ()
-@property (nonatomic, copy) 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<HMKCShipObject *> *ships = [store objectsWithEntityName:@"Ship"
-                                                                                                                         error:&error
-                                                                                                       predicateFormat:@"id = %@", @([destroyedShipId integerValue])];
-       if(ships.count == 0) {
-               return;
-       }
-       
-       HMKCShipObject *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
diff --git a/KCD/HMSlotResetCommand.swift b/KCD/HMSlotResetCommand.swift
new file mode 100644 (file)
index 0000000..fdb8074
--- /dev/null
@@ -0,0 +1,54 @@
+//
+//  HMSlotResetCommand.swift
+//  KCD
+//
+//  Created by Hori,Masaki on 2017/01/09.
+//  Copyright © 2017年 Hori,Masaki. All rights reserved.
+//
+
+import Cocoa
+
+class HMSlotResetCommand: HMJSONCommand {
+    override class func canExcuteAPI(_ api: String) -> Bool {
+        if api == "/kcsapi/api_req_kaisou/slot_exchange_index" { return true }
+        return false
+    }
+    
+    override func execute() {
+        guard let si = arguments["api_id"] as? String,
+            let shipId = Int(si)
+            else { return print("api_id is wrong") }
+        let p = NSPredicate(format: "id = %ld", shipId)
+        let store = HMServerDataStore.oneTimeEditor()
+        guard let s = try? store?.objects(withEntityName: "Ship", predicate: p),
+            let ships = s as? [HMKCShipObject],
+            let ship = ships.first
+            else { return print("Not found Ship") }
+        guard let j = json as? [String:Any],
+            let sl = j["api_data"] as? [String:Any],
+            let slotItems = sl["api_slot"] as? [NSNumber]
+            else { return print("Can not parse api_data.api_slot") }
+        
+        guard let sli = try? store?.objects(withEntityName: "SlotItem", predicate: nil),
+            let storedSlotItems = sli as? [HMKCSlotItemObject]
+            else { return print("Can not load SlotItem") }
+        
+        slotItems.enumerated().forEach {
+            ship.setValue($0.element, forKey: "slot_\($0.offset)")
+        }
+        let newSet =
+            slotItems.flatMap { (slotItem) -> HMKCSlotItemObject? in
+                if slotItem == 0 || slotItem == -1 { return nil }
+                let found = storedSlotItems
+                    .filter{ $0.id == slotItem }
+                if let item = found.first {
+                    return item
+                }
+                print("Item \(slotItem) is not found")
+                return nil
+        }
+        let current = ship.mutableOrderedSetValue(forKey: "equippedItem")
+        current.removeAllObjects()
+        current.addObjects(from: newSet)
+    }
+}
index d23d5cd..2a0d1f8 100644 (file)
@@ -58,7 +58,6 @@
 #import "HMShipDeckCommand.h"
 #import "HMChangeHenseiCommand.h"
 #import "HMKaisouLockCommand.h"
-#import "HMSlotResetCommand.h"
 #import "HMResetSortieCommand.h"
 #import "HMDropShipHistoryCommand.h"
 #import "HMDummyShipCommand.h"