package admin; import player.PlayerProcessor; import utils.*; import utils.comparator.*; import edu.neu.ccs.demeterf.demfgen.lib.List; import edu.neu.ccs.demeterf.demfgen.lib.Option; import classes.*; /** * 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); Pair sellerIDAndStore = existingStore.stores.find(new PlayerStoreComparator(der.seller)); sellerIDAndStore.b.forSale = sellerIDAndStore.b.forSale .push(new Derivative(der.name, der.seller, Option.none(), der.price, der.type, Option.none(), Option.none())); existingStore.stores = existingStore.stores.replace(new PlayerStoreComparator(sellerIDAndStore.a), 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); 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 */ private static void updateStore(PlayerTransaction trans) { Store existingStore = DocumentHandler.getStore(); Store newStore = StoreUpdater.updateStore(existingStore, trans); DocumentHandler.commitStore(newStore); } /** Updates the accounts with the player transaction */ private static void updateAccounts(PlayerTransaction pTrans) { Accounts existingAccounts = DocumentHandler.getAccounts(); Accounts newAccounts = AccountUpdater.updateAccounts(existingAccounts, pTrans); DocumentHandler.commitAccounts(newAccounts); } /** Updates the history with the round */ private static void updateHistory(Round round) { History history = DocumentHandler.getHistory(); history.rounds = history.rounds.push(round); DocumentHandler.commitHistory(history); } /** Passes the turn to player */ private static void passTurn(Player player) { PlayerProcessor playerProcessor = new PlayerProcessor(player); playerProcessor.takeTurn(); } }