
#import "HiScore.h"

/*
   Note: At this time (12-11-94) MiscSortedStorage does not seem to recognize
   Misc_DESCENDING order as being different than Misc_ASCENDING order.  Thus
   some of the strange behavior in the for loops and choice of comparisons. 
*/

@implementation HiScore

- init
{
    [super initCount:0 elementSize:sizeof (ascore) description :"{@ii}"];
    [self setSortEnabled:YES];
    [self setSortOrder:Misc_ASCENDING];	/* not that this matters... */
    return self;
}

- addElement:(void *)anElement
{
    [super addElement:anElement];
    if (0 != maxScores)
    {
	while ([self count] > maxScores)
	{
	    [self removeElementAt:0];
	}
    }
    return self;
}

- (int)compare:(void *)objectA to:(void *)objectB caseCheck:(BOOL)flag
{
    if (![self count])
	return 1;
    if (NULL == objectA)
	return -1;
    if (NULL == objectB)
	return 1;
    if (((ascore *) objectA)->score < ((ascore *) objectB)->score)
	return -1;
    if (((ascore *) objectA)->score > ((ascore *) objectB)->score)
	return 1;
    return 0;
}

- (BOOL)newScoreForPlayer:(MiscString *) theName Of:(int)thescore At:(int)thelevel
{
    ascore              newscore;

    newscore.playerName = [MiscString alloc];
    if (theName)
	[newscore.playerName initString:[theName stringValue]];
    else
	[newscore.playerName initString:[NXApp userRealName]];
    newscore.score = thescore;
    newscore.level = thelevel;
    if (([self count] < maxScores) || (maxScores == 0)
      || (1 == [self compare:&newscore to:[self elementAt:0] caseCheck:NO]))
    {
	[self addElement:&newscore];
	return YES;
    }
    return NO;
}

- load:sender
{
    NXTypedStream      *scorestream;
    MiscString         *aDirectory = [[MiscString alloc] init];

    [aDirectory setStringValue:[[NXBundle mainBundle] directory]];
    [aDirectory cat:"/highscores"];
    scorestream = NXOpenTypedStreamForFile ([aDirectory stringValue],
					    NX_READONLY);
    if (scorestream)
    {
	[self read:scorestream];
	NXCloseTypedStream (scorestream);
    }

    [aDirectory free];
    return self;
}

- save:sender
{
    NXTypedStream      *scorestream;
    MiscString         *aDirectory = [[MiscString alloc] init];

    [aDirectory setStringValue:[[NXBundle mainBundle] directory]];
    [aDirectory cat:"/highscores"];
    scorestream = NXOpenTypedStreamForFile ([aDirectory stringValue],
					    NX_WRITEONLY);
    if (scorestream)
    {
	[self write:scorestream];
	NXCloseTypedStream (scorestream);
    }

    [aDirectory free];
    return self;
}

- reset:sender
{
    [self empty];
    return self;
}

- setMaxScores:(int)maxnumberofscores
{
    maxScores = maxnumberofscores;
    return self;
}

- (int)browser:sender fillMatrix:matrix inColumn:(int)column
{
    ascore             *highscore;
    int                 i;
    id                  newCell, cellString = [[MiscString alloc] init];

    for (i = [self count] - 1; i >= 0; i--)
    {
	highscore = [self elementAt:i];

	[cellString initFromFormat:"%-.25s Level %2d  Score %d",
	 [highscore->playerName stringValue],
	 highscore->level, highscore->score];

	[matrix addRow];
	newCell = [matrix cellAt:[matrix cellCount] - 1:0];
	[newCell setStringValue:[cellString stringValue]];
	[newCell setLoaded:YES];
	[newCell setLeaf:YES];
	[newCell setEnabled:YES];
    }
    [cellString free];
    return[self count];
}

@end
