package utils; import utils.comparator.PlayerAccountComparator; import edu.neu.ccs.demeterf.TUCombiner; import edu.neu.ccs.demeterf.demfgen.lib.List; import gen.Accounts; import gen.Buy; import gen.Create; import gen.Deliver; import gen.Finish; import gen.Pair; import gen.PlayerID; import gen.PlayerTransaction; import gen.Reoffer; import gen.Round; import gen.Transaction; import gen.TransactionType; /** 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); // existingAccounts.accounts = existingAccounts.accounts.remove(new PlayerAccountComparator(accountChange)); // existingAccounts.accounts = existingAccounts.accounts.push(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(); } }