package participant; import java.util.ArrayList; import java.util.Iterator; import java.lang.Comparable; import org.w3c.dom.Document; import input.InputReader; import actor.CreationAgent; import data.Money; import data.Derivative; import data.DerivativeType; import game.Store; import output.TransactionsFileWriter; //import participant.PublicKey; import transaction.*; import game.Config; /** * class to represent a player in the game * * @author Rukmal Fernando * @author Hardik Kotecha * @author Radhika Srinivasan */ public class Player implements output.XMLizable, Comparable { private String name; //name of player private Money account; //money in player's account private boolean turnStatus; //is it this player's turn? private TransactionsFileWriter tfw; //writer for this players transactions private static final String CONFIG_PATH = "config.dat"; private static final String PLAYER_NAME = "Fernando_Kotecha_Srinivasan"; //private static final String PLAYER_NAME = "Mock_Player"; private int currentTurn = 0; public static void main( String[] args ) { Config.initialize( CONFIG_PATH ); Player ourPlayer = new Player( PLAYER_NAME, findCurrentFinancialStatus(), false ); ourPlayer.takeTurn(); } /** * constructor for Player * * @param pName * @param pAccount * @param pTurn */ public Player( String pName, Money pAccount, boolean pTurn ) { setName( pName ); setAccount( pAccount ); setTurn( pTurn ); } /*======================================================================== | getters and setters +-----------------------------------------------------------------------*/ /** * set the name field to the given String * * @param pName : String to be set as name */ private void setName( String pName ) { name = pName; } /** * set the account field to the given Money * * @param pAccount : Money to be set as account */ private void setAccount( Money pAccount ) { account = pAccount; } /** * set the turnStatus field to the given boolean * * @param pTurn : is it this player's turn? */ public void setTurn( boolean pTurn ) { turnStatus = pTurn; } /** * get the name of this player * * @return String : name of player */ public String getName() { return name; } /** * get the account value of this player * * @return Money : money in player's account */ public Money getAccount() { return account; } /** * is it this player's turn? * * @return */ public boolean getTurn() { return turnStatus; } /*----------------------------------------------------------------------- | end getters and setters +======================================================================*/ /** * update the player account with the given value * * @param pValue : Float */ public void updateAccount( Float pValue ) { setAccount( new Money( getAccount().getAmount() + pValue ) ); } /** * generate the xml representation of a player * * @return String */ public String generateXML() { return new String( "\n" + getName() + "\n" + getAccount().generateXML() + "\n" + getTurn() + "\n\n" ); } /** * print the XML representation of this class to the console */ public void print() { System.out.println( generateXML() ); } /** * return a list of deliver transactions given the list of derivatives * * @param pDerivatives : ArrayList * @return ArrayList */ ArrayList buildDeliverTransactions( ArrayList pDerivatives ) { ArrayList deliverTransactions = new ArrayList(); Iterator derivativesIterator = pDerivatives.iterator(); while ( derivativesIterator.hasNext() ) { Derivative currentDerivative = derivativesIterator.next(); currentDerivative.adjustForDelivery(); deliverTransactions.add( new Deliver( getName(), currentDerivative ) ); // print out action System.out.println( "Preparing RawMaterial for derivative " + currentDerivative.getName()); } return deliverTransactions; } /** * return a list of finish transactions given the list of derivatives * * @param pDerivatives : ArrayList * @return ArrayList */ ArrayList buildFinishTransactions( ArrayList pDerivatives ) { ArrayList finishTransactions = new ArrayList(); Iterator derivativesIterator = pDerivatives.iterator(); //System.out.println("Going to finish transactions " + pDerivatives.size()); while ( derivativesIterator.hasNext() ) { Derivative currentDerivative = derivativesIterator.next(); // print out action System.out.print( "Preparing a finished product for derivative \"" + currentDerivative.getName()); currentDerivative.adjustForFinishing(); finishTransactions.add( new Finish( getName(), currentDerivative ) ); } return finishTransactions; } /** * return a list of buy transactions given the list of derivatives * * @param pDerivatives : ArrayList * @return ArrayList */ ArrayList buildBuyTransactions( ArrayList pDerivatives ) { ArrayList buyTransactions = new ArrayList(); Iterator derivativesIterator = pDerivatives.iterator(); while ( derivativesIterator.hasNext() ) { Derivative currentDerivative = derivativesIterator.next(); //System.out.println( "Considering derivative \"" + currentDerivative.getName() + "\" to buy"); if ( afford( currentDerivative ) ) { Float derivativePrice = currentDerivative.getPrice().getValue(); currentDerivative.adjustForBuying( getName() ); buyTransactions.add( new Buy( getName(), currentDerivative ) ); updateAccount( 0 - derivativePrice ); // print out action //System.out.println( "Buying derivative \"" + currentDerivative.getName() + " for " + currentDerivative.getPrice().getValue()); } } return buyTransactions; } /** * return a list of create transactions given the list of derivatives * * @param pDerivatives : ArrayList * @return ArrayList */ ArrayList buildCreateTransactions( ArrayList pDerivatives ) { ArrayList createTransactions = new ArrayList(); Iterator derivativesIterator = pDerivatives.iterator(); while ( derivativesIterator.hasNext() ) { Derivative currentDerivative = derivativesIterator.next(); createTransactions.add( new Create( getName(), currentDerivative ) ); // print out action System.out.print( "Creating derivative \"" + currentDerivative.getName()); System.out.println( "\" at " + currentDerivative.getPrice().getValue() ); } return createTransactions; } /** * create a derivative given the available types * * @param pTypes : ArrayList * @return Derivative */ Derivative createDerivative( ArrayList pTypes ) { CreationAgent ca = new CreationAgent( pTypes, getName() ); return ca.createDerivative(); } /** * determine player transactions given the current store * * @return ArrayList */ public ArrayList prepareTransactions() { ArrayList transactions = new ArrayList(); // if store is empty, create a derivative if ( Store.isEmpty() ) { System.out.println( "Store is empty, creating a new derivative." ); ArrayList onlyOneDerivative = new ArrayList(); // Derivative brandNew = createDerivative( Store.findAllTypes() ); Derivative brandNew = createDerivative(Store.findTakenTypes()); onlyOneDerivative.add( brandNew ); ArrayList onlyOneTransaction = buildCreateTransactions( onlyOneDerivative ); transactions.addAll( onlyOneTransaction ); } else { ArrayList rawMaterialsToDeliver = Store.findDerivativesThatNeedRawMaterials( getName() ); ArrayList finishedProductsToDeliver = Store.findDerivativesThatNeedFinishedProducts( getName() ); ArrayList derivativesToBuy = Store.checkForDerivativesToBuy( getName() ); // add deliver transactions if necessary if ( rawMaterialsToDeliver.size() != 0 ) { System.out.println( "Preparing Raw Materials for delivery." ); ArrayList deliverTransactions = buildDeliverTransactions( rawMaterialsToDeliver ); transactions.addAll( deliverTransactions ); } // add finish transactions if necessary if ( finishedProductsToDeliver.size() != 0 ) { System.out.println( "Preparing Finished Products for finishing." ); ArrayList finishTransactions = buildFinishTransactions( finishedProductsToDeliver ); // System.out.println( "Got finished transactions " + finishTransactions.size() ); transactions.addAll( finishTransactions ); } // look at derivatives worth buying and build buy transactions if (( derivativesToBuy.size() != 0 ) && (currentTurn != 20)) { System.out.println( "Found a derivative worth buying." ); ArrayList buyTransactions = buildBuyTransactions( derivativesToBuy ); transactions.addAll( buyTransactions ); } else // must re-offer all derivatives in store at lower price { System.out.println( "No derivative worth buying, rebuilding derivatives at lower price. Turn " + currentTurn ); ArrayList rebuiltDerivatives = Store.rebuildDerivatives( getName() ); ArrayList createTransactions = buildCreateTransactions( rebuiltDerivatives ); transactions.addAll( createTransactions ); } // if any other types are available, create a new derivative ArrayList onlyOneDerivative = new ArrayList(); Derivative brandNew = createDerivative( Store.findTakenTypes() ); onlyOneDerivative.add( brandNew ); ArrayList onlyOneTransaction = buildCreateTransactions( onlyOneDerivative ); transactions.addAll( onlyOneTransaction ); } return transactions; } /** * write the player transactions * * @param ArrayList : transactions to write */ public void writeTransactions( ArrayList pTransactions ) { String allTransactions = new String( "\n" ); //begin xml Iterator transactionsIterator = pTransactions.iterator(); while( transactionsIterator.hasNext() ) { Transaction tmp = transactionsIterator.next(); String tmpXML = tmp.generateXML(); allTransactions = allTransactions.concat( tmpXML ); //System.out.println("Writing transaction " + tmpXML); } allTransactions = allTransactions.concat( "" ); //end xml String filePath = Config.getBlackBoardPath(); if(filePath.charAt(filePath.length() - 1) != java.io.File.separatorChar) filePath = filePath + java.io.File.separator; filePath = filePath + name + "_go.xml"; tfw = TransactionsFileWriter.initialize(); //get the FileWrite for player transactions tfw.setPath( filePath ); //set the path for the new file if( !tfw.writeFile( allTransactions ) ) //write the generated XML to the file { System.out.println( "failed to create player done file" ); } } /** * carry out the actions for the player's turn */ private void takeTurn() { System.out.println("Starting our turn..."); // try to find which turn it is /* try { java.io.File turnFile = new java.io.File("turn"); if(turnFile.exists()) { java.io.FileReader reader = new java.io.FileReader(turnFile); int turn = reader.read(); if(turn > 0) this.currentTurn = turn; } turnFile.delete(); // no longer needed } catch(Exception e) { } currentTurn++; */ Store.readStore(); System.out.println("Preparing Transactions..."); ArrayList transactions = prepareTransactions(); System.out.println("Done, writing Transactions..."); writeTransactions( transactions ); /* // Write our turn number try { java.io.File turnFile = new java.io.File("turn"); java.io.FileWriter writer = new java.io.FileWriter(turnFile); writer.write(this.currentTurn); writer.close(); } catch(Exception e) { } // end write turn */ System.out.println("Done with our turn."); } /** * compare two Players based on their account value * * @param pPlayer : parameter player being compared to this player * @return int */ public int compareTo( Player pPlayer ) { Float current = getAccount().getAmount(); Float param = pPlayer.getAccount().getAmount(); return current.compareTo( param ); } /** * looks at players xml file and finds current money value for this player * * @return Money */ private static Money findCurrentFinancialStatus() { try { InputReader xmlReader = InputReader.initialize(); Document playersDoc = xmlReader.prepareDocument( Config.getBlackBoardPath() + "/players.xml" ); ArrayList players = xmlReader.parsePlayers( playersDoc ); Iterator playersIterator = players.iterator(); Money playerMoney = null; while ( playersIterator.hasNext() ) { Player currentPlayer = playersIterator.next(); if ( PLAYER_NAME.compareTo( currentPlayer.getName() ) == 0 ) { playerMoney = currentPlayer.getAccount(); } } return playerMoney; } catch( Exception e ) { return Config.getInitialMoney(); } } /** * can I afford the given derivative? * * @param pDerivative : Derivative * @return boolean */ private boolean afford( Derivative pDerivative ) { return ( pDerivative.getPrice().getValue() < getAccount().getAmount() ); } /** * generate the done file path for this player * * @return String */ public String generateDoneFilePath() { return new String( Config.getBlackBoardPath() + java.io.File.separator + getName() + "_done.xml" ); } }