
#import "EditingHSController.h"
#import <strings.h>
#import <remote/NXProxy.h>

@implementation EditingHSController

- init
{
	[super init];
	[self setGameName:[NXApp appName]];
	return self;
}

- displayHighScores:sender 	// bring up high scores w/info loaded into it
{
	char *tempString = malloc(256);

    [super displayHighScores:sender];
	sprintf(tempString, [strings valueForStringKey:"WindowTitle"],
			gameName);
    [highScorePanel setTitle:tempString];
	free(tempString);
    return self;
}

- (const char *)gameName { return gameName; }
- setGameName:(const char *)name
{
	if (gameName) free(gameName);
	gameName = NXCopyStringBufferFromZone(name, [self zone]);
	return self;
}

- setLocalScores:(BOOL)flag
{
	HighScoreDistributor *distributor;
	NXConnection *conn;
	
	if (flag == localScores) return self;
	localScores = flag;
	if (!localScores && !server) { // establish server connection
		distributor = (HighScoreDistributor *)[NXConnection
				connectToName:"DAYHighScoreServer"
				onHost:serverHost fromZone:[self zone]];
		if (!distributor) {
			localScores = YES; // can't attach to server, so go local
			NXRunAlertPanel("Score Server",
					[strings valueForStringKey:"noServer"],
					NULL, NULL, [strings valueForStringKey:"OK"]);
			return self;
		}
		conn = [(NXProxy *)distributor connectionForProxy];
		[conn registerForInvalidationNotification:self];
		[conn runFromAppKit];
		server = [distributor getServerFor:gameName];
		if (!server) {
			localScores = YES; // can't attach to server, so go local
			NXRunAlertPanel("Score Server",
					[strings valueForStringKey:"noServer"],
					NULL, NULL, [strings valueForStringKey:"OK"]);
			return self;
		}
		[server setProtocolForProxy:@protocol(HighScoreServer)];
		[server clientCheckIn:self]; // let server know I'm here
	}
	if (localScores) [clearMenu setEnabled:YES];
	else [clearMenu setEnabled:NO];
	[self readHighScores];	// reload matrices
	[self showHighScores];	// and then display the changes
	return self;
}

@end
