diff options
author | Starla Insigna <starla4444@gmail.com> | 2011-07-30 11:19:14 -0400 |
---|---|---|
committer | Starla Insigna <starla4444@gmail.com> | 2011-07-30 11:19:14 -0400 |
commit | 9cd57b731ab1c666d4a1cb725538fdc137763d12 (patch) | |
tree | 5bac45ae5157a1cb10c6e45500cbf72789917980 /Classes/HighscoreListController.m | |
download | cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.gz cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.bz2 cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.zip |
Initial commit (version 0.2.1)
Diffstat (limited to 'Classes/HighscoreListController.m')
-rwxr-xr-x | Classes/HighscoreListController.m | 371 |
1 files changed, 371 insertions, 0 deletions
diff --git a/Classes/HighscoreListController.m b/Classes/HighscoreListController.m new file mode 100755 index 0000000..9980727 --- /dev/null +++ b/Classes/HighscoreListController.m | |||
@@ -0,0 +1,371 @@ | |||
1 | // | ||
2 | // HighscoreListController.m | ||
3 | // Cart Collect | ||
4 | // | ||
5 | // Created by iD Student Account on 7/20/11. | ||
6 | // Copyright 2011 __MyCompanyName__. All rights reserved. | ||
7 | // | ||
8 | |||
9 | #import "HighscoreListController.h" | ||
10 | |||
11 | |||
12 | @implementation HighscoreListController | ||
13 | |||
14 | |||
15 | #pragma mark - | ||
16 | #pragma mark Initialization | ||
17 | |||
18 | - (id)initWithStyle:(UITableViewStyle)style { | ||
19 | // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. | ||
20 | self = [super initWithStyle:style]; | ||
21 | if (self) { | ||
22 | NSMutableArray* highscores = [NSMutableArray arrayWithCapacity:15]; | ||
23 | const char* sqlQuery = "SELECT * FROM highscores ORDER BY score DESC LIMIT 15"; | ||
24 | sqlite3_stmt* compiled_statement; | ||
25 | |||
26 | if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK) | ||
27 | { | ||
28 | while (sqlite3_step(compiled_statement) == SQLITE_ROW) | ||
29 | { | ||
30 | NSString* name = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiled_statement, 1)]; | ||
31 | int score = sqlite3_column_int(compiled_statement, 2); | ||
32 | |||
33 | NSDate* date = nil; | ||
34 | char* dateStr = (char*)sqlite3_column_text(compiled_statement, 3); | ||
35 | if (dateStr != NULL) | ||
36 | { | ||
37 | NSString* theDate = [NSString stringWithUTF8String:dateStr]; | ||
38 | NSDateComponents* comps = [[NSDateComponents alloc] init]; | ||
39 | [comps setYear:[[theDate substringToIndex:4] intValue]]; | ||
40 | [comps setMonth:[[theDate substringWithRange:NSMakeRange(5, 2)] intValue]]; | ||
41 | [comps setDay:[[theDate substringWithRange:NSMakeRange(8, 2)] intValue]]; | ||
42 | [comps setHour:[[theDate substringWithRange:NSMakeRange(11, 2)] intValue]]; | ||
43 | [comps setMinute:[[theDate substringWithRange:NSMakeRange(14, 2)] intValue]]; | ||
44 | [comps setSecond:[[theDate substringWithRange:NSMakeRange(17, 2)] intValue]]; | ||
45 | date = [[NSCalendar currentCalendar] dateFromComponents:comps]; | ||
46 | date = [date dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]]; | ||
47 | [comps release]; | ||
48 | } | ||
49 | |||
50 | Highscore* highscore = [[Highscore alloc] initWithName:name score:score date:date]; | ||
51 | [highscores addObject:highscore]; | ||
52 | [highscore release]; | ||
53 | } | ||
54 | } | ||
55 | |||
56 | localHighscores = [highscores copy]; | ||
57 | |||
58 | navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; | ||
59 | myNavigationItem = [[UINavigationItem alloc] initWithTitle:@"Highscores"]; | ||
60 | UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(back)]; | ||
61 | myNavigationItem.leftBarButtonItem = barButton; | ||
62 | [barButton release]; | ||
63 | |||
64 | UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Local", @"Global", nil]]; | ||
65 | segmentedControl.selectedSegmentIndex = 0; | ||
66 | segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; | ||
67 | [segmentedControl addTarget:self action:@selector(switchLists:) forControlEvents:UIControlEventValueChanged]; | ||
68 | UIBarButtonItem* barButton2 = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl]; | ||
69 | [segmentedControl release]; | ||
70 | myNavigationItem.rightBarButtonItem = barButton2; | ||
71 | [barButton2 release]; | ||
72 | |||
73 | [navigationBar pushNavigationItem:myNavigationItem animated:NO]; | ||
74 | |||
75 | showGlobal = NO; | ||
76 | loadingGlobal = NO; | ||
77 | |||
78 | tableView = [(UITableView*)self.view retain]; | ||
79 | UIView* parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; | ||
80 | [tableView setFrame:CGRectMake(0, 44, 320, 480-44)]; | ||
81 | [parentView addSubview:navigationBar]; | ||
82 | [parentView addSubview:tableView]; | ||
83 | self.view = parentView; | ||
84 | [parentView release]; | ||
85 | } | ||
86 | return self; | ||
87 | } | ||
88 | |||
89 | |||
90 | #pragma mark - | ||
91 | #pragma mark View lifecycle | ||
92 | |||
93 | /* | ||
94 | - (void)viewDidLoad { | ||
95 | [super viewDidLoad]; | ||
96 | |||
97 | // Uncomment the following line to display an Edit button in the navigation bar for this view controller. | ||
98 | // self.navigationItem.rightBarButtonItem = self.editButtonItem; | ||
99 | } | ||
100 | */ | ||
101 | |||
102 | /* | ||
103 | - (void)viewWillAppear:(BOOL)animated { | ||
104 | [super viewWillAppear:animated]; | ||
105 | } | ||
106 | */ | ||
107 | /* | ||
108 | - (void)viewDidAppear:(BOOL)animated { | ||
109 | [super viewDidAppear:animated]; | ||
110 | } | ||
111 | */ | ||
112 | /* | ||
113 | - (void)viewWillDisappear:(BOOL)animated { | ||
114 | [super viewWillDisappear:animated]; | ||
115 | } | ||
116 | */ | ||
117 | /* | ||
118 | - (void)viewDidDisappear:(BOOL)animated { | ||
119 | [super viewDidDisappear:animated]; | ||
120 | } | ||
121 | */ | ||
122 | /* | ||
123 | // Override to allow orientations other than the default portrait orientation. | ||
124 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { | ||
125 | // Return YES for supported orientations. | ||
126 | return YES; | ||
127 | } | ||
128 | */ | ||
129 | |||
130 | |||
131 | #pragma mark - | ||
132 | #pragma mark Table view data source | ||
133 | |||
134 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { | ||
135 | // Return the number of sections. | ||
136 | return 1; | ||
137 | } | ||
138 | |||
139 | |||
140 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | ||
141 | // Return the number of rows in the section. | ||
142 | if (showGlobal) | ||
143 | { | ||
144 | return [globalHighscores count]; | ||
145 | } else { | ||
146 | return [localHighscores count]; | ||
147 | } | ||
148 | } | ||
149 | |||
150 | |||
151 | // Customize the appearance of table view cells. | ||
152 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | ||
153 | { | ||
154 | NSString* cellIdentifier; | ||
155 | Highscore* highscore; | ||
156 | |||
157 | // Configure the cell... | ||
158 | if (showGlobal) | ||
159 | { | ||
160 | cellIdentifier = [NSString stringWithFormat:@"Global %d", [indexPath row]]; | ||
161 | highscore = [globalHighscores objectAtIndex:[indexPath row]]; | ||
162 | } else { | ||
163 | cellIdentifier = [NSString stringWithFormat:@"Local %d", [indexPath row]]; | ||
164 | highscore = [localHighscores objectAtIndex:[indexPath row]]; | ||
165 | } | ||
166 | |||
167 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; | ||
168 | if (cell == nil) { | ||
169 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease]; | ||
170 | } | ||
171 | |||
172 | cell.textLabel.text = highscore.name; | ||
173 | |||
174 | if (highscore.date != nil) | ||
175 | { | ||
176 | NSDate* todayDate = [NSDate date]; | ||
177 | double ti = [highscore.date timeIntervalSinceDate:todayDate]; | ||
178 | ti = ti * -1; | ||
179 | |||
180 | if (ti < 1) | ||
181 | { | ||
182 | cell.detailTextLabel.text = @"What is this I don't even"; | ||
183 | } else if (ti < 60) | ||
184 | { | ||
185 | cell.detailTextLabel.text = @"Less than a minute ago"; | ||
186 | } else if (ti < 3600) | ||
187 | { | ||
188 | int diff = round(ti / 60); | ||
189 | cell.detailTextLabel.text = [NSString stringWithFormat:@"%d minutes ago", diff]; | ||
190 | } else if (ti < 86400) | ||
191 | { | ||
192 | int diff = round(ti / 60 / 60); | ||
193 | cell.detailTextLabel.text = [NSString stringWithFormat:@"%d hours ago", diff]; | ||
194 | } else { | ||
195 | NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; | ||
196 | [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; | ||
197 | [dateFormatter setDateStyle:NSDateFormatterLongStyle]; | ||
198 | cell.detailTextLabel.text = [dateFormatter stringFromDate:highscore.date]; | ||
199 | [dateFormatter release]; | ||
200 | } | ||
201 | } | ||
202 | |||
203 | UILabel* scoreLabel = [[UILabel alloc] init]; | ||
204 | scoreLabel.text = [NSString stringWithFormat:@"%d", highscore.score]; | ||
205 | CGSize labelSize = [scoreLabel.text sizeWithFont:scoreLabel.font constrainedToSize:CGSizeMake(160, 44) lineBreakMode:UILineBreakModeClip]; | ||
206 | scoreLabel.frame = CGRectMake(320-10-labelSize.width, 22-labelSize.height/2, labelSize.width, labelSize.height); | ||
207 | [cell addSubview:scoreLabel]; | ||
208 | [scoreLabel release]; | ||
209 | |||
210 | cell.selectionStyle = UITableViewCellSelectionStyleNone; | ||
211 | |||
212 | return cell; | ||
213 | } | ||
214 | |||
215 | |||
216 | /* | ||
217 | // Override to support conditional editing of the table view. | ||
218 | - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { | ||
219 | // Return NO if you do not want the specified item to be editable. | ||
220 | return YES; | ||
221 | } | ||
222 | */ | ||
223 | |||
224 | |||
225 | /* | ||
226 | // Override to support editing the table view. | ||
227 | - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { | ||
228 | |||
229 | if (editingStyle == UITableViewCellEditingStyleDelete) { | ||
230 | // Delete the row from the data source. | ||
231 | [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; | ||
232 | } | ||
233 | else if (editingStyle == UITableViewCellEditingStyleInsert) { | ||
234 | // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. | ||
235 | } | ||
236 | } | ||
237 | */ | ||
238 | |||
239 | |||
240 | /* | ||
241 | // Override to support rearranging the table view. | ||
242 | - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { | ||
243 | } | ||
244 | */ | ||
245 | |||
246 | |||
247 | /* | ||
248 | // Override to support conditional rearranging of the table view. | ||
249 | - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { | ||
250 | // Return NO if you do not want the item to be re-orderable. | ||
251 | return YES; | ||
252 | } | ||
253 | */ | ||
254 | |||
255 | |||
256 | #pragma mark - | ||
257 | #pragma mark Table view delegate | ||
258 | |||
259 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | ||
260 | // Navigation logic may go here. Create and push another view controller. | ||
261 | /* | ||
262 | <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; | ||
263 | // ... | ||
264 | // Pass the selected object to the new view controller. | ||
265 | [self.navigationController pushViewController:detailViewController animated:YES]; | ||
266 | [detailViewController release]; | ||
267 | */ | ||
268 | } | ||
269 | |||
270 | |||
271 | #pragma mark - | ||
272 | #pragma mark Memory management | ||
273 | |||
274 | - (void)didReceiveMemoryWarning { | ||
275 | // Releases the view if it doesn't have a superview. | ||
276 | [super didReceiveMemoryWarning]; | ||
277 | |||
278 | // Relinquish ownership any cached data, images, etc. that aren't in use. | ||
279 | } | ||
280 | |||
281 | - (void)viewDidUnload { | ||
282 | // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. | ||
283 | // For example: self.myOutlet = nil; | ||
284 | } | ||
285 | |||
286 | |||
287 | - (void)dealloc { | ||
288 | [super dealloc]; | ||
289 | } | ||
290 | |||
291 | - (void)back | ||
292 | { | ||
293 | RootViewController* viewController = [[[UIApplication sharedApplication] delegate] viewController]; | ||
294 | [[[[UIApplication sharedApplication] delegate] window] setRootViewController:viewController]; | ||
295 | } | ||
296 | |||
297 | - (void)switchLists:(id)sender | ||
298 | { | ||
299 | if ([(UISegmentedControl*)sender selectedSegmentIndex] == 0) | ||
300 | { | ||
301 | if (loadingGlobal) | ||
302 | { | ||
303 | [loadingView removeFromSuperview]; | ||
304 | [self.view addSubview:tableView]; | ||
305 | } | ||
306 | |||
307 | showGlobal = NO; | ||
308 | [tableView reloadData]; | ||
309 | } else { | ||
310 | if (globalHighscores == nil) | ||
311 | { | ||
312 | loadingGlobal = YES; | ||
313 | |||
314 | loadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 480-44)]; | ||
315 | activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(150, 228-44, 20, 20)]; | ||
316 | activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray; | ||
317 | statusText = [[UILabel alloc] initWithFrame:CGRectMake(0, 256-44, 320, 21)]; | ||
318 | statusText.text = @"Downloading highscores..."; | ||
319 | statusText.textAlignment = UITextAlignmentCenter; | ||
320 | |||
321 | [loadingView addSubview:activity]; | ||
322 | [loadingView addSubview:statusText]; | ||
323 | [loadingView setBackgroundColor:[UIColor whiteColor]]; | ||
324 | [activity startAnimating]; | ||
325 | [tableView removeFromSuperview]; | ||
326 | [self.view addSubview:loadingView]; | ||
327 | |||
328 | CLScoreServerRequest* request = [[CLScoreServerRequest alloc] initWithGameName:@"Cart Collect" delegate:self]; | ||
329 | tQueryFlags flags = kQueryFlagIgnore; | ||
330 | [request requestScores:kQueryAllTime limit:15 offset:0 flags:flags category:@"Classic"]; | ||
331 | [request release]; | ||
332 | } else { | ||
333 | showGlobal = YES; | ||
334 | [tableView reloadData]; | ||
335 | } | ||
336 | } | ||
337 | } | ||
338 | |||
339 | - (void)scoreRequestOk:(id)sender | ||
340 | { | ||
341 | NSArray* highscores = [sender parseScores]; | ||
342 | NSMutableArray* highscoreTemp = [[NSMutableArray alloc] init]; | ||
343 | |||
344 | for (NSDictionary* data in highscores) | ||
345 | { | ||
346 | NSString* name = [data objectForKey:@"cc_playername"]; | ||
347 | int score = [[data objectForKey:@"cc_score"] intValue]; | ||
348 | NSDate* date = [NSDate dateWithTimeIntervalSince1970:[[data objectForKey:@"cc_when"] intValue]]; | ||
349 | Highscore* highscore = [[Highscore alloc] initWithName:name score:score date:date]; | ||
350 | [highscoreTemp addObject:highscore]; | ||
351 | [highscore release]; | ||
352 | } | ||
353 | |||
354 | globalHighscores = [highscoreTemp copy]; | ||
355 | [highscoreTemp release]; | ||
356 | |||
357 | loadingGlobal = NO; | ||
358 | [loadingView removeFromSuperview]; | ||
359 | [self.view addSubview:tableView]; | ||
360 | showGlobal = YES; | ||
361 | [tableView reloadData]; | ||
362 | } | ||
363 | |||
364 | - (void)scoreRequestFail:(id)sender | ||
365 | { | ||
366 | [activity stopAnimating]; | ||
367 | [statusText setText:@"Unable to download highscores."]; | ||
368 | } | ||
369 | |||
370 | @end | ||
371 | |||