1616import players .human .HumanGUIPlayer ;
1717
1818import javax .swing .*;
19- import java .util .ArrayList ;
2019import java .awt .*;
20+ import java .util .ArrayList ;
2121import java .util .List ;
2222import java .util .Map ;
2323import java .util .Set ;
@@ -59,7 +59,7 @@ public class GameSession {
5959 // the size of the browser's space for the GUI, and its device pixel ratio (Swing thread only)
6060 private Dimension space ;
6161 private double dpr ;
62- // the image is drawn at dpr * the scale the browser shows the frame at (see fitFrame)
62+ // image pixels per frame pixel (see fitFrame)
6363 private volatile double imageScale ;
6464 private volatile long lastActivity = System .currentTimeMillis ();
6565 private final long startedAt = System .currentTimeMillis ();
@@ -79,9 +79,7 @@ public GameSession(SessionConfig config, OpponentCatalog opponents, Sender out,
7979 }
8080
8181 /**
82- * Swing settings for a JVM whose GUIs are all streamed. Swing's own tooltips are switched off, as Swing moves them
83- * on to a real screen and so out of the frame; the InputForwarder sends their text to the browser instead. Messages
84- * a GUI would show in a dialog are sent to its browser.
82+ * Swing settings for a JVM whose GUIs are all streamed.
8583 *
8684 * @param lookAndFeel "flat" for FlatLaf's light look and feel, or "default" for Swing's own
8785 */
@@ -90,8 +88,11 @@ public static void configureSwing(String lookAndFeel) {
9088 FlatLightLaf .setup ();
9189 else if (!lookAndFeel .equalsIgnoreCase ("default" ))
9290 throw new IllegalArgumentException ("Unknown look and feel (flat or default): " + lookAndFeel );
91+ // Swing moves a tooltip on to a real screen, and so out of the frame; the InputForwarder sends their text to
92+ // the browser instead
9393 ToolTipManager .sharedInstance ().setEnabled (false );
9494 JPopupMenu .setDefaultLightWeightPopupEnabled (true );
95+ // a message a GUI would show in a dialog goes to the browser of the session the GUI belongs to
9596 GUIMessages .setHandler ((parent , title , message ) -> {
9697 GameSession session = byFrame .get (SwingUtilities .getWindowAncestor (parent ));
9798 if (session != null ) session .sendMessage (title , message );
@@ -179,6 +180,7 @@ private void start(int w, int h, double dpr) {
179180 sendStarted (seed , players );
180181 guiUpdater = new Timer ((int ) game .getCoreParameters ().frameSleepMS , e -> {
181182 game .updateGUI (gui , frame );
183+ // a GUI can grow as the game goes on
182184 fitFrame ();
183185 chrome .update ();
184186 reportProgress ();
@@ -238,8 +240,7 @@ private JsonArray playerNames(List<AbstractPlayer> players) {
238240 }
239241
240242 /**
241- * Tells the browser whose turn it is when that changes, and the results once the game is over. Runs on the Swing
242- * thread, after each GUI update.
243+ * Tells the browser whose turn it is when that changes, and the results once the game is over.
243244 */
244245 private void reportProgress () {
245246 AbstractGameState state = game .getGameState ();
@@ -274,9 +275,8 @@ private void reportProgress() {
274275 }
275276
276277 /**
277- * A record of the game so far, for a bug report or to set the game up again: the setup (game, seed, players, every
278- * parameter's value) and the actions taken, described as the browser player saw them (so hiding what they could not
279- * see), and the results if the game is over.
278+ * A record of the game so far, for a bug report or to set the game up again. The actions are described as the
279+ * browser player saw them.
280280 */
281281 private JsonObject gameLog () {
282282 JsonObject log = new JsonObject ();
@@ -327,10 +327,8 @@ private JsonObject gameLog() {
327327 return log ;
328328 }
329329
330- /**
331- * The game's action history. The game thread adds to it, so copying it may meet a change; it is then copied again.
332- */
333330 private static List <utilities .Pair <Integer , core .actions .AbstractAction >> history (AbstractGameState state ) {
331+ // the game thread adds to the history, so copying it may meet a change; it is then copied again
334332 for (int attempt = 0 ; ; attempt ++) {
335333 try {
336334 return state .getHistory ();
@@ -356,14 +354,14 @@ private void resize(int w, int h, double dpr) {
356354 }
357355
358356 /**
359- * Sizes the frame to the browser's space or, where the GUI's layout needs more (many GUIs are laid out at a fixed
360- * size), to a larger frame of the same proportions, which the browser scales down to fit. The layout's own preferred size is used, not the
361- * size the GUI may have set on its panel, as that counts the panels the ChromeReader hides. A GUI can grow as the
362- * game goes on, so this runs after every update. Swing thread only.
357+ * Sizes the frame to the browser's space or, where the GUI needs more, to a larger frame of the same proportions,
358+ * which the browser scales down to fit. Swing thread only.
363359 */
364360 private void fitFrame () {
365361 Container content = frame .getContentPane ();
366362 LayoutManager layout = content .getLayout ();
363+ // Many GUIs are laid out at a fixed size. The layout's own preferred size is used, not the size the GUI may have
364+ // set on its panel, as that counts the panels the ChromeReader hides.
367365 Dimension needed = layout != null ? layout .preferredLayoutSize (content ) : content .getPreferredSize ();
368366 // scaled down by s, a frame of the space's proportions fills the space exactly
369367 double s = Math .min (1 , Math .min ((double ) space .width / needed .width , (double ) space .height / needed .height ));
@@ -378,23 +376,22 @@ private void fitFrame() {
378376 }
379377
380378 /**
381- * Ends the session because the browser player has been away too long: tells the browser, then closes the
382- * connection, which stops the session.
379+ * Tells the browser the game was ended for want of activity, and closes the connection (which stops the session).
383380 */
384381 public void endIdle (int minutes ) {
385382 sendError ("This game was ended after " + minutes + (minutes == 1 ? " minute" : " minutes" ) + " without activity." );
386383 out .close ();
387384 }
388385
389386 /**
390- * Ends the game (the game thread is interrupted while it waits for the browser player's action) and disposes of
391- * the frame.
387+ * Ends the game and disposes of the frame.
392388 */
393389 public synchronized void stop () {
394390 if (stopped ) return ;
395391 stopped = true ;
396392 if (!started ) return ;
397393 game .setStopped (true );
394+ // the game thread may be waiting for the browser player's action
398395 gameThread .interrupt ();
399396 streamer .stop ();
400397 SwingUtilities .invokeLater (() -> {
0 commit comments