package admin; 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; import java.io.*; /** * Administrator of the game * @authors animesh, mkoscumb, and alex */ 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.name+" 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.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); //Final class, cannot edit. // history.rounds.append(round); DocumentHandler.commitHistory(history); } /** Passes the turn to player * @param player */ private void passTurn(Player player) { //Determine if the player is C# or Java String cmdline; String fName = "./blackboard/players/" + player.name; String jcmdline = "java -jar " + fName + ".jar " + player.id.id + " " + player.name; String ccmdline = fName + ".exe"; File file; file = new File(fName + ".jar"); if (file.exists()){ cmdline = jcmdline; } else{ file = new File(fName + ".exe"); if (file.exists()){ cmdline = ccmdline; } else { return; } } Process p; Runtime r = Runtime.getRuntime(); long sTime = 0, eTime = 0, cTime = 0; try{ sTime = System.currentTimeMillis(); p = r.exec(cmdline); cTime = System.currentTimeMillis(); //If they take too long and appear to be looping indefinitely, terminate with extreme prejudice. if ((cTime - sTime) > (config.Time * 3)){ p.destroy(); takeViolationAction(players, player); return; } int input; InputStreamReader isr = new InputStreamReader(p.getInputStream()); boolean open = true; while(open) { input = isr.read(); if(input == -1) open = false; else System.out.print((char)input); } try{ p.waitFor(); } catch(InterruptedException e) { System.out.println(e); } eTime = System.currentTimeMillis(); if ((eTime - sTime) > config.Time){ takeViolationAction(players, player); return; } } catch(IOException e) { System.out.println(e); } } }