/* ********************************** * AdminDocumentHandler.java * AdminDocumentHandler * **********************************/ package sdg.grammar; import sdg.local.config.AdminConfig; import sdg.local.config.GlobalConfig; import sdg.local.utils.ComputeQuality; import edu.neu.ccs.demeterf.Bc; import edu.neu.ccs.demeterf.Control; import edu.neu.ccs.demeterf.demfgen.lib.List; import edu.neu.ccs.demeterf.demfgen.lib.None; import edu.neu.ccs.demeterf.perform.HeapTrav; import gen.*; import general.grammar.DocumentHandler; public class AdminDocumentHandler extends DocumentHandler{ /** Reset the files in the blackboard directory to their default contents * @author Greg Ayer, Alex Dubreuil */ @SuppressWarnings("unchecked") public static void resetFilesToDefault(){ final Config config = getConfig(); // reset the history file to its default (empty history) write("history[ ", AdminConfig.BLACKBOARD_PATH+GlobalConfig.SEPAR+AdminConfig.HISTORY_FILE); // reset the store file to its default (empty store) commitStore(new Store(List.>create())); // reset the accounts file to its default (each player starts with config.Money) commitAccounts(new Accounts( getPlayers().players.map( new List.Map>(){ public Pair map(Player p){ return new Pair(p.id, config.Money); }}))); } /** Writes the history */ public static void commitHistory(History history) { String fileName = AdminConfig.HISTORY_FILE; write(history.print(), AdminConfig.BLACKBOARD_PATH+GlobalConfig.SEPAR+fileName); } /** Writes a new Round to the history file */ public static void updateHistory(Round round){ String fileName = AdminConfig.HISTORY_FILE; write(round.print(), AdminConfig.BLACKBOARD_PATH+GlobalConfig.SEPAR+fileName, true); } /** "Closes" the history file to give it correct History syntax. */ public static void closeHistory(){ String fileName = AdminConfig.HISTORY_FILE; write("]", AdminConfig.BLACKBOARD_PATH+GlobalConfig.SEPAR+fileName, true); } /** Writes the accounts */ public static void commitAccounts(Accounts accounts) { String fileName = AdminConfig.ACCOUNTS_FILE; write(accounts.print(),AdminConfig.BLACKBOARD_PATH+GlobalConfig.SEPAR+fileName); } /** Writes the store */ public static void commitStore(Store store) { String fileName = AdminConfig.STORE_FILE; store = RemoveSecrets.desecret(store); write(store.print(),AdminConfig.BLACKBOARD_PATH+GlobalConfig.SEPAR+fileName); } public static Accounts getAccounts(){ return DocumentHandler.getAccounts(AdminConfig.BLACKBOARD_PATH); } public static Store getStore(){ return DocumentHandler.getStore(AdminConfig.BLACKBOARD_PATH); } public static History getHistory(){ return DocumentHandler.getHistory(AdminConfig.BLACKBOARD_PATH); } public static PlayerTransaction getPlayerTrans(Player player){ return DocumentHandler.getPlayerTrans(player, AdminConfig.BLACKBOARD_PATH); } public static void deletePlayerTrans(Player player){ DocumentHandler.deletePlayerTrans(player, AdminConfig.BLACKBOARD_PATH); } public static Players getPlayers(){ return DocumentHandler.getPlayers(AdminConfig.BLACKBOARD_ADMIN_PATH); } public static Config getConfig(){ return DocumentHandler.getConfig(AdminConfig.BLACKBOARD_ADMIN_PATH); } private static class RemoveSecrets extends Bc { private static RemoveSecrets rs = new RemoveSecrets(); private static Control c = Control.builtins(RawMaterialInstance.class); public static Store desecret(Store s) { return new HeapTrav(rs, c).traverse(s); } public RawMaterialInstance combine(RawMaterialInstance rmi) { if(rmi.secret.isSome()) { Quality q = ComputeQuality.quality(new RawMaterial(rmi), rmi.secret.inner().assign.inner()); return new RawMaterialInstance(rmi.cs, new SecretOfRawMaterial(new None(), q)); } return rmi; } } }