// // Highscore.m // Cart Collect // // Created by iD Student Account on 7/20/11. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import "Highscore.h" #import #import "Cart_CollectAppDelegate.h" @implementation Highscore @synthesize name, score, date; + (NSArray*)localHighscoreListForGameMode:(NSString*)gameMode { NSMutableArray* highscores = [NSMutableArray arrayWithCapacity:15]; const char* sqlQuery = [[NSString stringWithFormat:@"SELECT * FROM highscores WHERE gameMode = \"%@\" ORDER BY score DESC LIMIT 15", gameMode] UTF8String]; sqlite3_stmt* compiled_statement; if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK) { while (sqlite3_step(compiled_statement) == SQLITE_ROW) { NSString* name = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiled_statement, 1)]; int score = sqlite3_column_int(compiled_statement, 2); NSDate* date = nil; char* dateStr = (char*)sqlite3_column_text(compiled_statement, 3); if (dateStr != NULL) { NSString* theDate = [NSString stringWithUTF8String:dateStr]; NSDateComponents* comps = [[NSDateComponents alloc] init]; [comps setYear:[[theDate substringToIndex:4] intValue]]; [comps setMonth:[[theDate substringWithRange:NSMakeRange(5, 2)] intValue]]; [comps setDay:[[theDate substringWithRange:NSMakeRange(8, 2)] intValue]]; [comps setHour:[[theDate substringWithRange:NSMakeRange(11, 2)] intValue]]; [comps setMinute:[[theDate substringWithRange:NSMakeRange(14, 2)] intValue]]; [comps setSecond:[[theDate substringWithRange:NSMakeRange(17, 2)] intValue]]; date = [[NSCalendar currentCalendar] dateFromComponents:comps]; date = [date dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]]; [comps release]; } Highscore* highscore = [[Highscore alloc] initWithName:name score:score date:date]; [highscores addObject:highscore]; [highscore release]; } } return [[highscores copy] autorelease]; } + (Highscore*)localHighscoreForGameMode:(NSString*)gameMode { NSArray* localHighscores = [Highscore localHighscoreListForGameMode:gameMode]; if ([localHighscores count] > 0) { return [localHighscores objectAtIndex:0]; } else { return nil; } } - (id)initWithName:(NSString*)m_name score:(int)m_score date:(NSDate*)m_date { self = [super init]; if (nil != self) { name = [m_name retain]; score = m_score; date = [m_date retain]; } return self; } - (void)dealloc { [name release]; [date release]; [super dealloc]; } @end