package admin; import player.PlayerProcessor; import utils.AccountUpdater; import utils.DocumentHandler; import utils.RuleChecker; import utils.StoreUpdater; import utils.comparator.PlayerAccountComparator; import utils.comparator.PlayerStoreComparator; import edu.neu.ccs.demeterf.demfgen.lib.List; import edu.neu.ccs.demeterf.demfgen.lib.None; import gen.Accounts; import gen.Config; import gen.Derivative; import gen.FinishedProduct; import gen.History; import gen.Pair; import gen.Player; import gen.PlayerID; import gen.PlayerStore; import gen.PlayerTransaction; import gen.Players; import gen.RawMaterial; import gen.Round; import gen.Store; /** * Administrator of the game * @author animesh */ public class Administrator { private static Players players; private static Config config; /** * Main method for Administrator * @param args */ public static void main(String[] args) { players = DocumentHandler.getPlayers(); config = DocumentHandler.getConfig(); Administrator administrator = new Administrator(); int i=0; while(i pTransactions = List.create(); Round round = new Round(i, pTransactions); for(Player player: players.players){ administrator.passTurn(player); PlayerTransaction pTrans = DocumentHandler.getPlayerTrans(player); boolean isRuleFollowed = new RuleChecker().isRuleFollowed(pTrans); if(isRuleFollowed){ administrator.updateStore(pTrans); administrator.updateAccounts(pTrans); } else{ System.out.println("**************Player "+player.id.id+" has violated the rule.*************** "); administrator.takeViolationAction(players, player); } round.ptransactions = round.ptransactions.push(pTrans); } administrator.updateHistory(round); i++; } } /** Takes appropriate actions in case one of the players violates rules * @param players * @param player */ private void takeViolationAction(Players players, Player player) { Store existingStore = DocumentHandler.getStore(); Accounts existingAccounts = DocumentHandler.getAccounts(); if(existingStore.stores.contains(new PlayerStoreComparator(player.id))){ // for each derivative in the of the player // deduct price value from the account of the seller // add the derivative back to the store of the seller Pair pIDAndStore = existingStore.stores.find(new PlayerStoreComparator(player.id)); List playerBoughtStore = pIDAndStore.b.bought; for(Derivative der: playerBoughtStore){ Pair dummyAccount = new Pair(der.seller, 0.0); Pair sellerAccount = existingAccounts.accounts.find(new PlayerAccountComparator(dummyAccount)); sellerAccount.b = sellerAccount.b-der.price.val; existingAccounts.accounts = existingAccounts.accounts.replace(new PlayerAccountComparator(sellerAccount), sellerAccount); // existingAccounts.accounts = existingAccounts.accounts.remove(new PlayerAccountComparator(sellerAccount)); // existingAccounts.accounts = existingAccounts.accounts.push(sellerAccount); Pair sellerIDAndStore = existingStore.stores.find(new PlayerStoreComparator(der.seller)); sellerIDAndStore.b.forSale = sellerIDAndStore.b.forSale.push(new Derivative(der.name, der.seller, new None(), der.price, der.type, new None(), new None())); existingStore.stores = existingStore.stores.replace(new PlayerStoreComparator(sellerIDAndStore.a), sellerIDAndStore); // existingStore.stores = existingStore.stores.remove(new PlayerStoreComparator(sellerIDAndStore.a)); // existingStore.stores = existingStore.stores.push(sellerIDAndStore); } // for each derivative that is created by the player AND in the of Players // add price value to the account of the buyer // remove derivative from the bought store of the buyer for(Player otherPlayer: players.players){ Pair otherPIDAndStore = existingStore.stores.find(new PlayerStoreComparator(otherPlayer.id)); List otherPlayerBoughtStore = otherPIDAndStore.b.bought; for(Derivative der: otherPlayerBoughtStore){ if(der.seller.equals(player.id)){ Pair dummyAccount = new Pair(otherPIDAndStore.a, 0.0); Pair buyerAccount = existingAccounts.accounts.find(new PlayerAccountComparator(dummyAccount)); buyerAccount.b = buyerAccount.b+der.price.val; existingAccounts.accounts = existingAccounts.accounts.replace(new PlayerAccountComparator(buyerAccount), buyerAccount); // existingAccounts.accounts = existingAccounts.accounts.remove(new PlayerAccountComparator(buyerAccount)); // existingAccounts.accounts = existingAccounts.accounts.push(buyerAccount); otherPlayerBoughtStore = otherPlayerBoughtStore.remove(der); } } otherPIDAndStore.b.bought = otherPlayerBoughtStore; existingStore.stores = existingStore.stores.replace(new PlayerStoreComparator(otherPlayer.id), otherPIDAndStore); // existingStore.stores = existingStore.stores.remove(new PlayerStoreComparator(otherPlayer.id)); // existingStore.stores = existingStore.stores.push(otherPIDAndStore); } DocumentHandler.commitStore(existingStore); DocumentHandler.commitAccounts(existingAccounts); } // remove the player store from store existingStore.stores = existingStore.stores.remove(new PlayerStoreComparator(player.id)); DocumentHandler.commitStore(existingStore); // remove the player account from accounts Pair dummyAccount = new Pair(player.id, 0.0); existingAccounts.accounts = existingAccounts.accounts.remove(new PlayerAccountComparator(dummyAccount)); DocumentHandler.commitAccounts(existingAccounts); // remove the player from players players.players = players.players.remove(player); } /** Updates the store with the player transaction * @param trans */ private void updateStore(PlayerTransaction trans) { Store existingStore = DocumentHandler.getStore(); Store newStore = StoreUpdater.updateStore(existingStore, trans); DocumentHandler.commitStore(newStore); } /** Updates the accounts with the player transaction * @param pTrans */ private void updateAccounts(PlayerTransaction pTrans) { Accounts existingAccounts = DocumentHandler.getAccounts(); Accounts newAccounts = AccountUpdater.updateAccounts(existingAccounts, pTrans); DocumentHandler.commitAccounts(newAccounts); } /** Updates the history with the round * @param round */ private void updateHistory(Round round) { History history = DocumentHandler.getHistory(); history.rounds = history.rounds.push(round); DocumentHandler.commitHistory(history); } /** Passes the turn to player * @param player */ private void passTurn(Player player) { PlayerProcessor playerProcessor = new PlayerProcessor(player); playerProcessor.takeTurn(); } }