TableDataFactory tdFactory = new TableDataFactory();
CalcEngineFactory calcFactory = new CalcEngineFactory();
//Create the objects used to store the results...
//This is the class the holds all results (or errors) for serialization
CalculateResults results = new CalculateResults();
//Holds the results for each individual player if there are no errors
PlayerResults[] playerResults;
//The table data factory is responsible for deserializing the input
//array and returning the appropriate concrete Table Data implementation
TableDataIntf td = tdFactory.createTableData( in, flag );
//The calc engine factory uses the table data instance to determine the
//appropriate calc engine implementation to use
calcEngine = calcFactory.createCalcEngine( td );
//Set the calc engine options - do this 1st because the options may
//affect how game data is set and processed
calcEngine.setOtherOptions( td.getOptions() );
//Use a try..catch here in case generating the results throws an error
try {
//Set the game data - this is the player & cards
calcEngine.setGameData( td );
//Get the calculation results and return the PlayerResults array
playerResults = calcEngine.getCalculationResults();
//Add the player results to the return object
results.setPlayerResults( playerResults );
//Serialize the results as a byte array
return TransportUtils.getSerializedResult( results );
} catch ( CalcEngineException e ) {
e.printStackTrace(System.out);
//There was an error thrown by a lower process. Format the message
//and assign it to the results class. Use getLocalizedMessage() in
//case so other engines require an extension of CalcEngineException
results.setError( true );
results.setErrorMessage( e.getLocalizedMessage() );
return TransportUtils.getSerializedResult( results );
}
}