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"
15 #import "HMKCBattle.h"
16 #import "HMKCMasterMapInfo.h"
17 #import "HMKCMasterMapArea.h"
19 @implementation HMDropShipHistoryCommand
21 - (HMKCBattle *)battle
23 HMTemporaryDataStore *store = [HMTemporaryDataStore defaultManager];
25 NSArray<HMKCBattle *> *battles = [store objectsWithEntityName:@"Battle" predicate:nil error:&error];
27 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
29 return battles.count > 0 ? battles[0] : nil;
33 if([self.api isEqualToString:@"/kcsapi/api_port/port"]
34 || [self.api isEqualToString:@"/kcsapi/api_get_member/ship_deck"]) {
35 [self storeToVisible];
37 if(![self.api hasSuffix:@"battleresult"]) return;
39 id data = [self.json valueForKey:@"api_data"];
40 id getShip = [data valueForKey:@"api_get_ship"];
41 if(!getShip || [getShip isKindOfClass:[NSNull class]]) return;
43 HMKCBattle *battle = [self battle];
45 NSLog(@"Can not get battle object");
49 NSNumber *mapAreaId = battle.mapArea;
50 NSNumber *mapInfoId = battle.mapInfo;
51 NSNumber *mapCellNo = battle.no;
53 HMServerDataStore *store = [HMServerDataStore defaultManager];
55 NSArray<HMKCMasterMapInfo *> *mapInfos = [store objectsWithEntityName:@"MasterMapInfo"
57 predicateFormat:@"maparea_id = %@ AND %K = %@", mapAreaId, @"no", mapInfoId];
59 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
61 if(mapInfos.count == 0) {
62 NSLog(@"%s error: Can not get mapInfo", __PRETTY_FUNCTION__);
65 NSString *mapInfoName = mapInfos[0].name;
68 NSArray<HMKCMasterMapArea *> *mapAreas = [store objectsWithEntityName:@"MasterMapArea"
70 predicateFormat:@"id = %@", mapAreaId];
72 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
74 if(mapAreas.count == 0) {
75 NSLog(@"%s error: Can not get mapArea", __PRETTY_FUNCTION__);
78 NSString *mapAreaName = mapAreas[0].name;
80 HMLocalDataStore *lds = [HMLocalDataStore oneTimeEditor];
81 HMDropShipHistory *newObejct = [lds insertNewObjectForEntityForName:@"HiddenDropShipHistory"];
83 newObejct.shipName = [getShip valueForKey:@"api_ship_name"];
84 newObejct.mapArea = [NSString stringWithFormat:@"%@", mapAreaId];
85 newObejct.mapAreaName = mapAreaName;
86 newObejct.mapInfo = mapInfoId;
87 newObejct.mapInfoName = mapInfoName;
88 newObejct.mapCell = mapCellNo;
89 newObejct.winRank = [data valueForKey:@"api_win_rank"];
90 newObejct.date = [NSDate dateWithTimeIntervalSinceNow:0];
95 - (void)storeToVisible
97 HMLocalDataStore *lds = [HMLocalDataStore oneTimeEditor];
99 NSArray<HMDropShipHistory *> *dropShipHistories = [lds objectsWithEntityName:@"HiddenDropShipHistory"
103 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
105 for(HMDropShipHistory *history in dropShipHistories) {
106 HMDropShipHistory *newObejct = [lds insertNewObjectForEntityForName:@"DropShipHistory"];
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 [lds deleteObject:history];
120 [lds saveAction:nil];