/* $Id$
 *
 * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
 *
 * This software is protected by copyright. You are hereby notified from
 * now by reading this message. This software is also the confidential 
 * and proprietary information of Xenonsoft Limited.  ("Confidential
 * Information"). 
 *
 * This software is distributed under the Xenonsoft Public end user 
 * License ("XPeL"), where the machine-readable source code is provided 
 * under the "Open Source" model.
 * For more information, please read the file "LICENSE-XPL.txt" 
 */

// File:`ScoreTuple.java'
//
//
// DESCRIPTION
//	Score tuple
// 
// Author
//
//	Peter Pilgrim
//	Mon Jul 05 22:35:43 BST 1999
// 
// RCS HEADER ``ScoreTuple.java''
// 
//	$Author$
//	$Date$
//	$Revision$    $State$    $Locker$
//

package xenon.gamekit;

import java.io.Serializable;
import java.util.StringTokenizer;

/**
 * An objects that maintains a player's score
 * 
 * @author Peter Pilgrim, Mon Jul 05 21:18:10 BST 1999
 * @version $Id$
 */
public class ScoreTuple implements Serializable, Cloneable
{
    protected String		player;
    protected int		score;
    protected int		level;

    /** 
     * constructs a tuple for a player's <b>high score</b>.
     * @param player the player's name
     * @param score the player's final score
     * @param player the player's final level
     */
    public ScoreTuple( String player, int score, int level )
    {
	this.player = player;
	this.score  = score;
	this.level  = level;
    }
    
    /** sets the player's name */
    public void setPlayer( String newPlayer )
    {
	player = newPlayer;
    }
    
    /** gets the player's name */
    public String getPlayer()
    {
	return (player);
    }
    
    /** sets the player's points score */
    public void setScore( int newScore )
    {
	score = newScore;
    }
    
    /** gets the player's points score */
    public int getScore()
    {
	return (score);
    }
    
    /** sets the player's level */
    public void setLevel( int newLevel )
    {
	level = newLevel;
    }
    
    /** gets the player's level */
    public int getLevel()
    {
	return (level);
    }

    /** create a clone of this score tuple */
    public Object clone() 
    {
	try {
	    return super.clone();
	}
	catch ( CloneNotSupportedException e ) {
	    throw new Error("Can't clone: this should never happen!");
	}
    }
    
    public final static String	DELIMITER="|";
    
    /** 
     * parse contents of a input string to a score tuple
     * providing the string has the format
     *
     * <pre>
     * String :  Level  '|'  Score  '|'  Name ; 
     * 
     * Level : INTEGER ;
     * Score : INTEGER ;
     * Name : ARBITARY STRING
     *
     * </pre>
     */
    public static ScoreTuple parseString( String str )
    {
	String strLevel=null, strScore=null, strName=null;
	StringTokenizer st = new StringTokenizer( str, DELIMITER );

	if ( st.hasMoreTokens() )
	    strLevel = st.nextToken().trim();
	if ( st.hasMoreTokens() )
	    strScore = st.nextToken().trim();
	if ( st.hasMoreTokens() )
	    strName = st.nextToken().trim();
	// System.out.println( "strLevel="+strLevel );
	// System.out.println( "strScore="+strScore );
	// System.out.println( " strName="+strName );
	
	if ( strLevel == null || strScore == null || strName == null ) {
	    System.err.println( "cannot parse string:`"+str+
				"' expected three fields at least." );
	    return (null);
	}
	
	int tmpLevel, tmpScore;
	try {
	    tmpLevel = Integer.parseInt( strLevel );
	    tmpScore = Integer.parseInt( strScore );
	}
	catch ( NumberFormatException nfe ) {
	    System.err.println( "cannot parse string:`"+str+
				"' to proper number: "+ nfe.getMessage() );
	    return (null);
	}

	return new ScoreTuple( strName, tmpScore, tmpLevel );
    }
    
    public String getOutputString()
    {
	return level+DELIMITER+score+DELIMITER+player;
    }
    
    public String toString() 
    {
	return "{"+level+", "+score+", "+player+" }";
    }
}

// fini
