OSDN Git Service

deleteObject:をHMCoreDataManagerで行うように変更
[kcd/KCD.git] / KCD / HMDropShipHistoryCommand.m
1 //
2 //  HMDropShipHistoryCommand.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2015/02/18.
6 //  Copyright (c) 2015年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMDropShipHistoryCommand.h"
10
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"
18
19 @implementation HMDropShipHistoryCommand
20
21 - (HMKCBattle *)battle
22 {
23         HMTemporaryDataStore *store = [HMTemporaryDataStore defaultManager];
24         NSError *error = nil;
25         NSArray<HMKCBattle *> *battles  = [store objectsWithEntityName:@"Battle" predicate:nil error:&error];
26         if(error) {
27                 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
28         }
29         return battles.count > 0 ? battles[0] : nil;
30 }
31 - (void)execute
32 {
33         if([self.api isEqualToString:@"/kcsapi/api_port/port"]
34            || [self.api isEqualToString:@"/kcsapi/api_get_member/ship_deck"]) {
35                 [self storeToVisible];
36         }
37         if(![self.api hasSuffix:@"battleresult"]) return;
38         
39         id data = [self.json valueForKey:@"api_data"];
40         id getShip = [data valueForKey:@"api_get_ship"];
41         if(!getShip || [getShip isKindOfClass:[NSNull class]]) return;
42         
43         HMKCBattle *battle = [self battle];
44         if(!battle) {
45                 NSLog(@"Can not get battle object");
46                 return;
47         }
48         
49         NSNumber *mapAreaId = battle.mapArea;
50         NSNumber *mapInfoId = battle.mapInfo;
51         NSNumber *mapCellNo = battle.no;
52         
53         HMServerDataStore *store = [HMServerDataStore defaultManager];
54         NSError *error = nil;
55         NSArray<HMKCMasterMapInfo *> *mapInfos = [store objectsWithEntityName:@"MasterMapInfo"
56                                                                                                                                         error:&error
57                                                                                                                   predicateFormat:@"maparea_id = %@ AND %K = %@", mapAreaId, @"no", mapInfoId];
58         if(error) {
59                 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
60         }
61         if(mapInfos.count == 0) {
62                 NSLog(@"%s error: Can not get mapInfo", __PRETTY_FUNCTION__);
63                 return;
64         }
65         NSString *mapInfoName = mapInfos[0].name;
66         
67         error = nil;
68         NSArray<HMKCMasterMapArea *> *mapAreas = [store objectsWithEntityName:@"MasterMapArea"
69                                                                                                                                  error:&error
70                                                                                                            predicateFormat:@"id = %@", mapAreaId];
71         if(error) {
72                 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
73         }
74         if(mapAreas.count == 0) {
75                 NSLog(@"%s error: Can not get mapArea", __PRETTY_FUNCTION__);
76                 return;
77         }
78         NSString *mapAreaName = mapAreas[0].name;
79         
80         HMLocalDataStore *lds = [HMLocalDataStore oneTimeEditor];
81         HMDropShipHistory *newObejct = [lds insertNewObjectForEntityForName:@"HiddenDropShipHistory"];
82         
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];
91         
92         [lds saveAction:nil];
93 }
94
95 - (void)storeToVisible
96 {
97         HMLocalDataStore *lds = [HMLocalDataStore oneTimeEditor];
98         NSError *error = nil;
99         NSArray<HMDropShipHistory *> *dropShipHistories = [lds objectsWithEntityName:@"HiddenDropShipHistory"
100                                                                                                                                            predicate:nil
101                                                                                                                                                    error:&error];
102         if(error) {
103                 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
104         }
105         for(HMDropShipHistory *history in dropShipHistories) {
106                 HMDropShipHistory *newObejct = [lds insertNewObjectForEntityForName:@"DropShipHistory"];
107                 
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;
116                 
117                 [lds deleteObject:history];
118         }
119         
120         [lds saveAction:nil];
121 }
122 @end