From ccd7bc1e131554a3201383ca6221c47d2eb7fb15 Mon Sep 17 00:00:00 2001 From: zxw Date: Sun, 9 Aug 2020 23:53:18 +0800 Subject: [PATCH 01/21] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ratel=20javafx=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E5=AD=90=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- landlords-client-javafx/pom.xml | 41 +++ .../landlords/client/javafx/BeanUtil.java | 17 ++ .../landlords/client/javafx/NettyClient.java | 89 +++++++ .../landlords/client/javafx/SimpleClient.java | 36 +++ .../client/javafx/entity/CurrentRoomInfo.java | 88 +++++++ .../client/javafx/entity/RoomInfo.java | 41 +++ .../landlords/client/javafx/entity/User.java | 67 +++++ .../client/javafx/event/LobbyEvent.java | 50 ++++ .../client/javafx/event/LoginEvent.java | 26 ++ .../client/javafx/event/RoomEvent.java | 25 ++ .../handler/DefaultChannelInitializer.java | 35 +++ .../javafx/handler/SecondProtobufCodec.java | 29 +++ .../javafx/handler/TransferHandler.java | 64 +++++ .../listener/AbstractClientListener.java | 28 +++ .../ClientConfirmLandlordListener.java | 52 ++++ .../listener/ClientConnectListener.java | 25 ++ .../ClientJoinRoomSuccessfulListener.java | 28 +++ .../javafx/listener/ClientListener.java | 14 ++ .../javafx/listener/ClientListenerUtils.java | 100 ++++++++ .../listener/ClientPokerPlayListener.java | 16 ++ .../ClientPokerPlayRedirectListener.java | 31 +++ .../listener/ClientRestartGameListener.java | 16 ++ .../ClientRoomCreateSuccessfulListener.java | 28 +++ .../ClientRoomFullJoinFailListener.java | 23 ++ .../ClientRoomNotExistsJoinFailListener.java | 23 ++ .../ClientSelectLandlordListener.java | 38 +++ .../listener/ClientSetNicknameListener.java | 26 ++ .../listener/ClientShowOptionsListener.java | 27 ++ .../ClientShowOptionsPVEListener.java | 22 ++ .../ClientShowOptionsPVPListener.java | 28 +++ .../listener/ClientShowPokersListener.java | 41 +++ .../listener/ClientShowRoomsListner.java | 27 ++ .../listener/ClientStartGameListener.java | 63 +++++ .../landlords/client/javafx/ui/UIService.java | 21 ++ .../client/javafx/ui/event/ILobbyEvent.java | 15 ++ .../client/javafx/ui/event/ILoginEvent.java | 7 + .../client/javafx/ui/event/IRoomEvent.java | 7 + .../client/javafx/ui/view/AlertUtils.java | 29 +++ .../client/javafx/ui/view/EventRegister.java | 7 + .../client/javafx/ui/view/Method.java | 10 + .../client/javafx/ui/view/UIObject.java | 32 +++ .../javafx/ui/view/index/IndexController.java | 43 ++++ .../javafx/ui/view/lobby/LobbyController.java | 84 +++++++ .../ui/view/lobby/LobbyEventRegister.java | 44 ++++ .../javafx/ui/view/lobby/LobbyMethod.java | 18 ++ .../client/javafx/ui/view/lobby/RoomPane.java | 61 +++++ .../javafx/ui/view/login/LoginController.java | 49 ++++ .../ui/view/login/LoginEventRegister.java | 42 ++++ .../client/javafx/ui/view/room/PokerPane.java | 146 +++++++++++ .../javafx/ui/view/room/RearPokerPane.java | 25 ++ .../javafx/ui/view/room/RoomController.java | 234 ++++++++++++++++++ .../ui/view/room/RoomEventRegister.java | 46 ++++ .../javafx/ui/view/room/RoomMethod.java | 29 +++ .../javafx/ui/view/room/SurplusPokerPane.java | 23 ++ .../main/resources/view/assets/css/poker.css | 13 + .../main/resources/view/assets/css/room.css | 172 +++++++++++++ .../resources/view/assets/image/loading.gif | Bin 0 -> 23230 bytes .../main/resources/view/assets/image/quit.png | Bin 0 -> 3121 bytes .../main/resources/view/assets/image/rear.png | Bin 0 -> 1451 bytes .../src/main/resources/view/index.fxml | 10 + .../src/main/resources/view/lobby.fxml | 31 +++ .../src/main/resources/view/login.fxml | 15 ++ .../src/main/resources/view/room.fxml | 95 +++++++ .../event/ClientListenerUtilsTests.java | 23 ++ 64 files changed, 2595 insertions(+) create mode 100644 landlords-client-javafx/pom.xml create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/BeanUtil.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/NettyClient.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/SimpleClient.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/CurrentRoomInfo.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/RoomInfo.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/User.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/LobbyEvent.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/LoginEvent.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/RoomEvent.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/handler/DefaultChannelInitializer.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/handler/SecondProtobufCodec.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/handler/TransferHandler.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/AbstractClientListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientConfirmLandlordListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientConnectListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientJoinRoomSuccessfulListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientListenerUtils.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerPlayListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerPlayRedirectListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRestartGameListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRoomCreateSuccessfulListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRoomFullJoinFailListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRoomNotExistsJoinFailListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientSelectLandlordListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientSetNicknameListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowOptionsListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowOptionsPVEListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowOptionsPVPListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowPokersListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowRoomsListner.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientStartGameListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/UIService.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/ILobbyEvent.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/ILoginEvent.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/IRoomEvent.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/AlertUtils.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/EventRegister.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/Method.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/UIObject.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/index/IndexController.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/LobbyController.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/LobbyEventRegister.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/LobbyMethod.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/RoomPane.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/login/LoginController.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/login/LoginEventRegister.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/PokerPane.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RearPokerPane.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomController.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomEventRegister.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomMethod.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/SurplusPokerPane.java create mode 100644 landlords-client-javafx/src/main/resources/view/assets/css/poker.css create mode 100644 landlords-client-javafx/src/main/resources/view/assets/css/room.css create mode 100644 landlords-client-javafx/src/main/resources/view/assets/image/loading.gif create mode 100644 landlords-client-javafx/src/main/resources/view/assets/image/quit.png create mode 100644 landlords-client-javafx/src/main/resources/view/assets/image/rear.png create mode 100644 landlords-client-javafx/src/main/resources/view/index.fxml create mode 100644 landlords-client-javafx/src/main/resources/view/lobby.fxml create mode 100644 landlords-client-javafx/src/main/resources/view/login.fxml create mode 100644 landlords-client-javafx/src/main/resources/view/room.fxml create mode 100644 landlords-client-javafx/src/test/java/priv/zxw/ratel/landlords/client/javafx/event/ClientListenerUtilsTests.java diff --git a/landlords-client-javafx/pom.xml b/landlords-client-javafx/pom.xml new file mode 100644 index 0000000..262b62a --- /dev/null +++ b/landlords-client-javafx/pom.xml @@ -0,0 +1,41 @@ + + + + landlords + com.smallnico.ratel + 1.2.2 + + 4.0.0 + + landlords-client-javafx + + + + com.smallnico.ratel + landlords-common + 1.2.2 + + + + + ch.qos.logback + logback-core + 1.2.3 + + + + ch.qos.logback + logback-classic + 1.2.3 + + + + + com.alibaba + fastjson + 1.2.56 + + + \ No newline at end of file diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/BeanUtil.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/BeanUtil.java new file mode 100644 index 0000000..4b5c7b1 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/BeanUtil.java @@ -0,0 +1,17 @@ +package priv.zxw.ratel.landlords.client.javafx; + + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class BeanUtil { + private static final Map CACHE_MAP = new ConcurrentHashMap<>(); + + public static void addBean(String name, Object object) { + CACHE_MAP.put(name, object); + } + + public static T getBean(String name) { + return (T) CACHE_MAP.get(name); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/NettyClient.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/NettyClient.java new file mode 100644 index 0000000..d94f69e --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/NettyClient.java @@ -0,0 +1,89 @@ +package priv.zxw.ratel.landlords.client.javafx; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.Channel; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; +import priv.zxw.ratel.landlords.client.javafx.listener.ClientListenerUtils; +import priv.zxw.ratel.landlords.client.javafx.handler.DefaultChannelInitializer; +import priv.zxw.ratel.landlords.client.javafx.ui.UIService; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + + +public class NettyClient { + private static String host = "127.0.0.1"; + private static int port = 1024; + + private ExecutorService executorService; + + private Channel channel; + private EventLoopGroup workerGroup; + + private int id = -1; + private String username; + + public NettyClient(UIService uiService) { + executorService = Executors.newSingleThreadExecutor(); + + ClientListenerUtils.setUIService(uiService); + } + + public void start() { + // 直接启动netty会别javafx阻塞消息接受 + // 使用线程池启动则可以正常运行,why !!!! + executorService.submit(new ClientThread()); + } + + private class ClientThread implements Callable { + + @Override + public Channel call() throws Exception { + workerGroup = new NioEventLoopGroup(); + Bootstrap bootstrap = new Bootstrap() + .group(workerGroup) + .channel(NioSocketChannel.class) + .handler(new DefaultChannelInitializer()); + channel = bootstrap.connect(host, port).syncUninterruptibly().channel(); + BeanUtil.addBean("channel", channel); + BeanUtil.addBean("nettyClient", NettyClient.this); + + return channel; + } + } + + public void destroy() { + if (channel == null) { + return; + } + + channel.close().syncUninterruptibly(); + workerGroup.shutdownGracefully().syncUninterruptibly(); + + executorService.shutdown(); + try { + executorService.awaitTermination(10, TimeUnit.SECONDS); + } catch (InterruptedException e) { + } + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/SimpleClient.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/SimpleClient.java new file mode 100644 index 0000000..bb8478a --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/SimpleClient.java @@ -0,0 +1,36 @@ +package priv.zxw.ratel.landlords.client.javafx; + +import javafx.application.Application; +import javafx.stage.Stage; +import priv.zxw.ratel.landlords.client.javafx.event.LobbyEvent; +import priv.zxw.ratel.landlords.client.javafx.event.LoginEvent; +import priv.zxw.ratel.landlords.client.javafx.event.RoomEvent; +import priv.zxw.ratel.landlords.client.javafx.ui.UIService; +import priv.zxw.ratel.landlords.client.javafx.ui.view.index.IndexController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.lobby.LobbyController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.login.LoginController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomController; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + + +public class SimpleClient extends Application { + + private static ExecutorService executorService = Executors.newSingleThreadExecutor(); + + @Override + public void start(Stage primaryStage) throws Exception { + UIService uiService = new UIService(); + uiService.addMethods(new IndexController(), new LoginController(new LoginEvent()), + new LobbyController(new LobbyEvent()), new RoomController(new RoomEvent())); + uiService.getMethod(IndexController.METHOD_NAME).doShow(); + + NettyClient nettyClient = new NettyClient(uiService); + nettyClient.start(); + } + + public static void main(String[] args) { + launch(args); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/CurrentRoomInfo.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/CurrentRoomInfo.java new file mode 100644 index 0000000..ccd2782 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/CurrentRoomInfo.java @@ -0,0 +1,88 @@ +package priv.zxw.ratel.landlords.client.javafx.entity; + +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.enums.ClientType; + +import java.util.List; + +public class CurrentRoomInfo { + private int roomId; + private String roomOwner; + + private User player; + private String prevPlayerName; + private ClientType prevPlayerRole; + private String nextPlayerName; + private ClientType nextPlayerRole; + + private String recentPlayerName; + private List recentPokers; + + public CurrentRoomInfo(int roomId, String roomOwner) { + this.roomId = roomId; + this.roomOwner = roomOwner; + } + + public void setLandlord(String landlordName) { + if (landlordName.equals(player.getNickname())) { + player.setRole(ClientType.LANDLORD); + prevPlayerRole = nextPlayerRole = ClientType.PEASANT; + } else if (landlordName.equals(prevPlayerName)) { + prevPlayerRole = ClientType.LANDLORD; + nextPlayerRole = ClientType.PEASANT; + player.setRole(ClientType.PEASANT); + } else { + nextPlayerRole = ClientType.LANDLORD; + prevPlayerRole = ClientType.PEASANT; + player.setRole(ClientType.PEASANT); + } + } + + public String getRecentPlayerName() { + return recentPlayerName; + } + + public void setRecentPlayerName(String recentPlayerName) { + this.recentPlayerName = recentPlayerName; + } + + public List getRecentPokers() { + return recentPokers; + } + + public void setRecentPokers(List recentPokers) { + this.recentPokers = recentPokers; + } + + public void setPlayer(User player) { + this.player = player; + } + + public User getPlayer() { + return player; + } + + public void setPrevPlayerName(String prevPlayerName) { + this.prevPlayerName = prevPlayerName; + } + + public void setNextPlayerName(String nextPlayerName) { + this.nextPlayerName = nextPlayerName; + } + + public String getPrevPlayerName() { + return prevPlayerName; + } + + public String getNextPlayerName() { + return nextPlayerName; + } + + public ClientType getPrevPlayerRole() { + return prevPlayerRole; + } + + public ClientType getNextPlayerRole() { + return nextPlayerRole; + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/RoomInfo.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/RoomInfo.java new file mode 100644 index 0000000..104d019 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/RoomInfo.java @@ -0,0 +1,41 @@ +package priv.zxw.ratel.landlords.client.javafx.entity; + + +public class RoomInfo { + private Integer roomId; + private String roomOwner; + private Integer roomClientCount; + private String roomType; + + public Integer getRoomId() { + return roomId; + } + + public void setRoomId(Integer roomId) { + this.roomId = roomId; + } + + public String getRoomOwner() { + return roomOwner; + } + + public void setRoomOwner(String roomOwner) { + this.roomOwner = roomOwner; + } + + public Integer getRoomClientCount() { + return roomClientCount; + } + + public void setRoomClientCount(Integer roomClientCount) { + this.roomClientCount = roomClientCount; + } + + public String getRoomType() { + return roomType; + } + + public void setRoomType(String roomType) { + this.roomType = roomType; + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/User.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/User.java new file mode 100644 index 0000000..10ae40d --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/User.java @@ -0,0 +1,67 @@ +package priv.zxw.ratel.landlords.client.javafx.entity; + +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.enums.ClientType; + +import java.util.List; +import java.util.stream.Collectors; + +public class User { + private String nickname; + + private boolean playing = false; + private int currentRoomId = -1; + private List pokers; + + // null, ClientType.LANDLORD, ClientType.PEASANT + private ClientType role; + + public User(String nickname) { + this.nickname = nickname; + } + + public void joinRoom(int roomId) { + currentRoomId = roomId; + playing = true; + } + + public void exitRoom() { + currentRoomId = -1; + playing = false; + role = null; + pokers.clear(); + pokers = null; + } + + public void addPokers(List pokers) { + if (this.pokers != null) { + this.pokers.addAll(pokers); + } else { + this.pokers = pokers; + } + + this.pokers = this.pokers.stream() + .sorted((a, b) -> Integer.compare(b.getLevel().getLevel(), a.getLevel().getLevel())) + .collect(Collectors.toList()); + } + + public List getPokers() { + return pokers; + } + + public ClientType getRole() { + return role; + } + + public boolean isPlaying() { + return playing; + } + + public void setRole(ClientType role) { + this.role = role; + } + + public String getNickname() { + return nickname; + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/LobbyEvent.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/LobbyEvent.java new file mode 100644 index 0000000..392eed2 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/LobbyEvent.java @@ -0,0 +1,50 @@ +package priv.zxw.ratel.landlords.client.javafx.event; + + +import io.netty.channel.Channel; +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; +import priv.zxw.ratel.landlords.client.javafx.BeanUtil; +import priv.zxw.ratel.landlords.client.javafx.listener.ClientListener; +import priv.zxw.ratel.landlords.client.javafx.listener.ClientListenerUtils; +import priv.zxw.ratel.landlords.client.javafx.ui.event.ILobbyEvent; + + +public class LobbyEvent implements ILobbyEvent { + + @Override + public void selectPVPModal() { + ClientListener listener = ClientListenerUtils.getListener(ClientEventCode.CODE_SHOW_OPTIONS_PVP); + + listener.handle(BeanUtil.getBean("channel"), "."); + } + + @Override + public void selectPVEModal() { + ClientListener listener = ClientListenerUtils.getListener(ClientEventCode.CODE_SHOW_OPTIONS_PVE); + + listener.handle(BeanUtil.getBean("channel"), "."); + } + + @Override + public void createPVPRoom() { + Channel channel = BeanUtil.getBean("channel"); + + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_ROOM_CREATE, null); + } + + @Override + public void showRooms() { + Channel channel = BeanUtil.getBean("channel"); + + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GET_ROOMS, null); + } + + @Override + public void joinRoom(int roomId) { + Channel channel = BeanUtil.getBean("channel"); + + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_ROOM_JOIN, String.valueOf(roomId)); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/LoginEvent.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/LoginEvent.java new file mode 100644 index 0000000..248383a --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/LoginEvent.java @@ -0,0 +1,26 @@ +package priv.zxw.ratel.landlords.client.javafx.event; + + +import io.netty.channel.Channel; +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.enums.ServerEventCode; +import priv.zxw.ratel.landlords.client.javafx.BeanUtil; +import priv.zxw.ratel.landlords.client.javafx.NettyClient; +import priv.zxw.ratel.landlords.client.javafx.entity.User; +import priv.zxw.ratel.landlords.client.javafx.ui.event.ILoginEvent; + +public class LoginEvent implements ILoginEvent { + + @Override + public void setNickname(String nickname) { + NettyClient nettyClient = BeanUtil.getBean("nettyClient"); + nettyClient.setUsername(nickname); + + User user = new User(nickname); + BeanUtil.addBean("user", user); + + Channel channel = BeanUtil.getBean("channel"); + + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_CLIENT_NICKNAME_SET, nickname); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/RoomEvent.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/RoomEvent.java new file mode 100644 index 0000000..fdeaeb7 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/RoomEvent.java @@ -0,0 +1,25 @@ +package priv.zxw.ratel.landlords.client.javafx.event; + + +import io.netty.channel.Channel; +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.enums.ServerEventCode; +import priv.zxw.ratel.landlords.client.javafx.BeanUtil; +import priv.zxw.ratel.landlords.client.javafx.ui.event.IRoomEvent; + +public class RoomEvent implements IRoomEvent { + + @Override + public void robLandlord() { + Channel channel = BeanUtil.getBean("channel"); + + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "TRUE"); + } + + @Override + public void notRobLandlord() { + Channel channel = BeanUtil.getBean("channel"); + + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "FALSE"); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/handler/DefaultChannelInitializer.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/handler/DefaultChannelInitializer.java new file mode 100644 index 0000000..9dd77e2 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/handler/DefaultChannelInitializer.java @@ -0,0 +1,35 @@ +package priv.zxw.ratel.landlords.client.javafx.handler; + +import io.netty.channel.ChannelInitializer; +import io.netty.channel.socket.SocketChannel; +import io.netty.handler.codec.protobuf.ProtobufDecoder; +import io.netty.handler.codec.protobuf.ProtobufEncoder; +import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder; +import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender; +import io.netty.handler.timeout.IdleStateHandler; +import org.nico.ratel.landlords.entity.ClientTransferData; + +import java.util.concurrent.TimeUnit; + +/** + * @ClassName DefaultChannelInitializer + * @Desc TODO + * @Author zxw + * @Date 2020/8/5 13:25 + * @Version 1.0 + */ +public class DefaultChannelInitializer extends ChannelInitializer { + + @Override + protected void initChannel(SocketChannel ch) throws Exception { + ch.pipeline() + .addLast(new IdleStateHandler(0, 4, 0, TimeUnit.SECONDS)) + .addLast(new ProtobufVarint32FrameDecoder()) + .addLast(new ProtobufDecoder(ClientTransferData.ClientTransferDataProtoc.getDefaultInstance())) + .addLast(new ProtobufVarint32LengthFieldPrepender()) + .addLast(new ProtobufEncoder()) + .addLast(new SecondProtobufCodec()) + .addLast(new TransferHandler()); + } + +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/handler/SecondProtobufCodec.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/handler/SecondProtobufCodec.java new file mode 100644 index 0000000..845efad --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/handler/SecondProtobufCodec.java @@ -0,0 +1,29 @@ +package priv.zxw.ratel.landlords.client.javafx.handler; + +import com.google.protobuf.MessageLite; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageCodec; +import org.nico.ratel.landlords.entity.ClientTransferData; + +import java.util.List; + +/** + * @ClassName SecondProtobufCodec + * @Desc TODO + * @Author zxw + * @Date 2020/8/5 13:26 + * @Version 1.0 + */ +public class SecondProtobufCodec extends MessageToMessageCodec { + + @Override + protected void encode(ChannelHandlerContext ctx, MessageLite msg, List out) throws Exception { + out.add(msg); + } + + @Override + protected void decode(ChannelHandlerContext ctx, ClientTransferData.ClientTransferDataProtoc msg, List out) throws Exception { + out.add(msg); + } + +} \ No newline at end of file diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/handler/TransferHandler.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/handler/TransferHandler.java new file mode 100644 index 0000000..96c89bd --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/handler/TransferHandler.java @@ -0,0 +1,64 @@ +package priv.zxw.ratel.landlords.client.javafx.handler; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.handler.timeout.IdleState; +import io.netty.handler.timeout.IdleStateEvent; +import io.netty.util.ReferenceCountUtil; +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.entity.ClientTransferData.ClientTransferDataProtoc; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; +import org.nico.ratel.landlords.print.SimplePrinter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import priv.zxw.ratel.landlords.client.javafx.listener.ClientListener; +import priv.zxw.ratel.landlords.client.javafx.listener.ClientListenerUtils; + + +public class TransferHandler extends ChannelInboundHandlerAdapter { + private static final Logger LOGGER = LoggerFactory.getLogger(TransferHandler.class); + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + try { + ClientTransferDataProtoc clientTransferData = (ClientTransferDataProtoc) msg; + + if (clientTransferData.getInfo() != null && !clientTransferData.getInfo().isEmpty()) { + SimplePrinter.printNotice(clientTransferData.getInfo()); + } + + ClientEventCode code = ClientEventCode.valueOf(clientTransferData.getCode()); + + LOGGER.info("接受服务端信息, 编码:{},数据:{}.", code, clientTransferData.getData()); + + if (code != null) { + ClientListener listener = ClientListenerUtils.getListener(code); + + if (listener != null) { + listener.handle(ctx.channel(), clientTransferData.getData()); + } else { + LOGGER.warn("未知的消息编码 {},忽略该条消息: {}", code, clientTransferData.getData()); + } + } + } finally { + ReferenceCountUtil.release(msg); + } + } + + @Override + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { + if (evt instanceof IdleStateEvent) { + IdleStateEvent event = (IdleStateEvent) evt; + if (event.state() == IdleState.WRITER_IDLE) { + ChannelUtils.pushToServer(ctx.channel(), ServerEventCode.CODE_CLIENT_HEAD_BEAT, "heartbeat"); + } + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { + cause.printStackTrace(); + context.close(); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/AbstractClientListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/AbstractClientListener.java new file mode 100644 index 0000000..0450c3c --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/AbstractClientListener.java @@ -0,0 +1,28 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.ui.UIService; + +import java.util.Objects; + + +public abstract class AbstractClientListener implements ClientListener { + protected ClientEventCode code; + + protected UIService uiService; + + public AbstractClientListener(ClientEventCode code) { + Objects.requireNonNull(code, "子事件监听器需要自定义专属的事件编码"); + this.code = code; + } + + @Override + public ClientEventCode getCode() { + return code; + } + + @Override + public void setUIService(UIService uiService) { + this.uiService = uiService; + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientConfirmLandlordListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientConfirmLandlordListener.java new file mode 100644 index 0000000..da8e496 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientConfirmLandlordListener.java @@ -0,0 +1,52 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import com.alibaba.fastjson.JSONObject; +import io.netty.channel.Channel; +import javafx.application.Platform; +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; +import priv.zxw.ratel.landlords.client.javafx.BeanUtil; +import priv.zxw.ratel.landlords.client.javafx.entity.CurrentRoomInfo; +import priv.zxw.ratel.landlords.client.javafx.entity.User; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomMethod; + +import java.util.List; + +public class ClientConfirmLandlordListener extends AbstractClientListener { + + public ClientConfirmLandlordListener() { + super(ClientEventCode.CODE_GAME_LANDLORD_CONFIRM); + } + + @Override + public void handle(Channel channel, String json) { + JSONObject jsonObject = JSONObject.parseObject(json); + String landlordName = jsonObject.getString("landlordNickname"); + List surplusPokerList = jsonObject.getJSONArray("additionalPokers").toJavaList(Poker.class); + + // 给地主发底牌 + User user = BeanUtil.getBean("user"); + if (landlordName.equals(user.getNickname())) { + user.addPokers(surplusPokerList); + } + + // 设置玩家的角色 + CurrentRoomInfo currentRoomInfo = BeanUtil.getBean("currentRoomInfo"); + currentRoomInfo.setLandlord(landlordName); + + // 视图更新 + RoomMethod method = (RoomMethod) uiService.getMethod(RoomController.METHOD_NAME); + + Platform.runLater(() -> { + method.showSurplusPokers(surplusPokerList); + method.hideRobButtons(); + method.setLandLord(landlordName); + }); + + // 重定向玩家 + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT, null); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientConnectListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientConnectListener.java new file mode 100644 index 0000000..acf61c6 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientConnectListener.java @@ -0,0 +1,25 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import io.netty.channel.Channel; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import priv.zxw.ratel.landlords.client.javafx.BeanUtil; +import priv.zxw.ratel.landlords.client.javafx.NettyClient; + + +public class ClientConnectListener extends AbstractClientListener { + private static final Logger LOGGER = LoggerFactory.getLogger(ClientConnectListener.class); + + public ClientConnectListener() { + super(ClientEventCode.CODE_CLIENT_CONNECT); + } + + @Override + public void handle(Channel channel, String json) { + NettyClient nettyClient = BeanUtil.getBean("nettyClient"); + nettyClient.setId(Integer.parseInt(json)); + + LOGGER.info("当前客户端被分配的id为 {}", nettyClient.getId()); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientJoinRoomSuccessfulListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientJoinRoomSuccessfulListener.java new file mode 100644 index 0000000..39859f1 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientJoinRoomSuccessfulListener.java @@ -0,0 +1,28 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + + +import io.netty.channel.Channel; +import javafx.application.Platform; +import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.ui.view.Method; +import priv.zxw.ratel.landlords.client.javafx.ui.view.lobby.LobbyController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomMethod; + +public class ClientJoinRoomSuccessfulListener extends AbstractClientListener { + + public ClientJoinRoomSuccessfulListener() { + super(ClientEventCode.CODE_ROOM_JOIN_SUCCESS); + } + + @Override + public void handle(Channel channel, String json) { + Method lobbyMethod = uiService.getMethod(LobbyController.METHOD_NAME); + RoomMethod roomMethod = (RoomMethod) uiService.getMethod(RoomController.METHOD_NAME); + + Platform.runLater(() -> { + lobbyMethod.doClose(); + roomMethod.joinRoom(); + }); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientListener.java new file mode 100644 index 0000000..4265cd3 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientListener.java @@ -0,0 +1,14 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import io.netty.channel.Channel; +import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.ui.UIService; + + +public interface ClientListener { + void handle(Channel channel, String json); + + ClientEventCode getCode(); + + void setUIService(UIService uiService); +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientListenerUtils.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientListenerUtils.java new file mode 100644 index 0000000..96cea8c --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientListenerUtils.java @@ -0,0 +1,100 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import priv.zxw.ratel.landlords.client.javafx.ui.UIService; + +import java.io.File; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + + +public class ClientListenerUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(ClientListenerUtils.class); + + /** code - listener 映射 */ + private static final Map LISTENER_MAP = new HashMap<>(16); + + /** + * 获取code对应的事件监听器 + * + * @param code 事件编码 + * @return code对应的事件监听器,如果不存在对应的事件监听器则返回null + */ + public static ClientListener getListener(ClientEventCode code) { + ClientListener clientListener = LISTENER_MAP.get(code); + + return clientListener; + } + + public static ClientEventCode[] supportCodes() { + return LISTENER_MAP.keySet().toArray(new ClientEventCode[] {}); + } + + public static void setUIService(UIService uiService) { + for (ClientListener listener : LISTENER_MAP.values()) { + listener.setUIService(uiService); + } + } + + static { + List> listenerClassList = findListener(); + + for (Class clazz : listenerClassList) { + try { + ClientListener listener = clazz.newInstance(); + LISTENER_MAP.put(listener.getCode(), listener); + + LOGGER.info("添加 {} -> {} 事件监听器映射", listener.getCode(), listener.getClass().getName()); + } catch (InstantiationException e) { + LOGGER.warn(clazz.getName() + " 不能被实例化"); + } catch (IllegalAccessException e) { + LOGGER.warn(clazz.getName() + " 没有默认构造函数或默认构造函数不可访问", e); + } + } + } + + private static List> findListener() { + ClassLoader defaultClassLoader = ClientListenerUtils.class.getClassLoader(); + URL classWorkPath = ClientListenerUtils.class.getResource(""); + File classWorkDir = new File(classWorkPath.getPath()); + + return loadClasses(defaultClassLoader, classWorkDir.listFiles(ClientListenerUtils::isNormalClass)) + .stream() + .filter(clazz -> clazz.getSuperclass() == AbstractClientListener.class) + .map(clazz -> (Class) clazz) + .collect(Collectors.toList()); + } + + private static boolean isNormalClass(File file) { + String fileName = file.getName(); + boolean isClassFile = fileName.endsWith(".class"); + boolean isNotInnerClassFile = !fileName.matches("[A-Z]\\w+\\$\\w+.class"); + + return isClassFile && isNotInnerClassFile; + } + + private static List> loadClasses(ClassLoader classLoader, File[] classFiles) { + String classpath = classLoader.getResource("").getPath(); + + List> classList = new ArrayList<>(classFiles.length); + for (File classFile : classFiles) { + String absolutePath = classFile.getAbsolutePath(); + String classFullName = absolutePath.substring(classpath.length() - 1, absolutePath.lastIndexOf(".")) + .replace(File.separator, "."); + + try { + classList.add(classLoader.loadClass(classFullName)); + } catch (ClassNotFoundException e) { + LOGGER.warn("默认类加载器在 {} 路径下没有找到 {} 类", classpath, classFullName); + } + } + + return classList; + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerPlayListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerPlayListener.java new file mode 100644 index 0000000..1a83655 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerPlayListener.java @@ -0,0 +1,16 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import io.netty.channel.Channel; +import org.nico.ratel.landlords.enums.ClientEventCode; + +public class ClientPokerPlayListener extends AbstractClientListener { + + public ClientPokerPlayListener() { + super(ClientEventCode.CODE_GAME_POKER_PLAY); + } + + @Override + public void handle(Channel channel, String json) { + System.out.println("轮到当前用户出牌"); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerPlayRedirectListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerPlayRedirectListener.java new file mode 100644 index 0000000..feab2c7 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerPlayRedirectListener.java @@ -0,0 +1,31 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import com.alibaba.fastjson.JSONObject; +import io.netty.channel.Channel; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import priv.zxw.ratel.landlords.client.javafx.BeanUtil; +import priv.zxw.ratel.landlords.client.javafx.NettyClient; + +public class ClientPokerPlayRedirectListener extends AbstractClientListener { + private static final Logger LOGGER = LoggerFactory.getLogger(ClientPokerPlayRedirectListener.class); + + public ClientPokerPlayRedirectListener() { + super(ClientEventCode.CODE_GAME_POKER_PLAY_REDIRECT); + } + + @Override + public void handle(Channel channel, String json) { + JSONObject jsonObject = JSONObject.parseObject(json); + + NettyClient nettyClient = BeanUtil.getBean("nettyClient"); + int sellClientId = jsonObject.getIntValue("sellClientId"); + + if (sellClientId == nettyClient.getId()) { + ClientListenerUtils.getListener(ClientEventCode.CODE_GAME_POKER_PLAY).handle(channel, json); + } + + LOGGER.info("下一个出牌玩家为 {} ", jsonObject.getString("sellClinetNickname")); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRestartGameListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRestartGameListener.java new file mode 100644 index 0000000..e5367dd --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRestartGameListener.java @@ -0,0 +1,16 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import io.netty.channel.Channel; +import org.nico.ratel.landlords.enums.ClientEventCode; + +public class ClientRestartGameListener extends AbstractClientListener { + + public ClientRestartGameListener() { + super(ClientEventCode.CODE_GAME_LANDLORD_CYCLE); + } + + @Override + public void handle(Channel channel, String json) { + System.out.println("无人抢地主,重新发牌"); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRoomCreateSuccessfulListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRoomCreateSuccessfulListener.java new file mode 100644 index 0000000..ca5481b --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRoomCreateSuccessfulListener.java @@ -0,0 +1,28 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + + +import io.netty.channel.Channel; +import javafx.application.Platform; +import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.ui.view.Method; +import priv.zxw.ratel.landlords.client.javafx.ui.view.lobby.LobbyController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomMethod; + +public class ClientRoomCreateSuccessfulListener extends AbstractClientListener { + + public ClientRoomCreateSuccessfulListener() { + super(ClientEventCode.CODE_ROOM_CREATE_SUCCESS); + } + + @Override + public void handle(Channel channel, String json) { + Method lobbyMethod = uiService.getMethod(LobbyController.METHOD_NAME); + RoomMethod roomMethod = (RoomMethod) uiService.getMethod(RoomController.METHOD_NAME); + + Platform.runLater(() -> { + lobbyMethod.doClose(); + roomMethod.joinRoom(); + }); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRoomFullJoinFailListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRoomFullJoinFailListener.java new file mode 100644 index 0000000..b6832de --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRoomFullJoinFailListener.java @@ -0,0 +1,23 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import io.netty.channel.Channel; +import javafx.application.Platform; +import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.ui.view.lobby.LobbyController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.lobby.LobbyMethod; + +public class ClientRoomFullJoinFailListener extends AbstractClientListener { + + public ClientRoomFullJoinFailListener() { + super(ClientEventCode.CODE_ROOM_JOIN_FAIL_BY_FULL); + } + + @Override + public void handle(Channel channel, String json) { + LobbyMethod lobbyMethod = (LobbyMethod) uiService.getMethod(LobbyController.METHOD_NAME); + + Platform.runLater(() -> { + lobbyMethod.joinRoomFail("房间已经满人", "该房间人数已满,开始游戏,请挑选其它未满房间进行游戏。"); + }); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRoomNotExistsJoinFailListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRoomNotExistsJoinFailListener.java new file mode 100644 index 0000000..78739df --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientRoomNotExistsJoinFailListener.java @@ -0,0 +1,23 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import io.netty.channel.Channel; +import javafx.application.Platform; +import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.ui.view.lobby.LobbyController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.lobby.LobbyMethod; + +public class ClientRoomNotExistsJoinFailListener extends AbstractClientListener { + + public ClientRoomNotExistsJoinFailListener() { + super(ClientEventCode.CODE_ROOM_JOIN_FAIL_BY_INEXIST); + } + + @Override + public void handle(Channel channel, String json) { + LobbyMethod lobbyMethod = (LobbyMethod) uiService.getMethod(LobbyController.METHOD_NAME); + + Platform.runLater(() -> { + lobbyMethod.joinRoomFail("房间已经不存在", "该房间已经不存在,可能房主已经解散该房间了,请挑选其它房间进行游戏。"); + }); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientSelectLandlordListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientSelectLandlordListener.java new file mode 100644 index 0000000..396a495 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientSelectLandlordListener.java @@ -0,0 +1,38 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import com.alibaba.fastjson.JSONObject; +import io.netty.channel.Channel; +import javafx.application.Platform; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import priv.zxw.ratel.landlords.client.javafx.BeanUtil; +import priv.zxw.ratel.landlords.client.javafx.NettyClient; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomMethod; + +public class ClientSelectLandlordListener extends AbstractClientListener { + private static final Logger LOGGER = LoggerFactory.getLogger(ClientSelectLandlordListener.class); + + public ClientSelectLandlordListener() { + super(ClientEventCode.CODE_GAME_LANDLORD_ELECT); + } + + @Override + public void handle(Channel channel, String json) { + // 计算出玩家的顺序 + JSONObject jsonObject = JSONObject.parseObject(json); + String nextClientNickname = jsonObject.getString("nextClientNickname"); + + // 决定接下来谁抢地主 + int turnClientId = jsonObject.getIntValue("nextClientId"); + NettyClient nettyClient = BeanUtil.getBean("nettyClient"); + + if (turnClientId == nettyClient.getId()) { + RoomMethod method = (RoomMethod) uiService.getMethod(RoomController.METHOD_NAME); + Platform.runLater(() -> method.showRobButtons()); + } + + LOGGER.info("接下来由 {} 抢地主", nextClientNickname); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientSetNicknameListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientSetNicknameListener.java new file mode 100644 index 0000000..21a24d9 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientSetNicknameListener.java @@ -0,0 +1,26 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import io.netty.channel.Channel; +import javafx.application.Platform; +import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.ui.view.Method; +import priv.zxw.ratel.landlords.client.javafx.ui.view.index.IndexController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.login.LoginController; + +public class ClientSetNicknameListener extends AbstractClientListener { + + public ClientSetNicknameListener() { + super(ClientEventCode.CODE_CLIENT_NICKNAME_SET); + } + + @Override + public void handle(Channel channel, String json) { + Method indexMethod = uiService.getMethod(IndexController.METHOD_NAME); + Method loginMethod = uiService.getMethod(LoginController.METHOD_NAME); + + Platform.runLater(() -> { + indexMethod.doClose(); + loginMethod.doShow(); + }); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowOptionsListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowOptionsListener.java new file mode 100644 index 0000000..6baa43e --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowOptionsListener.java @@ -0,0 +1,27 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + + +import io.netty.channel.Channel; +import javafx.application.Platform; +import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.ui.view.Method; +import priv.zxw.ratel.landlords.client.javafx.ui.view.lobby.LobbyController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.login.LoginController; + +public class ClientShowOptionsListener extends AbstractClientListener { + + public ClientShowOptionsListener() { + super(ClientEventCode.CODE_SHOW_OPTIONS); + } + + @Override + public void handle(Channel channel, String json) { + Method loginMethod = uiService.getMethod(LoginController.METHOD_NAME); + Method lobbyMethod = uiService.getMethod(LobbyController.METHOD_NAME); + + Platform.runLater(() -> { + loginMethod.doClose(); + lobbyMethod.doShow(); + }); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowOptionsPVEListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowOptionsPVEListener.java new file mode 100644 index 0000000..9b014c3 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowOptionsPVEListener.java @@ -0,0 +1,22 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import io.netty.channel.Channel; +import org.nico.ratel.landlords.enums.ClientEventCode; + +/** + * @ClassName ClientShowOptionsPVEListener + * @Desc TODO + * @Author zxw + * @Date 2020/8/7 10:14 + * @Version 1.0 + */ +public class ClientShowOptionsPVEListener extends AbstractClientListener { + + public ClientShowOptionsPVEListener() { + super(ClientEventCode.CODE_SHOW_OPTIONS_PVE); + } + + @Override + public void handle(Channel channel, String json) { + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowOptionsPVPListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowOptionsPVPListener.java new file mode 100644 index 0000000..8e57928 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowOptionsPVPListener.java @@ -0,0 +1,28 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import io.netty.channel.Channel; +import javafx.application.Platform; +import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.ui.view.lobby.LobbyController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.lobby.LobbyMethod; + +/** + * @ClassName ClientShowOptionsPVPListener + * @Desc TODO + * @Author zxw + * @Date 2020/8/7 10:13 + * @Version 1.0 + */ +public class ClientShowOptionsPVPListener extends AbstractClientListener { + + public ClientShowOptionsPVPListener() { + super(ClientEventCode.CODE_SHOW_OPTIONS_PVP); + } + + @Override + public void handle(Channel channel, String json) { + LobbyMethod lobbyMethod = (LobbyMethod) uiService.getMethod(LobbyController.METHOD_NAME); + + Platform.runLater(lobbyMethod::toggleToPVPMenu); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowPokersListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowPokersListener.java new file mode 100644 index 0000000..0fd0655 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowPokersListener.java @@ -0,0 +1,41 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import com.alibaba.fastjson.JSONObject; +import io.netty.channel.Channel; +import javafx.application.Platform; +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.BeanUtil; +import priv.zxw.ratel.landlords.client.javafx.entity.CurrentRoomInfo; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomMethod; + +public class ClientShowPokersListener extends AbstractClientListener { + + public ClientShowPokersListener() { + super(ClientEventCode.CODE_SHOW_POKERS); + } + + @Override + public void handle(Channel channel, String json) { + // 显示上一把牌和上一个出牌玩家 + JSONObject jsonObject = JSONObject.parseObject(json); + CurrentRoomInfo currentRoomInfo = BeanUtil.getBean("currentRoomInfo"); + currentRoomInfo.setRecentPlayerName(jsonObject.getString("clientNickname")); + currentRoomInfo.setRecentPokers(jsonObject.getJSONArray("pokers").toJavaList(Poker.class)); + + RoomMethod method = (RoomMethod) uiService.getMethod(RoomController.METHOD_NAME); + + Platform.runLater(() -> { + method.showRecentPokers(currentRoomInfo.getRecentPokers()); + method.showPlayerMessage(currentRoomInfo.getRecentPlayerName(), "出牌"); + }); + + // 显示下一个出牌玩家 + if (jsonObject.containsKey("sellClinetNickname")) { + Platform.runLater(() -> + method.showTimer(jsonObject.getString("sellClinetNickname"), 30) + ); + } + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowRoomsListner.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowRoomsListner.java new file mode 100644 index 0000000..f25c4bf --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowRoomsListner.java @@ -0,0 +1,27 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + + +import com.alibaba.fastjson.JSONArray; +import io.netty.channel.Channel; +import javafx.application.Platform; +import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.entity.RoomInfo; +import priv.zxw.ratel.landlords.client.javafx.ui.view.lobby.LobbyController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.lobby.LobbyMethod; + +import java.util.List; + +public class ClientShowRoomsListner extends AbstractClientListener { + + public ClientShowRoomsListner() { + super(ClientEventCode.CODE_SHOW_ROOMS); + } + + @Override + public void handle(Channel channel, String json) { + List rooms = JSONArray.parseArray(json, RoomInfo.class); + + LobbyMethod method = (LobbyMethod) uiService.getMethod(LobbyController.METHOD_NAME); + Platform.runLater(() -> method.showRoomList(rooms)); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientStartGameListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientStartGameListener.java new file mode 100644 index 0000000..0769973 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientStartGameListener.java @@ -0,0 +1,63 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import com.alibaba.fastjson.JSONObject; +import io.netty.channel.Channel; +import javafx.application.Platform; +import org.nico.ratel.landlords.entity.ClientSide; +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.BeanUtil; +import priv.zxw.ratel.landlords.client.javafx.entity.CurrentRoomInfo; +import priv.zxw.ratel.landlords.client.javafx.entity.User; +import priv.zxw.ratel.landlords.client.javafx.ui.view.Method; +import priv.zxw.ratel.landlords.client.javafx.ui.view.lobby.LobbyController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomMethod; + +import java.util.List; + +public class ClientStartGameListener extends AbstractClientListener { + + public ClientStartGameListener() { + super(ClientEventCode.CODE_GAME_STARTING); + } + + @Override + public void handle(Channel channel, String json) { + JSONObject jsonObject = JSONObject.parseObject(json); + + // 设置用户信息和当前对局房间信息 + User user = BeanUtil.getBean("user"); + user.addPokers(jsonObject.getJSONArray("pokers").toJavaList(Poker.class)); + user.joinRoom(jsonObject.getIntValue("roomId")); + + CurrentRoomInfo currentRoomInfo = new CurrentRoomInfo(jsonObject.getIntValue("roomId"), + jsonObject.getString("roomOwner")); + currentRoomInfo.setPlayer(user); + BeanUtil.addBean("currentRoomInfo", currentRoomInfo); + + // 计算出玩家的顺序 + List clientOrderList = jsonObject.getJSONArray("clientOrderList").toJavaList(ClientSide.class); + ClientSide clientSide = clientOrderList.stream().filter(c -> user.getNickname().equals(c.getNickname())).findFirst().get(); + currentRoomInfo.setPrevPlayerName(clientSide.getPre().getNickname()); + currentRoomInfo.setNextPlayerName(clientSide.getNext().getNickname()); + + // 更新试图 + Method lobbyMethod = uiService.getMethod(LobbyController.METHOD_NAME); + RoomMethod roomMethod = (RoomMethod) uiService.getMethod(RoomController.METHOD_NAME); + + Platform.runLater(() -> { + // 客户端加入房间时,可能没有触发 joinRoomSuccessful 的事件 + // 导致视图未正常切换,判断视图是否切换,否则进行试图切换 + if (!roomMethod.isShow()) { + lobbyMethod.doClose(); + roomMethod.joinRoom(); + } + + roomMethod.startGame(user.getPokers()); + }); + + // 触发抢地主(CODE_GAME_LANDLORD_ELECT)事件 + ClientListenerUtils.getListener(ClientEventCode.CODE_GAME_LANDLORD_ELECT).handle(channel, json); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/UIService.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/UIService.java new file mode 100644 index 0000000..dee6ceb --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/UIService.java @@ -0,0 +1,21 @@ +package priv.zxw.ratel.landlords.client.javafx.ui; + + +import priv.zxw.ratel.landlords.client.javafx.ui.view.Method; + +import java.util.HashMap; +import java.util.Map; + +public class UIService { + private Map methodMap = new HashMap<>(16); + + public void addMethods(Method... methods) { + for (Method method : methods) { + methodMap.put(method.getName(), method); + } + } + + public Method getMethod(String name) { + return methodMap.get(name); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/ILobbyEvent.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/ILobbyEvent.java new file mode 100644 index 0000000..ea96ce9 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/ILobbyEvent.java @@ -0,0 +1,15 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.event; + + +public interface ILobbyEvent { + + void selectPVPModal(); + + void selectPVEModal(); + + void createPVPRoom(); + + void showRooms(); + + void joinRoom(int roomId); +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/ILoginEvent.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/ILoginEvent.java new file mode 100644 index 0000000..df84c07 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/ILoginEvent.java @@ -0,0 +1,7 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.event; + + +public interface ILoginEvent { + + void setNickname(String nickname); +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/IRoomEvent.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/IRoomEvent.java new file mode 100644 index 0000000..021f45f --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/IRoomEvent.java @@ -0,0 +1,7 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.event; + +public interface IRoomEvent { + void robLandlord(); + + void notRobLandlord(); +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/AlertUtils.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/AlertUtils.java new file mode 100644 index 0000000..1120f4f --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/AlertUtils.java @@ -0,0 +1,29 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view; + +import javafx.scene.control.Alert; + +public class AlertUtils { + + public static void info(String headerText, String contentText) { + createAndShowAlert(Alert.AlertType.INFORMATION, headerText, contentText); + } + + private static void createAndShowAlert(Alert.AlertType alertType, + String headerText, String contentText) { + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle(Alert.AlertType.INFORMATION.equals(alertType) ? "信息" : + Alert.AlertType.WARNING.equals(alertType) ? "警告" : "错误"); + alert.setHeaderText(headerText); + alert.setContentText(contentText); + + alert.showAndWait(); + } + + public static void warn(String headerText, String contentText) { + createAndShowAlert(Alert.AlertType.WARNING, headerText, contentText); + } + + public static void error(String headerText, String contentText) { + createAndShowAlert(Alert.AlertType.ERROR, headerText, contentText); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/EventRegister.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/EventRegister.java new file mode 100644 index 0000000..db82869 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/EventRegister.java @@ -0,0 +1,7 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view; + + +public interface EventRegister { + + void registerEvent(); +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/Method.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/Method.java new file mode 100644 index 0000000..69976d5 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/Method.java @@ -0,0 +1,10 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view; + + +public interface Method { + String getName(); + + void doShow(); + + void doClose(); +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/UIObject.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/UIObject.java new file mode 100644 index 0000000..cf52437 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/UIObject.java @@ -0,0 +1,32 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view; + + +import javafx.application.Platform; +import javafx.scene.Parent; +import javafx.stage.Stage; +import priv.zxw.ratel.landlords.client.javafx.BeanUtil; +import priv.zxw.ratel.landlords.client.javafx.NettyClient; + +public abstract class UIObject extends Stage { + + protected Parent root; + + public UIObject() { + setTitle("ratel javafx客户端v1.0.0"); + + // 退出程序 + setOnCloseRequest(e -> { + close(); + Platform.exit(); + + NettyClient nettyClient = BeanUtil.getBean("nettyClient"); + nettyClient.destroy(); + }); + } + + public T $(String id, Class clazz) { + return (T) root.lookup("#" + id); + } + + public abstract void registerEvent(); +} \ No newline at end of file diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/index/IndexController.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/index/IndexController.java new file mode 100644 index 0000000..cd28a98 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/index/IndexController.java @@ -0,0 +1,43 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view.index; + + +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import priv.zxw.ratel.landlords.client.javafx.ui.view.Method; +import priv.zxw.ratel.landlords.client.javafx.ui.view.UIObject; + +import java.io.IOException; + +public class IndexController extends UIObject implements Method { + public static final String METHOD_NAME = "index"; + + private static final String RESOURCE_NAME = "view/index.fxml"; + + public IndexController() throws IOException { + super(); + + root = FXMLLoader.load(getClass().getClassLoader().getResource(RESOURCE_NAME)); + setScene(new Scene(root)); + + registerEvent(); + } + + @Override + public String getName() { + return METHOD_NAME; + } + + @Override + public void doShow() { + super.show(); + } + + @Override + public void doClose() { + super.close(); + } + + @Override + public void registerEvent() {} +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/LobbyController.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/LobbyController.java new file mode 100644 index 0000000..9145522 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/LobbyController.java @@ -0,0 +1,84 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view.lobby; + + +import javafx.collections.ObservableList; +import javafx.fxml.FXMLLoader; +import javafx.scene.Node; +import javafx.scene.Scene; +import javafx.scene.layout.Pane; +import priv.zxw.ratel.landlords.client.javafx.entity.RoomInfo; +import priv.zxw.ratel.landlords.client.javafx.ui.event.ILobbyEvent; +import priv.zxw.ratel.landlords.client.javafx.ui.view.AlertUtils; +import priv.zxw.ratel.landlords.client.javafx.ui.view.UIObject; + +import java.io.IOException; +import java.util.List; + +public class LobbyController extends UIObject implements LobbyMethod { + public static final String METHOD_NAME = "lobby"; + + private static final String RESOURCE_NAME = "view/lobby.fxml"; + + private ILobbyEvent lobbyEvent; + private LobbyEventRegister lobbyEventRegister; + + public LobbyController(ILobbyEvent lobbyEvent) throws IOException { + super(); + + root = FXMLLoader.load(getClass().getClassLoader().getResource(RESOURCE_NAME)); + setScene(new Scene(root)); + + this.lobbyEvent = lobbyEvent; + + registerEvent(); + } + + @Override + public void registerEvent() { + lobbyEventRegister = new LobbyEventRegister(this, lobbyEvent); + } + + @Override + public void toggleToPVPMenu() { + Pane modalPane = $("modalPane", Pane.class); + modalPane.setVisible(false); + + Pane pvpMenuPane = $("pvpMenuPane", Pane.class); + pvpMenuPane.setVisible(true); + } + + @Override + public void toggleToPVEMenu() {} + + @Override + public void showRoomList(List roomInfoList) { + Pane buttonsPane = $("buttonsPane", Pane.class); + buttonsPane.setVisible(false); + + Pane roomsPane = $("roomsPane", Pane.class); + roomsPane.setVisible(true); + + ObservableList children = roomsPane.getChildren(); + roomInfoList.forEach(roomInfo -> children.add(new RoomPane(roomInfo, lobbyEvent).getPane())); + } + + @Override + public void joinRoomFail(String message, String commentMessage) { + AlertUtils.warn(message, commentMessage); + } + + @Override + public String getName() { + return METHOD_NAME; + } + + @Override + public void doShow() { + super.show(); + } + + @Override + public void doClose() { + super.close(); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/LobbyEventRegister.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/LobbyEventRegister.java new file mode 100644 index 0000000..9735f82 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/LobbyEventRegister.java @@ -0,0 +1,44 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view.lobby; + + +import javafx.scene.control.Button; +import priv.zxw.ratel.landlords.client.javafx.ui.event.ILobbyEvent; +import priv.zxw.ratel.landlords.client.javafx.ui.view.EventRegister; +import priv.zxw.ratel.landlords.client.javafx.ui.view.UIObject; + +public class LobbyEventRegister implements EventRegister { + + private UIObject uiObject; + private ILobbyEvent lobbyEvent; + + public LobbyEventRegister(UIObject uiObject, ILobbyEvent lobbyEvent) { + this.uiObject = uiObject; + this.lobbyEvent = lobbyEvent; + + registerEvent(); + } + + @Override + public void registerEvent() { + selectPVPModal(); + selectPVEModal(); + createPVPRoom(); + showRooms(); + } + + private void selectPVPModal() { + uiObject.$("pvpButton", Button.class).setOnAction(e -> lobbyEvent.selectPVPModal()); + } + + private void selectPVEModal() { + uiObject.$("pveButton", Button.class).setOnAction(e -> lobbyEvent.selectPVEModal()); + } + + private void createPVPRoom() { + uiObject.$("createRoomButton", Button.class).setOnAction(e -> lobbyEvent.createPVPRoom()); + } + + private void showRooms() { + uiObject.$("listRoomsButton", Button.class).setOnAction(e -> lobbyEvent.showRooms()); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/LobbyMethod.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/LobbyMethod.java new file mode 100644 index 0000000..185e9fd --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/LobbyMethod.java @@ -0,0 +1,18 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view.lobby; + + +import priv.zxw.ratel.landlords.client.javafx.entity.RoomInfo; +import priv.zxw.ratel.landlords.client.javafx.ui.view.Method; + +import java.util.List; + +public interface LobbyMethod extends Method { + + void toggleToPVPMenu(); + + void toggleToPVEMenu(); + + void showRoomList(List roomInfoList); + + void joinRoomFail(String message, String commentMessage); +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/RoomPane.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/RoomPane.java new file mode 100644 index 0000000..735db05 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/lobby/RoomPane.java @@ -0,0 +1,61 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view.lobby; + + +import javafx.collections.ObservableList; +import javafx.scene.Cursor; +import javafx.scene.Node; +import javafx.scene.control.Label; +import javafx.scene.layout.Pane; +import priv.zxw.ratel.landlords.client.javafx.entity.RoomInfo; +import priv.zxw.ratel.landlords.client.javafx.ui.event.ILobbyEvent; + +public class RoomPane { + private Pane pane; + + private Label idLabel; + private Label roomOwnerLabel; + private Label modalLabel; + private Label clientCountLabel; + + public RoomPane(RoomInfo roomInfo, ILobbyEvent lobbyEvent) { + pane = new Pane(); + pane.setId("roomPane" + roomInfo.getRoomId()); + pane.setPrefSize(200, 150); + pane.setStyle("-fx-border-color: black"); + pane.setCursor(Cursor.HAND); + pane.setOnMouseClicked(e -> lobbyEvent.joinRoom(roomInfo.getRoomId())); + ObservableList children = pane.getChildren(); + + // 房间id label + idLabel = new Label(); + idLabel.setLayoutX(14); + idLabel.setLayoutY(14); + idLabel.setText(roomInfo.getRoomId().toString()); + children.add(idLabel); + + // 房主label + roomOwnerLabel = new Label(); + roomOwnerLabel.setLayoutX(14); + roomOwnerLabel.setLayoutY(40); + roomOwnerLabel.setText("房主:" + roomInfo.getRoomOwner()); + children.add(roomOwnerLabel); + + // 模式label + modalLabel = new Label(); + modalLabel.setLayoutX(14); + modalLabel.setLayoutY(112); + modalLabel.setText(roomInfo.getRoomType() + "模式"); + children.add(modalLabel); + + // 当前人数 + clientCountLabel = new Label(); + clientCountLabel.setLayoutX(136); + clientCountLabel.setLayoutY(112); + clientCountLabel.setText(roomInfo.getRoomClientCount() + "/3人"); + children.add(clientCountLabel); + } + + public Pane getPane() { + return pane; + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/login/LoginController.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/login/LoginController.java new file mode 100644 index 0000000..2be430f --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/login/LoginController.java @@ -0,0 +1,49 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view.login; + +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import priv.zxw.ratel.landlords.client.javafx.ui.event.ILoginEvent; +import priv.zxw.ratel.landlords.client.javafx.ui.view.Method; +import priv.zxw.ratel.landlords.client.javafx.ui.view.UIObject; + +import java.io.IOException; + +public class LoginController extends UIObject implements Method { + public static final String METHOD_NAME = "login"; + + private static final String RESOURCE_NAME = "view/login.fxml"; + + private ILoginEvent loginEvent; + private LoginEventRegister loginEventRegister; + + public LoginController(ILoginEvent loginEvent) throws IOException { + super(); + + root = FXMLLoader.load(getClass().getClassLoader().getResource(RESOURCE_NAME)); + setScene(new Scene(root)); + + this.loginEvent = loginEvent; + + registerEvent(); + } + + @Override + public void registerEvent() { + this.loginEventRegister = new LoginEventRegister(this, loginEvent); + } + + @Override + public String getName() { + return METHOD_NAME; + } + + @Override + public void doShow() { + super.show(); + } + + @Override + public void doClose() { + super.close(); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/login/LoginEventRegister.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/login/LoginEventRegister.java new file mode 100644 index 0000000..5f4fadf --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/login/LoginEventRegister.java @@ -0,0 +1,42 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view.login; + + +import javafx.scene.control.Button; +import javafx.scene.control.TextField; +import priv.zxw.ratel.landlords.client.javafx.ui.event.ILoginEvent; +import priv.zxw.ratel.landlords.client.javafx.ui.view.EventRegister; +import priv.zxw.ratel.landlords.client.javafx.ui.view.UIObject; + +public class LoginEventRegister implements EventRegister { + + private UIObject uiObject; + private ILoginEvent loginEvent; + + public LoginEventRegister(UIObject uiObject, ILoginEvent loginEvent) { + this.uiObject = uiObject; + this.loginEvent = loginEvent; + + registerEvent(); + } + + @Override + public void registerEvent() { + submitNickname(); + } + + private void submitNickname() { + TextField field = uiObject.$("input", TextField.class); + + uiObject.$("submitBtn", Button.class).setOnAction(e -> { + String nickname = field.getText().trim(); + loginEvent.setNickname(nickname); + }); + } + + private void verifyNickname() { + uiObject.$("input", TextField.class).setOnAction(e -> { + TextField field = (TextField) e.getSource(); + String nickname = field.getText().trim(); + }); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/PokerPane.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/PokerPane.java new file mode 100644 index 0000000..71b3162 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/PokerPane.java @@ -0,0 +1,146 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view.room; + +import javafx.collections.ObservableList; +import javafx.scene.Node; +import javafx.scene.layout.Pane; +import javafx.scene.paint.Paint; +import javafx.scene.text.Text; +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.enums.PokerLevel; +import org.nico.ratel.landlords.enums.PokerType; + +public class PokerPane { + private static final double MARGIN_LEFT = 40; + + private Poker poker; + // start at 0 + private int index; + + private Pane pane; + + public PokerPane(int index, Poker poker) { + this.poker = poker; + this.index = index; + + if (PokerLevel.LEVEL_SMALL_KING.equals(poker.getLevel()) || + PokerLevel.LEVEL_BIG_KING.equals(poker.getLevel())) { + createJokerPokerPane(); + } else { + createNormalPokerPane(); + } + + pane.setOnMouseClicked(e -> { + double y = pane.getLayoutY(); + pane.setLayoutY(y == 20 ? y - 20 : y + 20); + }); + } + + private void createNormalPokerPane() { + pane = new Pane(); + pane.getStyleClass().add("horizontal-poker"); + pane.setLayoutX(index * MARGIN_LEFT); + pane.setLayoutY(20); + + Text level = new Text(); + level.getStyleClass().add("level"); + level.setLayoutX(8); + level.setLayoutY(34); + level.setText(poker.getLevel().getName()); + + Text typeSmall = new Text(); + typeSmall.getStyleClass().add("type-small"); + typeSmall.setLayoutX(8); + typeSmall.setLayoutY(60); + typeSmall.setText(poker.getType().getName()); + + Text typeBig = new Text(); + typeBig.getStyleClass().add("type-big"); + typeBig.setLayoutX(50); + typeBig.setLayoutY(120); + typeBig.setText(poker.getType().getName()); + + if (PokerType.CLUB.equals(poker.getType()) || PokerType.SPADE.equals(poker.getType())) { + level.setFill(Paint.valueOf("black")); + typeSmall.setFill(Paint.valueOf("black")); + typeBig.setFill(Paint.valueOf("black")); + } else if (PokerType.DIAMOND.equals(poker.getType()) || PokerType.HEART.equals(poker.getType())) { + level.setFill(Paint.valueOf("#9c2023")); + typeSmall.setFill(Paint.valueOf("#9c2023")); + typeBig.setFill(Paint.valueOf("#9c2023")); + } + + ObservableList children = pane.getChildren(); + children.add(level); + children.add(typeSmall); + children.add(typeBig); + } + + private void createJokerPokerPane() { + pane = new Pane(); + pane.getStyleClass().add("horizontal-poker"); + pane.setLayoutX(index * MARGIN_LEFT); + pane.setLayoutY(20); + + Text text1 = new Text(); + text1.getStyleClass().add("joker-level"); + text1.setLayoutX(13); + text1.setLayoutY(26); + text1.setText("J"); + + Text text2 = new Text(); + text2.getStyleClass().add("joker-level"); + text2.setLayoutX(8); + text2.setLayoutY(44); + text2.setText("O"); + + Text text3 = new Text(); + text3.getStyleClass().add("joker-level"); + text3.setLayoutX(10); + text3.setLayoutY(62); + text3.setText("K"); + + Text text4 = new Text(); + text4.getStyleClass().add("joker-level"); + text4.setLayoutX(10); + text4.setLayoutY(80); + text4.setText("E"); + + Text text5 = new Text(); + text5.getStyleClass().add("joker-level"); + text5.setLayoutX(10); + text5.setLayoutY(98); + text5.setText("R"); + + Text logo = new Text(); + logo.setLayoutX(40); + logo.setLayoutY(126); + logo.setStyle("-fx-font-size: 28"); + logo.setStyle("-fx-text-fill: silver"); + logo.setText("ratel"); + + if (PokerLevel.LEVEL_SMALL_KING.equals(poker.getLevel())) { + text1.setFill(Paint.valueOf("black")); + text2.setFill(Paint.valueOf("black")); + text3.setFill(Paint.valueOf("black")); + text4.setFill(Paint.valueOf("black")); + text5.setFill(Paint.valueOf("black")); + } else if (PokerLevel.LEVEL_BIG_KING.equals(poker.getLevel())) { + text1.setFill(Paint.valueOf("#9c2023")); + text2.setFill(Paint.valueOf("#9c2023")); + text3.setFill(Paint.valueOf("#9c2023")); + text4.setFill(Paint.valueOf("#9c2023")); + text5.setFill(Paint.valueOf("#9c2023")); + } + + ObservableList children = pane.getChildren(); + children.add(text1); + children.add(text2); + children.add(text3); + children.add(text4); + children.add(text5); + } + + public Pane getPane() { + return pane; + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RearPokerPane.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RearPokerPane.java new file mode 100644 index 0000000..fa8b9de --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RearPokerPane.java @@ -0,0 +1,25 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view.room; + +import javafx.scene.layout.Pane; + +public class RearPokerPane { + private static final double MARGIN_TOP = 20; + + // start at 0 + private int index; + + private Pane pane; + + RearPokerPane(int index) { + this.index = index; + + pane = new Pane(); + pane.getStyleClass().add("vertical-poker"); + pane.setLayoutX(15); + pane.setLayoutY(index * MARGIN_TOP); + } + + public Pane getPane() { + return pane; + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomController.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomController.java new file mode 100644 index 0000000..2ad787e --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomController.java @@ -0,0 +1,234 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view.room; + + +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.layout.Pane; +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.enums.ClientType; +import priv.zxw.ratel.landlords.client.javafx.BeanUtil; +import priv.zxw.ratel.landlords.client.javafx.entity.CurrentRoomInfo; +import priv.zxw.ratel.landlords.client.javafx.entity.User; +import priv.zxw.ratel.landlords.client.javafx.ui.event.IRoomEvent; +import priv.zxw.ratel.landlords.client.javafx.ui.view.UIObject; + +import java.io.IOException; +import java.util.List; + +public class RoomController extends UIObject implements RoomMethod { + public static final String METHOD_NAME = "room"; + + private static final String RESOURCE_NAME = "view/room.fxml"; + + private IRoomEvent roomEvent; + private RoomEventRegister roomEventDefine; + + public RoomController(IRoomEvent roomEvent) throws IOException { + super(); + + root = FXMLLoader.load(getClass().getClassLoader().getResource(RESOURCE_NAME)); + setScene(new Scene(root)); + + this.roomEvent = roomEvent; + + registerEvent(); + } + + @Override + public void startGame(List pokers) { + // 1,组件状态改变(遮蔽罩隐藏,游戏面板可用状态改变) + // 2,组件内容填充 + // 3,元素隐藏 + // 4,牌初始化(己方和对方) + $("waitingPane", Pane.class).setVisible(false); + $("playingPane", Pane.class).setDisable(false); + + Button robButton = $("robButton", Button.class); + robButton.setText("抢地主"); + robButton.setVisible(false); + + Button notRobButton = $("notRobButton", Button.class); + notRobButton.setText("不抢"); + notRobButton.setVisible(false); + + $("prevPlayerPane", Pane.class).lookup(".tips").setVisible(false); + $("nextPlayerPane", Pane.class).lookup(".tips").setVisible(false); + $("quitButton", Button.class).setText("退出"); + + initPokers(pokers); + } + + private static final int PER_PLAYER_DEFAULT_POKER_COUNT = 17; + private static final int SURPLUS_POKER_COUNT = 3; + + private void initPokers(List pokers) { + // 己方牌pane + Pane pokersPane = $("pokersPane", Pane.class); + + // 可能之前有牌,先清理再填充 + pokersPane.getChildren().clear(); + for (int i = 0, size = pokers.size(); i < size; i++) { + pokersPane.getChildren().add(new PokerPane(i, pokers.get(i)).getPane()); + } + + // 上下游牌pane + Pane prevPokersPane = $("prevPokersPane", Pane.class); + Pane nextPokersPane = $("nextPokersPane", Pane.class); + + prevPokersPane.getChildren().clear(); + nextPokersPane.getChildren().clear(); + for (int j = 0; j < PER_PLAYER_DEFAULT_POKER_COUNT; j++) { + prevPokersPane.getChildren().add(new RearPokerPane(j).getPane()); + nextPokersPane.getChildren().add(new RearPokerPane(j).getPane()); + } + + // 底牌 + Pane surplusPokersPane = $("surplusPokersPane", Pane.class); + + surplusPokersPane.getChildren().clear(); + for (int n = 0; n < SURPLUS_POKER_COUNT; n++) { + surplusPokersPane.getChildren().add(new SurplusPokerPane(n).getPane()); + } + } + + @Override + public void showRobButtons() { + $("robButton", Button.class).setVisible(true); + $("notRobButton", Button.class).setVisible(true); + } + + @Override + public void hideRobButtons() { + $("robButton", Button.class).setVisible(false); + $("notRobButton", Button.class).setVisible(false); + } + + @Override + public void showSurplusPokers(List surplusPokers) { + Pane surplusPokersPane = $("surplusPokersPane", Pane.class); + + surplusPokersPane.getChildren().clear(); + for (int n = 0, size = surplusPokers.size(); n < size; n++) { + Pane surplusPokerPane = new PokerPane(n, surplusPokers.get(n)).getPane(); + surplusPokerPane.setLayoutX(45 + n * (SurplusPokerPane.MARGIN_LEFT + 110)); + surplusPokerPane.setLayoutY(0); + + surplusPokersPane.getChildren().add(surplusPokerPane); + } + } + + @Override + public void setLandLord(String landlordName) { + // 1,为地主加底牌(重新洗牌) + CurrentRoomInfo currentRoomInfo = BeanUtil.getBean("currentRoomInfo"); + if (ClientType.LANDLORD.equals(currentRoomInfo.getPrevPlayerRole())) { + Pane prevPokersPane = $("prevPokersPane", Pane.class); + + prevPokersPane.getChildren().clear(); + for (int j = 0; j < PER_PLAYER_DEFAULT_POKER_COUNT + SURPLUS_POKER_COUNT; j++) { + prevPokersPane.getChildren().add(new RearPokerPane(j).getPane()); + } + } else if (ClientType.LANDLORD.equals(currentRoomInfo.getNextPlayerRole())) { + Pane nextPokersPane = $("nextPokersPane", Pane.class); + + nextPokersPane.getChildren().clear(); + for (int j = 0; j < PER_PLAYER_DEFAULT_POKER_COUNT + SURPLUS_POKER_COUNT; j++) { + nextPokersPane.getChildren().add(new RearPokerPane(j).getPane()); + } + } else { + Pane pokersPane = $("pokersPane", Pane.class); + User user = BeanUtil.getBean("user"); + List pokerList = user.getPokers(); + + pokersPane.getChildren().clear(); + for (int i = 0, size = pokerList.size(); i < size; i++) { + pokersPane.getChildren().add(new PokerPane(i, pokerList.get(i)).getPane()); + } + } + + // 2,显示每个人的角色(地主|农民)和姓名 + $("prevPlayerRole", Label.class).setText(ClientType.LANDLORD.equals(currentRoomInfo.getPrevPlayerRole()) ? "地主" : "农民"); + $("nextPlayerRole", Label.class).setText(ClientType.LANDLORD.equals(currentRoomInfo.getNextPlayerRole()) ? "地主" : "农民"); + $("playerRole", Label.class).setText(ClientType.LANDLORD.equals(currentRoomInfo.getPlayer().getRole()) ? "地主" : "农民"); + + $("prevPlayerNickname", Label.class).setText(currentRoomInfo.getPrevPlayerName()); + $("nextPlayerNickname", Label.class).setText(currentRoomInfo.getNextPlayerName()); + $("playerNickname", Label.class).setText(currentRoomInfo.getPlayer().getNickname()); + } + + @Override + public void showRecentPokers(List recentPokers) { + Pane showPokersPane = $("showPokersPane", Pane.class); + + showPokersPane.getChildren().clear(); + for (int i = 0, size = recentPokers.size(); i < size; i++) { + showPokersPane.getChildren().add(new PokerPane(i, recentPokers.get(i)).getPane()); + } + } + + @Override + public void showPlayerMessage(String playerName, String message) { + CurrentRoomInfo currentRoomInfo = BeanUtil.getBean("currentRoomInfo"); + + Label tips; + if (playerName.equals(currentRoomInfo.getPrevPlayerName())) { + tips = ((Label) $("prevPlayerPane", Pane.class).lookup(".tips")); + } else if (playerName.equals(currentRoomInfo.getNextPlayerName())) { + tips = ((Label) $("nextPlayerPane", Pane.class).lookup(".tips")); + } else { + tips = ((Label) $("playerPane", Pane.class).lookup(".tips")); + } + + tips.setText(message); + tips.setVisible(true); + } + + @Override + public void showTimer(String playerName, int secondTime) { + CurrentRoomInfo currentRoomInfo = BeanUtil.getBean("currentRoomInfo"); + + Label timer; + if (playerName.equals(currentRoomInfo.getPrevPlayerName())) { + timer = ((Label) $("prevPlayerPane", Pane.class).lookup(".timer")); + } else if (playerName.equals(currentRoomInfo.getNextPlayerName())) { + timer = ((Label) $("nextPlayerPane", Pane.class).lookup(".timer")); + } else { + timer = ((Label) $("playerPane", Pane.class).lookup(".timer")); + } + + timer.setText(String.valueOf(secondTime)); + timer.setVisible(true); + } + + @Override + public boolean isShow() { + return super.isShowing(); + } + + @Override + public void joinRoom() { + super.show(); + } + + @Override + public void doShow() { + super.show(); + } + + @Override + public void doClose() { + super.close(); + } + + @Override + public void registerEvent() { + roomEventDefine = new RoomEventRegister(this, roomEvent); + } + + @Override + public String getName() { + return METHOD_NAME; + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomEventRegister.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomEventRegister.java new file mode 100644 index 0000000..0ae1624 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomEventRegister.java @@ -0,0 +1,46 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view.room; + + +import javafx.scene.control.Button; +import priv.zxw.ratel.landlords.client.javafx.ui.event.IRoomEvent; +import priv.zxw.ratel.landlords.client.javafx.ui.view.EventRegister; +import priv.zxw.ratel.landlords.client.javafx.ui.view.UIObject; + +import java.awt.*; + +public class RoomEventRegister implements EventRegister { + + private UIObject uiObject; + private IRoomEvent roomEvent; + + public RoomEventRegister(UIObject uiObject, IRoomEvent roomEvent) { + this.uiObject = uiObject; + this.roomEvent = roomEvent; + + registerEvent(); + } + + @Override + public void registerEvent() { + robLandlord(); + notRobLandlord(); + } + + private void robLandlord() { + uiObject.$("robButton", Button.class).setOnAction(e -> { + RoomController roomController = (RoomController) uiObject; + roomController.hideRobButtons(); + + roomEvent.robLandlord(); + }); + } + + private void notRobLandlord() { + uiObject.$("notRobButton", Button.class).setOnAction(e -> { + RoomController roomController = (RoomController) uiObject; + roomController.hideRobButtons(); + + roomEvent.notRobLandlord(); + }); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomMethod.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomMethod.java new file mode 100644 index 0000000..1f86bfb --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomMethod.java @@ -0,0 +1,29 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view.room; + + +import org.nico.ratel.landlords.entity.Poker; +import priv.zxw.ratel.landlords.client.javafx.ui.view.Method; + +import java.util.List; + +public interface RoomMethod extends Method { + boolean isShow(); + + void joinRoom(); + + void startGame(List pokers); + + void showRobButtons(); + + void hideRobButtons(); + + void showSurplusPokers(List surplusPokers); + + void setLandLord(String landlordName); + + void showRecentPokers(List recentPokers); + + void showPlayerMessage(String playerName, String message); + + void showTimer(String playerName, int secondTime); +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/SurplusPokerPane.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/SurplusPokerPane.java new file mode 100644 index 0000000..7a9e207 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/SurplusPokerPane.java @@ -0,0 +1,23 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view.room; + +import javafx.scene.layout.Pane; + +public class SurplusPokerPane { + public static final double MARGIN_LEFT = 40; + + private int index; + + private Pane pane; + + public SurplusPokerPane(int index) { + this.index = index; + + pane = new Pane(); + pane.getStyleClass().add("surplus-poker"); + pane.setLayoutX(45 + index * (MARGIN_LEFT + 110)); + } + + public Pane getPane() { + return pane; + } +} diff --git a/landlords-client-javafx/src/main/resources/view/assets/css/poker.css b/landlords-client-javafx/src/main/resources/view/assets/css/poker.css new file mode 100644 index 0000000..f5066a3 --- /dev/null +++ b/landlords-client-javafx/src/main/resources/view/assets/css/poker.css @@ -0,0 +1,13 @@ +.horizontal-poker { + -fx-pref-width: 120; + -fx-pref-height: 140; + -fx-border-color: black; + /*-fx-background-color: #dddbd8;*/ + -fx-background-color: red; + -fx-background-radius: 10; + -fx-border-radius: 10; +} + +.vertical-poker { + +} \ No newline at end of file diff --git a/landlords-client-javafx/src/main/resources/view/assets/css/room.css b/landlords-client-javafx/src/main/resources/view/assets/css/room.css new file mode 100644 index 0000000..edbb159 --- /dev/null +++ b/landlords-client-javafx/src/main/resources/view/assets/css/room.css @@ -0,0 +1,172 @@ +@import "poker.css"; + +#waitingPane { + -fx-background-color: silver; + -fx-opacity: 0.8; +} + +#waitingTips { + -fx-font-size: 20; +} + +#prevPlayerPane, +#nextPlayerPane { + -fx-pref-height: 510; + -fx-pref-width: 220; +} + +#surplusPokersPane { + -fx-pref-height: 140; + -fx-pref-width: 500; + -fx-background-color: silver; + -fx-background-radius: 0 0 10 10; + -fx-border-radius: 0 0 10 10; +} + +#quitButton { + -fx-alignment: center; + -fx-pref-width: 80; + -fx-background-size: 43px 43px; + -fx-background-color: #40b1f5; + -fx-cursor: hand; + -fx-border-width: 1px; + -fx-text-fill: black; + -fx-font-size: 18px; + -fx-border-radius: 8; + -fx-background-radius: 8; +} + +#robButton, +#notRobButton, +#submitButton, +#passButton { + -fx-alignment: center; + -fx-pref-width: 60; + -fx-pref-height: 30; + -fx-background-color: #40b1f5; + -fx-text-fill: black; + -fx-cursor: hand; + -fx-border-width: 1px; + -fx-border-radius: 8; + -fx-background-radius: 8; +} + +#prevPlayerRole, +#nextPlayerRole, +#playerRole { + -fx-alignment: center; + -fx-pref-width: 50; + -fx-pref-height: 25; + -fx-background-color: #a58a65; + -fx-text-fill: black; + -fx-border-color: #afadab; + -fx-background-radius: 6; + -fx-border-radius: 6; +} + +#prevPlayerNickname, +#nextPlayerNickname, +#playerNickname { + -fx-alignment: center; + -fx-pref-width: 100; + -fx-pref-height: 25; + -fx-text-fill: black; + -fx-border-color: black; + -fx-border-width: 0 0 1 0; +} + +.top { + -fx-pref-height: 150; + -fx-pref-width: 1020; +} + +.middle { + -fx-pref-height: 570; + -fx-pref-width: 1020; +} + +.bottom { + -fx-pref-height: 230; + -fx-pref-width: 1020; +} + +.cards { + -fx-alignment: center; + -fx-pref-width: 870; + -fx-pref-height: 180; + -fx-background-color: silver; + -fx-border-radius: 8; + -fx-background-radius: 8; +} + +.otherCards { + -fx-pref-width: 170; + -fx-pref-height: 510; + -fx-background-color: silver; + -fx-border-radius: 8; + -fx-background-radius: 8; +} + +.tips { + -fx-alignment: center; + -fx-pref-height: 30; + -fx-pref-width: 70; + -fx-background-color: silver; + -fx-border-radius: 8; + -fx-background-radius: 8; +} + +.horizontal-poker { + -fx-pref-width: 110; + -fx-pref-height: 140; + -fx-border-color: #afadab; + -fx-background-color: #dddbd8; + -fx-background-radius: 10; + -fx-border-radius: 10; + -fx-cursor: hand; +} + +.horizontal-poker > .level { + -fx-font-size: 35; + -fx-text-fill: black; +} + +.horizontal-poker > .joker-level { + -fx-font-size: 22; +} + +.horizontal-poker > .type-small { + -fx-font-size: 35; + -fx-text-fill: black; +} + +.horizontal-poker > .type-big { + -fx-font-size: 70; + -fx-text-fill: black; +} + +.vertical-poker { + -fx-pref-width: 140; + -fx-pref-height: 90; + -fx-border-color: #716f6d; + -fx-background-image: url("../image/rear.png"); + -fx-background-color: transparent; +} + +.surplus-poker { + -fx-pref-width: 110; + -fx-pref-height: 140; + -fx-border-color: #716f6d; + -fx-background-image: url("../image/rear.png"); +} + +.timer { + -fx-alignment: center; + -fx-font-size: 20; + -fx-pref-width: 60; + -fx-pref-height: 50; + -fx-border-color: #afadab; + -fx-background-color: #dddbd8; + -fx-background-radius: 10; + -fx-border-radius: 10; +} \ No newline at end of file diff --git a/landlords-client-javafx/src/main/resources/view/assets/image/loading.gif b/landlords-client-javafx/src/main/resources/view/assets/image/loading.gif new file mode 100644 index 0000000000000000000000000000000000000000..dad350d4a01ba2f58ef181f44999c35230ebe9d0 GIT binary patch literal 23230 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 literal 0 HcmV?d00001 diff --git a/landlords-client-javafx/src/main/resources/view/assets/image/quit.png b/landlords-client-javafx/src/main/resources/view/assets/image/quit.png new file mode 100644 index 0000000000000000000000000000000000000000..8e05ce605250c4b6c69ac454176623252d5b88b9 GIT binary patch literal 3121 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 LNkvXXu0mjf%W`iV literal 0 HcmV?d00001 diff --git a/landlords-client-javafx/src/main/resources/view/assets/image/rear.png b/landlords-client-javafx/src/main/resources/view/assets/image/rear.png new file mode 100644 index 0000000000000000000000000000000000000000..9af2eb32182271251bfe7bf730616638b0b9e711 GIT binary patch literal 1451 zcmZ9MeN<9s9LKTDFeB3<2dS-W>d72V5N#<*Q_v-1)+yhny-6aWqH|zmW@@QnosN|b zd`mq_W|}q)3*A(Z63I3~Gbr;iGFMW*h3BZbhsyo2Kc4%X-?_j0d+zsszTcY{5==&{ zLa&0sV2Ho~zfiCjfE58Z1J|5bl{?r>QbNhQVGWOL$3S6voJb+UU`>}S<`1s`wRuuN zcnS=*rrWqoWQoFKFxU#;KtJMsPV{&Qk@41Tb@uS$qF}tmr&UG}|D0cW_2`bHI~m29 z#INvE{?ZME?2p`xjK#Ki(%$F+UZuDm<)0Rg&7W*;7#kiP9UVEJn3y=`;lMKJ2zZ4| zCL8IjN974Tt56zQO{UzE{8qw~JTIE_q=ztlM~04(%unqVp9}rndJF}IUKkX zqA)gbi*fKAd6rA?84;Itv(QXsb`opNj!EFuPP7uS!M+Np!}0D8(4ORiGagv>f7^7ZdcSE|4(*EWdOnvDZXbV6a8Jp(6kjt4eQ@bzwn@j!a6$+E!=zKlyVUTD zjkSVTccVkiJN_>#{|04hVIduUr${)!zbrB&k5Q&(W>VpgK#_*6W1T<258d;$ciq?e z^r(2{6<>xpuIIBVm8jbhQI|e~bn?~AXf_MXdeE3tG!pbeZbzVp$SoSZO`rztnsy~e zO%kL0x4+3o2&?{hwaqzZ7t)=Z=mRbx|*gJ&`r>c9kA}*Wh*h{!08eAW1g8`3ykrvZ3{|y`cyS<9n&%7la-{*R8(WA0^ z3ESZGEL{Haadc$_g9Pd23vG;7b z>f{S*A9@HGS2!}eEup;GHiqh{NNlgRKBZbxVl*Iay$-3|ba=>(n-_lzmB+=f9~5!% zUO?I_g|Nr&LqU>xWZE)G`=vchzI9xIjiUIn=sO4ZDm43PHL(RFHVP`+c})37m^P;$ z$mX_nJ0arJMy>*YshaC-bfP-fvnn^%ZsHKhO8!06$=h-*Rg#7BuZQTsocP<&-+6OT zq1omnCUYZth&MXhB1#b{!3_E#-NSWkd-gx2JsS&dEBuCzZ!qxR4nICQR86#NqPyG1 z0GX~C4@-L*$sXF2NCsl~`0_#M>@HoW(U|Qb zRbW>9@^&LYPcg7!1IamwZ@s9!pBT5R*=m!|;8d zod*&zsVhVL7pZiIsICKLFxES5sG!C)LhFyEZB6LQ?E?_;^pJ6jMj)tRbj=XXZGba^ z#abJp_3s0-BBD7u^R;X{_UqCfH6Btp9| + + + + + + + + + diff --git a/landlords-client-javafx/src/main/resources/view/lobby.fxml b/landlords-client-javafx/src/main/resources/view/lobby.fxml new file mode 100644 index 0000000..1a9bc1b --- /dev/null +++ b/landlords-client-javafx/src/main/resources/view/lobby.fxml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/landlords-client-javafx/src/main/resources/view/login.fxml b/landlords-client-javafx/src/main/resources/view/login.fxml new file mode 100644 index 0000000..2b030e2 --- /dev/null +++ b/landlords-client-javafx/src/main/resources/view/login.fxml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + diff --git a/landlords-client-javafx/src/test/java/priv/zxw/ratel/landlords/client/javafx/event/ClientListenerUtilsTests.java b/landlords-client-javafx/src/test/java/priv/zxw/ratel/landlords/client/javafx/event/ClientListenerUtilsTests.java new file mode 100644 index 0000000..c4e8d8b --- /dev/null +++ b/landlords-client-javafx/src/test/java/priv/zxw/ratel/landlords/client/javafx/event/ClientListenerUtilsTests.java @@ -0,0 +1,23 @@ +package priv.zxw.ratel.landlords.client.javafx.event; + +import org.junit.Ignore; +import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.listener.ClientListenerUtils; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsNull.notNullValue; + +public class ClientListenerUtilsTests { + + /** + * always fail + * + * 因为junit启动时的classpath为target/test-classes,而不是target/classes + */ + @Ignore + public void testLoadListener() { + ClientEventCode connectListenerCode = ClientEventCode.CODE_CLIENT_CONNECT; + + assertThat(ClientListenerUtils.getListener(connectListenerCode), notNullValue()); + } +} From ab1e1b30a136e49e28a5c369b63354fa5fb9c466 Mon Sep 17 00:00:00 2001 From: zxw Date: Sun, 9 Aug 2020 23:53:54 +0800 Subject: [PATCH 02/21] =?UTF-8?q?=E5=A2=9E=E5=8A=A0landlords-client-javafx?= =?UTF-8?q?=E5=AD=90=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index c322db6..9f3ba38 100644 --- a/pom.xml +++ b/pom.xml @@ -95,6 +95,7 @@ landlords-client landlords-server landlords-common + landlords-client-javafx From 2fc6003484f63b9f0f3dd72281362324436202b0 Mon Sep 17 00:00:00 2001 From: zxw Date: Mon, 10 Aug 2020 00:00:55 +0800 Subject: [PATCH 03/21] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=AB=AF=20CODE=5FGAME?= =?UTF-8?q?=5FSTARTING=20=E7=9B=91=E5=90=AC=E5=99=A8=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E4=B8=AD=E6=B7=BB=E5=8A=A0=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E6=8E=92=E5=BA=8F=E5=88=97=E8=A1=A8=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E7=8E=A9=E5=AE=B6=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/event/ServerEventListener_CODE_GAME_STARTING.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java index 3e0edbd..5da8f17 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java @@ -54,6 +54,8 @@ public class ServerEventListener_CODE_GAME_STARTING implements ServerEventListen .put("nextClientNickname", startGrabClient.getNickname()) .put("nextClientId", startGrabClient.getId()) .put("pokers", client.getPokers()) + // this key-value use to client order to show + .put("clientOrderList", roomClientList) .json(); if(client.getRole() == ClientRole.PLAYER) { From af1919f4d05d08474b7de13a2285047e7a06ce0b Mon Sep 17 00:00:00 2001 From: zxw Date: Tue, 11 Aug 2020 00:17:41 +0800 Subject: [PATCH 04/21] =?UTF-8?q?=E7=8E=A9=E5=AE=B6=E5=87=BA=E7=89=8C?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/javafx/entity/CurrentRoomInfo.java | 37 +++++++ .../landlords/client/javafx/entity/User.java | 6 ++ .../client/javafx/event/RoomEvent.java | 23 +++++ .../listener/ClientCantPassListener.java | 32 ++++++ .../javafx/listener/ClientKickListener.java | 32 ++++++ .../listener/ClientPokerInvalidListener.java | 33 +++++++ .../listener/ClientPokerLessListener.java | 32 ++++++ .../listener/ClientPokerMismatchListener.java | 32 ++++++ .../listener/ClientPokerPlayListener.java | 26 ++++- .../listener/ClientShowPokersListener.java | 56 +++++++++-- .../client/javafx/ui/event/IRoomEvent.java | 8 ++ .../client/javafx/ui/view/CountDownTask.java | 93 ++++++++++++++++++ .../client/javafx/ui/view/UIObject.java | 79 +++++++++++++-- .../client/javafx/ui/view/room/PokerPane.java | 16 ++- .../javafx/ui/view/room/RoomController.java | 98 +++++++++++-------- .../ui/view/room/RoomEventRegister.java | 45 ++++++++- .../javafx/ui/view/room/RoomMethod.java | 13 ++- .../main/resources/view/assets/css/room.css | 9 ++ .../src/main/resources/view/room.fxml | 2 +- .../listener/ClientListenerUtilsTests.java | 23 +++++ 20 files changed, 636 insertions(+), 59 deletions(-) create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientCantPassListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientKickListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerInvalidListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerLessListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerMismatchListener.java create mode 100644 landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/CountDownTask.java create mode 100644 landlords-client-javafx/src/test/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientListenerUtilsTests.java diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/CurrentRoomInfo.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/CurrentRoomInfo.java index ccd2782..1d9b7d8 100644 --- a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/CurrentRoomInfo.java +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/CurrentRoomInfo.java @@ -3,6 +3,7 @@ package priv.zxw.ratel.landlords.client.javafx.entity; import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.enums.ClientType; +import java.util.ArrayList; import java.util.List; public class CurrentRoomInfo { @@ -12,15 +13,20 @@ public class CurrentRoomInfo { private User player; private String prevPlayerName; private ClientType prevPlayerRole; + private int prevPlayerSurplusPokerCount; private String nextPlayerName; private ClientType nextPlayerRole; + private int nextPlayerSurplusPokerCount; private String recentPlayerName; private List recentPokers; + private List checkedPokers; + public CurrentRoomInfo(int roomId, String roomOwner) { this.roomId = roomId; this.roomOwner = roomOwner; + this.checkedPokers = new ArrayList<>(); } public void setLandlord(String landlordName) { @@ -85,4 +91,35 @@ public class CurrentRoomInfo { public ClientType getNextPlayerRole() { return nextPlayerRole; } + + public void addCheckedPoker(Poker poker) { + checkedPokers.add(poker); + } + + public void removeUncheckedPoker(Poker poker) { + checkedPokers.remove(poker); + } + + public List pollCheckedPokers() { + List pokers = new ArrayList<>(checkedPokers); + checkedPokers.clear(); + + return pokers; + } + + public int getPrevPlayerSurplusPokerCount() { + return prevPlayerSurplusPokerCount; + } + + public void setPrevPlayerSurplusPokerCount(int prevPlayerSurplusPokerCount) { + this.prevPlayerSurplusPokerCount = prevPlayerSurplusPokerCount; + } + + public int getNextPlayerSurplusPokerCount() { + return nextPlayerSurplusPokerCount; + } + + public void setNextPlayerSurplusPokerCount(int nextPlayerSurplusPokerCount) { + this.nextPlayerSurplusPokerCount = nextPlayerSurplusPokerCount; + } } diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/User.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/User.java index 10ae40d..8633c64 100644 --- a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/User.java +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/entity/User.java @@ -45,6 +45,12 @@ public class User { .collect(Collectors.toList()); } + public void removePokers(List sellPokerList) { + for (Poker sellPoker : sellPokerList) { + pokers.remove(sellPoker); + } + } + public List getPokers() { return pokers; } diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/RoomEvent.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/RoomEvent.java index fdeaeb7..3286545 100644 --- a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/RoomEvent.java +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/event/RoomEvent.java @@ -1,12 +1,18 @@ package priv.zxw.ratel.landlords.client.javafx.event; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import io.netty.channel.Channel; import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.enums.ServerEventCode; import priv.zxw.ratel.landlords.client.javafx.BeanUtil; import priv.zxw.ratel.landlords.client.javafx.ui.event.IRoomEvent; +import java.util.List; +import java.util.stream.Collectors; + public class RoomEvent implements IRoomEvent { @Override @@ -22,4 +28,21 @@ public class RoomEvent implements IRoomEvent { ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "FALSE"); } + + @Override + public void submitPokers(List pokerList) { + Channel channel = BeanUtil.getBean("channel"); + + String[] chars = pokerList.stream() + .map(p -> p.getLevel().getName()).collect(Collectors.toList()) + .toArray(new String[] {}); + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY, JSONObject.toJSONString(chars)); + } + + @Override + public void passRound() { + Channel channel = BeanUtil.getBean("channel"); + + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_PASS, null); + } } diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientCantPassListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientCantPassListener.java new file mode 100644 index 0000000..8d26b6e --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientCantPassListener.java @@ -0,0 +1,32 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import io.netty.channel.Channel; +import javafx.application.Platform; +import javafx.scene.control.Label; +import javafx.scene.layout.Pane; +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomController; + +public class ClientCantPassListener extends AbstractClientListener { + + public ClientCantPassListener() { + super(ClientEventCode.CODE_GAME_POKER_PLAY_CANT_PASS); + } + + @Override + public void handle(Channel channel, String json) { + // 不允许跳过,即简单的不响应用户操作即可 + RoomController roomController = (RoomController) uiService.getMethod(RoomController.METHOD_NAME); + + Platform.runLater(() -> { + Label tips = ((Label) roomController.$("playerPane", Pane.class).lookup(".primary-tips")); + tips.setVisible(true); + tips.setText("不能跳过"); + roomController.delayHidden(tips, 2); + }); + + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT, null); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientKickListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientKickListener.java new file mode 100644 index 0000000..0873ff3 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientKickListener.java @@ -0,0 +1,32 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + +import io.netty.channel.Channel; +import javafx.application.Platform; +import javafx.scene.control.Label; +import javafx.scene.layout.Pane; +import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.ui.view.Method; +import priv.zxw.ratel.landlords.client.javafx.ui.view.lobby.LobbyController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomController; + +public class ClientKickListener extends AbstractClientListener { + + public ClientKickListener() { + super(ClientEventCode.CODE_CLIENT_KICK); + } + + @Override + public void handle(Channel channel, String json) { + Method lobbyMethod = uiService.getMethod(LobbyController.METHOD_NAME); + RoomController roomController = (RoomController) uiService.getMethod(RoomController.METHOD_NAME); + + Platform.runLater(() -> { + Label tips = ((Label) roomController.$("playerPane", Pane.class).lookup(".primary-tips")); + tips.setVisible(true); + tips.setText("长时间未操作,请出房间"); + + roomController.doClose(); + lobbyMethod.doShow(); + }); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerInvalidListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerInvalidListener.java new file mode 100644 index 0000000..36b6ddc --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerInvalidListener.java @@ -0,0 +1,33 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + + +import io.netty.channel.Channel; +import javafx.application.Platform; +import javafx.scene.control.Label; +import javafx.scene.layout.Pane; +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomController; + +public class ClientPokerInvalidListener extends AbstractClientListener { + + public ClientPokerInvalidListener() { + super(ClientEventCode.CODE_GAME_POKER_PLAY_INVALID); + } + + @Override + public void handle(Channel channel, String json) { + // 牌无效,不允许出牌,即简单的不响应用户操作即可 + RoomController roomController = (RoomController) uiService.getMethod(RoomController.METHOD_NAME); + + Platform.runLater(() -> { + Label tips = ((Label) roomController.$("playerPane", Pane.class).lookup(".primary-tips")); + tips.setVisible(true); + tips.setText("牌不符合规则"); + roomController.delayHidden(tips, 2); + }); + + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT, null); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerLessListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerLessListener.java new file mode 100644 index 0000000..1266eaa --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerLessListener.java @@ -0,0 +1,32 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + + +import io.netty.channel.Channel; +import javafx.application.Platform; +import javafx.scene.control.Label; +import javafx.scene.layout.Pane; +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomController; + +public class ClientPokerLessListener extends AbstractClientListener { + + public ClientPokerLessListener() { + super(ClientEventCode.CODE_GAME_POKER_PLAY_LESS); + } + + @Override + public void handle(Channel channel, String json) { + // 出牌太少,不允许出牌,即简单的不响应用户操作即可 + RoomController roomController = (RoomController) uiService.getMethod(RoomController.METHOD_NAME); + Platform.runLater(() -> { + Label tips = ((Label) roomController.$("playerPane", Pane.class).lookup(".primary-tips")); + tips.setVisible(true); + tips.setText("牌不符合规则"); + roomController.delayHidden(tips, 2); + }); + + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT, null); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerMismatchListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerMismatchListener.java new file mode 100644 index 0000000..261255d --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerMismatchListener.java @@ -0,0 +1,32 @@ +package priv.zxw.ratel.landlords.client.javafx.listener; + + +import io.netty.channel.Channel; +import javafx.application.Platform; +import javafx.scene.control.Label; +import javafx.scene.layout.Pane; +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomController; + +public class ClientPokerMismatchListener extends AbstractClientListener { + + public ClientPokerMismatchListener() { + super(ClientEventCode.CODE_GAME_POKER_PLAY_MISMATCH); + } + + @Override + public void handle(Channel channel, String json) { + // 出牌不匹配,不允许出牌,即简单的不响应用户操作即可 + RoomController roomController = (RoomController) uiService.getMethod(RoomController.METHOD_NAME); + Platform.runLater(() -> { + Label tips = ((Label) roomController.$("playerPane", Pane.class).lookup(".primary-tips")); + tips.setVisible(true); + tips.setText("牌不符合规则"); + roomController.delayHidden(tips, 2); + }); + + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT, null); + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerPlayListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerPlayListener.java index 1a83655..025a954 100644 --- a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerPlayListener.java +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientPokerPlayListener.java @@ -1,7 +1,14 @@ package priv.zxw.ratel.landlords.client.javafx.listener; import io.netty.channel.Channel; +import javafx.application.Platform; +import javafx.scene.control.Label; import org.nico.ratel.landlords.enums.ClientEventCode; +import priv.zxw.ratel.landlords.client.javafx.BeanUtil; +import priv.zxw.ratel.landlords.client.javafx.entity.User; +import priv.zxw.ratel.landlords.client.javafx.ui.view.CountDownTask; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomController; +import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomMethod; public class ClientPokerPlayListener extends AbstractClientListener { @@ -11,6 +18,23 @@ public class ClientPokerPlayListener extends AbstractClientListener { @Override public void handle(Channel channel, String json) { - System.out.println("轮到当前用户出牌"); + // TODO 1,重新渲染玩家手中的牌 + + + // 2,展示按钮 + RoomMethod roomMethod = (RoomMethod) uiService.getMethod(RoomController.METHOD_NAME); + Platform.runLater(() -> roomMethod.showPokerPlayButtons()); + + // 3,设置倒计时定时器 + User user = BeanUtil.getBean("user"); + Label timer = (Label) roomMethod.getTimer(user.getNickname()); + + // 在倒计时任务最终被执行后,隐藏页面元素, + // 让服务端扫描到该客户端长时间未操作,发送踢出事件将其踢出 + CountDownTask task = new CountDownTask(timer, 30, + node -> Platform.runLater(() -> roomMethod.hidePokerPlayButtons()), + surplusTime -> Platform.runLater(() -> timer.setText(surplusTime.toString()))); + CountDownTask.CountDownFuture future = task.start(); + BeanUtil.addBean(user.getNickname(), future); } } diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowPokersListener.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowPokersListener.java index 0fd0655..1c2e86e 100644 --- a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowPokersListener.java +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/listener/ClientShowPokersListener.java @@ -3,13 +3,19 @@ package priv.zxw.ratel.landlords.client.javafx.listener; import com.alibaba.fastjson.JSONObject; import io.netty.channel.Channel; import javafx.application.Platform; +import javafx.scene.Node; +import javafx.scene.control.Label; import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.enums.ClientEventCode; import priv.zxw.ratel.landlords.client.javafx.BeanUtil; import priv.zxw.ratel.landlords.client.javafx.entity.CurrentRoomInfo; +import priv.zxw.ratel.landlords.client.javafx.entity.User; +import priv.zxw.ratel.landlords.client.javafx.ui.view.CountDownTask; import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomController; import priv.zxw.ratel.landlords.client.javafx.ui.view.room.RoomMethod; +import java.util.List; + public class ClientShowPokersListener extends AbstractClientListener { public ClientShowPokersListener() { @@ -18,24 +24,56 @@ public class ClientShowPokersListener extends AbstractClientListener { @Override public void handle(Channel channel, String json) { - // 显示上一把牌和上一个出牌玩家 JSONObject jsonObject = JSONObject.parseObject(json); - CurrentRoomInfo currentRoomInfo = BeanUtil.getBean("currentRoomInfo"); - currentRoomInfo.setRecentPlayerName(jsonObject.getString("clientNickname")); - currentRoomInfo.setRecentPokers(jsonObject.getJSONArray("pokers").toJavaList(Poker.class)); + // 找到上一个玩家的倒计时定时器将其关闭 + String clientNickname = jsonObject.getString("clientNickname"); + CountDownTask.CountDownFuture future = BeanUtil.getBean(clientNickname); + if (future != null && !future.isDone()) { + future.cancel(); + } + + User user = BeanUtil.getBean("user"); + CurrentRoomInfo currentRoomInfo = BeanUtil.getBean("currentRoomInfo"); RoomMethod method = (RoomMethod) uiService.getMethod(RoomController.METHOD_NAME); + // 把当前玩家的出牌和不出牌按钮隐藏掉 + // 更新玩家手中的牌 + List sellPokerList = jsonObject.getJSONArray("pokers").toJavaList(Poker.class); + user.removePokers(sellPokerList); + + if (user.getNickname().equals(clientNickname)) { + Platform.runLater(() -> { + method.hidePokerPlayButtons(); + method.refreshPlayPokers(user.getPokers()); + }); + } else { + int sellPokerCount = sellPokerList.size(); + + if (currentRoomInfo.getPrevPlayerName().equals(clientNickname)) { + currentRoomInfo.setPrevPlayerSurplusPokerCount(currentRoomInfo.getPrevPlayerSurplusPokerCount() - sellPokerCount); + Platform.runLater(() -> method.refreshPrevPlayerPokers(currentRoomInfo.getPrevPlayerSurplusPokerCount())); + } else if (currentRoomInfo.getNextPlayerName().equals(clientNickname)) { + currentRoomInfo.setNextPlayerSurplusPokerCount(currentRoomInfo.getNextPlayerSurplusPokerCount() - sellPokerCount); + Platform.runLater(() -> method.refreshNextPlayerPokers(currentRoomInfo.getNextPlayerSurplusPokerCount())); + } + } + + // 显示上一把牌和上一个出牌玩家 + currentRoomInfo.setRecentPlayerName(clientNickname); + currentRoomInfo.setRecentPokers(sellPokerList); + Platform.runLater(() -> { method.showRecentPokers(currentRoomInfo.getRecentPokers()); method.showPlayerMessage(currentRoomInfo.getRecentPlayerName(), "出牌"); }); - // 显示下一个出牌玩家 - if (jsonObject.containsKey("sellClinetNickname")) { - Platform.runLater(() -> - method.showTimer(jsonObject.getString("sellClinetNickname"), 30) - ); + // 找到下一个出牌玩家设置其定时器 + String nextPlayerName = jsonObject.getString("sellClinetNickname"); + if (nextPlayerName != null && !user.getNickname().equals(nextPlayerName)) { + Label timer = (Label) method.getTimer(nextPlayerName); + CountDownTask task = new CountDownTask(timer, 30, n -> {}, i -> Platform.runLater(() -> timer.setText(i.toString()))); + BeanUtil.addBean(nextPlayerName, task.start()); } } } diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/IRoomEvent.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/IRoomEvent.java index 021f45f..ab5e884 100644 --- a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/IRoomEvent.java +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/event/IRoomEvent.java @@ -1,7 +1,15 @@ package priv.zxw.ratel.landlords.client.javafx.ui.event; +import org.nico.ratel.landlords.entity.Poker; + +import java.util.List; + public interface IRoomEvent { void robLandlord(); void notRobLandlord(); + + void submitPokers(List pokerList); + + void passRound(); } diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/CountDownTask.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/CountDownTask.java new file mode 100644 index 0000000..0fcaa43 --- /dev/null +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/CountDownTask.java @@ -0,0 +1,93 @@ +package priv.zxw.ratel.landlords.client.javafx.ui.view; + + +import javafx.scene.Node; + +import java.util.Objects; +import java.util.function.Consumer; + +public class CountDownTask { + public static final int DEFAULT_TIME_OUT = 30; + + private Consumer finallyExecuteConsumer; + private Consumer preSecondExecuteConsumer; + private Node targetElement; + private int duration; + + public CountDownTask(Node targetElement, + Consumer finallyExecuteConsumer, Consumer preSecondExecuteConsumer) { + this.finallyExecuteConsumer = finallyExecuteConsumer; + this.preSecondExecuteConsumer = preSecondExecuteConsumer; + this.targetElement = targetElement; + } + + public CountDownTask(Node targetElement, int duration, + Consumer finallyExecuteConsumer, Consumer preSecondExecuteConsumer) { + Objects.requireNonNull(finallyExecuteConsumer); + + this.finallyExecuteConsumer = finallyExecuteConsumer; + this.preSecondExecuteConsumer = preSecondExecuteConsumer; + this.duration = duration < 0 ? DEFAULT_TIME_OUT : duration; + this.targetElement = targetElement; + } + + public CountDownFuture start() { + targetElement.setVisible(true); + + CountDownFuture future = new CountDownFuture(); + future.start(); + + return future; + } + + public class CountDownFuture extends Thread { + + private volatile boolean done = false; + + private CountDownFuture() { + setDaemon(true); + } + + @Override + public void run() { + long startTimeMillis = System.currentTimeMillis(); + + long interval; + try { + while (true) { + long currentTimeMillis = System.currentTimeMillis(); + interval = (currentTimeMillis - startTimeMillis) / 1000; + + if (interval > duration) { + break; + } + + preSecondExecuteConsumer.accept((int) (duration - interval)); + + sleep(1000L); + } + + finallyExecuteConsumer.accept(targetElement); + } catch (InterruptedException cancelFlag) { + // exit + return; + } finally { + done = true; + targetElement.setVisible(false); + } + } + + public void cancel() { + if (done) { + return; + } + + done = true; + interrupt(); + } + + public boolean isDone() { + return done; + } + } +} diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/UIObject.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/UIObject.java index cf52437..f7cb2ba 100644 --- a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/UIObject.java +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/UIObject.java @@ -1,32 +1,99 @@ package priv.zxw.ratel.landlords.client.javafx.ui.view; +import javafx.animation.FadeTransition; import javafx.application.Platform; +import javafx.scene.Node; import javafx.scene.Parent; import javafx.stage.Stage; +import javafx.util.Duration; import priv.zxw.ratel.landlords.client.javafx.BeanUtil; import priv.zxw.ratel.landlords.client.javafx.NettyClient; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.function.Consumer; + public abstract class UIObject extends Stage { protected Parent root; + private ExecutorService animationExecutorService = Executors.newFixedThreadPool(2); + public UIObject() { setTitle("ratel javafx客户端v1.0.0"); // 退出程序 - setOnCloseRequest(e -> { - close(); - Platform.exit(); + setOnCloseRequest(e -> closeApplication()); + } - NettyClient nettyClient = BeanUtil.getBean("nettyClient"); - nettyClient.destroy(); - }); + private void closeApplication() { + // 关闭视图 + close(); + Platform.exit(); + + // 关闭用于动画执行的线程池 + animationExecutorService.shutdownNow(); + + // 关闭netty + NettyClient nettyClient = BeanUtil.getBean("nettyClient"); + nettyClient.destroy(); } public T $(String id, Class clazz) { return (T) root.lookup("#" + id); } + public void delayShow(Node node, int secondDelay) { + animationExecutorService.execute(new DelayRunnable(node, n -> n.setVisible(true), secondDelay)); + } + + public void delayHidden(Node node, int secondDelay) { + animationExecutorService.execute(new DelayRunnable(node, n -> { + // 动画过程中的元素是无效的,不能被操作的 + n.setDisable(true); + useTransition(n); + n.setDisable(false); + n.setVisible(false); + }, secondDelay)); + } + + private class DelayRunnable implements Runnable { + private Object monitor = new Object(); + private Node node; + private Consumer operate; + private int delayTimes; + + DelayRunnable(Node node, Consumer operate, int delayTimes) { + this.node = node; + this.operate = operate; + this.delayTimes = delayTimes; + } + + @Override + public void run() { + try { + synchronized (monitor) { + monitor.wait(delayTimes * 1000L); + } + } catch (InterruptedException e) { + Thread.currentThread().isInterrupted(); + } + + operate.accept(node); + } + } + + private void useTransition(Node node) { + FadeTransition fade = new FadeTransition(); + fade.setDuration(Duration.millis(1000)); + fade.setFromValue(1); + fade.setToValue(0.1); + fade.setCycleCount(1000); + fade.setAutoReverse(true); + fade.setNode(node); + fade.play(); + } + public abstract void registerEvent(); } \ No newline at end of file diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/PokerPane.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/PokerPane.java index 71b3162..68a5bf8 100644 --- a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/PokerPane.java +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/PokerPane.java @@ -8,6 +8,8 @@ import javafx.scene.text.Text; import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.enums.PokerLevel; import org.nico.ratel.landlords.enums.PokerType; +import priv.zxw.ratel.landlords.client.javafx.BeanUtil; +import priv.zxw.ratel.landlords.client.javafx.entity.CurrentRoomInfo; public class PokerPane { private static final double MARGIN_LEFT = 40; @@ -31,7 +33,19 @@ public class PokerPane { pane.setOnMouseClicked(e -> { double y = pane.getLayoutY(); - pane.setLayoutY(y == 20 ? y - 20 : y + 20); + boolean alreadyChecked = y == 0; + CurrentRoomInfo currentRoomInfo = BeanUtil.getBean("currentRoomInfo"); + + // 取消选中 + if (alreadyChecked) { + pane.setLayoutY(y + 20); + currentRoomInfo.removeUncheckedPoker(poker); + } + // 选中 + else { + pane.setLayoutY(y - 20); + currentRoomInfo.addCheckedPoker(poker); + } }); } diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomController.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomController.java index 2ad787e..a52a13e 100644 --- a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomController.java +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomController.java @@ -2,6 +2,7 @@ package priv.zxw.ratel.landlords.client.javafx.ui.view.room; import javafx.fxml.FXMLLoader; +import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; @@ -65,24 +66,11 @@ public class RoomController extends UIObject implements RoomMethod { private void initPokers(List pokers) { // 己方牌pane - Pane pokersPane = $("pokersPane", Pane.class); - - // 可能之前有牌,先清理再填充 - pokersPane.getChildren().clear(); - for (int i = 0, size = pokers.size(); i < size; i++) { - pokersPane.getChildren().add(new PokerPane(i, pokers.get(i)).getPane()); - } + refreshPlayPokers(pokers); // 上下游牌pane - Pane prevPokersPane = $("prevPokersPane", Pane.class); - Pane nextPokersPane = $("nextPokersPane", Pane.class); - - prevPokersPane.getChildren().clear(); - nextPokersPane.getChildren().clear(); - for (int j = 0; j < PER_PLAYER_DEFAULT_POKER_COUNT; j++) { - prevPokersPane.getChildren().add(new RearPokerPane(j).getPane()); - nextPokersPane.getChildren().add(new RearPokerPane(j).getPane()); - } + refreshPrevPlayerPokers(PER_PLAYER_DEFAULT_POKER_COUNT); + refreshNextPlayerPokers(PER_PLAYER_DEFAULT_POKER_COUNT); // 底牌 Pane surplusPokersPane = $("surplusPokersPane", Pane.class); @@ -93,6 +81,38 @@ public class RoomController extends UIObject implements RoomMethod { } } + @Override + public void refreshPlayPokers(List pokers) { + Pane pokersPane = $("pokersPane", Pane.class); + + // 可能之前有牌,先清理再填充 + pokersPane.getChildren().clear(); + for (int i = 0, size = pokers.size(); i < size; i++) { + pokersPane.getChildren().add(new PokerPane(i, pokers.get(i)).getPane()); + } + } + + @Override + public void refreshPrevPlayerPokers(int pokerCount) { + Pane prevPokersPane = $("prevPokersPane", Pane.class); + + prevPokersPane.getChildren().clear(); + for (int j = 0; j < pokerCount; j++) { + prevPokersPane.getChildren().add(new RearPokerPane(j).getPane()); + } + } + + @Override + public void refreshNextPlayerPokers(int pokerCount) { + Pane nextPokersPane = $("nextPokersPane", Pane.class); + + nextPokersPane.getChildren().clear(); + for (int j = 0; j < pokerCount; j++) { + nextPokersPane.getChildren().add(new RearPokerPane(j).getPane()); + } + } + + @Override public void showRobButtons() { $("robButton", Button.class).setVisible(true); @@ -124,28 +144,16 @@ public class RoomController extends UIObject implements RoomMethod { // 1,为地主加底牌(重新洗牌) CurrentRoomInfo currentRoomInfo = BeanUtil.getBean("currentRoomInfo"); if (ClientType.LANDLORD.equals(currentRoomInfo.getPrevPlayerRole())) { - Pane prevPokersPane = $("prevPokersPane", Pane.class); - - prevPokersPane.getChildren().clear(); - for (int j = 0; j < PER_PLAYER_DEFAULT_POKER_COUNT + SURPLUS_POKER_COUNT; j++) { - prevPokersPane.getChildren().add(new RearPokerPane(j).getPane()); - } + currentRoomInfo.setPrevPlayerSurplusPokerCount(PER_PLAYER_DEFAULT_POKER_COUNT + SURPLUS_POKER_COUNT); + currentRoomInfo.setNextPlayerSurplusPokerCount(PER_PLAYER_DEFAULT_POKER_COUNT); + refreshPrevPlayerPokers(PER_PLAYER_DEFAULT_POKER_COUNT + SURPLUS_POKER_COUNT); } else if (ClientType.LANDLORD.equals(currentRoomInfo.getNextPlayerRole())) { - Pane nextPokersPane = $("nextPokersPane", Pane.class); - - nextPokersPane.getChildren().clear(); - for (int j = 0; j < PER_PLAYER_DEFAULT_POKER_COUNT + SURPLUS_POKER_COUNT; j++) { - nextPokersPane.getChildren().add(new RearPokerPane(j).getPane()); - } + currentRoomInfo.setNextPlayerSurplusPokerCount(PER_PLAYER_DEFAULT_POKER_COUNT + SURPLUS_POKER_COUNT); + currentRoomInfo.setPrevPlayerSurplusPokerCount(PER_PLAYER_DEFAULT_POKER_COUNT); + refreshNextPlayerPokers(PER_PLAYER_DEFAULT_POKER_COUNT + SURPLUS_POKER_COUNT); } else { - Pane pokersPane = $("pokersPane", Pane.class); User user = BeanUtil.getBean("user"); - List pokerList = user.getPokers(); - - pokersPane.getChildren().clear(); - for (int i = 0, size = pokerList.size(); i < size; i++) { - pokersPane.getChildren().add(new PokerPane(i, pokerList.get(i)).getPane()); - } + refreshPlayPokers(user.getPokers()); } // 2,显示每个人的角色(地主|农民)和姓名 @@ -178,15 +186,16 @@ public class RoomController extends UIObject implements RoomMethod { } else if (playerName.equals(currentRoomInfo.getNextPlayerName())) { tips = ((Label) $("nextPlayerPane", Pane.class).lookup(".tips")); } else { - tips = ((Label) $("playerPane", Pane.class).lookup(".tips")); + tips = ((Label) $("playerPane", Pane.class).lookup(".primary-tips")); } tips.setText(message); tips.setVisible(true); + delayHidden(tips, 3); } @Override - public void showTimer(String playerName, int secondTime) { + public Node getTimer(String playerName) { CurrentRoomInfo currentRoomInfo = BeanUtil.getBean("currentRoomInfo"); Label timer; @@ -198,8 +207,19 @@ public class RoomController extends UIObject implements RoomMethod { timer = ((Label) $("playerPane", Pane.class).lookup(".timer")); } - timer.setText(String.valueOf(secondTime)); - timer.setVisible(true); + return timer; + } + + @Override + public void showPokerPlayButtons() { + $("submitButton", Button.class).setVisible(true); + $("passButton", Button.class).setVisible(true); + } + + @Override + public void hidePokerPlayButtons() { + $("submitButton", Button.class).setVisible(false); + $("passButton", Button.class).setVisible(false); } @Override diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomEventRegister.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomEventRegister.java index 0ae1624..e95c52f 100644 --- a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomEventRegister.java +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomEventRegister.java @@ -1,12 +1,18 @@ package priv.zxw.ratel.landlords.client.javafx.ui.view.room; +import javafx.scene.Node; import javafx.scene.control.Button; +import org.nico.ratel.landlords.entity.Poker; +import priv.zxw.ratel.landlords.client.javafx.BeanUtil; +import priv.zxw.ratel.landlords.client.javafx.entity.CurrentRoomInfo; +import priv.zxw.ratel.landlords.client.javafx.entity.User; import priv.zxw.ratel.landlords.client.javafx.ui.event.IRoomEvent; +import priv.zxw.ratel.landlords.client.javafx.ui.view.CountDownTask; import priv.zxw.ratel.landlords.client.javafx.ui.view.EventRegister; import priv.zxw.ratel.landlords.client.javafx.ui.view.UIObject; -import java.awt.*; +import java.util.List; public class RoomEventRegister implements EventRegister { @@ -24,6 +30,8 @@ public class RoomEventRegister implements EventRegister { public void registerEvent() { robLandlord(); notRobLandlord(); + submitPokers(); + passRound(); } private void robLandlord() { @@ -43,4 +51,39 @@ public class RoomEventRegister implements EventRegister { roomEvent.notRobLandlord(); }); } + + private void submitPokers() { + uiObject.$("submitButton", Button.class).setOnAction(e -> { + CurrentRoomInfo currentRoomInfo = BeanUtil.getBean("currentRoomInfo"); + List checkedPokers = currentRoomInfo.pollCheckedPokers(); + + if (checkedPokers.isEmpty()) { + return; + } + + // 执行对应的事件 + roomEvent.submitPokers(checkedPokers); + }); + } + + private void hidePlayButtonsAndCancelCountDownTask() { + // 取消CountDownFuture,如果CountDownFuture已经结束(代表用户已经操作超时)则什么都不做 + User user = BeanUtil.getBean("user"); + CountDownTask.CountDownFuture future = BeanUtil.getBean(user.getNickname()); + if (future.isDone()) { + return; + } + + future.cancel(); + + // 隐藏对应的 buttons 和 timer + RoomController roomController = (RoomController) uiObject; + roomController.hidePokerPlayButtons(); + } + + private void passRound() { + uiObject.$("passButton", Button.class).setOnAction(e -> { + roomEvent.passRound(); + }); + } } diff --git a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomMethod.java b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomMethod.java index 1f86bfb..8a25faa 100644 --- a/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomMethod.java +++ b/landlords-client-javafx/src/main/java/priv/zxw/ratel/landlords/client/javafx/ui/view/room/RoomMethod.java @@ -1,6 +1,7 @@ package priv.zxw.ratel.landlords.client.javafx.ui.view.room; +import javafx.scene.Node; import org.nico.ratel.landlords.entity.Poker; import priv.zxw.ratel.landlords.client.javafx.ui.view.Method; @@ -13,6 +14,12 @@ public interface RoomMethod extends Method { void startGame(List pokers); + void refreshPlayPokers(List pokers); + + void refreshPrevPlayerPokers(int pokerCount); + + void refreshNextPlayerPokers(int pokerCount); + void showRobButtons(); void hideRobButtons(); @@ -25,5 +32,9 @@ public interface RoomMethod extends Method { void showPlayerMessage(String playerName, String message); - void showTimer(String playerName, int secondTime); + Node getTimer(String playerName); + + void showPokerPlayButtons(); + + void hidePokerPlayButtons(); } diff --git a/landlords-client-javafx/src/main/resources/view/assets/css/room.css b/landlords-client-javafx/src/main/resources/view/assets/css/room.css index edbb159..90aa9ef 100644 --- a/landlords-client-javafx/src/main/resources/view/assets/css/room.css +++ b/landlords-client-javafx/src/main/resources/view/assets/css/room.css @@ -116,6 +116,15 @@ -fx-background-radius: 8; } +.primary-tips { + -fx-alignment: center; + -fx-pref-height: 30; + -fx-pref-width: 140; + -fx-background-color: silver; + -fx-border-radius: 8; + -fx-background-radius: 8; +} + .horizontal-poker { -fx-pref-width: 110; -fx-pref-height: 140; diff --git a/landlords-client-javafx/src/main/resources/view/room.fxml b/landlords-client-javafx/src/main/resources/view/room.fxml index 8bb4a97..d0fc3cb 100644 --- a/landlords-client-javafx/src/main/resources/view/room.fxml +++ b/landlords-client-javafx/src/main/resources/view/room.fxml @@ -80,7 +80,7 @@