1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
//
// Highscore.m
// Cart Collect
//
// Created by iD Student Account on 7/20/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "Highscore.h"
#import <sqlite3.h>
#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];
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
|