Commit 17920b6f authored by 刘家荣's avatar 刘家荣 💬
Browse files

fix(futures): optimized future invokes;

Now Frame's futures are completely private and inaccessible; Only themselves could pass(authorize) their futures to components who need them.
parent 54c8cf45
Loading
Loading
Loading
Loading
+20 −8
Original line number Diff line number Diff line
@@ -55,6 +55,13 @@ public class AppController {
		return savedGame;
	}


	/**
	 * 进入房间选择页面
	 *
	 * @throws ExecutionException
	 * @throws InterruptedException
	 */
	public static void goRooms() throws ExecutionException, InterruptedException {
		while(true) {
			CompletableFuture<FrameReturnType<RoomFrame>> roomFuture = new CompletableFuture<>();
@@ -71,6 +78,12 @@ public class AppController {
		}
	}

	/**
	 * 进入本地游戏
	 *
	 * @throws ExecutionException
	 * @throws InterruptedException
	 */
	public static void goOfflineGame(boolean useSave, GameType type) throws ExecutionException, InterruptedException {
		CompletableFuture<FrameReturnType<ChessGameFrame>> gameFuture = new CompletableFuture<>();
		SwingUtilities.invokeLater(() -> {
@@ -90,6 +103,12 @@ public class AppController {
		ChessGameFrame.instance.dispose();
	}

	/**
	 * 进入联机游戏
	 *
	 * @throws ExecutionException
	 * @throws InterruptedException
	 */
	public static void goOnlineGame(OnlineStatus onlineMode, Room room) throws ExecutionException, InterruptedException {
		CompletableFuture<FrameReturnType<ChessGameFrame>> gameFuture = new CompletableFuture<>();
		Game.instance = new Game(GameType.ONLINE);
@@ -152,9 +171,7 @@ public class AppController {
			jsonObject.put("Game", Game.instance);
			jsonObject.put("History", History.instance);
			File save = new File(path);
			if (!save.exists()) {
				save.createNewFile();
			}
			save.createNewFile();   // 不覆盖已有存档
			Files.writeString(Path.of(path), JSON.toJSONString(jsonObject));
			read(path);
			return true;
@@ -181,11 +198,6 @@ public class AppController {
				CompletableFuture<FrameReturnType<AppFrame>> appFuture = new CompletableFuture<>();
				SwingUtilities.invokeLater(() -> {
					AppFrame.instance = new AppFrame(appFuture);
					AppFrame.instance.addWindowListener(new WindowAdapter() {
						public void windowClosing(WindowEvent e) {
							super.windowClosing(e);
						}
					});
				});
				FrameReturnType<AppFrame> result = appFuture.get();
				AppFrame.instance.setVisible(false);
+2 −2
Original line number Diff line number Diff line
@@ -94,11 +94,11 @@ public enum ChessClickController {
        ChessGameFrame.instance.refreshScore(1);
        if(Game.instance.getScoreboard()[0] >= 60){
            JOptionPane.showMessageDialog(null, "红方胜利", "提示", JOptionPane.WARNING_MESSAGE);
            ChessGameFrame.instance.getFuture().complete(new exitGame());
            ChessGameFrame.instance.exit();
        }
        if(Game.instance.getScoreboard()[1] >= 60){
            JOptionPane.showMessageDialog(null, "黑方胜利", "提示", JOptionPane.WARNING_MESSAGE);
            ChessGameFrame.instance.getFuture().complete(new exitGame());
            ChessGameFrame.instance.exit();
            
        }
    }
+14 −8
Original line number Diff line number Diff line
package view.Frame;

import view.Frame.AppFrameReturnType.exitApp;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.CompletableFuture;

public class AppFrame extends JFrame {
    
    public static AppFrame instance;

    private CompletableFuture<FrameReturnType<AppFrame>> future;

    public CompletableFuture<FrameReturnType<AppFrame>> getFuture() {
        return future;
    }
    private final CompletableFuture<FrameReturnType<AppFrame>> future;

    public AppFrame(CompletableFuture<FrameReturnType<AppFrame>> future) {
        this.future = future;
    public AppFrame(CompletableFuture<FrameReturnType<AppFrame>> appFuture) {
        this.future = appFuture;
        this.setLocation(400,0);
        this.setContentPane(new StartForm().rootPanel);
        this.setContentPane(new StartForm(this.future).rootPanel);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                future.complete(new exitApp());
                super.windowClosing(e);
            }
        });
        this.pack();
        this.setVisible(true);
    }
+4 −4
Original line number Diff line number Diff line
@@ -37,10 +37,6 @@ public class ChessGameFrame extends JFrame {
    private JTextArea actionHistoryArea ;
    
    public JTextArea[] score = new JTextArea[2];

    public CompletableFuture<FrameReturnType<ChessGameFrame>> getFuture() {
        return future;
    }
    public ChessGameFrame(CompletableFuture<FrameReturnType<ChessGameFrame>> gameFuture) {
        this.future = gameFuture;
        setTitle("2022 CS109 Project Demo"); //设置标题
@@ -355,6 +351,10 @@ public class ChessGameFrame extends JFrame {
        playerListArea.append(s + "\r\n");
    }

    public void exit(){
        future.complete(new exitGame());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
//            ChessGameFrame mainFrame = new ChessGameFrame();
+1 −5
Original line number Diff line number Diff line
@@ -18,10 +18,6 @@ public class RoomFrame extends JFrame {

	public static RoomFrame instance;

	public CompletableFuture<FrameReturnType<RoomFrame>> getFuture() {
		return future;
	}

	private CompletableFuture<FrameReturnType<RoomFrame>> future;
	private JPanel roomPane;

@@ -77,7 +73,7 @@ public class RoomFrame extends JFrame {
		button.setFont(new Font("Rockwell", Font.BOLD, 20));

		button.addActionListener(e -> {
			future.complete(new goOnline(OnlineStatus.SERVER_WAITING, null));
			this.future.complete(new goOnline(OnlineStatus.SERVER_WAITING, null));
		});
		setPane.add(button);
	}
Loading