package utils; import utils.comparator.PlayerAccountComparator; import edu.neu.ccs.demeterf.TUCombiner; import edu.neu.ccs.demeterf.demfgen.lib.List; import classes.*; /** Class for updating the accounts * @author animesh */ public class AccountUpdater{ /** Returns a list of new accounts that are updated after a round * @param existingAccounts * @param trans * @return {@link List} */ public static Accounts updateAccounts(Accounts existingAccounts, PlayerTransaction trans) { List> accountChanges = TUCombiner.traverse(trans, new AccountChangesFinder()); for(Pair accountChange: accountChanges){ Pair existingAccount = existingAccounts.accounts.find(new PlayerAccountComparator(accountChange)); Pair newAccount = new Pair(accountChange.a, existingAccount.b+accountChange.b); existingAccounts.accounts = existingAccounts.accounts.replace(new PlayerAccountComparator(accountChange), newAccount); } return existingAccounts; } } /** Class for traversing * @author animesh */ class AccountChangesFinder extends ListTUCombiner>{ TransactionType combine(TransactionType t){ return t; } List> combine(Transaction trans, Buy b) { //deduct from account of buyer Pair accountC1 = new Pair(trans.deriv.optbuyer.inner(), -trans.deriv.price.val); //add to the account of seller Pair accountC2 = new Pair(trans.deriv.seller, trans.deriv.price.val); return List.>create(accountC1, accountC2); } List> combine(Transaction trans, Finish f) { //add to the account of buyer Pair accountC1 = new Pair(trans.deriv.optbuyer.inner(), trans.deriv.optfinished.inner().quality.val); //deduct from the account of seller Pair accountC2 = new Pair(trans.deriv.seller, -trans.deriv.optfinished.inner().quality.val); return List.>create(accountC1, accountC2); } List> combine(Transaction trans, TransactionType b){ return List.create(); } }