OSDN Git Service

UIを調整
[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
16 @implementation HMDropShipHistoryCommand
17
18 - (NSManagedObject *)battle
19 {
20         HMTemporaryDataStore *store = [HMTemporaryDataStore defaultManager];
21         NSError *error = nil;
22         NSArray *array  = [store objectsWithEntityName:@"Battle" predicate:nil error:&error];
23         if(error) {
24                 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
25         }
26         return array.count > 0 ? array[0] : nil;
27 }
28 - (void)execute
29 {
30         if([self.api isEqualToString:@"/kcsapi/api_port/port"]
31            || [self.api isEqualToString:@"/kcsapi/api_get_member/ship2"]) {
32                 [self storeToVisible];
33         }
34         if(![self.api hasSuffix:@"battleresult"]) return;
35         
36         id data = [self.json valueForKey:@"api_data"];
37         id getShip = [data valueForKey:@"api_get_ship"];
38         if(!getShip || [getShip isKindOfClass:[NSNull class]]) return;
39         
40         id battle = [self battle];
41         if(!battle) {
42                 NSLog(@"Can not get battle object");
43                 return;
44         }
45         
46         id mapAreaId = [battle valueForKey:@"mapArea"];
47         id mapInfoId = [battle valueForKey:@"mapInfo"];
48         id mapCellNo = [battle valueForKey:@"no"];
49         
50         HMServerDataStore *store = [HMServerDataStore defaultManager];
51         NSError *error = nil;
52         NSArray *array = [store objectsWithEntityName:@"MasterMapInfo"
53                                                                                         error:&error
54                                                                   predicateFormat:@"maparea_id = %@ AND %K = %@", mapAreaId, @"no", mapInfoId];
55         if(error) {
56                 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
57         }
58         if(array.count == 0) {
59                 NSLog(@"%s error: Can not get mapInfo", __PRETTY_FUNCTION__);
60                 return;
61         }
62         id mapInfoName = [array[0] valueForKey:@"name"];
63         
64         error = nil;
65         array = [store objectsWithEntityName:@"MasterMapArea"
66                                                                    error:&error
67                                                  predicateFormat:@"id = %@", mapAreaId];
68         if(error) {
69                 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
70         }
71         if(array.count == 0) {
72                 NSLog(@"%s error: Can not get mapArea", __PRETTY_FUNCTION__);
73                 return;
74         }
75         id mapAreaName = [array[0] valueForKey:@"name"];
76         
77         HMLocalDataStore *lds = [HMLocalDataStore oneTimeEditor];
78         HMDropShipHistory *newObejct = [NSEntityDescription insertNewObjectForEntityForName:@"HiddenDropShipHistory"
79                                                                                                                                 inManagedObjectContext:[lds managedObjectContext]];
80         
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];
89         
90         [lds saveAction:nil];
91 }
92
93 - (void)storeToVisible
94 {
95         HMLocalDataStore *lds = [HMLocalDataStore oneTimeEditor];
96         NSError *error = nil;
97         NSArray *array = [lds objectsWithEntityName:@"HiddenDropShipHistory"
98                                                                           predicate:nil
99                                                                                   error:&error];
100         if(error) {
101                 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
102         }
103         NSManagedObjectContext *context = lds.managedObjectContext;
104         for(HMDropShipHistory *history in array) {
105                 HMDropShipHistory *newObejct = [NSEntityDescription insertNewObjectForEntityForName:@"DropShipHistory"
106                                                                                                                                          inManagedObjectContext:[lds managedObjectContext]];
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                 [context deleteObject:history];
118         }
119         
120         [lds saveAction:nil];
121 }
122 @end