2 // HMFleetInformation.m
5 // Created by Hori,Masaki on 2014/09/28.
6 // Copyright (c) 2014年 Hori,Masaki. All rights reserved.
9 #import "HMFleetInformation.h"
11 #import "HMServerDataStore.h"
12 #import "HMKCShipObject+Extensions.h"
13 #import "HMKCSlotItemObject+Extensions.h"
14 #import "HMKCMasterSlotItemObject.h"
15 #import "HMKCDeck+Extension.h"
21 @interface HMFleetInformation ()
22 @property (strong) NSArray *shipNameKeys;
24 @property (strong) NSArrayController *deckController;
26 @property (strong) id selectedFleet;
29 @property (nonatomic, strong) NSString *name;
31 @property (readwrite, strong) HMKCShipObject *flagShip;
32 @property (readwrite, strong) HMKCShipObject *secondShip;
33 @property (readwrite, strong) HMKCShipObject *thirdShip;
34 @property (readwrite, strong) HMKCShipObject *fourthShip;
35 @property (readwrite, strong) HMKCShipObject *fifthShip;
36 @property (readwrite, strong) HMKCShipObject *sixthShip;
39 @implementation HMFleetInformation
40 @synthesize selectedFleetNumber = _selectedFleetNumber;
46 _shipNameKeys = @[@"flagShip", @"secondShip", @"thirdShip",
47 @"fourthShip", @"fifthShip", @"sixthShip"];
49 _deckController = [NSArrayController new];
50 [_deckController setEntityName:@"Deck"];
51 [_deckController setManagedObjectContext:[[HMServerDataStore defaultManager] managedObjectContext]];
52 [_deckController setAvoidsEmptySelection:YES];
53 [_deckController setPreservesSelection:YES];
54 [_deckController setAutomaticallyPreparesContent:YES];
55 [_deckController setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"id" ascending:YES]]];
57 [_deckController fetchWithRequest:nil merge:NO error:&error];
62 [self.deckController addObserver:self
63 forKeyPath:@"selection.ship_0"
66 [self.deckController addObserver:self
67 forKeyPath:@"selection.ship_1"
70 [self.deckController addObserver:self
71 forKeyPath:@"selection.ship_2"
74 [self.deckController addObserver:self
75 forKeyPath:@"selection.ship_3"
78 [self.deckController addObserver:self
79 forKeyPath:@"selection.ship_4"
82 [self.deckController addObserver:self
83 forKeyPath:@"selection.ship_5"
87 self.selectedFleetNumber = @1;
92 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
94 id contextObject = (__bridge id)context;
96 if([contextObject isKindOfClass:[NSString class]]) {
98 [self changeShipWithNumber:[contextObject integerValue]];
103 [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
106 - (HMKCShipObject *)shipForID:(NSInteger)shipId inStore:(HMCoreDataManager *)store
108 HMKCShipObject *ship = nil;
109 NSError *error = nil;
110 NSArray *array = nil;
112 array = [store objectsWithEntityName:@"Ship"
114 predicateFormat:@"id = %ld", shipId];
115 if(array.count == 0) {
116 NSLog(@"Could not found ship of id %ld", shipId);
125 - (void)changeShipWithNumber:(NSInteger)shipNumber
128 HMServerDataStore *store = [HMServerDataStore defaultManager];
129 NSString *key = [NSString stringWithFormat:@"ship_%ld", shipNumber];
130 NSNumber *shipIdNumber = [self.selectedFleet valueForKey:key];
131 NSInteger shipId = [shipIdNumber integerValue];
132 ship = [self shipForID:shipId inStore:store];
134 [self setValue:ship forKey:self.shipNameKeys[shipNumber]];
138 for(id key in self.shipNameKeys) {
139 [self setValue:nil forKey:key];
143 NSInteger fleetId = [self.selectedFleetNumber integerValue];
144 if(fleetId > 6 || fleetId < 0) {
148 NSArray *decks = [self.deckController arrangedObjects];
149 if(decks.count == 0) return;
150 self.selectedFleet = [decks objectAtIndex:fleetId-1];
151 self.name = [self.selectedFleet valueForKey:@"name"];
153 for(NSInteger i = 0; i < 6; i++) {
154 [self changeShipWithNumber:i];
158 + (NSSet *)keyPathsForValuesAffectingTotalSakuteki
160 return [NSSet setWithObjects:
161 @"flagShip", @"secondShip", @"thirdShip",
162 @"fourthShip", @"fifthShip", @"sixthShip",
163 @"flagShip.sakuteki_0",
164 @"secondShip.sakuteki_0",
165 @"thirdShip.sakuteki_0",
166 @"fourthShip.sakuteki_0",
167 @"fifthShip.sakuteki_0",
168 @"sixthShip.sakuteki_0",
171 + (NSSet *)keyPathsForValuesAffectingTotalSeiku
173 return [NSSet setWithObjects:
174 @"flagShip", @"secondShip", @"thirdShip",
175 @"fourthShip", @"fifthShip", @"sixthShip",
176 @"flagShip.onslot_0",
177 @"flagShip.onslot_1",
178 @"flagShip.onslot_2",
179 @"flagShip.onslot_3",
180 @"flagShip.onslot_4",
182 @"secondShip.onslot_0",
183 @"secondShip.onslot_1",
184 @"secondShip.onslot_2",
185 @"secondShip.onslot_3",
186 @"secondShip.onslot_4",
188 @"thirdShip.onslot_0",
189 @"thirdShip.onslot_1",
190 @"thirdShip.onslot_2",
191 @"thirdShip.onslot_3",
192 @"thirdShip.onslot_4",
194 @"fourthShip.onslot_0",
195 @"fourthShip.onslot_1",
196 @"fourthShip.onslot_2",
197 @"fourthShip.onslot_3",
198 @"fourthShip.onslot_4",
200 @"fifthShip.onslot_0",
201 @"fifthShip.onslot_1",
202 @"fifthShip.onslot_2",
203 @"fifthShip.onslot_3",
204 @"fifthShip.onslot_4",
206 @"sixthShip.onslot_0",
207 @"sixthShip.onslot_1",
208 @"sixthShip.onslot_2",
209 @"sixthShip.onslot_3",
210 @"sixthShip.onslot_4",
212 @"flagShip.equippedItem",
213 @"secondShip.equippedItem",
214 @"thirdShip.equippedItem",
215 @"fourthShip.equippedItem",
216 @"fifthShip.equippedItem",
217 @"sixthShip.equippedItem",
222 - (void)setSelectedFleetNumber:(NSNumber *)fleetNumber
224 _selectedFleetNumber = fleetNumber;
225 self.deckController.selectionIndex = [fleetNumber integerValue] - 1;
229 - (NSNumber *)totalSakuteki
232 total += [self.flagShip.sakuteki_0 integerValue];
233 total += [self.secondShip.sakuteki_0 integerValue];
234 total += [self.thirdShip.sakuteki_0 integerValue];
235 total += [self.fourthShip.sakuteki_0 integerValue];
236 total += [self.fifthShip.sakuteki_0 integerValue];
237 total += [self.sixthShip.sakuteki_0 integerValue];
242 - (NSUInteger)seikuAtIndex:(NSUInteger)index
248 HMKCShipObject *ship = nil;
249 HMServerDataStore *store = [HMServerDataStore defaultManager];
250 NSString *key = [NSString stringWithFormat:@"ship_%ld", index];
251 NSNumber *shipIdNumber = [self.selectedFleet valueForKey:key];
252 if(!shipIdNumber) return 0;
253 NSInteger shipId = [shipIdNumber integerValue];
254 ship = [self shipForID:shipId inStore:store];
257 return ship.seiku.integerValue;
260 - (NSNumber *)totalSeiku
262 NSUInteger totalSeiku = 0;
263 for(NSUInteger i = 0; i < 6; i++) {
264 totalSeiku += [self seikuAtIndex:i];
267 return @(totalSeiku);
270 - (HMKCDeck *)fleetAtIndex:(NSInteger)fleetNumner
272 NSArray *decks = [self.deckController arrangedObjects];
273 if(fleetNumner == 0 || decks.count < fleetNumner) return nil;
274 return decks[fleetNumner - 1];
276 - (NSArray *)fleetMemberAtIndex:(NSInteger)fleetNumber
278 HMKCDeck *deck = [self fleetAtIndex:fleetNumber];
279 NSMutableArray *result = [NSMutableArray arrayWithCapacity:6];
281 HMServerDataStore *store = [HMServerDataStore defaultManager];
282 for(NSInteger i = 0; i < 6; i++) {
283 NSString *key = [NSString stringWithFormat:@"ship_%ld", i];
284 NSNumber *shipIdNumber = [deck valueForKey:key];
285 NSInteger shipId = [shipIdNumber integerValue];
286 if(shipId == -1) continue;
287 HMKCShipObject *ship = [self shipForID:shipId inStore:store];
289 [result addObject:ship];