From d8f00310e486aed1ab289a0f816acec571193c32 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Wed, 24 Aug 2011 11:53:40 -0400 Subject: Added support for multiple local highscore lists This revision increments the database version to 2. Also fixed a small problem with GameModeSelection that aligned the highscore text incorrectly. --- Classes/Highscore.m | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'Classes/Highscore.m') diff --git a/Classes/Highscore.m b/Classes/Highscore.m index 20ce56f..546cbfe 100755 --- a/Classes/Highscore.m +++ b/Classes/Highscore.m @@ -7,11 +7,64 @@ // #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]; +} + ++ (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]; -- cgit 1.4.1