
#import "Keno.h"
#import "KenoCard.h"
#import "KenoCardView.h"

@implementation Keno
/***************************************************************************************
 *                                 Module Methods                                      *
 ***************************************************************************************/
- awakeFromNib
/*
	Sent to every object instantiated in a nib file.  A good place to perform some
	initialization...
*/
{
 	return self;
}

- newCard:sender
{
	char path[MAXPATHLEN+1];
	
	if ([bundle getPath:path forResource:"card" ofType:"nib"]) 
	{
		[NXApp loadNibFile:path 
						owner:self 
					withNames:NO 
					fromZone:[self zone]];
	}
	else
	{
		fprintf(stderr, "Couldn't load card nib file!\n");
		return nil;
	}
	
	[newCard setOwner:[PBoss() currentPlayer]];
	[newCard orderFront:self];
	
	return self;
}

- startGame:sender
{
	[[self getDraw] displayDraw];

	return self;
}

- getDraw
{
	int i, j, nextDraw;
	BOOL isUnique;
	
	for(i=0; i<KENO_NUMTODRAW; i++)
	{
		do
		{
			isUnique = YES;
			nextDraw = [self randomIntBetween:1 :80];
			for(j=0; j<=i; j++)
			{
				if(nextDraw == lastDraw[j])
				{
					isUnique = NO;
					break;
				}
			}
		} while (!isUnique);
		
		lastDraw[i] = nextDraw;
	}
	
	return self;
}

- displayDraw
{
	int i, j, ballNum;
	char buf[5], drawBuf[100];
	
	ballNum = 0;
	for(i=0; i<KENO_NUMTODRAW; i++)
	{
		strcpy(drawBuf, "");
		for(j=0; j<ballNum+1; j++)
		{
			sprintf(buf, "%d ", lastDraw[j]);
			strcat(drawBuf, buf);
		}
		NXBeep();
		[drawText setStringValue:drawBuf];
		NXPing();
		sleep(1);
		ballNum++;
	}
	
	return self;
}

/***************************************************************************************
 *                            Inspector And Rules Methods                              *
 ***************************************************************************************/
- (BOOL)hasRules
/*
	NO if the module does not have a Rules Window, YES if it does.  The default
	method returns NO.
*/
{
	return YES;
}

- inspectorWillBeRemoved:sender
/*
	Sent when this module's inspector is about to be replace with another one.
*/
{
	return self;
}

- inspectorWasInstalled:sender
/*
	Sent just after this module's inspector has been installed.
*/
{
	return self;
}

/***************************************************************************************
 *                                  Player Methods                                     *
 ***************************************************************************************/
- (int)collectAllBetsForPlayer:(int)playerNum
{
	return 0;
}

- playerDidBet:aPlayer
{
	return self;
}

- playerDidClose:aPlayer
/*
 *  Sent after a player has closed and is no longer part of the game.  No messages
 *  should be sent to aPlayer.  This method should just do some internal cleanup
 *  if necessary.
 */
{
	return self;
}

- playerDidJoin:player
{
	return self;
}

- (BOOL)playerWillClose:aPlayer
/*
 *  Should return YES if it is okay to close this player, NO if it is not.  The
 *  default implementation returns YES.
 */
{
    return YES;
}

- (BOOL)playerWillJoin:sender
{
	return YES;
}

/***************************************************************************************
 *                                  Table Methods                                      *
 ***************************************************************************************/
- view:aView wasLoadedOnTable:tableObject
{
	table = tableObject;

	return self;
}

/***************************************************************************************
 *                               Other Misc. Methods                                   *
 ***************************************************************************************/
- finishSessionAndClose
/*
	This message is sent when a new game is being loaded.  If there is any clean up
	to do, this is where to do it.
*/
{
	return self;
}

@end

