2 // HMDocksViewController.m
5 // Created by Hori,Masaki on 2014/02/20.
6 // Copyright (c) 2014年 Hori,Masaki. All rights reserved.
9 #import "HMDocksViewController.h"
11 #import "HMAppDelegate.h"
13 #import "HMServerDataStore.h"
15 #import "HMMissionStatus.h"
16 #import "HMNyukyoDockStatus.h"
17 #import "HMKenzoDockStatus.h"
19 #import "HMQuestListViewController.h"
21 #import "HMTemporaryDataStore.h"
24 @interface HMDocksViewController ()
26 @property (strong) HMMissionStatus *mission2Status;
27 @property (strong) HMMissionStatus *mission3Status;
28 @property (strong) HMMissionStatus *mission4Status;
30 @property (strong) HMNyukyoDockStatus *ndock1Status;
31 @property (strong) HMNyukyoDockStatus *ndock2Status;
32 @property (strong) HMNyukyoDockStatus *ndock3Status;
33 @property (strong) HMNyukyoDockStatus *ndock4Status;
35 @property (strong) HMKenzoDockStatus *kdock1Status;
36 @property (strong) HMKenzoDockStatus *kdock2Status;
37 @property (strong) HMKenzoDockStatus *kdock3Status;
38 @property (strong) HMKenzoDockStatus *kdock4Status;
41 @property (readonly) NSManagedObjectContext *battleManagedObjectController;
42 @property (nonatomic, weak) IBOutlet NSObjectController *battleContoller;
43 @property (readonly) NSManagedObject *battle;
45 @property (nonatomic, weak) IBOutlet NSView *questListViewPlaceholder;
47 @property (nonatomic, strong) HMQuestListViewController *questListViewController;
50 @property (nonatomic, weak) IBOutlet NSTextField *cellNumberField;
51 @property (readonly) NSNumber *cellNumber;
55 @implementation HMDocksViewController
58 - (NSManagedObjectContext *)managedObjectContext
60 return [HMServerDataStore defaultManager].managedObjectContext;
65 self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil];
67 _mission2Status = [[HMMissionStatus alloc] initWithDeckNumber:2];
68 _mission2Status.managedObjectContext = self.managedObjectContext;
69 [self bind:@"deck2Time" toObject:self.mission2Status withKeyPath:@"time" options:nil];
70 [self bind:@"mission2Name" toObject:self.mission2Status withKeyPath:@"name" options:nil];
72 _mission3Status = [[HMMissionStatus alloc] initWithDeckNumber:3];
73 _mission3Status.managedObjectContext = self.managedObjectContext;
74 [self bind:@"deck3Time" toObject:self.mission3Status withKeyPath:@"time" options:nil];
75 [self bind:@"mission3Name" toObject:self.mission3Status withKeyPath:@"name" options:nil];
77 _mission4Status = [[HMMissionStatus alloc] initWithDeckNumber:4];
78 _mission4Status.managedObjectContext = self.managedObjectContext;
79 [self bind:@"deck4Time" toObject:self.mission4Status withKeyPath:@"time" options:nil];
80 [self bind:@"mission4Name" toObject:self.mission4Status withKeyPath:@"name" options:nil];
84 _ndock1Status = [[HMNyukyoDockStatus alloc] initWithDockNumber:1];
85 _ndock1Status.managedObjectContext = self.managedObjectContext;
86 [self bind:@"nDock1Time" toObject:self.ndock1Status withKeyPath:@"time" options:nil];
87 [self bind:@"nDock1ShipName" toObject:self.ndock1Status withKeyPath:@"name" options:nil];
89 _ndock2Status = [[HMNyukyoDockStatus alloc] initWithDockNumber:2];
90 _ndock2Status.managedObjectContext = self.managedObjectContext;
91 [self bind:@"nDock2Time" toObject:self.ndock2Status withKeyPath:@"time" options:nil];
92 [self bind:@"nDock2ShipName" toObject:self.ndock2Status withKeyPath:@"name" options:nil];
94 _ndock3Status = [[HMNyukyoDockStatus alloc] initWithDockNumber:3];
95 _ndock3Status.managedObjectContext = self.managedObjectContext;
96 [self bind:@"nDock3Time" toObject:self.ndock3Status withKeyPath:@"time" options:nil];
97 [self bind:@"nDock3ShipName" toObject:self.ndock3Status withKeyPath:@"name" options:nil];
99 _ndock4Status = [[HMNyukyoDockStatus alloc] initWithDockNumber:4];
100 _ndock4Status.managedObjectContext = self.managedObjectContext;
101 [self bind:@"nDock4Time" toObject:self.ndock4Status withKeyPath:@"time" options:nil];
102 [self bind:@"nDock4ShipName" toObject:self.ndock4Status withKeyPath:@"name" options:nil];
106 _kdock1Status = [[HMKenzoDockStatus alloc] initWithDockNumber:1];
107 _kdock1Status.managedObjectContext = self.managedObjectContext;
108 [self bind:@"kDock1Time" toObject:self.kdock1Status withKeyPath:@"time" options:nil];
110 _kdock2Status = [[HMKenzoDockStatus alloc] initWithDockNumber:2];
111 _kdock2Status.managedObjectContext = self.managedObjectContext;
112 [self bind:@"kDock2Time" toObject:self.kdock2Status withKeyPath:@"time" options:nil];
114 _kdock3Status = [[HMKenzoDockStatus alloc] initWithDockNumber:3];
115 _kdock3Status.managedObjectContext = self.managedObjectContext;
116 [self bind:@"kDock3Time" toObject:self.kdock3Status withKeyPath:@"time" options:nil];
118 _kdock4Status = [[HMKenzoDockStatus alloc] initWithDockNumber:4];
119 _kdock4Status.managedObjectContext = self.managedObjectContext;
120 [self bind:@"kDock4Time" toObject:self.kdock4Status withKeyPath:@"time" options:nil];
128 HMAppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
129 [appDelegate addCounterUpdateBlock:^{
131 [self.ndock1Status update];
132 [self.ndock2Status update];
133 [self.ndock3Status update];
134 [self.ndock4Status update];
137 [self.kdock1Status update];
138 [self.kdock2Status update];
139 [self.kdock3Status update];
140 [self.kdock4Status update];
143 [self.mission2Status update];
144 [self.mission3Status update];
145 [self.mission4Status update];
148 _questListViewController = [HMQuestListViewController new];
150 [self.questListViewController.view setFrame:[self.questListViewPlaceholder frame]];
151 [self.questListViewController.view setAutoresizingMask:[self.questListViewPlaceholder autoresizingMask]];
152 [[self.questListViewPlaceholder superview] replaceSubview:self.questListViewPlaceholder with:self.questListViewController.view];
155 [self.battleContoller addObserver:self
156 forKeyPath:@"selection"
159 [self.battleContoller addObserver:self
160 forKeyPath:@"selection.no"
163 [self.battleContoller addObserver:self
164 forKeyPath:@"content.battleCell"
169 self.cellNumberField.hidden = NO;
173 - (NSManagedObjectContext *)battleManagedObjectController
175 return [[HMTemporaryDataStore defaultManager] managedObjectContext];
177 - (NSManagedObject *)battle
179 HMTemporaryDataStore *store = [HMTemporaryDataStore defaultManager];
180 NSError *error = nil;
181 NSArray *array = [store objectsWithEntityName:@"Battle" predicate:nil error:&error];
183 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
185 return array.count > 0 ? array[0] : nil;
188 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
190 if([keyPath isEqualToString:@"selection"] || [keyPath isEqualToString:@"content.battleCell"] ) {
191 [self willChangeValueForKey:@"sortieString"];
192 [self didChangeValueForKey:@"sortieString"];
195 if([keyPath isEqualToString:@"selection.no"]) {
196 [self willChangeValueForKey:@"cellNumber"];
197 [self didChangeValueForKey:@"cellNumber"];
201 return [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
204 - (NSString *)sortieString
206 NSString *result = nil;
207 if(self.fleetName && self.areaName && self.areaNumber) {
208 if(self.battleCellNumber.integerValue == 0) {
209 NSString *format = NSLocalizedString(@"%@ in sortie into %@ (%@)", @"Sortie");
210 result = [NSString stringWithFormat:format, self.fleetName, self.areaName, self.areaNumber];
212 NSString *format = NSLocalizedString(@"%@ battle at %@ war zone in %@ (%@) now", @"Sortie");
213 result = [NSString stringWithFormat:format, self.fleetName, self.battleCellNumber, self.areaName, self.areaNumber];
219 - (NSString *)fleetName
221 HMServerDataStore *store = [HMServerDataStore defaultManager];
223 NSError *error = nil;
224 id deckId = [self.battleContoller valueForKeyPath:@"content.deckId"];
225 NSArray *array = [store objectsWithEntityName:@"Deck"
227 predicateFormat:@"id = %@", deckId];
229 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
231 if(array.count == 0) return nil;
233 return [NSString stringWithFormat:@"%@", [array[0] valueForKey:@"name"]];
235 - (NSString *)areaNumber
237 id mapArea = [self.battleContoller valueForKeyPath:@"content.mapArea"];
238 if([mapArea integerValue] > 10) {
241 return [NSString stringWithFormat:@"%@-%@",
243 [self.battleContoller valueForKeyPath:@"content.mapInfo"]];
245 - (NSString *)areaName
247 HMServerDataStore *store = [HMServerDataStore defaultManager];
249 NSError *error = nil;
250 id mapAreaId = [self.battleContoller valueForKeyPath:@"content.mapArea"];
251 id mapInfoId = [self.battleContoller valueForKeyPath:@"content.mapInfo"];
252 NSArray *array = [store objectsWithEntityName:@"MasterMapInfo"
254 predicateFormat:@"maparea_id = %@ AND %K = %@", mapAreaId, @"no", mapInfoId];
256 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
258 if(array.count == 0) return nil;
260 return [NSString stringWithFormat:@"%@", [array[0] valueForKey:@"name"]];
263 - (NSNumber *)cellNumber
265 return [self.battleContoller valueForKeyPath:@"content.no"];
268 - (NSNumber *)battleCellNumber
270 return [self.battleContoller valueForKeyPath:@"content.battleCell"];