summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorStarla Insigna <starla4444@gmail.com>2011-08-24 11:53:40 -0400
committerStarla Insigna <starla4444@gmail.com>2011-08-24 11:53:40 -0400
commitd8f00310e486aed1ab289a0f816acec571193c32 (patch)
tree5f2ce4b1a1187035514bcd4e3c8b0b0f17b7c12c
parent21294a54c49ab67e04898aac3482035bab6c79ae (diff)
downloadcartcollect-d8f00310e486aed1ab289a0f816acec571193c32.tar.gz
cartcollect-d8f00310e486aed1ab289a0f816acec571193c32.tar.bz2
cartcollect-d8f00310e486aed1ab289a0f816acec571193c32.zip
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.
-rwxr-xr-xClasses/Cart_CollectAppDelegate.m40
-rw-r--r--Classes/GameModeSelection.h2
-rw-r--r--Classes/GameModeSelection.m53
-rw-r--r--Classes/GameModeSelectionLayer.m14
-rwxr-xr-xClasses/GameOverScene.m6
-rwxr-xr-xClasses/Highscore.h2
-rwxr-xr-xClasses/Highscore.m53
-rwxr-xr-xClasses/HighscoreListController.m37
-rwxr-xr-xResources/cartdata.sqlite3bin16384 -> 16384 bytes
9 files changed, 117 insertions, 90 deletions
diff --git a/Classes/Cart_CollectAppDelegate.m b/Classes/Cart_CollectAppDelegate.m index 0af3ac0..ba9d60b 100755 --- a/Classes/Cart_CollectAppDelegate.m +++ b/Classes/Cart_CollectAppDelegate.m
@@ -142,6 +142,8 @@
142 142
143-(void) applicationDidEnterBackground:(UIApplication*)application { 143-(void) applicationDidEnterBackground:(UIApplication*)application {
144 [[CCDirector sharedDirector] stopAnimation]; 144 [[CCDirector sharedDirector] stopAnimation];
145
146 [[NSUserDefaults standardUserDefaults] synchronize];
145 147
146 if ([[CCDirector sharedDirector] runningScene].tag == GAME_SCENE) 148 if ([[CCDirector sharedDirector] runningScene].tag == GAME_SCENE)
147 { 149 {
@@ -194,20 +196,48 @@
194 { 196 {
195 NSString* databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 197 NSString* databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
196 [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 198 [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
197 //[fileManager release];
198 199
199 [userDefaults setInteger:1 forKey:@"databaseVersion"]; 200 [userDefaults setInteger:2 forKey:@"databaseVersion"];
201 }
202
203 if (sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK)
204 {
205 NSLog(@"Failed to open database");
200 } 206 }
201 207
208 // Database migrations!
202 if ([userDefaults integerForKey:@"databaseVersion"] < 1) 209 if ([userDefaults integerForKey:@"databaseVersion"] < 1)
203 { 210 {
204 // Upgrade the database to version 1, which is completely unnecessary as no prior version exists 211 // Upgrade the database to version 1, which is completely unnecessary as no prior version exists
205 } 212 }
206 213
207 if (sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK) 214 if ([userDefaults integerForKey:@"databaseVersion"] < 2)
208 { 215 {
209 NSLog(@"Failed to open database"); 216 // Database version 2 adds a "gameMode" column to highscores to allow for multiple highscore lists
217 const char* sqlQuery = "ALTER TABLE highscores ADD gameMode varchar(255)";
218 sqlite3_stmt* compiled_statement;
219
220 if (sqlite3_prepare_v2(database, sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK)
221 {
222 sqlite3_step(compiled_statement);
223
224 sqlQuery = "UPDATE highscores SET gameMode = \"Collect\"";
225 if (sqlite3_prepare_v2(database, sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK)
226 {
227 sqlite3_step(compiled_statement);
228
229 NSLog(@"Updated database to version 2.");
230 [userDefaults setInteger:2 forKey:@"databaseVersion"];
231 } else {
232 NSLog(@"Error while updating database to version 2. '%s'", sqlite3_errmsg(database));
233 }
234 } else {
235 NSLog(@"Error while updating database to version 2. '%s'", sqlite3_errmsg(database));
236 }
210 } 237 }
238
239 // It's rather important that the database version gets saved
240 [userDefaults synchronize];
211 } 241 }
212 242
213 return database; 243 return database;
diff --git a/Classes/GameModeSelection.h b/Classes/GameModeSelection.h index f69ea37..bdb38fb 100644 --- a/Classes/GameModeSelection.h +++ b/Classes/GameModeSelection.h
@@ -22,10 +22,8 @@
22@property (readonly) BOOL unlocked; 22@property (readonly) BOOL unlocked;
23@property (nonatomic,retain) id<GameModeSelectionDelegate> delegate; 23@property (nonatomic,retain) id<GameModeSelectionDelegate> delegate;
24+ (id)selectionWithName:(NSString*)name location:(NSString*)location filename:(NSString*)filename unlocked:(BOOL)unlocked; 24+ (id)selectionWithName:(NSString*)name location:(NSString*)location filename:(NSString*)filename unlocked:(BOOL)unlocked;
25+ (id)selectionWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename highscore:(int)highscore;
26+ (id)selectionWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename unlockCondition:(NSString*)unlockCondition; 25+ (id)selectionWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename unlockCondition:(NSString*)unlockCondition;
27- (id)initWithName:(NSString*)name location:(NSString*)location filename:(NSString*)filename unlocked:(BOOL)unlocked; 26- (id)initWithName:(NSString*)name location:(NSString*)location filename:(NSString*)filename unlocked:(BOOL)unlocked;
28- (id)initWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename highscore:(int)highscore;
29- (id)initWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename unlockCondition:(NSString*)unlockCondition; 27- (id)initWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename unlockCondition:(NSString*)unlockCondition;
30- (void)buttonTapped; 28- (void)buttonTapped;
31 29
diff --git a/Classes/GameModeSelection.m b/Classes/GameModeSelection.m index 582c9dd..69f1ddb 100644 --- a/Classes/GameModeSelection.m +++ b/Classes/GameModeSelection.m
@@ -9,6 +9,7 @@
9#import "GameModeSelection.h" 9#import "GameModeSelection.h"
10#import "UIImage+ColorMasking.h" 10#import "UIImage+ColorMasking.h"
11#import "NMPanelMenu.h" 11#import "NMPanelMenu.h"
12#import "Highscore.h"
12 13
13@implementation GameModeSelection 14@implementation GameModeSelection
14 15
@@ -19,11 +20,6 @@
19 return [[[GameModeSelection alloc] initWithName:name location:location filename:filename unlocked:unlocked] autorelease]; 20 return [[[GameModeSelection alloc] initWithName:name location:location filename:filename unlocked:unlocked] autorelease];
20} 21}
21 22
22+ (id)selectionWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename highscore:(int)highscore
23{
24 return [[[GameModeSelection alloc] initWithName:name location:location filename:filename highscore:highscore] autorelease];
25}
26
27+ (id)selectionWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename unlockCondition:(NSString*)unlockCondition 23+ (id)selectionWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename unlockCondition:(NSString*)unlockCondition
28{ 24{
29 return [[[GameModeSelection alloc] initWithName:name location:location filename:filename unlockCondition:unlockCondition] autorelease]; 25 return [[[GameModeSelection alloc] initWithName:name location:location filename:filename unlockCondition:unlockCondition] autorelease];
@@ -162,34 +158,29 @@
162 CCSprite* titleSprite = [CCSprite spriteWithCGImage:titleImage key:[NSString stringWithFormat:@"gms-%@-title", filenameMod]]; 158 CCSprite* titleSprite = [CCSprite spriteWithCGImage:titleImage key:[NSString stringWithFormat:@"gms-%@-title", filenameMod]];
163 titleSprite.position = ccp(-10, (boxSize.height)/2+(combinedTitleSize.height)/2); 159 titleSprite.position = ccp(-10, (boxSize.height)/2+(combinedTitleSize.height)/2);
164 [self addChild:titleSprite]; 160 [self addChild:titleSprite];
165 } 161
166 162 if (unlocked)
167 return self;
168}
169
170- (id)initWithName:(NSString *)m_name location:(NSString *)m_location filename:(NSString *)m_filename highscore:(int)m_highscore
171{
172 self = [self initWithName:m_name location:m_location filename:m_filename unlocked:YES];
173
174 if (nil != self)
175 {
176 if (m_highscore != 0)
177 { 163 {
178 // Render the highscore label 164 Highscore* localHighscore = [Highscore localHighscoreForGameMode:name];
179 NSString* highscoreString = [NSString stringWithFormat:@"Highscore: %d", m_highscore];
180 UIFont* highscoreFont = [UIFont fontWithName:@"AmericanTypewriter" size:16.0f];
181 CGSize highscoreSize = [highscoreString sizeWithFont:highscoreFont];
182
183 UIGraphicsBeginImageContext(CGSizeMake(highscoreSize.width+10, highscoreSize.height+10));
184 CGContextRef context = UIGraphicsGetCurrentContext();
185 CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
186 [highscoreString drawInRect:CGRectMake(10, 0, highscoreSize.width, highscoreSize.height) withFont:highscoreFont];
187 165
188 CGImageRef highscoreImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage]; 166 if (localHighscore != nil)
189 UIGraphicsEndImageContext(); 167 {
190 CCSprite* highscoreSprite = [CCSprite spriteWithCGImage:highscoreImage key:[NSString stringWithFormat:@"gms-%@-highscore", m_filename]]; 168 // Render the highscore label
191 highscoreSprite.position = ccp((128-highscoreSize.width)/2, 0-64-(highscoreSize.height)/2-10); 169 NSString* highscoreString = [NSString stringWithFormat:@"Highscore: %d", localHighscore.score];
192 [self addChild:highscoreSprite]; 170 UIFont* highscoreFont = [UIFont fontWithName:@"AmericanTypewriter" size:16.0f];
171 CGSize highscoreSize = [highscoreString sizeWithFont:highscoreFont];
172
173 UIGraphicsBeginImageContext(CGSizeMake(highscoreSize.width+10, highscoreSize.height+10));
174 CGContextRef context = UIGraphicsGetCurrentContext();
175 CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
176 [highscoreString drawInRect:CGRectMake(10, 0, highscoreSize.width, highscoreSize.height) withFont:highscoreFont];
177
178 CGImageRef highscoreImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
179 UIGraphicsEndImageContext();
180 CCSprite* highscoreSprite = [CCSprite spriteWithCGImage:highscoreImage key:[NSString stringWithFormat:@"gms-%@-highscore", filename]];
181 highscoreSprite.position = ccp(-5, 0-64-(highscoreSize.height)/2-10);
182 [self addChild:highscoreSprite];
183 }
193 } 184 }
194 } 185 }
195 186
diff --git a/Classes/GameModeSelectionLayer.m b/Classes/GameModeSelectionLayer.m index 4c4845d..db08146 100644 --- a/Classes/GameModeSelectionLayer.m +++ b/Classes/GameModeSelectionLayer.m
@@ -49,19 +49,7 @@
49 49
50 if ([defaults boolForKey:@"hasDoneTutorial"]) 50 if ([defaults boolForKey:@"hasDoneTutorial"])
51 { 51 {
52 const char* sqlQuery = "SELECT * FROM highscores ORDER BY score DESC LIMIT 1"; 52 collectSelection = [GameModeSelection selectionWithName:@"Collect" location:@"Paris" filename:@"paris" unlocked:YES];
53 sqlite3_stmt* compiled_statement;
54 int score = 0;
55
56 if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK)
57 {
58 if (sqlite3_step(compiled_statement) == SQLITE_ROW)
59 {
60 score = sqlite3_column_int(compiled_statement, 2);
61 }
62 }
63
64 collectSelection = [GameModeSelection selectionWithName:@"Collect" location:@"Paris" filename:@"paris" highscore:score];
65 } else { 53 } else {
66 collectSelection = [GameModeSelection selectionWithName:@"Collect" location:@"Paris" filename:@"paris" unlockCondition:@"Beat the tutorial!"]; 54 collectSelection = [GameModeSelection selectionWithName:@"Collect" location:@"Paris" filename:@"paris" unlockCondition:@"Beat the tutorial!"];
67 } 55 }
diff --git a/Classes/GameOverScene.m b/Classes/GameOverScene.m index bc081ff..94236ff 100755 --- a/Classes/GameOverScene.m +++ b/Classes/GameOverScene.m
@@ -88,15 +88,15 @@
88 textField.enabled = NO; 88 textField.enabled = NO;
89 submitSwitch.enabled = NO; 89 submitSwitch.enabled = NO;
90 90
91 const char* sqlQuery = [[NSString stringWithFormat:@"INSERT INTO highscores (name, score) VALUES (\"%@\",%d)", [textField text], score] UTF8String]; 91 const char* sqlQuery = [[NSString stringWithFormat:@"INSERT INTO highscores (name, score, gameMode) VALUES (\"%@\",%d,\"Collect\")", [textField text], score] UTF8String];
92 sqlite3_stmt* compiled_statement; 92 sqlite3_stmt* compiled_statement;
93 93
94 if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK) 94 if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK)
95 { 95 {
96 sqlite3_step(compiled_statement); 96 sqlite3_step(compiled_statement);
97 NSLog(@"awesome, %@", [textField text]); 97 NSLog(@"awesome, %@, %d", [textField text], score);
98 } else { 98 } else {
99 NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg([Cart_CollectAppDelegate database])); 99 NSLog(@"Error while adding highscore to local highscore list. '%s'", sqlite3_errmsg([Cart_CollectAppDelegate database]));
100 } 100 }
101 101
102 if (submitSwitch.on) 102 if (submitSwitch.on)
diff --git a/Classes/Highscore.h b/Classes/Highscore.h index bc6a8fb..fa2ea71 100755 --- a/Classes/Highscore.h +++ b/Classes/Highscore.h
@@ -17,6 +17,8 @@
17@property (readonly) NSString* name; 17@property (readonly) NSString* name;
18@property (readonly) int score; 18@property (readonly) int score;
19@property (readonly) NSDate* date; 19@property (readonly) NSDate* date;
20+ (NSArray*)localHighscoreListForGameMode:(NSString*)gameMode;
21+ (Highscore*)localHighscoreForGameMode:(NSString*)gameMode;
20- (id)initWithName:(NSString*)name score:(int)score date:(NSDate*)date; 22- (id)initWithName:(NSString*)name score:(int)score date:(NSDate*)date;
21 23
22@end 24@end
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];
diff --git a/Classes/HighscoreListController.m b/Classes/HighscoreListController.m index 932b147..d7bffcb 100755 --- a/Classes/HighscoreListController.m +++ b/Classes/HighscoreListController.m
@@ -10,7 +10,6 @@
10#import "Highscore.h" 10#import "Highscore.h"
11#import "RootViewController.h" 11#import "RootViewController.h"
12#import "cocoslive.h" 12#import "cocoslive.h"
13#import <sqlite3.h>
14#import "Cart_CollectAppDelegate.h" 13#import "Cart_CollectAppDelegate.h"
15 14
16@implementation HighscoreListController 15@implementation HighscoreListController
@@ -23,41 +22,7 @@
23 // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 22 // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
24 self = [super initWithStyle:style]; 23 self = [super initWithStyle:style];
25 if (self) { 24 if (self) {
26 NSMutableArray* highscores = [NSMutableArray arrayWithCapacity:15]; 25 localHighscores = [Highscore localHighscoreListForGameMode:@"Collect"];
27 const char* sqlQuery = "SELECT * FROM highscores ORDER BY score DESC LIMIT 15";
28 sqlite3_stmt* compiled_statement;
29
30 if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK)
31 {
32 while (sqlite3_step(compiled_statement) == SQLITE_ROW)
33 {
34 NSString* name = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiled_statement, 1)];
35 int score = sqlite3_column_int(compiled_statement, 2);
36
37 NSDate* date = nil;
38 char* dateStr = (char*)sqlite3_column_text(compiled_statement, 3);
39 if (dateStr != NULL)
40 {
41 NSString* theDate = [NSString stringWithUTF8String:dateStr];
42 NSDateComponents* comps = [[NSDateComponents alloc] init];
43 [comps setYear:[[theDate substringToIndex:4] intValue]];
44 [comps setMonth:[[theDate substringWithRange:NSMakeRange(5, 2)] intValue]];
45 [comps setDay:[[theDate substringWithRange:NSMakeRange(8, 2)] intValue]];
46 [comps setHour:[[theDate substringWithRange:NSMakeRange(11, 2)] intValue]];
47 [comps setMinute:[[theDate substringWithRange:NSMakeRange(14, 2)] intValue]];
48 [comps setSecond:[[theDate substringWithRange:NSMakeRange(17, 2)] intValue]];
49 date = [[NSCalendar currentCalendar] dateFromComponents:comps];
50 date = [date dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]];
51 [comps release];
52 }
53
54 Highscore* highscore = [[Highscore alloc] initWithName:name score:score date:date];
55 [highscores addObject:highscore];
56 [highscore release];
57 }
58 }
59
60 localHighscores = [highscores copy];
61 26
62 navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 27 navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
63 myNavigationItem = [[UINavigationItem alloc] initWithTitle:@"Highscores"]; 28 myNavigationItem = [[UINavigationItem alloc] initWithTitle:@"Highscores"];
diff --git a/Resources/cartdata.sqlite3 b/Resources/cartdata.sqlite3 index 6b7b656..5afd6aa 100755 --- a/Resources/cartdata.sqlite3 +++ b/Resources/cartdata.sqlite3
Binary files differ