OSDN Git Service

f0188e2d507a56ce8cb25fdf8cb0925007ebad1a
[kcd/KCD.git] / KCD / HMDocksViewController.m
1 //
2 //  HMDocksViewController.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2014/02/20.
6 //  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMDocksViewController.h"
10
11 #import "HMAppDelegate.h"
12
13 #import "HMServerDataStore.h"
14
15 #import "HMMissionStatus.h"
16 #import "HMNyukyoDockStatus.h"
17 #import "HMKenzoDockStatus.h"
18
19 #import "HMQuestListViewController.h"
20
21 #import "HMTemporaryDataStore.h"
22
23
24 @interface HMDocksViewController ()
25
26 @property (strong) HMMissionStatus *mission2Status;
27 @property (strong) HMMissionStatus *mission3Status;
28 @property (strong) HMMissionStatus *mission4Status;
29
30 @property (strong) HMNyukyoDockStatus *ndock1Status;
31 @property (strong) HMNyukyoDockStatus *ndock2Status;
32 @property (strong) HMNyukyoDockStatus *ndock3Status;
33 @property (strong) HMNyukyoDockStatus *ndock4Status;
34
35 @property (strong) HMKenzoDockStatus *kdock1Status;
36 @property (strong) HMKenzoDockStatus *kdock2Status;
37 @property (strong) HMKenzoDockStatus *kdock3Status;
38 @property (strong) HMKenzoDockStatus *kdock4Status;
39
40
41 @property (readonly) NSManagedObjectContext *battleManagedObjectController;
42 @property (nonatomic, weak) IBOutlet NSObjectController *battleContoller;
43 @property (readonly) NSManagedObject *battle;
44
45 @property (nonatomic, weak) IBOutlet NSView *questListViewPlaceholder;
46
47 @property (nonatomic, strong) HMQuestListViewController *questListViewController;
48
49
50 @property (nonatomic, weak) IBOutlet NSTextField *cellNumberField;
51 @property (readonly) NSNumber *cellNumber;
52
53 @end
54
55 @implementation HMDocksViewController
56
57
58 - (NSManagedObjectContext *)managedObjectContext
59 {
60         return [HMServerDataStore defaultManager].managedObjectContext;
61 }
62
63 - (id)init
64 {
65         self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil];
66         if(self) {
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];
71                 
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];
76                 
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];
81                 
82                 
83                 //
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];
88                 
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];
93                 
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];
98                 
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];
103                 
104                 
105                 //
106                 _kdock1Status = [[HMKenzoDockStatus alloc] initWithDockNumber:1];
107                 _kdock1Status.managedObjectContext = self.managedObjectContext;
108                 [self bind:@"kDock1Time" toObject:self.kdock1Status withKeyPath:@"time" options:nil];
109                 
110                 _kdock2Status = [[HMKenzoDockStatus alloc] initWithDockNumber:2];
111                 _kdock2Status.managedObjectContext = self.managedObjectContext;
112                 [self bind:@"kDock2Time" toObject:self.kdock2Status withKeyPath:@"time" options:nil];
113                 
114                 _kdock3Status = [[HMKenzoDockStatus alloc] initWithDockNumber:3];
115                 _kdock3Status.managedObjectContext = self.managedObjectContext;
116                 [self bind:@"kDock3Time" toObject:self.kdock3Status withKeyPath:@"time" options:nil];
117                 
118                 _kdock4Status = [[HMKenzoDockStatus alloc] initWithDockNumber:4];
119                 _kdock4Status.managedObjectContext = self.managedObjectContext;
120                 [self bind:@"kDock4Time" toObject:self.kdock4Status withKeyPath:@"time" options:nil];
121                 
122         }
123         return self;
124 }
125
126 - (void)awakeFromNib
127 {
128         HMAppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
129         [appDelegate addCounterUpdateBlock:^{
130                 // 入渠ドック
131                 [self.ndock1Status update];
132                 [self.ndock2Status update];
133                 [self.ndock3Status update];
134                 [self.ndock4Status update];
135                 
136                 // 建造ドック
137                 [self.kdock1Status update];
138                 [self.kdock2Status update];
139                 [self.kdock3Status update];
140                 [self.kdock4Status update];
141                 
142                 // 遠征
143                 [self.mission2Status update];
144                 [self.mission3Status update];
145                 [self.mission4Status update];
146         }];
147         
148         _questListViewController = [HMQuestListViewController new];
149         
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];
153         
154         //
155         [self.battleContoller addObserver:self
156                                                    forKeyPath:@"selection"
157                                                           options:0
158                                                           context:NULL];
159         [self.battleContoller addObserver:self
160                                                    forKeyPath:@"selection.no"
161                                                           options:0
162                                                           context:NULL];
163         [self.battleContoller addObserver:self
164                                                    forKeyPath:@"content.battleCell"
165                                                           options:0
166                                                           context:NULL];
167         
168 #ifdef DEBUG
169         self.cellNumberField.hidden = NO;
170 #endif
171 }
172
173 - (NSManagedObjectContext *)battleManagedObjectController
174 {
175         return [[HMTemporaryDataStore defaultManager] managedObjectContext];
176 }
177 - (NSManagedObject *)battle
178 {
179         HMTemporaryDataStore *store = [HMTemporaryDataStore defaultManager];
180         NSError *error = nil;
181         NSArray *array  = [store objectsWithEntityName:@"Battle" predicate:nil error:&error];
182         if(error) {
183                 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
184         }
185         return array.count > 0 ? array[0] : nil;
186 }
187
188 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
189 {
190         if([keyPath isEqualToString:@"selection"] || [keyPath isEqualToString:@"content.battleCell"] ) {
191                 [self willChangeValueForKey:@"sortieString"];
192                 [self didChangeValueForKey:@"sortieString"];
193                 return;
194         }
195         if([keyPath isEqualToString:@"selection.no"]) {
196                 [self willChangeValueForKey:@"cellNumber"];
197                 [self didChangeValueForKey:@"cellNumber"];
198                 return;
199         }
200         
201         return [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
202 }
203
204 - (NSString *)sortieString
205 {
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];
211                 } else {
212                         NSString *format = NSLocalizedString(@"%@ battle at %@ war zone in %@ (%@) now", @"Sortie");
213                         result = [NSString stringWithFormat:format, self.fleetName, self.battleCellNumber, self.areaName, self.areaNumber];
214                 }
215         }
216         return result;
217 }
218
219 - (NSString *)fleetName
220 {
221         HMServerDataStore *store = [HMServerDataStore defaultManager];
222         
223         NSError *error = nil;
224         id deckId = [self.battleContoller valueForKeyPath:@"content.deckId"];
225         NSArray *array = [store objectsWithEntityName:@"Deck"
226                                                                                         error:&error
227                                                                   predicateFormat:@"id = %@", deckId];
228         if(error) {
229                 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
230         }
231         if(array.count == 0) return nil;
232         
233         return [NSString stringWithFormat:@"%@", [array[0] valueForKey:@"name"]];
234 }
235 - (NSString *)areaNumber
236 {
237         id mapArea = [self.battleContoller valueForKeyPath:@"content.mapArea"];
238         if([mapArea integerValue] > 10) {
239                 mapArea = @"E";
240         }
241         return [NSString stringWithFormat:@"%@-%@",
242                         mapArea,
243                         [self.battleContoller valueForKeyPath:@"content.mapInfo"]];
244 }
245 - (NSString *)areaName
246 {
247         HMServerDataStore *store = [HMServerDataStore defaultManager];
248         
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"
253                                                                                         error:&error
254                                                                   predicateFormat:@"maparea_id = %@ AND %K = %@", mapAreaId, @"no", mapInfoId];
255         if(error) {
256                 NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
257         }
258         if(array.count == 0) return nil;
259         
260         return [NSString stringWithFormat:@"%@", [array[0] valueForKey:@"name"]];
261 }
262
263 - (NSNumber *)cellNumber
264 {
265         return [self.battleContoller valueForKeyPath:@"content.no"];
266 }
267
268 - (NSNumber *)battleCellNumber
269 {
270         return [self.battleContoller valueForKeyPath:@"content.battleCell"];
271 }
272
273 @end