2 // HMMemberMaterialCommand.m
5 // Created by Hori,Masaki on 2014/02/22.
6 // Copyright (c) 2014年 Hori,Masaki. All rights reserved.
9 #import "HMMemberMaterialCommand.h"
11 #import "HMServerDataStore.h"
13 @implementation HMMemberMaterialCommand
16 static dispatch_once_t onceToken;
17 dispatch_once(&onceToken, ^{
18 [HMJSONCommand registerClass:self];
22 + (BOOL)canExcuteAPI:(NSString *)api
24 if([api isEqualToString:@"/kcsapi/api_get_member/material"]) return YES;
25 // if([api isEqualToString:@"/kcsapi/api_req_hokyu/charge"]) return YES;
30 if([self.api isEqualToString:@"/kcsapi/api_port/port"]
31 || [self.api isEqualToString:@"/kcsapi/api_req_kousyou/createitem"]
32 || [self.api isEqualToString:@"/kcsapi/api_req_kousyou/destroyship"]
33 || [self.api isEqualToString:@"/kcsapi/api_req_hokyu/charge"]) {
34 return @"api_data.api_material";
36 if([self.api isEqualToString:@"/kcsapi/api_req_kousyou/remodel_slot"]) {
37 return @"api_data.api_after_material";
39 return [super dataKey];
43 NSArray *api_data = [self.json valueForKeyPath:self.dataKey];
44 if(![api_data isKindOfClass:[NSArray class]]) {
45 [self log:@"%@ is NOT NSArray.", self.dataKey];
49 HMServerDataStore *serverDataStore = [HMServerDataStore oneTimeEditor];
50 NSManagedObjectContext *managedObjectContext = [serverDataStore managedObjectContext];
53 id result = [serverDataStore objectsWithEntityName:@"Material" predicate:nil error:&error];
55 [self log:@"Fetch error: %@", error];
58 NSManagedObject *object = nil;
59 if(!result || [result count] == 0) {
60 object = [NSEntityDescription insertNewObjectForEntityForName:@"Material"
61 inManagedObjectContext:managedObjectContext];
66 NSArray *keys = @[@"fuel", @"bull", @"steel", @"bauxite", @"kousokukenzo", @"kousokushuhuku", @"kaihatusizai", @"screw"];
69 for(id dict in api_data) {
70 if([dict isKindOfClass:[NSDictionary class]]) {
71 NSNumber *idValue = [dict valueForKey:@"api_id"];
72 NSString *key = keys[[idValue integerValue] - 1];
73 if([key isEqualToString:@"DUMMY"]) continue;
74 [object setValue:[dict valueForKey:@"api_value"] forKey:key];
76 NSString *key = keys[i++];
77 if([key isEqualToString:@"DUMMY"]) continue;
78 [object setValue:@([dict integerValue]) forKey:key];