2 // HMDropShipHistoryCommand.m
5 // Created by Hori,Masaki on 2015/02/18.
6 // Copyright (c) 2015年 Hori,Masaki. All rights reserved.
9 #import "HMDropShipHistoryCommand.h"
11 #import "HMServerDataStore.h"
12 #import "HMLocalDataStore.h"
13 #import "HMTemporaryDataStore.h"
14 #import "HMDropShipHistory.h"
16 @implementation HMDropShipHistoryCommand
18 - (NSManagedObject *)battle
20 HMTemporaryDataStore *store = [HMTemporaryDataStore defaultManager];
22 NSArray *array = [store objectsWithEntityName:@"Battle" predicate:nil error:&error];
24 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
26 return array.count > 0 ? array[0] : nil;
30 if([self.api isEqualToString:@"/kcsapi/api_port/port"]
31 || [self.api isEqualToString:@"/kcsapi/api_get_member/ship2"]) {
32 [self storeToVisible];
34 if(![self.api hasSuffix:@"battleresult"]) return;
36 id data = [self.json valueForKey:@"api_data"];
37 id getShip = [data valueForKey:@"api_get_ship"];
38 if(!getShip || [getShip isKindOfClass:[NSNull class]]) return;
40 id battle = [self battle];
42 NSLog(@"Can not get battle object");
46 id mapAreaId = [battle valueForKey:@"mapArea"];
47 id mapInfoId = [battle valueForKey:@"mapInfo"];
48 id mapCellNo = [battle valueForKey:@"no"];
50 HMServerDataStore *store = [HMServerDataStore defaultManager];
52 NSArray *array = [store objectsWithEntityName:@"MasterMapInfo"
54 predicateFormat:@"maparea_id = %@ AND %K = %@", mapAreaId, @"no", mapInfoId];
56 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
58 if(array.count == 0) {
59 NSLog(@"%s error: Can not get mapInfo", __PRETTY_FUNCTION__);
62 id mapInfoName = [array[0] valueForKey:@"name"];
65 array = [store objectsWithEntityName:@"MasterMapArea"
67 predicateFormat:@"id = %@", mapAreaId];
69 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
71 if(array.count == 0) {
72 NSLog(@"%s error: Can not get mapArea", __PRETTY_FUNCTION__);
75 id mapAreaName = [array[0] valueForKey:@"name"];
77 HMLocalDataStore *lds = [HMLocalDataStore oneTimeEditor];
78 HMDropShipHistory *newObejct = [NSEntityDescription insertNewObjectForEntityForName:@"HiddenDropShipHistory"
79 inManagedObjectContext:[lds managedObjectContext]];
81 newObejct.shipName = [getShip valueForKey:@"api_ship_name"];
82 newObejct.mapArea = [NSString stringWithFormat:@"%@", mapAreaId];
83 newObejct.mapAreaName = mapAreaName;
84 newObejct.mapInfo = mapInfoId;
85 newObejct.mapInfoName = mapInfoName;
86 newObejct.mapCell = mapCellNo;
87 newObejct.winRank = [data valueForKey:@"api_win_rank"];
88 newObejct.date = [NSDate dateWithTimeIntervalSinceNow:0];
93 - (void)storeToVisible
95 HMLocalDataStore *lds = [HMLocalDataStore oneTimeEditor];
97 NSArray *array = [lds objectsWithEntityName:@"HiddenDropShipHistory"
101 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
103 NSManagedObjectContext *context = lds.managedObjectContext;
104 for(HMDropShipHistory *history in array) {
105 HMDropShipHistory *newObejct = [NSEntityDescription insertNewObjectForEntityForName:@"DropShipHistory"
106 inManagedObjectContext:[lds managedObjectContext]];
108 newObejct.shipName = history.shipName;
109 newObejct.mapArea = history.mapArea;
110 newObejct.mapAreaName = history.mapAreaName;
111 newObejct.mapInfo = history.mapInfo;
112 newObejct.mapInfoName = history.mapInfoName;
113 newObejct.mapCell = history.mapCell;
114 newObejct.winRank = history.winRank;
115 newObejct.date = history.date;
117 [context deleteObject:history];
120 [lds saveAction:nil];