From d8bbde8e63fc6542b6e2ff808a635e262eee0a8b Mon Sep 17 00:00:00 2001 From: doveeeee Date: Wed, 14 Jul 2021 18:42:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=87=86=E5=A4=87=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=20=E6=B8=B8=E6=88=8F=E7=BB=93=E6=9D=9F=E5=90=8E?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA=E6=88=BF?= =?UTF-8?q?=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ClientEventListener_CODE_GAME_OVER.java | 3 +- ...entEventListener_CODE_GAME_POKER_PLAY.java | 2 +- .../ClientEventListener_CODE_GAME_READY.java | 37 +++++++++++++ .../ClientEventListener_CODE_GAME_WATCH.java | 1 - ...ventListener_CODE_ROOM_CREATE_SUCCESS.java | 2 + ...tEventListener_CODE_ROOM_JOIN_SUCCESS.java | 1 + .../org/nico/ratel/landlords/entity/Room.java | 2 +- .../landlords/enums/ClientEventCode.java | 2 + .../ratel/landlords/enums/ClientStatus.java | 2 +- .../landlords/enums/ServerEventCode.java | 2 + ...verEventListener_CODE_GAME_POKER_PLAY.java | 4 +- .../ServerEventListener_CODE_GAME_READY.java | 54 +++++++++++++++++++ ...erverEventListener_CODE_GAME_STARTING.java | 1 + .../ServerEventListener_CODE_ROOM_CREATE.java | 5 +- .../ServerEventListener_CODE_ROOM_JOIN.java | 5 +- 15 files changed, 110 insertions(+), 13 deletions(-) create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_READY.java create mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_OVER.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_OVER.java index 4136ca5..a75e0e7 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_OVER.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_OVER.java @@ -13,7 +13,6 @@ public class ClientEventListener_CODE_GAME_OVER extends ClientEventListener { public void call(Channel channel, String data) { Map map = MapHelper.parser(data); SimplePrinter.printNotice("\nPlayer " + map.get("winnerNickname") + "[" + map.get("winnerType") + "]" + " won the game"); - SimplePrinter.printNotice("Game over, friendship first, competition second\n"); + ClientEventListener_CODE_GAME_READY.gameReady(channel); } - } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY.java index 9136c0d..cbb2427 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY.java @@ -54,7 +54,7 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY extends ClientEventListene List lastSellPokers = Noson.convert(lastSellPokersObj, new NoType>() { }); List sells = PokerHelper.validSells(PokerHelper.checkPokerType(lastSellPokers), pokers); - if (sells == null || sells.size() == 0) { + if (sells.size() == 0) { SimplePrinter.printNotice("It is a pity that, there is no winning combination..."); call(channel, data); return; diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_READY.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_READY.java new file mode 100644 index 0000000..6ebfc89 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_READY.java @@ -0,0 +1,37 @@ +package org.nico.ratel.landlords.client.event; + +import io.netty.channel.Channel; +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.client.SimpleClient; +import org.nico.ratel.landlords.enums.ServerEventCode; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.print.SimplePrinter; +import org.nico.ratel.landlords.print.SimpleWriter; + +import java.util.Map; + +public class ClientEventListener_CODE_GAME_READY extends ClientEventListener { + @Override + public void call(Channel channel, String data) { + Map map = MapHelper.parser(data); + if (SimpleClient.id == (int) map.get("clientId")) { + SimplePrinter.printNotice("you are ready to play game."); + return; + } + SimplePrinter.printNotice(map.get("clientNickName").toString() + " is ready to play game."); + } + + static void gameReady(Channel channel) { + SimplePrinter.printNotice("\nPress any key to get ready(Press [exit|e] to exit the room)"); + String line = SimpleWriter.write("notReady"); + if (line.equals("")) { + gameReady(channel); + return; + } + if (line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("e")) { + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT, ""); + return; + } + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_READY, ""); + } +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_WATCH.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_WATCH.java index 1a65451..54dc969 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_WATCH.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_WATCH.java @@ -135,7 +135,6 @@ public class ClientEventListener_CODE_GAME_WATCH extends ClientEventListener { Map map = MapHelper.parser(rawData.toString()); printNoticeWithTime("Player [" + map.get("winnerNickname") + "](" + map.get("winnerType") + ") won the game."); - quitWatch(channel); } private void printKickInfo(Object rawData) { diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_CREATE_SUCCESS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_CREATE_SUCCESS.java index 024ec65..d3dfa44 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_CREATE_SUCCESS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_CREATE_SUCCESS.java @@ -17,6 +17,8 @@ public class ClientEventListener_CODE_ROOM_CREATE_SUCCESS extends ClientEventLis SimplePrinter.printNotice("You have created a room with id " + room.getId()); SimplePrinter.printNotice("Please wait for other players to join !"); + + ClientEventListener_CODE_GAME_READY.gameReady(channel); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_SUCCESS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_SUCCESS.java index 3c4509d..090a4ad 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_SUCCESS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_SUCCESS.java @@ -20,6 +20,7 @@ public class ClientEventListener_CODE_ROOM_JOIN_SUCCESS extends ClientEventListe if (SimpleClient.id == joinClientId) { SimplePrinter.printNotice("You have joined room:" + map.get("roomId") + ". There are " + map.get("roomClientCount") + " players in the room now."); SimplePrinter.printNotice("Please wait for other players to join. The game would start at three players!"); + ClientEventListener_CODE_GAME_READY.gameReady(channel); } else { SimplePrinter.printNotice(map.get("clientNickname") + " joined room, there are currently " + map.get("roomClientCount") + " in the room."); } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java index 751aebc..64c565a 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java @@ -51,7 +51,7 @@ public class Room { this.id = id; this.clientSideMap = new ConcurrentSkipListMap<>(); this.clientSideList = new LinkedList<>(); - this.status = RoomStatus.BLANK; + this.status = RoomStatus.WAIT; } public final long getCreateTime() { diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java index 9df5fa5..972b4de 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java @@ -62,6 +62,8 @@ public enum ClientEventCode implements Serializable { CODE_PVE_DIFFICULTY_NOT_SUPPORT("人机难度不支持"), + CODE_GAME_READY("准备开始游戏"), + CODE_GAME_WATCH("观战"), CODE_GAME_WATCH_SUCCESSFUL("观战成功"); diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java index efbf92e..fed401f 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java @@ -4,7 +4,7 @@ public enum ClientStatus { TO_CHOOSE, - NO_READY, + NOT_READY, READY, diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerEventCode.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerEventCode.java index 2d3dc8c..0ad997e 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerEventCode.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerEventCode.java @@ -22,6 +22,8 @@ public enum ServerEventCode implements Serializable { CODE_GAME_STARTING("游戏开始"), + CODE_GAME_READY("玩家准备"), + CODE_GAME_LANDLORD_ELECT("抢地主"), CODE_GAME_POKER_PLAY("出牌环节"), diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY.java index 92dba8b..62d2f8b 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY.java @@ -101,12 +101,12 @@ public class ServerEventListener_CODE_GAME_POKER_PLAY implements ServerEventList for(ClientSide client: room.getClientSideList()) { if(client.getRole() == ClientRole.PLAYER) { ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, result); + client.setStatus(ClientStatus.NOT_READY); } } + room.setStatus(RoomStatus.WAIT); notifyWatcherGameOver(room, result); - - ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(clientSide, data); } else { if(next.getRole() == ClientRole.PLAYER) { ServerEventListener.get(ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT).call(next, result); diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java new file mode 100644 index 0000000..6868de7 --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java @@ -0,0 +1,54 @@ +package org.nico.ratel.landlords.server.event; + +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.entity.ClientSide; +import org.nico.ratel.landlords.entity.Room; +import org.nico.ratel.landlords.enums.*; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.print.SimplePrinter; +import org.nico.ratel.landlords.server.ServerContains; + +import java.util.concurrent.ConcurrentSkipListMap; + +public class ServerEventListener_CODE_GAME_READY implements ServerEventListener { + @Override + public void call(ClientSide clientSide, String data) { + Room room = ServerContains.getRoom(clientSide.getRoomId()); + SimplePrinter.serverLog("房间状态:" + room.getStatus()); + SimplePrinter.serverLog("玩家状态:" + clientSide.getStatus()); + if (room.getStatus() == RoomStatus.STARTING) { + return; + } + if (clientSide.getStatus() == ClientStatus.PLAYING || clientSide.getStatus() == ClientStatus.TO_CHOOSE || clientSide.getStatus() == ClientStatus.CALL_LANDLORD) { + return; + } + clientSide.setStatus(clientSide.getStatus() == ClientStatus.READY ? ClientStatus.NOT_READY : ClientStatus.READY); + String result = MapHelper.newInstance() + .put("clientNickName", clientSide.getNickname()) + .put("status", clientSide.getStatus()) + .put("clientId", clientSide.getId()) + .json(); + boolean allReady = true; + ConcurrentSkipListMap roomClientMap = (ConcurrentSkipListMap) room.getClientSideMap(); + if (roomClientMap.size() < 3) { + allReady = false; + } else { + for (ClientSide client : room.getClientSideList()) { + if (client.getRole() == ClientRole.PLAYER && client.getStatus() != ClientStatus.READY) { + allReady = false; + break; + } + } + } + + for (ClientSide client : room.getClientSideList()) { + if (client.getRole() == ClientRole.PLAYER) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_READY, result); + } + } + + if (allReady) { + ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, data); + } + } +} 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 4e7cc71..cd32d9d 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 @@ -43,6 +43,7 @@ public class ServerEventListener_CODE_GAME_STARTING implements ServerEventListen for (ClientSide client : roomClientList) { client.setType(ClientType.PEASANT); + client.setStatus(ClientStatus.PLAYING); String result = MapHelper.newInstance() .put("roomId", room.getId()) diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java index 274ffd7..77e8b1c 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java @@ -5,6 +5,7 @@ import org.nico.ratel.landlords.channel.ChannelUtils; import org.nico.ratel.landlords.entity.ClientSide; import org.nico.ratel.landlords.entity.Room; import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ClientStatus; import org.nico.ratel.landlords.enums.RoomStatus; import org.nico.ratel.landlords.enums.RoomType; import org.nico.ratel.landlords.server.ServerContains; @@ -15,7 +16,7 @@ public class ServerEventListener_CODE_ROOM_CREATE implements ServerEventListener public void call(ClientSide clientSide, String data) { Room room = new Room(ServerContains.getServerId()); - room.setStatus(RoomStatus.BLANK); + room.setStatus(RoomStatus.WAIT); room.setType(RoomType.PVP); room.setRoomOwner(clientSide.getNickname()); room.getClientSideMap().put(clientSide.getId(), clientSide); @@ -27,6 +28,8 @@ public class ServerEventListener_CODE_ROOM_CREATE implements ServerEventListener clientSide.setRoomId(room.getId()); ServerContains.addRoom(room); + clientSide.setStatus(ClientStatus.NOT_READY); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_CREATE_SUCCESS, Noson.reversal(room)); } } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_JOIN.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_JOIN.java index ffddc41..ce7655d 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_JOIN.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_JOIN.java @@ -9,7 +9,6 @@ import org.nico.ratel.landlords.entity.Room; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.ClientStatus; import org.nico.ratel.landlords.enums.RoomStatus; -import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.server.ServerContains; @@ -52,12 +51,10 @@ public class ServerEventListener_CODE_ROOM_JOIN implements ServerEventListener { if (roomClientMap.size() == 3) { clientSide.setNext(roomClientList.getFirst()); roomClientList.getFirst().setPre(clientSide); - ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(room.getId())); - return; } room.setStatus(RoomStatus.WAIT); - + clientSide.setStatus(ClientStatus.NOT_READY); String result = MapHelper.newInstance() .put("clientId", clientSide.getId()) .put("clientNickname", clientSide.getNickname())