From: Erik V. <ev...@us...> - 2009-10-08 21:14:28
|
Update of /cvsroot/rails/18xx/rails/game In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv25800/rails/game Modified Files: ReportBuffer.java GameManagerI.java GameManager.java DisplayBuffer.java Log Message: ReportBuffer and DisplayBuffer made instantiatable Index: GameManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/GameManager.java,v retrieving revision 1.58 retrieving revision 1.59 diff -C2 -d -r1.58 -r1.59 *** GameManager.java 7 Oct 2009 21:03:36 -0000 1.58 --- GameManager.java 8 Oct 2009 21:14:15 -0000 1.59 *************** *** 125,129 **** * All other objects will access it via NDC. */ ! protected static final String GM_KEY = "GM-1"; /** --- 125,130 ---- * All other objects will access it via NDC. */ ! protected static final String GM_KEY = "01"; ! protected static final String GM_NAME = "GameManager"; /** *************** *** 132,136 **** --- 133,148 ---- protected MoveStack moveStack = new MoveStack(); + /** + * The DisplayBuffer instance collects messages to be displayed in the UI. + */ + protected DisplayBuffer displayBuffer; + + /** + * The ReportBuffer collectes messages to be shown in the Game Report. + */ + protected ReportBuffer reportBuffer; + protected String name; + protected String key; protected StartPacket startPacket; *************** *** 163,167 **** --- 175,185 ---- */ public GameManager() { + name = GM_NAME; + key = GM_KEY; + NDC.clear(); + NDC.push (GM_KEY); gameManagerMap.put(GM_KEY, this); + displayBuffer = new DisplayBuffer(); + reportBuffer = new ReportBuffer(); } *************** *** 379,385 **** public void startGame() { - NDC.clear(); - NDC.push (GM_KEY); - setGameParameters(); --- 397,400 ---- *************** *** 704,710 **** public void processOnReload(List<PossibleAction> actions) throws Exception { - NDC.clear(); - NDC.push (GM_KEY); - for (PossibleAction action : actions) { --- 719,722 ---- *************** *** 1165,1170 **** } public String getName () { ! return "GameManager"; } --- 1177,1190 ---- } + /** + * Get name of the GM instance. Currently, the name is fixed, + * but that will change whenever a multi-game server will be implemented. + */ public String getName () { ! return name; ! } ! ! public String getKey () { ! return key; } *************** *** 1173,1175 **** --- 1193,1202 ---- } + public DisplayBuffer getDisplayBuffer() { + return displayBuffer; + } + + public ReportBuffer getReportBuffer() { + return reportBuffer; + } } Index: ReportBuffer.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/ReportBuffer.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** ReportBuffer.java 20 Nov 2008 21:49:38 -0000 1.4 --- ReportBuffer.java 8 Oct 2009 21:14:14 -0000 1.5 *************** *** 3,14 **** import java.io.*; ! import java.text.*; ! import java.util.ArrayList; ! import java.util.Date; ! import java.util.List; import org.apache.log4j.Logger; ! import rails.util.*; /** --- 3,14 ---- import java.io.*; ! import java.text.DateFormat; ! import java.text.SimpleDateFormat; ! import java.util.*; import org.apache.log4j.Logger; ! import rails.util.Config; ! import rails.util.Util; /** *************** *** 17,26 **** */ public final class ReportBuffer { - protected static Logger log = - Logger.getLogger(ReportBuffer.class.getPackage().getName()); protected static String reportDirectory = null; ! protected static String reportPathname = null; ! protected static PrintWriter report = null; protected static boolean wantReport = false; static { --- 17,30 ---- */ public final class ReportBuffer { protected static String reportDirectory = null; ! protected String reportPathname = null; ! protected PrintWriter report = null; protected static boolean wantReport = false; + protected static List<String> initialQueue = new ArrayList<String>(); + + private static final String DEFAULT_DTS_PATTERN = "yyyyMMdd_HHmm"; + private static final String DEFAULT_REPORT_EXTENSION = "txt"; + protected static Logger log = + Logger.getLogger(ReportBuffer.class.getPackage().getName()); static { *************** *** 29,34 **** } ! /** This class is not instantiated */ ! private ReportBuffer() {} /** --- 33,44 ---- } ! public ReportBuffer() { ! if (!initialQueue.isEmpty()) { ! for (String s : initialQueue) { ! addMessage (s); ! } ! initialQueue.clear(); ! } ! } /** *************** *** 37,44 **** * rails.game report. */ ! private static StringBuffer reportBuffer = new StringBuffer(); /** Add a message to the log buffer (and display it on the console) */ public static void add(String message) { if (Util.hasValue(message)) { reportBuffer.append(message).append("\n"); --- 47,66 ---- * rails.game report. */ ! private StringBuffer reportBuffer = new StringBuffer(); /** Add a message to the log buffer (and display it on the console) */ public static void add(String message) { + GameManagerI gm = GameManager.getInstance(); + ReportBuffer instance = null; + if (gm != null) instance = gm.getReportBuffer(); + if (gm == null || instance == null) { + // Queue in a static buffer until the instance is created + initialQueue.add(message); + } else { + instance.addMessage(message); + } + } + + private void addMessage (String message) { if (Util.hasValue(message)) { reportBuffer.append(message).append("\n"); *************** *** 52,61 **** /** Get the current log buffer, and clear it */ public static String get() { ! String result = reportBuffer.toString(); ! reportBuffer = new StringBuffer(); return result; } ! private static void writeToReport(String message) { /* Get out if we don't want a report */ --- 74,88 ---- /** Get the current log buffer, and clear it */ public static String get() { ! ReportBuffer instance = getInstance(); ! String result = instance.reportBuffer.toString(); ! instance.reportBuffer = new StringBuffer(); return result; } ! private static ReportBuffer getInstance() { ! return GameManager.getInstance().getReportBuffer(); ! } ! ! private void writeToReport(String message) { /* Get out if we don't want a report */ *************** *** 70,74 **** } ! private static void openReportFile() { /* Get any configured date/time pattern, or else set the default */ --- 97,101 ---- } ! private void openReportFile() { /* Get any configured date/time pattern, or else set the default */ *************** *** 76,80 **** Config.get("report.filename.date_time_pattern"); if (!Util.hasValue(reportFilenamePattern)) { ! reportFilenamePattern = "yyyyMMdd_HHmm"; } /* Get any configured extension, or else set the default */ --- 103,107 ---- Config.get("report.filename.date_time_pattern"); if (!Util.hasValue(reportFilenamePattern)) { ! reportFilenamePattern = ReportBuffer.DEFAULT_DTS_PATTERN; } /* Get any configured extension, or else set the default */ *************** *** 82,86 **** Config.get("report.filename.extension"); if (!Util.hasValue(reportFilenameExtension)) { ! reportFilenameExtension = "txt"; } /* Create a date formatter */ --- 109,113 ---- Config.get("report.filename.extension"); if (!Util.hasValue(reportFilenameExtension)) { ! reportFilenameExtension = ReportBuffer.DEFAULT_REPORT_EXTENSION; } /* Create a date formatter */ *************** *** 89,92 **** --- 116,120 ---- reportPathname = reportDirectory + "/" + Game.getName() + "_" + + GameManager.getInstance().getKey() + "_" + dateFormat.format(new Date()) + "." + reportFilenameExtension; *************** *** 101,117 **** } } ! /* A stack for messages that must "wait" for other messages */ ! private static List<String> waitQueue = new ArrayList<String> (); public static void addWaiting (String string) { ! waitQueue.add (string); } ! public static void getAllWaiting () { ! for (String message : waitQueue) { ! ReportBuffer.add (message); } ! waitQueue.clear(); } --- 129,146 ---- } } ! /* A stack for messages that must "wait" for other messages */ ! private List<String> waitQueue = new ArrayList<String> (); public static void addWaiting (String string) { ! getInstance().waitQueue.add (string); } ! public static void getAllWaiting () { ! ReportBuffer instance = getInstance(); ! for (String message : instance.waitQueue) { ! instance.addMessage (message); } ! instance.waitQueue.clear(); } Index: DisplayBuffer.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/DisplayBuffer.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** DisplayBuffer.java 6 Sep 2009 12:26:11 -0000 1.7 --- DisplayBuffer.java 8 Oct 2009 21:14:15 -0000 1.8 *************** *** 7,11 **** import org.apache.log4j.Logger; ! import rails.util.*; /** --- 7,11 ---- import org.apache.log4j.Logger; ! import rails.util.Util; /** *************** *** 15,23 **** public final class DisplayBuffer { protected static Logger log = Logger.getLogger(DisplayBuffer.class.getPackage().getName()); ! /** This class is not instantiated */ ! private DisplayBuffer() {} /** --- 15,34 ---- public final class DisplayBuffer { + /** List to catch messages before the buffer is instantiated, + * based on the supposition that never 2 games will be initialised simultaneously... + */ + protected static List<String> initialQueue = new ArrayList<String>(); + protected static Logger log = Logger.getLogger(DisplayBuffer.class.getPackage().getName()); ! public DisplayBuffer() { ! if (!initialQueue.isEmpty()) { ! for (String s : initialQueue) { ! addMessage (s, true); ! } ! initialQueue.clear(); ! } ! } /** *************** *** 26,32 **** * interest to players. */ ! private static List<String> displayBuffer = new ArrayList<String>(); ! ! private static boolean autoDisplay = true; /** --- 37,43 ---- * interest to players. */ ! private List<String> displayBuffer = new ArrayList<String>(); ! ! private boolean autoDisplay = true; /** *************** *** 35,55 **** */ public static void add(String message) { ! add (message, true); } ! ! public static void add (String message, boolean autoDisplay) { ! DisplayBuffer.autoDisplay = autoDisplay; if (Util.hasValue(message)) { ! displayBuffer.add(message); ! /* Also log the message */ ! //log.info("To display: " + message); } } /** Get the current message buffer, and clear it */ public static String[] get() { ! if (displayBuffer.size() > 0) { ! String[] message = (String[]) displayBuffer.toArray(new String[0]); ! displayBuffer.clear(); return message; } else { --- 46,86 ---- */ public static void add(String message) { ! add (message, true); } ! ! public static void add(String message, boolean autoDisplay) { ! GameManagerI gm = GameManager.getInstance(); ! DisplayBuffer instance = null; ! if (gm != null) instance = gm.getDisplayBuffer(); ! if (gm == null || instance == null) { ! // Queue in a static buffer until the instance is created ! initialQueue.add(message); ! } else { ! instance.addMessage(message, autoDisplay); ! } ! } ! ! private void addMessage (String message, boolean autoDisplay) { ! DisplayBuffer instance = getInstance(); ! instance.autoDisplay = autoDisplay; if (Util.hasValue(message)) { ! instance.displayBuffer.add(message); ! /* Also log the message (don't remove this, ! * otherwise the message will not be logged during a reload, ! * which may hinder troubleshooting) */ ! log.debug("To display: " + message); } } + private static DisplayBuffer getInstance() { + return GameManager.getInstance().getDisplayBuffer(); + } + /** Get the current message buffer, and clear it */ public static String[] get() { ! DisplayBuffer instance = getInstance(); ! if (instance.displayBuffer.size() > 0) { ! String[] message = instance.displayBuffer.toArray(new String[0]); ! instance.displayBuffer.clear(); return message; } else { *************** *** 57,71 **** } } ! public static int getSize() { ! return displayBuffer.size(); } ! public static boolean getAutoDisplay () { ! return autoDisplay; } public static void clear() { ! displayBuffer.clear(); } --- 88,102 ---- } } ! public static int getSize() { ! return getInstance().displayBuffer.size(); } ! public static boolean getAutoDisplay () { ! return getInstance().autoDisplay; } public static void clear() { ! getInstance().displayBuffer.clear(); } Index: GameManagerI.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/GameManagerI.java,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** GameManagerI.java 7 Oct 2009 19:00:38 -0000 1.14 --- GameManagerI.java 8 Oct 2009 21:14:15 -0000 1.15 *************** *** 180,184 **** --- 180,187 ---- Class<T> clazz, boolean includeExercised); + public String getKey (); public MoveStack getMoveStack (); + public DisplayBuffer getDisplayBuffer(); + public ReportBuffer getReportBuffer(); } \ No newline at end of file |