-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoredb.c
135 lines (102 loc) · 3.35 KB
/
scoredb.c
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
125
126
127
128
129
130
131
132
133
134
#include <PalmOS.h>
#include "hercule.h"
#include "scoredb.h"
#define SCOREDB "herculeScoreDB"
static Int16 ScoreCompare( SCORE_RECORD* rec1, SCORE_RECORD* rec2 );
DmOpenRef OpenScoreDB( void )
{
LocalID dbId;
dbId = DmFindDatabase( 0, SCOREDB );
if( dbId == 0 ) {
if( DmCreateDatabase( 0, SCOREDB, DBID, DBTYPE, false ) != errNone ) {
return NULL;
}
dbId = DmFindDatabase( 0, SCOREDB );
}
return DmOpenDatabase( 0, dbId, dmModeReadWrite );
}
void CloseScoreDB( DmOpenRef scoreDB )
{
DmCloseDatabase( scoreDB );
}
Boolean ScoreInTop( DmOpenRef scoreDB, SCORE_RECORD* score, Int16 topScoreCount )
{
MemHandle record;
UInt16 idx = 0;
UInt16 count;
SCORE_RECORD* recordP;
Boolean found_in_top = false;
count = DmNumRecords( scoreDB );
if( count < topScoreCount ) return true;
/* find where to add the record */
while( idx < count ) {
record = DmQueryRecord( scoreDB, idx );
recordP = (SCORE_RECORD*)MemHandleLock( record );
found_in_top = ( ScoreCompare( score, recordP ) < 0 );
MemHandleUnlock( record );
DmReleaseRecord( scoreDB, idx, false );
if( found_in_top ) break;
idx++;
}
return found_in_top;
}
Int16 AddScore( DmOpenRef scoreDB, SCORE_RECORD* score, Int16 topScoreCount, UInt16* rank )
{
MemHandle record;
UInt16 idx = 0;
UInt16 count;
SCORE_RECORD* recordP;
Boolean found_insert = false;
count = DmNumRecords( scoreDB );
/* find where to add the record */
while( idx < count ) {
record = DmQueryRecord( scoreDB, idx );
recordP = (SCORE_RECORD*)MemHandleLock( record );
found_insert = ( ScoreCompare( score, recordP ) < 0 );
MemHandleUnlock( record );
DmReleaseRecord( scoreDB, idx, false );
if( found_insert ) break;
idx++;
}
idx = ( found_insert ? idx : dmMaxRecordIndex );
/* add the record */
record = DmNewRecord( scoreDB, &idx, sizeof( SCORE_RECORD ) );
recordP = (SCORE_RECORD*)MemHandleLock( record );
DmWrite( recordP, 0, score, sizeof( SCORE_RECORD ) );
MemHandleUnlock( record );
DmReleaseRecord( scoreDB, idx, true );
if( rank != NULL ) {
*rank = idx+1;
}
/* delete trailing scores beyond the topScoreCount */
while( ( idx = DmNumRecords( scoreDB ) ) > topScoreCount ) {
DmRemoveRecord( scoreDB, idx-1 );
}
return 0;
}
void ClearScores( DmOpenRef scoreDB )
{
while( DmNumRecords( scoreDB ) > 0 ) {
DmRemoveRecord( scoreDB, 0 );
}
}
#define SCORE(x) ((SCORE_RECORD*)x)
#define DATE(x) SCORE(x)->date
#define SEED(x) SCORE(x)->seed
#define SECONDS(x) SCORE(x)->seconds
#define HANDICAP(x) SCORE(x)->handicap
#define AUTODEDUCE(x) SCORE(x)->auto_deduce
#define WARNONBAD(x) SCORE(x)->warn_on_bad
#define WARNINGS(x) SCORE(x)->warnings
#define UNDOS(x) SCORE(x)->undos
#define HINTS(x) SCORE(x)->hints
#define EVALUATE(sort_before,x,y,z) ( (x) < (y) ? (sort_before) : ( (x) > (y) ? -(sort_before) : (z) ) )
static Int16 ScoreCompare( SCORE_RECORD* rec1, SCORE_RECORD* rec2 )
{
return EVALUATE( -1, HANDICAP(rec1), HANDICAP(rec2),
EVALUATE( -1, HINTS(rec1), HINTS(rec2),
EVALUATE( -1, WARNINGS(rec1), WARNINGS(rec2),
EVALUATE( -1, SECONDS(rec1), SECONDS(rec2),
EVALUATE( -1, DATE(rec1), DATE(rec2),
EVALUATE( -1, SEED(rec1), SEED(rec2), 0 ) ) ) ) ) );
}