-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMBScorelist.m
90 lines (74 loc) · 2.24 KB
/
MBScorelist.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
#import "MBtypes.h"
#import "MBScorelist.h"
#import "MBUI.h"
#define NAMELEN 20
#define SCORES 10
static NSMutableArray *scores;
@implementation MBScorelist
- (void)Scorelist_read
{
id tmpArray;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
scores = [[NSMutableArray alloc] init];
if ((tmpArray = [defaults arrayForKey:@"scores"]) != nil) {
[scores setArray:tmpArray];
} else {
int i;
NSValue *zero = [NSNumber numberWithInt:0];
for (i = 0; i < SCORES; i++) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"Anonymous" forKey:@"name"];
[dict setObject:zero forKey:@"level"];
[dict setObject:zero forKey:@"score"];
[scores addObject:dict];
}
}
}
- (void)Scorelist_write
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:scores forKey:@"scores"];
[defaults synchronize];
}
/* Add new high score to list */
- (void)Scorelist_recalc:(const char *)str :(int)level :(int)score
{
int i;
NSMutableDictionary *dict;
NSString *strName;
if ([[[scores objectAtIndex:SCORES - 1] objectForKey:@"score"] intValue] >= score)
return;
for (i = SCORES - 1; i > 0; i--) {
if ([[[scores objectAtIndex:i - 1] objectForKey:@"score"] intValue] >= score) {
break;
}
}
if (str == NULL || str[0] == 0) {
strName = @"Anonymous";
} else {
strName = [NSString stringWithCString:str encoding:NSUTF8StringEncoding];
}
dict = [NSMutableDictionary dictionary];
[dict setObject:strName forKey:@"name"];
[dict setObject:[NSNumber numberWithInt:level] forKey:@"level"];
[dict setObject:[NSNumber numberWithInt:score] forKey:@"score"];
[scores insertObject:dict atIndex:i];
[scores removeLastObject];
}
- (int)Scorelist_ishighscore:(int)val
{
return (val > [[[scores objectAtIndex:SCORES - 1] objectForKey:@"score"] intValue]);
}
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return (SCORES);
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
id theRecord, theValue;
NSParameterAssert(rowIndex >= 0 && rowIndex < SCORES);
theRecord = [scores objectAtIndex:rowIndex];
theValue = [theRecord objectForKey:[aTableColumn identifier]];
return theValue;
}
@end