diff options
Diffstat (limited to 'Classes/Highscore.m')
-rwxr-xr-x | Classes/Highscore.m | 53 |
1 files changed, 53 insertions, 0 deletions
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 @@ | |||
7 | // | 7 | // |
8 | 8 | ||
9 | #import "Highscore.h" | 9 | #import "Highscore.h" |
10 | #import <sqlite3.h> | ||
11 | #import "Cart_CollectAppDelegate.h" | ||
10 | 12 | ||
11 | @implementation Highscore | 13 | @implementation Highscore |
12 | 14 | ||
13 | @synthesize name, score, date; | 15 | @synthesize name, score, date; |
14 | 16 | ||
17 | + (NSArray*)localHighscoreListForGameMode:(NSString*)gameMode | ||
18 | { | ||
19 | NSMutableArray* highscores = [NSMutableArray arrayWithCapacity:15]; | ||
20 | const char* sqlQuery = [[NSString stringWithFormat:@"SELECT * FROM highscores WHERE gameMode = \"%@\" ORDER BY score DESC LIMIT 15", gameMode] UTF8String]; | ||
21 | sqlite3_stmt* compiled_statement; | ||
22 | |||
23 | if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK) | ||
24 | { | ||
25 | while (sqlite3_step(compiled_statement) == SQLITE_ROW) | ||
26 | { | ||
27 | NSString* name = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiled_statement, 1)]; | ||
28 | int score = sqlite3_column_int(compiled_statement, 2); | ||
29 | |||
30 | NSDate* date = nil; | ||
31 | char* dateStr = (char*)sqlite3_column_text(compiled_statement, 3); | ||
32 | if (dateStr != NULL) | ||
33 | { | ||
34 | NSString* theDate = [NSString stringWithUTF8String:dateStr]; | ||
35 | NSDateComponents* comps = [[NSDateComponents alloc] init]; | ||
36 | [comps setYear:[[theDate substringToIndex:4] intValue]]; | ||
37 | [comps setMonth:[[theDate substringWithRange:NSMakeRange(5, 2)] intValue]]; | ||
38 | [comps setDay:[[theDate substringWithRange:NSMakeRange(8, 2)] intValue]]; | ||
39 | [comps setHour:[[theDate substringWithRange:NSMakeRange(11, 2)] intValue]]; | ||
40 | [comps setMinute:[[theDate substringWithRange:NSMakeRange(14, 2)] intValue]]; | ||
41 | [comps setSecond:[[theDate substringWithRange:NSMakeRange(17, 2)] intValue]]; | ||
42 | date = [[NSCalendar currentCalendar] dateFromComponents:comps]; | ||
43 | date = [date dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]]; | ||
44 | [comps release]; | ||
45 | } | ||
46 | |||
47 | Highscore* highscore = [[Highscore alloc] initWithName:name score:score date:date]; | ||
48 | [highscores addObject:highscore]; | ||
49 | [highscore release]; | ||
50 | } | ||
51 | } | ||
52 | |||
53 | return [highscores copy]; | ||
54 | } | ||
55 | |||
56 | + (Highscore*)localHighscoreForGameMode:(NSString*)gameMode | ||
57 | { | ||
58 | NSArray* localHighscores = [Highscore localHighscoreListForGameMode:gameMode]; | ||
59 | |||
60 | if ([localHighscores count] > 0) | ||
61 | { | ||
62 | return [localHighscores objectAtIndex:0]; | ||
63 | } else { | ||
64 | return nil; | ||
65 | } | ||
66 | } | ||
67 | |||
15 | - (id)initWithName:(NSString*)m_name score:(int)m_score date:(NSDate*)m_date | 68 | - (id)initWithName:(NSString*)m_name score:(int)m_score date:(NSDate*)m_date |
16 | { | 69 | { |
17 | self = [super init]; | 70 | self = [super init]; |