2 // HMDestroyItem2Command.m
5 // Created by Hori,Masaki on 2014/04/29.
6 // Copyright (c) 2014年 Hori,Masaki. All rights reserved.
9 #import "HMDestroyItem2Command.h"
11 #import "HMServerDataStore.h"
14 @implementation HMDestroyItem2Command
17 static dispatch_once_t onceToken;
18 dispatch_once(&onceToken, ^{
19 [HMJSONCommand registerClass:self];
23 + (BOOL)canExcuteAPI:(NSString *)api
25 if([api isEqualToString:@"/kcsapi/api_req_kousyou/destroyitem2"]) return YES;
31 HMServerDataStore *store = [HMServerDataStore oneTimeEditor];
32 NSManagedObjectContext *moc = store.managedObjectContext;
34 NSString *itemsString = self.arguments[@"api_slotitem_ids"];
35 NSArray *itemsStrings = [itemsString componentsSeparatedByString:@","];
36 NSMutableArray *items = [NSMutableArray new];
37 for(id item in itemsStrings) {
38 [items addObject:@([item integerValue])];
42 NSArray *array = [store objectsWithEntityName:@"SlotItem"
44 predicateFormat:@"id IN %@", items];
45 if([array count] == 0) {
46 NSLog(@"SlotItem is invalid.");
50 for(id obj in array) {
51 [moc deleteObject:obj];
56 array = [store objectsWithEntityName:@"Material" predicate:nil error:&error];
58 [self log:@"Fetch error: %@", error];
61 if([array count] == 0) {
62 NSLog(@"SlotItem is invalid.");
66 id material = array[0];
67 NSArray *keys = @[@"fuel", @"bull", @"steel", @"bauxite", @"kousokukenzo", @"kousokushuhuku", @"kaihatusizai", @"screw"];
69 NSArray *materials = [self.json valueForKeyPath:@"api_data.api_get_material"];
70 for(NSInteger i = 0; i < 4; i++) {
71 NSInteger current = [[material valueForKey:keys[i]] integerValue];
72 NSInteger increase = [materials[i] integerValue];
73 [material setValue:@(current + increase) forKey:keys[i]];