OSDN Git Service

004bff17059f5099a6f1a46069f7b7f7d210d5e8
[kcd/KCD.git] / KCD / HMDestroyItem2Command.m
1 //
2 //  HMDestroyItem2Command.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2014/04/29.
6 //  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMDestroyItem2Command.h"
10
11 #import "HMServerDataStore.h"
12
13
14 @implementation HMDestroyItem2Command
15 + (void)load
16 {
17         static dispatch_once_t onceToken;
18         dispatch_once(&onceToken, ^{
19                 [HMJSONCommand registerClass:self];
20         });
21 }
22
23 + (BOOL)canExcuteAPI:(NSString *)api
24 {
25         if([api isEqualToString:@"/kcsapi/api_req_kousyou/destroyitem2"]) return YES;
26         
27         return NO;
28 }
29 - (void)execute
30 {
31         HMServerDataStore *store = [HMServerDataStore oneTimeEditor];
32         NSManagedObjectContext *moc = store.managedObjectContext;
33         
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])];
39         }
40         
41         NSError *error = nil;
42         NSArray *array = [store objectsWithEntityName:@"SlotItem"
43                                                                                    error:&error
44                                                                  predicateFormat:@"id IN %@", items];
45         if([array count] == 0) {
46                 NSLog(@"SlotItem is invalid.");
47                 return;
48         }
49         
50         for(id obj in array) {
51                 [moc deleteObject:obj];
52         }
53         
54         //
55         error = nil;
56         array = [store objectsWithEntityName:@"Material" predicate:nil error:&error];
57         if(error) {
58                 [self log:@"Fetch error: %@", error];
59                 return;
60         }
61         if([array count] == 0) {
62                 NSLog(@"SlotItem is invalid.");
63                 return;
64         }
65         
66         id material = array[0];
67         NSArray *keys = @[@"fuel", @"bull", @"steel", @"bauxite", @"kousokukenzo", @"kousokushuhuku", @"kaihatusizai", @"screw"];
68         
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]];
74         }
75 }
76 @end