-
Notifications
You must be signed in to change notification settings - Fork 0
/
Card.m
124 lines (114 loc) · 3.66 KB
/
Card.m
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Card.m
// Magic Minder
// Created by Shawn Stricker on 6/7/08.
#import "Card.h"
#import "Inventory.h"
#import "Magic_MinderAppDelegate.h"
static sqlite3_stmt *initCardStatement = nil;
static sqlite3_stmt *hydrateCardStatement = nil;
@implementation Card
@synthesize cardName, cardText, rarity, rarityImage, mana, cardType, inventory, toughness, power;
+ (void)finalizeStatements {
if (initCardStatement) sqlite3_finalize(initCardStatement);
if (hydrateCardStatement) sqlite3_finalize(hydrateCardStatement);
}
- (id)initWithPrimaryKey:(NSInteger)pk{
if (self = [super init]) {
cardID = pk;
if (initCardStatement == nil) {
const char *sql = "SELECT cardName,cardNumber,rarity FROM cardInfo WHERE id=?";
if (sqlite3_prepare_v2(dictionary, sql, -1, &initCardStatement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(dictionary));
}
}
sqlite3_bind_int(initCardStatement, 1, cardID);
if (sqlite3_step(initCardStatement) == SQLITE_ROW) {
self.cardName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(initCardStatement, 0)];
cardNumber = sqlite3_column_int(initCardStatement,1);
self.rarity = [NSString stringWithUTF8String:(char *)sqlite3_column_text(initCardStatement, 2)];
} else {
self.cardName = @"No title";
cardNumber = 0;
self.rarity = @"C";
}
// Reset the statement for future reuse.
sqlite3_reset(initCardStatement);
dirty = NO;
}
return self;
}
- (void)dealloc {
[self dehydrate];
[inventory dealloc];
[cardName release];
[cardText release];
[rarity release];
[mana release];
[cardType release];
[power release];
[toughness release];
[super dealloc];
}
-(NSInteger)cardID {
return cardID;
}
-(NSInteger)cardNumber {
return cardNumber;
}
- (void)hydrate {
if (hydrated) return;
if (hydrateCardStatement == nil) {
const char *sql = "SELECT text, mana, type, power, tough FROM cardInfo WHERE id=?";
if (sqlite3_prepare_v2(dictionary, sql, -1, &hydrateCardStatement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(dictionary));
}
}
sqlite3_bind_int(hydrateCardStatement, 1, cardID);
int success =sqlite3_step(hydrateCardStatement);
if (success == SQLITE_ROW) {
char *tmpText = (char *)sqlite3_column_text(hydrateCardStatement, 0);
if(tmpText != nil){
self.cardText = [NSString stringWithUTF8String:tmpText];
}else{
self.cardText = @"";
}
char *tmpMana = (char *)sqlite3_column_text(hydrateCardStatement, 1);
if(tmpMana != nil){
self.mana = [NSString stringWithUTF8String:tmpMana];
}else{
self.mana = @"";
}
self.cardType = [NSString stringWithUTF8String:(char *)sqlite3_column_text(hydrateCardStatement, 2)];
char *tmpPower = (char*)sqlite3_column_text(hydrateCardStatement, 3);
if(tmpPower != nil){
self.power = [NSString stringWithUTF8String:tmpPower];
}else{
self.power = @"";
}
char *tmpTough = (char*)sqlite3_column_text(hydrateCardStatement, 4);
if(tmpTough != nil){
self.toughness = [NSString stringWithUTF8String:tmpTough];
}else{
self.toughness = @"";
}
} else {
self.cardText = @"Unknown";
self.mana = @"";
self.cardType = @"";
self.power = @"";
self.toughness = @"";
}
sqlite3_reset(hydrateCardStatement);
hydrated = YES;
}
- (void)dehydrate {
[inventory dehydrate];
[cardText release];
cardText = nil;
[mana release];
mana = nil;
[cardType release];
cardType = nil;
hydrated = NO;
}
@end