From 491e9426af6c959e6d09c6d1f7783069368b2fe0 Mon Sep 17 00:00:00 2001 From: nico Date: Thu, 8 Nov 2018 17:14:04 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=BA=A4=E4=BA=92?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../landlords/client/ClientContains.java | 11 ------ .../client/event/ClientEventListener.java | 17 +++----- .../ClientEventListener_CODE_CONNECT.java | 30 -------------- ...ventListener_CODE_CREATE_ROOM_SUCCESS.java | 24 ++++++++++++ .../ClientEventListener_CODE_GAME_OVER.java | 4 +- ...lientEventListener_CODE_GRAB_LANDLORD.java | 10 ++--- ...tEventListener_CODE_JOIN_ROOM_SUCCESS.java | 4 +- .../ClientEventListener_CODE_PLAY_ROUND.java | 12 +++--- ...lientEventListener_CODE_ROOM_STARTING.java | 4 +- ...ClientEventListener_CODE_SET_NICKNAME.java | 23 +++++++++++ ...ClientEventListener_CODE_SHOW_OPTIONS.java | 28 ++++++------- .../ClientEventListener_CODE_SHOW_POKERS.java | 4 +- ...ientEventListener_CODE_SHOW_ROOM_LIST.java | 17 ++++---- .../client/handler/TransferHandler.java | 4 +- .../ratel/landlords/channel/ChannelUtils.java | 14 +++---- .../landlords/entity/ClientTransferData.java | 37 ++++++++---------- .../org/nico/ratel/landlords/entity/Room.java | 3 ++ .../landlords/entity/ServerTransferData.java | 39 ++++++------------- .../landlords/enums/ClientEventCode.java | 2 +- .../landlords/enums/ServerEventCode.java | 2 +- .../landlords/server/ServerContains.java | 3 -- .../server/event/ServerEventListener.java | 10 ++--- .../ServerEventListener_CODE_CREATE_ROOM.java | 11 ++---- ...erverEventListener_CODE_GET_ROOM_LIST.java | 12 +++--- ...erverEventListener_CODE_GRAB_LANDLORD.java | 10 ++--- .../ServerEventListener_CODE_JOIN_ROOM.java | 17 ++++---- .../ServerEventListener_CODE_PLAYER_EXIT.java | 17 ++++---- .../ServerEventListener_CODE_PLAY_ROUND.java | 19 ++++----- .../ServerEventListener_CODE_RENAME.java | 23 ----------- ...ServerEventListener_CODE_SET_NICKNAME.java | 18 +++++++++ .../server/event/helper/RoomHelper.java | 3 +- .../server/handler/TransferHandler.java | 12 +++--- pom.xml | 2 +- 33 files changed, 196 insertions(+), 250 deletions(-) delete mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/ClientContains.java delete mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CONNECT.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CREATE_ROOM_SUCCESS.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SET_NICKNAME.java delete mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_RENAME.java create mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_SET_NICKNAME.java diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/ClientContains.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/ClientContains.java deleted file mode 100644 index 7ccafcd..0000000 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/ClientContains.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.nico.ratel.landlords.client; - -import org.nico.ratel.landlords.entity.ClientSide; - -public class ClientContains { - - public final static int port = 1024; - - public static ClientSide clientSide = null; - -} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java index d9800d9..faf0798 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java @@ -4,22 +4,21 @@ import java.util.HashMap; import java.util.Map; import org.nico.ratel.landlords.channel.ChannelUtils; -import org.nico.ratel.landlords.client.ClientContains; import org.nico.ratel.landlords.entity.ClientTransferData; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.ServerEventCode; import io.netty.channel.Channel; -public abstract class ClientEventListener { +public abstract class ClientEventListener { - public abstract void call(Channel channel, ClientTransferData clientTransferData); + public abstract void call(Channel channel, ClientTransferData clientTransferData); - public final static Map> LISTENER_MAP = new HashMap<>(); + public final static Map LISTENER_MAP = new HashMap<>(); private final static String LISTENER_PREFIX = "org.nico.ratel.landlords.client.event.ClientEventListener_"; - public static ClientEventListener get(ClientEventCode code){ + public static ClientEventListener get(ClientEventCode code){ ClientEventListener listener = null; try { if(ClientEventListener.LISTENER_MAP.containsKey(code)){ @@ -37,11 +36,7 @@ public abstract class ClientEventListener { return listener; } - protected void pushToServer(Channel channel, ServerEventCode code, Object datas){ - int clientId = -1; - if(ClientContains.clientSide != null){ - clientId = ClientContains.clientSide.getId(); - } - ChannelUtils.pushToServer(channel, clientId, code, datas); + protected void pushToServer(Channel channel, ServerEventCode code, String datas){ + ChannelUtils.pushToServer(channel, code, datas); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CONNECT.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CONNECT.java deleted file mode 100644 index 5329ec1..0000000 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CONNECT.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.nico.ratel.landlords.client.event; - -import org.nico.ratel.landlords.channel.ChannelUtils; -import org.nico.ratel.landlords.client.ClientContains; -import org.nico.ratel.landlords.entity.ClientSide; -import org.nico.ratel.landlords.entity.ClientTransferData; -import org.nico.ratel.landlords.enums.ServerEventCode; -import org.nico.ratel.landlords.print.SimplePrinter; -import org.nico.ratel.landlords.print.SimpleWriter; - -import io.netty.channel.Channel; - -public class ClientEventListener_CODE_CONNECT extends ClientEventListener{ - - @Override - public void call(Channel channel, ClientTransferData clientTransferData) { - SimplePrinter.println("You has been join Nico-Landlords, please set your nickname: "); - String nickname = SimpleWriter.write(); - - ClientSide clientSide = clientTransferData.getData(); - clientSide.setNickname(nickname); - - ClientContains.clientSide = clientSide; - - pushToServer(channel, ServerEventCode.CODE_RENAME, clientSide); - } - - - -} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CREATE_ROOM_SUCCESS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CREATE_ROOM_SUCCESS.java new file mode 100644 index 0000000..88de92c --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CREATE_ROOM_SUCCESS.java @@ -0,0 +1,24 @@ +package org.nico.ratel.landlords.client.event; + +import java.util.Map; + +import org.nico.noson.Noson; +import org.nico.ratel.landlords.entity.ClientTransferData; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_CREATE_ROOM_SUCCESS extends ClientEventListener{ + + @Override + public void call(Channel channel, ClientTransferData clientTransferData) { + + Map room = Noson.parseMap(clientTransferData.getData()); + + SimplePrinter.println("You has been create room:" + room); + SimplePrinter.println("Please wait for other players to join !"); + } + + + +} 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 4386c9c..2ac0574 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 @@ -5,10 +5,10 @@ import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; -public class ClientEventListener_CODE_GAME_OVER extends ClientEventListener{ +public class ClientEventListener_CODE_GAME_OVER extends ClientEventListener{ @Override - public void call(Channel channel, ClientTransferData clientTransferData) { + public void call(Channel channel, ClientTransferData clientTransferData) { SimplePrinter.println("Game over"); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GRAB_LANDLORD.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GRAB_LANDLORD.java index 59eb8c7..6bc0574 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GRAB_LANDLORD.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GRAB_LANDLORD.java @@ -1,7 +1,5 @@ package org.nico.ratel.landlords.client.event; -import org.nico.ratel.landlords.channel.ChannelUtils; -import org.nico.ratel.landlords.client.ClientContains; import org.nico.ratel.landlords.entity.ClientTransferData; import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.print.SimplePrinter; @@ -9,16 +7,16 @@ import org.nico.ratel.landlords.print.SimpleWriter; import io.netty.channel.Channel; -public class ClientEventListener_CODE_GRAB_LANDLORD extends ClientEventListener{ +public class ClientEventListener_CODE_GRAB_LANDLORD extends ClientEventListener{ @Override - public void call(Channel channel, ClientTransferData clientTransferData) { + public void call(Channel channel, ClientTransferData clientTransferData) { SimplePrinter.print("It's your turn to rob the landlord[Y/N]:"); String line = SimpleWriter.write(); if(line.equalsIgnoreCase("Y")){ - pushToServer(channel, ServerEventCode.CODE_GRAB_LANDLORD, true); + pushToServer(channel, ServerEventCode.CODE_GRAB_LANDLORD, "TRUE"); }else if(line.equalsIgnoreCase("N")){ - pushToServer(channel, ServerEventCode.CODE_GRAB_LANDLORD, false); + pushToServer(channel, ServerEventCode.CODE_GRAB_LANDLORD, "FALSE"); }else{ SimplePrinter.println("Invalid options"); call(channel, clientTransferData); diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_JOIN_ROOM_SUCCESS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_JOIN_ROOM_SUCCESS.java index 5adfd02..f800947 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_JOIN_ROOM_SUCCESS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_JOIN_ROOM_SUCCESS.java @@ -6,10 +6,10 @@ import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; -public class ClientEventListener_CODE_JOIN_ROOM_SUCCESS extends ClientEventListener{ +public class ClientEventListener_CODE_JOIN_ROOM_SUCCESS extends ClientEventListener{ @Override - public void call(Channel channel, ClientTransferData clientTransferData) { + public void call(Channel channel, ClientTransferData clientTransferData) { SimplePrinter.println("You has been join room:" + clientTransferData.getData()); SimplePrinter.println("Please wait for other players to join !"); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_PLAY_ROUND.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_PLAY_ROUND.java index 3b74b11..860afe7 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_PLAY_ROUND.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_PLAY_ROUND.java @@ -1,8 +1,10 @@ package org.nico.ratel.landlords.client.event; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import org.nico.noson.Noson; import org.nico.ratel.landlords.entity.ClientTransferData; import org.nico.ratel.landlords.enums.PokerLevel; import org.nico.ratel.landlords.enums.PokerType; @@ -13,10 +15,10 @@ import org.nico.ratel.landlords.utils.OptionsUtils; import io.netty.channel.Channel; -public class ClientEventListener_CODE_PLAY_ROUND extends ClientEventListener{ +public class ClientEventListener_CODE_PLAY_ROUND extends ClientEventListener{ @Override - public void call(Channel channel, ClientTransferData clientTransferData) { + public void call(Channel channel, ClientTransferData clientTransferData) { SimplePrinter.println(clientTransferData.getData()); SimplePrinter.println("Please enter the number you want:"); String line = SimpleWriter.write(); @@ -26,7 +28,7 @@ public class ClientEventListener_CODE_PLAY_ROUND extends ClientEventListener{ +public class ClientEventListener_CODE_ROOM_STARTING extends ClientEventListener{ @Override - public void call(Channel channel, ClientTransferData clientTransferData) { + public void call(Channel channel, ClientTransferData clientTransferData) { SimplePrinter.println(clientTransferData.getData() + " game starting !"); SimplePrinter.println("Wait for the server to issue the license..."); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SET_NICKNAME.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SET_NICKNAME.java new file mode 100644 index 0000000..fc8b372 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SET_NICKNAME.java @@ -0,0 +1,23 @@ +package org.nico.ratel.landlords.client.event; + +import org.nico.ratel.landlords.entity.ClientTransferData; +import org.nico.ratel.landlords.enums.ServerEventCode; +import org.nico.ratel.landlords.print.SimplePrinter; +import org.nico.ratel.landlords.print.SimpleWriter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_SET_NICKNAME extends ClientEventListener{ + + @Override + public void call(Channel channel, ClientTransferData clientTransferData) { + SimplePrinter.println("You has been join Nico-Landlords, please set your nickname: "); + String nickname = SimpleWriter.write(); + + + pushToServer(channel, ServerEventCode.CODE_SET_NICKNAME, nickname); + } + + + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java index d2e4811..56fe855 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java @@ -1,13 +1,7 @@ package org.nico.ratel.landlords.client.event; -import java.util.Map; -import java.util.Map.Entry; -import java.util.concurrent.ConcurrentSkipListMap; - -import org.nico.ratel.landlords.channel.ChannelUtils; -import org.nico.ratel.landlords.client.ClientContains; +import org.nico.noson.Noson; import org.nico.ratel.landlords.entity.ClientTransferData; -import org.nico.ratel.landlords.entity.Room; import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.print.SimplePrinter; import org.nico.ratel.landlords.print.SimpleWriter; @@ -15,10 +9,10 @@ import org.nico.ratel.landlords.utils.OptionsUtils; import io.netty.channel.Channel; -public class ClientEventListener_CODE_SHOW_OPTIONS extends ClientEventListener{ +public class ClientEventListener_CODE_SHOW_OPTIONS extends ClientEventListener{ @Override - public void call(Channel channel, ClientTransferData clientTransferData) { + public void call(Channel channel, ClientTransferData clientTransferData) { SimplePrinter.println("Options: "); SimplePrinter.println("1. Create Room"); SimplePrinter.println("2. Room List"); @@ -38,14 +32,14 @@ public class ClientEventListener_CODE_SHOW_OPTIONS extends ClientEventListener{ +public class ClientEventListener_CODE_SHOW_POKERS extends ClientEventListener{ @Override - public void call(Channel channel, ClientTransferData clientTransferData) { + public void call(Channel channel, ClientTransferData clientTransferData) { SimplePrinter.println(clientTransferData.getData()); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOM_LIST.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOM_LIST.java index d1db870..b801f2c 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOM_LIST.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOM_LIST.java @@ -3,32 +3,29 @@ package org.nico.ratel.landlords.client.event; import java.util.Map.Entry; import java.util.concurrent.ConcurrentSkipListMap; -import org.nico.ratel.landlords.channel.ChannelUtils; -import org.nico.ratel.landlords.client.ClientContains; -import org.nico.ratel.landlords.entity.ClientSide; +import org.nico.noson.Noson; +import org.nico.noson.entity.NoType; import org.nico.ratel.landlords.entity.ClientTransferData; import org.nico.ratel.landlords.entity.Room; import org.nico.ratel.landlords.enums.ClientEventCode; -import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.print.SimplePrinter; -import org.nico.ratel.landlords.print.SimpleWriter; import io.netty.channel.Channel; -public class ClientEventListener_CODE_SHOW_ROOM_LIST extends ClientEventListener>{ +public class ClientEventListener_CODE_SHOW_ROOM_LIST extends ClientEventListener{ @Override - public void call(Channel channel, ClientTransferData> clientTransferData) { - ConcurrentSkipListMap roomMap = clientTransferData.getData(); + public void call(Channel channel, ClientTransferData clientTransferData) { + ConcurrentSkipListMap roomMap = Noson.convert(clientTransferData.getData(), new NoType>() {}); if(roomMap != null && ! roomMap.isEmpty()){ SimplePrinter.println("Room list: "); for(Entry roomEntry: roomMap.entrySet()) { SimplePrinter.println(roomEntry.getKey() + "\t|\t" + roomEntry.getValue()); } - get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, new ClientTransferData<>()); + get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, new ClientTransferData()); }else { SimplePrinter.println("No available room, please create a room !"); - get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, new ClientTransferData<>()); + get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, new ClientTransferData()); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java index f894ba2..4b41fb3 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java @@ -17,8 +17,8 @@ public class TransferHandler extends ChannelInboundHandlerAdapter{ if(msg instanceof ClientTransferData) { ClientTransferData clientTransferData = (ClientTransferData) msg; - if(clientTransferData.getMsg() != null) { - SimplePrinter.println(clientTransferData.getMsg()); + if(clientTransferData.getInfo() != null) { + SimplePrinter.println(clientTransferData.getInfo()); } ClientEventCode code = clientTransferData.getCode(); diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/channel/ChannelUtils.java b/landlords-common/src/main/java/org/nico/ratel/landlords/channel/ChannelUtils.java index 2321fb9..f5d89f1 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/channel/ChannelUtils.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/channel/ChannelUtils.java @@ -1,5 +1,6 @@ package org.nico.ratel.landlords.channel; +import org.nico.noson.Noson; import org.nico.ratel.landlords.entity.ClientTransferData; import org.nico.ratel.landlords.entity.ServerTransferData; import org.nico.ratel.landlords.enums.ClientEventCode; @@ -9,19 +10,18 @@ import io.netty.channel.Channel; public class ChannelUtils { - public static void pushToClient(Channel channel, ClientEventCode code, T datas) { - pushToClient(channel, code, datas, null); + public static void pushToClient(Channel channel, ClientEventCode code, String data) { + pushToClient(channel, code, data, null); } - public static void pushToClient(Channel channel, ClientEventCode code, T datas, String msg) { - ClientTransferData clientTransferData = new ClientTransferData(code, datas, msg); + public static void pushToClient(Channel channel, ClientEventCode code, String data, String info) { + ClientTransferData clientTransferData = new ClientTransferData(code, data, info); channel.writeAndFlush(clientTransferData); } - public static void pushToServer(Channel channel, int clientId, ServerEventCode code, T datas) { - ServerTransferData serverTransferData = new ServerTransferData(clientId, -1, code, datas); + public static void pushToServer(Channel channel, ServerEventCode code, String data) { + ServerTransferData serverTransferData = new ServerTransferData(code, data); channel.writeAndFlush(serverTransferData); } - } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientTransferData.java b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientTransferData.java index cc057ae..d1a1a11 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientTransferData.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientTransferData.java @@ -4,36 +4,28 @@ import java.io.Serializable; import org.nico.ratel.landlords.enums.ClientEventCode; -public class ClientTransferData implements Serializable{ +public class ClientTransferData implements Serializable{ private static final long serialVersionUID = -5144173747228946445L; private ClientEventCode code; - private T data; + private String data; - private String msg; + private String info; public ClientTransferData() { } - public ClientTransferData(ClientEventCode code, T data) { + public ClientTransferData(ClientEventCode code, String data) { this.code = code; this.data = data; } - - public ClientTransferData(ClientEventCode code, T data, String msg) { - this.code = code; - this.data = data; - this.msg = msg; - } - - public final String getMsg() { - return msg; - } - public final void setMsg(String msg) { - this.msg = msg; + public ClientTransferData(ClientEventCode code, String data, String info) { + this.code = code; + this.data = data; + this.info = info; } public final ClientEventCode getCode() { @@ -44,17 +36,20 @@ public class ClientTransferData implements Serializable{ this.code = code; } - public final T getData() { + public final String getData() { return data; } - public final void setData(T data) { + public final void setData(String data) { this.data = data; } - @Override - public String toString() { - return "ClientTransferData [code=" + code + ", data=" + data + ", msg=" + msg + "]"; + public final String getInfo() { + return info; + } + + public final void setInfo(String info) { + this.info = info; } } 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 9943d80..ef607e1 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 @@ -29,6 +29,9 @@ public class Room implements Serializable{ private int lastSellClient = -1; + public Room() { + } + public Room(int id) { this.id = id; this.clientSideMap = new ConcurrentSkipListMap<>(); diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ServerTransferData.java b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ServerTransferData.java index 2dd2856..2a537fc 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ServerTransferData.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ServerTransferData.java @@ -4,41 +4,24 @@ import java.io.Serializable; import org.nico.ratel.landlords.enums.ServerEventCode; -public class ServerTransferData implements Serializable{ +public class ServerTransferData implements Serializable{ private static final long serialVersionUID = -5056177734642583364L; - private int clientId; - - private int roomId; - private ServerEventCode code; - private T data; - - public ServerTransferData() {} - - public ServerTransferData(int clientId, int roomId, ServerEventCode code, T data) { - this.clientId = clientId; - this.roomId = roomId; + private String data; + + public ServerTransferData() { + } + + public ServerTransferData(ServerEventCode code, String data) { this.code = code; this.data = data; } - public final int getClientId() { - return clientId; - } - - public final void setClientId(int clientId) { - this.clientId = clientId; - } - - public final int getRoomId() { - return roomId; - } - - public final void setRoomId(int roomId) { - this.roomId = roomId; + public ServerTransferData(ServerEventCode code) { + this.code = code; } public final ServerEventCode getCode() { @@ -49,11 +32,11 @@ public class ServerTransferData implements Serializable{ this.code = code; } - public final T getData() { + public final String getData() { return data; } - public final void setData(T data) { + public final void setData(String data) { this.data = data; } 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 d53912a..ff350a7 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 @@ -4,7 +4,7 @@ import java.io.Serializable; public enum ClientEventCode implements Serializable{ - CODE_CONNECT("进入系统"), + CODE_SET_NICKNAME("设置昵称"), CODE_SHOW_OPTIONS("展示选项"), 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 fe78598..72dfd3a 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 @@ -4,7 +4,7 @@ import java.io.Serializable; public enum ServerEventCode implements Serializable{ - CODE_RENAME("修改昵称"), + CODE_SET_NICKNAME("设置昵称"), CODE_CREATE_ROOM("创建房间"), diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/ServerContains.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/ServerContains.java index 82ee2f2..58d01db 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/ServerContains.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/ServerContains.java @@ -1,9 +1,6 @@ package org.nico.ratel.landlords.server; import java.util.Map; -import java.util.Queue; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.atomic.AtomicInteger; diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener.java index 6cc17b1..d2b45af 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener.java @@ -3,20 +3,18 @@ package org.nico.ratel.landlords.server.event; import java.util.HashMap; import java.util.Map; -import org.nico.ratel.landlords.entity.ServerTransferData; +import org.nico.ratel.landlords.entity.ClientSide; import org.nico.ratel.landlords.enums.ServerEventCode; -import io.netty.channel.Channel; +public interface ServerEventListener { -public interface ServerEventListener { - - public void call(Channel channel, ServerTransferData serverTransferData); + public void call(ClientSide client, String data); public final static Map LISTENER_MAP = new HashMap<>(); final static String LISTENER_PREFIX = "org.nico.ratel.landlords.server.event.ServerEventListener_"; - public static ServerEventListener get(ServerEventCode code){ + public static ServerEventListener get(ServerEventCode code){ ServerEventListener listener = null; try { if(ServerEventListener.LISTENER_MAP.containsKey(code)){ diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CREATE_ROOM.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CREATE_ROOM.java index 8b7dda5..9573e63 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CREATE_ROOM.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CREATE_ROOM.java @@ -1,5 +1,6 @@ package org.nico.ratel.landlords.server.event; +import org.nico.noson.Noson; import org.nico.ratel.landlords.channel.ChannelUtils; import org.nico.ratel.landlords.entity.ClientSide; import org.nico.ratel.landlords.entity.Room; @@ -8,14 +9,10 @@ import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.RoomStatus; import org.nico.ratel.landlords.server.ServerContains; -import io.netty.channel.Channel; - -public class ServerEventListener_CODE_CREATE_ROOM implements ServerEventListener{ +public class ServerEventListener_CODE_CREATE_ROOM implements ServerEventListener{ @Override - public void call(Channel channel, ServerTransferData serverTransferData) { - - ClientSide clientSide = ServerContains.CLIENT_SIDE_MAP.get(serverTransferData.getClientId()); + public void call(ClientSide clientSide, String data) { Room room = new Room(ServerContains.getServerId()); room.setStatus(RoomStatus.BLANK); @@ -25,7 +22,7 @@ public class ServerEventListener_CODE_CREATE_ROOM implements ServerEventListener clientSide.setRoomId(room.getId()); ServerContains.ROOM_MAP.put(room.getId(), room); - ChannelUtils.pushToClient(channel, ClientEventCode.CODE_JOIN_ROOM_SUCCESS, room); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_JOIN_ROOM_SUCCESS, Noson.reversal(room)); } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOM_LIST.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOM_LIST.java index e7ac2ff..d21c74b 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOM_LIST.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOM_LIST.java @@ -1,17 +1,17 @@ package org.nico.ratel.landlords.server.event; +import org.nico.noson.Noson; import org.nico.ratel.landlords.channel.ChannelUtils; -import org.nico.ratel.landlords.entity.ServerTransferData; +import org.nico.ratel.landlords.entity.ClientSide; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.server.ServerContains; -import io.netty.channel.Channel; - -public class ServerEventListener_CODE_GET_ROOM_LIST implements ServerEventListener{ +public class ServerEventListener_CODE_GET_ROOM_LIST implements ServerEventListener{ @Override - public void call(Channel channel, ServerTransferData serverTransferData) { - ChannelUtils.pushToClient(channel, ClientEventCode.CODE_SHOW_ROOM_LIST, ServerContains.ROOM_MAP); + public void call(ClientSide clientSide, String data) { + + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_SHOW_ROOM_LIST, Noson.reversal(ServerContains.ROOM_MAP)); } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GRAB_LANDLORD.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GRAB_LANDLORD.java index 38a6f47..1bbcfd0 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GRAB_LANDLORD.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GRAB_LANDLORD.java @@ -3,24 +3,20 @@ 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.entity.ServerTransferData; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.server.ServerContains; import org.nico.ratel.landlords.server.event.helper.PokerHelper; import org.nico.ratel.landlords.server.event.helper.RoomHelper; import org.nico.ratel.landlords.server.event.helper.TimeHelper; -import io.netty.channel.Channel; - -public class ServerEventListener_CODE_GRAB_LANDLORD implements ServerEventListener{ +public class ServerEventListener_CODE_GRAB_LANDLORD implements ServerEventListener{ @Override - public void call(Channel channel, ServerTransferData serverTransferData) { - ClientSide clientSide = ServerContains.CLIENT_SIDE_MAP.get(serverTransferData.getClientId()); + public void call(ClientSide clientSide, String data) { Room room = ServerContains.ROOM_MAP.get(clientSide.getRoomId()); - boolean isY = serverTransferData.getData(); + boolean isY = Boolean.valueOf(data); if(isY){ clientSide.getPokers().addAll(room.getLandlordPokers()); PokerHelper.sortPoker(clientSide.getPokers()); diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_JOIN_ROOM.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_JOIN_ROOM.java index 029d1e2..865520f 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_JOIN_ROOM.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_JOIN_ROOM.java @@ -3,30 +3,27 @@ package org.nico.ratel.landlords.server.event; import java.util.LinkedList; import java.util.concurrent.ConcurrentSkipListMap; +import org.nico.noson.Noson; 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.entity.ServerTransferData; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.RoomStatus; import org.nico.ratel.landlords.server.ServerContains; import org.nico.ratel.landlords.server.event.helper.RoomHelper; -import io.netty.channel.Channel; - -public class ServerEventListener_CODE_JOIN_ROOM implements ServerEventListener{ +public class ServerEventListener_CODE_JOIN_ROOM implements ServerEventListener{ @Override - public void call(Channel channel, ServerTransferData serverTransferData) { + public void call(ClientSide clientSide, String data) { - Room room = ServerContains.ROOM_MAP.get(serverTransferData.getData()); + Room room = ServerContains.ROOM_MAP.get(Integer.valueOf(data)); if(room == null) { - ChannelUtils.pushToClient(channel, ClientEventCode.CODE_SHOW_OPTIONS, ServerContains.ROOM_MAP, "Room inexistence !"); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_SHOW_OPTIONS, null, "Room inexistence !"); }else if(room.getClientSideList().size() == 3) { - ChannelUtils.pushToClient(channel, ClientEventCode.CODE_SHOW_OPTIONS, ServerContains.ROOM_MAP, "The room is full !"); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_SHOW_OPTIONS, null, "The room is full !"); }else { - ClientSide clientSide = ServerContains.CLIENT_SIDE_MAP.get(serverTransferData.getClientId()); clientSide.setRoomId(room.getId()); ConcurrentSkipListMap roomClientMap = (ConcurrentSkipListMap) room.getClientSideMap(); @@ -48,7 +45,7 @@ public class ServerEventListener_CODE_JOIN_ROOM implements ServerEventListener{ +public class ServerEventListener_CODE_PLAYER_EXIT implements ServerEventListener{ @Override - public void call(Channel channel, ServerTransferData serverTransferData) { + public void call(ClientSide client, String data) { - ClientSide clientSide = ServerContains.CLIENT_SIDE_MAP.get(serverTransferData.getClientId()); - Room room = ServerContains.ROOM_MAP.get(clientSide.getRoomId()); + Room room = ServerContains.ROOM_MAP.get(client.getRoomId()); if(room != null) { - for(ClientSide client: room.getClientSideList()) { - if(client.getId() != clientSide.getId()) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_OPTIONS, null, clientSide.getNickname() + " exited, room dissolve!!"); + for(ClientSide roomClient: room.getClientSideList()) { + if(client.getId() != roomClient.getId()) { + ChannelUtils.pushToClient(roomClient.getChannel(), ClientEventCode.CODE_SHOW_OPTIONS, null, client.getNickname() + " exited, room dissolve!!"); }else { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_OPTIONS, null, "you exited, room dissolve!!"); + ChannelUtils.pushToClient(roomClient.getChannel(), ClientEventCode.CODE_SHOW_OPTIONS, null, "you exited, room dissolve!!"); } - client.init(); + roomClient.init(); } ServerContains.ROOM_MAP.remove(room.getId()); } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_PLAY_ROUND.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_PLAY_ROUND.java index 6bfe685..eadd5c1 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_PLAY_ROUND.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_PLAY_ROUND.java @@ -1,31 +1,26 @@ package org.nico.ratel.landlords.server.event; -import java.util.Arrays; import java.util.List; +import org.nico.noson.Noson; import org.nico.ratel.landlords.channel.ChannelUtils; import org.nico.ratel.landlords.entity.ClientSide; import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.entity.PokerSell; import org.nico.ratel.landlords.entity.Room; -import org.nico.ratel.landlords.entity.ServerTransferData; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.SellType; import org.nico.ratel.landlords.server.ServerContains; import org.nico.ratel.landlords.server.event.helper.PokerHelper; import org.nico.ratel.landlords.server.event.helper.TimeHelper; -import io.netty.channel.Channel; - -public class ServerEventListener_CODE_PLAY_ROUND implements ServerEventListener{ +public class ServerEventListener_CODE_PLAY_ROUND implements ServerEventListener{ @Override - public void call(Channel channel, ServerTransferData serverTransferData) { - ClientSide clientSide = ServerContains.CLIENT_SIDE_MAP.get(serverTransferData.getClientId()); + public void call(ClientSide clientSide, String data) { Room room = ServerContains.ROOM_MAP.get(clientSide.getRoomId()); - Character[] options = serverTransferData.getData(); + Character[] options = Noson.convert(data, Character[].class); int[] indexes = PokerHelper.getIndexes(options, clientSide.getPokers()); - System.out.println("index:" + Arrays.toString(indexes)); if(PokerHelper.checkPokerIndex(indexes, clientSide.getPokers())){ boolean sellFlag = false; List currentPokers = PokerHelper.getPoker(indexes, clientSide.getPokers()); @@ -33,9 +28,9 @@ public class ServerEventListener_CODE_PLAY_ROUND implements ServerEventListener< if(room.getLastSellClient() != -1 && room.getLastSellClient() != clientSide.getId() && room.getLastPokerShell() != null){ PokerSell lastPokerShell = room.getLastPokerShell(); if(lastPokerShell.getSellType() != currentPokerShell.getSellType() && currentPokerShell.getSellType() != SellType.BOMB && currentPokerShell.getSellType() != SellType.KING_BOMB) { - ChannelUtils.pushToClient(channel, ClientEventCode.CODE_PLAY_ROUND, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "Your sell type is " + currentPokerShell.getSellType().getMsg() + " ,but last sell type is " + lastPokerShell.getSellType().getMsg()); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_PLAY_ROUND, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "Your sell type is " + currentPokerShell.getSellType().getMsg() + " ,but last sell type is " + lastPokerShell.getSellType().getMsg()); }else if(lastPokerShell.getScore() >= currentPokerShell.getScore()) { - ChannelUtils.pushToClient(channel, ClientEventCode.CODE_PLAY_ROUND, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "It's not as big as the other side"); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_PLAY_ROUND, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "It's not as big as the other side"); }else { sellFlag = true; } @@ -72,7 +67,7 @@ public class ServerEventListener_CODE_PLAY_ROUND implements ServerEventListener< ClientSide next = clientSide.getNext(); ChannelUtils.pushToClient(next.getChannel(), ClientEventCode.CODE_PLAY_ROUND, PokerHelper.unfoldPoker(next.getPokers(), true)); }else { - ChannelUtils.pushToClient(channel, ClientEventCode.CODE_PLAY_ROUND, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "The draw number is invalid"); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_PLAY_ROUND, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "The draw number is invalid"); } } } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_RENAME.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_RENAME.java deleted file mode 100644 index 66196b2..0000000 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_RENAME.java +++ /dev/null @@ -1,23 +0,0 @@ -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.ServerTransferData; -import org.nico.ratel.landlords.enums.ClientEventCode; -import org.nico.ratel.landlords.server.ServerContains; - -import io.netty.channel.Channel; - -public class ServerEventListener_CODE_RENAME implements ServerEventListener{ - - @Override - public void call(Channel channel, ServerTransferData serverTransferData) { - - ClientSide clientSide = serverTransferData.getData(); - - ServerContains.CLIENT_SIDE_MAP.get(clientSide.getId()).setNickname(clientSide.getNickname());; - - ChannelUtils.pushToClient(channel, ClientEventCode.CODE_SHOW_OPTIONS, ServerContains.ROOM_MAP); - } - -} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_SET_NICKNAME.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_SET_NICKNAME.java new file mode 100644 index 0000000..08a62c0 --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_SET_NICKNAME.java @@ -0,0 +1,18 @@ +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.enums.ClientEventCode; +import org.nico.ratel.landlords.server.ServerContains; + +public class ServerEventListener_CODE_SET_NICKNAME implements ServerEventListener{ + + @Override + public void call(ClientSide client, String data) { + + ServerContains.CLIENT_SIDE_MAP.get(client.getId()).setNickname(data); + + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_OPTIONS, null); + } + +} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/helper/RoomHelper.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/helper/RoomHelper.java index 6b4f64b..f1eec7e 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/helper/RoomHelper.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/helper/RoomHelper.java @@ -3,6 +3,7 @@ package org.nico.ratel.landlords.server.event.helper; import java.util.LinkedList; import java.util.List; +import org.nico.noson.Noson; import org.nico.ratel.landlords.channel.ChannelUtils; import org.nico.ratel.landlords.entity.ClientSide; import org.nico.ratel.landlords.entity.Poker; @@ -25,7 +26,7 @@ public class RoomHelper { room.setStatus(RoomStatus.STARTING); for(ClientSide client: roomClientList) { client.setType(ClientType.PEASANT); - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_ROOM_STARTING, room); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_ROOM_STARTING, Noson.reversal(room)); } TimeHelper.sleep(500); diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java index f149e52..0c25ce1 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java @@ -18,10 +18,6 @@ import io.netty.channel.ChannelInboundHandlerAdapter; public class TransferHandler extends ChannelInboundHandlerAdapter{ - private final static String LISTENER_PREFIX = "org.nico.ratel.landlords.server.event.ServerEventListener_"; - - - @Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { Channel ch = ctx.channel(); @@ -30,7 +26,7 @@ public class TransferHandler extends ChannelInboundHandlerAdapter{ clientSide.setNickname(String.valueOf(clientSide.getId())); ServerContains.CLIENT_SIDE_MAP.put(clientSide.getId(), clientSide); SimplePrinter.println("A client connects to the server:" + clientSide.getId()); - ChannelUtils.pushToClient(ch, ClientEventCode.CODE_CONNECT, clientSide); + ChannelUtils.pushToClient(ch, ClientEventCode.CODE_SET_NICKNAME, null); } @Override @@ -42,7 +38,9 @@ public class TransferHandler extends ChannelInboundHandlerAdapter{ ServerEventCode code = serverTransferData.getCode(); if(code != null) { - ServerEventListener.get(code).call(ctx.channel(), serverTransferData); + ClientSide client = ServerContains.CLIENT_SIDE_MAP.get(((InetSocketAddress)ctx.channel().remoteAddress()).getPort()); + + ServerEventListener.get(code).call(client, serverTransferData.getData()); } } @@ -54,7 +52,7 @@ public class TransferHandler extends ChannelInboundHandlerAdapter{ ClientSide client = ServerContains.CLIENT_SIDE_MAP.get(((InetSocketAddress)ctx.channel().remoteAddress()).getPort()); if(client != null) { SimplePrinter.println(client.getNickname() + " exit"); - ServerEventListener.get(ServerEventCode.CODE_PLAYER_EXIT).call(ctx.channel(), new ServerTransferData<>(client.getId(), client.getRoomId(), ServerEventCode.CODE_PLAYER_EXIT, null)); + ServerEventListener.get(ServerEventCode.CODE_PLAYER_EXIT).call(client, null); } } } diff --git a/pom.xml b/pom.xml index 991a837..ff9e121 100644 --- a/pom.xml +++ b/pom.xml @@ -60,7 +60,7 @@ com.smallnico noson - 1.1.4 + 1.1.5 From ae9db9c9bdd8d42a4502d506964c2a03d8d678a6 Mon Sep 17 00:00:00 2001 From: nico Date: Thu, 8 Nov 2018 21:26:59 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E9=80=BB=E8=BE=91=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=8F=8A=E8=B7=A8=E5=B9=B3=E5=8F=B0=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ratel/landlords/client/SimpleClient.java | 2 + .../client/event/ClientEventListener.java | 3 +- ...ientEventListener_CODE_CLIENT_CONNECT.java | 16 ++++ .../ClientEventListener_CODE_CLIENT_EXIT.java | 14 ++++ ...entListener_CODE_CLIENT_NICKNAME_SET.java} | 7 +- ...ventListener_CODE_CREATE_ROOM_SUCCESS.java | 24 ------ ...ntListener_CODE_GAME_LANDLORD_CONFIRM.java | 14 ++++ ...entListener_CODE_GAME_LANDLORD_ELECT.java} | 11 ++- .../ClientEventListener_CODE_GAME_OVER.java | 3 +- ...ntEventListener_CODE_GAME_POKER_PLAY.java} | 20 ++--- ...lientEventListener_CODE_GAME_STARTING.java | 15 ++++ ...tEventListener_CODE_JOIN_ROOM_SUCCESS.java | 19 ----- ...ventListener_CODE_ROOM_CREATE_SUCCESS.java | 22 ++++++ ...tListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java | 21 +++++ ...stener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java | 20 +++++ ...tEventListener_CODE_ROOM_JOIN_SUCCESS.java | 22 ++++++ ...tEventListener_CODE_ROOM_JOIN_TRIGGER.java | 22 ++++++ ...lientEventListener_CODE_ROOM_STARTING.java | 17 ---- ...ClientEventListener_CODE_SHOW_OPTIONS.java | 12 ++- .../ClientEventListener_CODE_SHOW_POKERS.java | 12 ++- ...entListener_CODE_SHOW_POKERS_LANDLORD.java | 22 ++++++ .../ClientEventListener_CODE_SHOW_ROOMS.java | 36 +++++++++ ...ientEventListener_CODE_SHOW_ROOM_LIST.java | 34 -------- .../handler/DefaultChannelInitializer.java | 1 - .../client/handler/TransferHandler.java | 3 +- .../org/nico/ratel/landlords/entity/Room.java | 17 ++-- .../landlords/enums/ClientEventCode.java | 32 ++++++-- .../landlords/enums/ServerEventCode.java | 17 ++-- .../ratel/landlords/helper/MapHelper.java | 37 +++++++++ .../ratel/landlords}/helper/PokerHelper.java | 2 +- .../ratel/landlords}/helper/RoomHelper.java | 0 .../ratel/landlords}/helper/TimeHelper.java | 2 +- .../ServerEventListener_CODE_CLIENT_EXIT.java | 25 ++++++ ...entListener_CODE_CLIENT_NICKNAME_SET.java} | 2 +- ...entListener_CODE_GAME_LANDLORD_ELECT.java} | 25 +++--- ...erEventListener_CODE_GAME_POKER_PLAY.java} | 16 ++-- ...erverEventListener_CODE_GAME_STARTING.java | 67 ++++++++++++++++ ...erverEventListener_CODE_GET_ROOM_LIST.java | 21 ----- .../ServerEventListener_CODE_JOIN_ROOM.java | 62 --------------- .../ServerEventListener_CODE_PLAYER_EXIT.java | 32 -------- ...ServerEventListener_CODE_ROOM_CREATE.java} | 6 +- .../ServerEventListener_CODE_ROOM_JOIN.java | 77 +++++++++++++++++++ ...erverEventListener_CODE_ROOM_LIST_GET.java | 36 +++++++++ .../server/handler/TransferHandler.java | 4 +- 44 files changed, 578 insertions(+), 294 deletions(-) create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_CONNECT.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_EXIT.java rename landlords-client/src/main/java/org/nico/ratel/landlords/client/event/{ClientEventListener_CODE_SET_NICKNAME.java => ClientEventListener_CODE_CLIENT_NICKNAME_SET.java} (58%) delete mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CREATE_ROOM_SUCCESS.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java rename landlords-client/src/main/java/org/nico/ratel/landlords/client/event/{ClientEventListener_CODE_GRAB_LANDLORD.java => ClientEventListener_CODE_GAME_LANDLORD_ELECT.java} (55%) rename landlords-client/src/main/java/org/nico/ratel/landlords/client/event/{ClientEventListener_CODE_PLAY_ROUND.java => ClientEventListener_CODE_GAME_POKER_PLAY.java} (61%) create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_STARTING.java delete mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_JOIN_ROOM_SUCCESS.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_CREATE_SUCCESS.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_SUCCESS.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_TRIGGER.java delete mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_STARTING.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS_LANDLORD.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java delete mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOM_LIST.java create mode 100644 landlords-common/src/main/java/org/nico/ratel/landlords/helper/MapHelper.java rename {landlords-server/src/main/java/org/nico/ratel/landlords/server/event => landlords-common/src/main/java/org/nico/ratel/landlords}/helper/PokerHelper.java (99%) rename {landlords-server/src/main/java/org/nico/ratel/landlords/server/event => landlords-common/src/main/java/org/nico/ratel/landlords}/helper/RoomHelper.java (100%) rename {landlords-server/src/main/java/org/nico/ratel/landlords/server/event => landlords-common/src/main/java/org/nico/ratel/landlords}/helper/TimeHelper.java (81%) create mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_EXIT.java rename landlords-server/src/main/java/org/nico/ratel/landlords/server/event/{ServerEventListener_CODE_SET_NICKNAME.java => ServerEventListener_CODE_CLIENT_NICKNAME_SET.java} (84%) rename landlords-server/src/main/java/org/nico/ratel/landlords/server/event/{ServerEventListener_CODE_GRAB_LANDLORD.java => ServerEventListener_CODE_GAME_LANDLORD_ELECT.java} (67%) rename landlords-server/src/main/java/org/nico/ratel/landlords/server/event/{ServerEventListener_CODE_PLAY_ROUND.java => ServerEventListener_CODE_GAME_POKER_PLAY.java} (79%) create mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java delete mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOM_LIST.java delete mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_JOIN_ROOM.java delete mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_PLAYER_EXIT.java rename landlords-server/src/main/java/org/nico/ratel/landlords/server/event/{ServerEventListener_CODE_CREATE_ROOM.java => ServerEventListener_CODE_ROOM_CREATE.java} (82%) create mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_JOIN.java create mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_LIST_GET.java diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java index ae6e84e..7fe548f 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java @@ -12,6 +12,8 @@ import io.netty.channel.socket.nio.NioSocketChannel; public class SimpleClient { + public static int id = -1; + public static void main(String[] args) throws InterruptedException, IOException { EventLoopGroup group = new NioEventLoopGroup(); diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java index faf0798..b360028 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java @@ -4,7 +4,6 @@ import java.util.HashMap; import java.util.Map; import org.nico.ratel.landlords.channel.ChannelUtils; -import org.nico.ratel.landlords.entity.ClientTransferData; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.ServerEventCode; @@ -12,7 +11,7 @@ import io.netty.channel.Channel; public abstract class ClientEventListener { - public abstract void call(Channel channel, ClientTransferData clientTransferData); + public abstract void call(Channel channel, String data); public final static Map LISTENER_MAP = new HashMap<>(); diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_CONNECT.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_CONNECT.java new file mode 100644 index 0000000..63088bf --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_CONNECT.java @@ -0,0 +1,16 @@ +package org.nico.ratel.landlords.client.event; + +import org.nico.ratel.landlords.client.SimpleClient; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_CLIENT_CONNECT extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + SimplePrinter.println("Connect success"); + SimpleClient.id = Integer.parseInt(data); + } + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_EXIT.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_EXIT.java new file mode 100644 index 0000000..514ef45 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_EXIT.java @@ -0,0 +1,14 @@ +package org.nico.ratel.landlords.client.event; + +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_CLIENT_EXIT extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + SimplePrinter.println(data + " exit, room dissolve!!"); + } + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SET_NICKNAME.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_NICKNAME_SET.java similarity index 58% rename from landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SET_NICKNAME.java rename to landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_NICKNAME_SET.java index fc8b372..96b2d48 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SET_NICKNAME.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_NICKNAME_SET.java @@ -1,21 +1,20 @@ package org.nico.ratel.landlords.client.event; -import org.nico.ratel.landlords.entity.ClientTransferData; import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.print.SimplePrinter; import org.nico.ratel.landlords.print.SimpleWriter; import io.netty.channel.Channel; -public class ClientEventListener_CODE_SET_NICKNAME extends ClientEventListener{ +public class ClientEventListener_CODE_CLIENT_NICKNAME_SET extends ClientEventListener{ @Override - public void call(Channel channel, ClientTransferData clientTransferData) { + public void call(Channel channel, String data) { SimplePrinter.println("You has been join Nico-Landlords, please set your nickname: "); String nickname = SimpleWriter.write(); - pushToServer(channel, ServerEventCode.CODE_SET_NICKNAME, nickname); + pushToServer(channel, ServerEventCode.CODE_CLIENT_NICKNAME_SET, nickname); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CREATE_ROOM_SUCCESS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CREATE_ROOM_SUCCESS.java deleted file mode 100644 index 88de92c..0000000 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CREATE_ROOM_SUCCESS.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.nico.ratel.landlords.client.event; - -import java.util.Map; - -import org.nico.noson.Noson; -import org.nico.ratel.landlords.entity.ClientTransferData; -import org.nico.ratel.landlords.print.SimplePrinter; - -import io.netty.channel.Channel; - -public class ClientEventListener_CODE_CREATE_ROOM_SUCCESS extends ClientEventListener{ - - @Override - public void call(Channel channel, ClientTransferData clientTransferData) { - - Map room = Noson.parseMap(clientTransferData.getData()); - - SimplePrinter.println("You has been create room:" + room); - SimplePrinter.println("Please wait for other players to join !"); - } - - - -} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java new file mode 100644 index 0000000..c3a4281 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java @@ -0,0 +1,14 @@ +package org.nico.ratel.landlords.client.event; + +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_GAME_LANDLORD_CONFIRM extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + SimplePrinter.print(data + " grabed landlord, he got three pokers"); + } + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GRAB_LANDLORD.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java similarity index 55% rename from landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GRAB_LANDLORD.java rename to landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java index 6bc0574..7421def 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GRAB_LANDLORD.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java @@ -1,25 +1,24 @@ package org.nico.ratel.landlords.client.event; -import org.nico.ratel.landlords.entity.ClientTransferData; import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.print.SimplePrinter; import org.nico.ratel.landlords.print.SimpleWriter; import io.netty.channel.Channel; -public class ClientEventListener_CODE_GRAB_LANDLORD extends ClientEventListener{ +public class ClientEventListener_CODE_GAME_LANDLORD_ELECT extends ClientEventListener{ @Override - public void call(Channel channel, ClientTransferData clientTransferData) { + public void call(Channel channel, String data) { SimplePrinter.print("It's your turn to rob the landlord[Y/N]:"); String line = SimpleWriter.write(); if(line.equalsIgnoreCase("Y")){ - pushToServer(channel, ServerEventCode.CODE_GRAB_LANDLORD, "TRUE"); + pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "TRUE"); }else if(line.equalsIgnoreCase("N")){ - pushToServer(channel, ServerEventCode.CODE_GRAB_LANDLORD, "FALSE"); + pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "FALSE"); }else{ SimplePrinter.println("Invalid options"); - call(channel, clientTransferData); + call(channel, data); } } 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 2ac0574..50fe7c2 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 @@ -1,6 +1,5 @@ package org.nico.ratel.landlords.client.event; -import org.nico.ratel.landlords.entity.ClientTransferData; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -8,7 +7,7 @@ import io.netty.channel.Channel; public class ClientEventListener_CODE_GAME_OVER extends ClientEventListener{ @Override - public void call(Channel channel, ClientTransferData clientTransferData) { + public void call(Channel channel, String data) { SimplePrinter.println("Game over"); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_PLAY_ROUND.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY.java similarity index 61% rename from landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_PLAY_ROUND.java rename to landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY.java index 860afe7..2005ce8 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_PLAY_ROUND.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY.java @@ -1,36 +1,32 @@ package org.nico.ratel.landlords.client.event; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.nico.noson.Noson; -import org.nico.ratel.landlords.entity.ClientTransferData; import org.nico.ratel.landlords.enums.PokerLevel; -import org.nico.ratel.landlords.enums.PokerType; import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.print.SimplePrinter; import org.nico.ratel.landlords.print.SimpleWriter; -import org.nico.ratel.landlords.utils.OptionsUtils; import io.netty.channel.Channel; -public class ClientEventListener_CODE_PLAY_ROUND extends ClientEventListener{ +public class ClientEventListener_CODE_GAME_POKER_PLAY extends ClientEventListener{ @Override - public void call(Channel channel, ClientTransferData clientTransferData) { - SimplePrinter.println(clientTransferData.getData()); + public void call(Channel channel, String data) { + SimplePrinter.println(data); SimplePrinter.println("Please enter the number you want:"); String line = SimpleWriter.write(); if(line == null){ SimplePrinter.println("Invalid input"); - call(channel, clientTransferData); + call(channel, data); }else{ if(line.equalsIgnoreCase("PASS")) { - pushToServer(channel, ServerEventCode.CODE_PLAY_ROUND, Noson.reversal(new Character[] {'p'})); + pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY, Noson.reversal(new Character[] {'p'})); }else if(line.equalsIgnoreCase("EXIT")){ - pushToServer(channel, ServerEventCode.CODE_PLAYER_EXIT, null); + pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT, null); }else { String[] strs = line.split(" "); List options = new ArrayList<>(); @@ -50,10 +46,10 @@ public class ClientEventListener_CODE_PLAY_ROUND extends ClientEventListener{ } } if(access){ - pushToServer(channel, ServerEventCode.CODE_PLAY_ROUND, Noson.reversal(options.toArray(new Character[] {}))); + pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY, Noson.reversal(options.toArray(new Character[] {}))); }else{ SimplePrinter.println("Invalid input"); - call(channel, clientTransferData); + call(channel, data); } } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_STARTING.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_STARTING.java new file mode 100644 index 0000000..0a43860 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_STARTING.java @@ -0,0 +1,15 @@ +package org.nico.ratel.landlords.client.event; + +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_GAME_STARTING extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + SimplePrinter.println(data + " game starting !"); + SimplePrinter.println("Wait for the server to issue the license..."); + } + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_JOIN_ROOM_SUCCESS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_JOIN_ROOM_SUCCESS.java deleted file mode 100644 index f800947..0000000 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_JOIN_ROOM_SUCCESS.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.nico.ratel.landlords.client.event; - -import org.nico.ratel.landlords.entity.ClientTransferData; -import org.nico.ratel.landlords.entity.Room; -import org.nico.ratel.landlords.print.SimplePrinter; - -import io.netty.channel.Channel; - -public class ClientEventListener_CODE_JOIN_ROOM_SUCCESS extends ClientEventListener{ - - @Override - public void call(Channel channel, ClientTransferData clientTransferData) { - SimplePrinter.println("You has been join room:" + clientTransferData.getData()); - SimplePrinter.println("Please wait for other players to join !"); - } - - - -} 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 new file mode 100644 index 0000000..a879d55 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_CREATE_SUCCESS.java @@ -0,0 +1,22 @@ +package org.nico.ratel.landlords.client.event; + +import org.nico.noson.Noson; +import org.nico.ratel.landlords.entity.Room; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_ROOM_CREATE_SUCCESS extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + + Room room = Noson.convert(data, Room.class); + + SimplePrinter.println("You has been create room:" + room.getId()); + SimplePrinter.println("Please wait for other players to join !"); + } + + + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java new file mode 100644 index 0000000..bc97f5b --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java @@ -0,0 +1,21 @@ +package org.nico.ratel.landlords.client.event; + +import java.util.Map; + +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + Map dataMap = MapHelper.parser(data); + + SimplePrinter.println("Join fail, room " + dataMap.get("roomId") + " is full!"); + } + + + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java new file mode 100644 index 0000000..fccc31a --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java @@ -0,0 +1,20 @@ +package org.nico.ratel.landlords.client.event; + +import java.util.Map; + +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + + SimplePrinter.println("Join fail, room " + data + " is full!"); + } + + + +} 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 new file mode 100644 index 0000000..9b13f73 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_SUCCESS.java @@ -0,0 +1,22 @@ +package org.nico.ratel.landlords.client.event; + +import java.util.Map; + +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_ROOM_JOIN_SUCCESS extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + Map dataMap = MapHelper.parser(data); + + SimplePrinter.println("You has been join room:" + dataMap.get("roomId") + ", There are " + dataMap.get("roomClientCount") + " people in the room now"); + SimplePrinter.println("Please wait for other players to join !"); + } + + + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_TRIGGER.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_TRIGGER.java new file mode 100644 index 0000000..57f5748 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_TRIGGER.java @@ -0,0 +1,22 @@ +package org.nico.ratel.landlords.client.event; + +import java.util.Map; + +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_ROOM_JOIN_TRIGGER extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + Map dataMap = MapHelper.parser(data); + + SimplePrinter.println(dataMap.get("clientNickname") + " has been join room:" + dataMap.get("roomId") + ", There are " + dataMap.get("roomClientCount") + " people in the room now"); + SimplePrinter.println("Please wait for other players to join !"); + } + + + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_STARTING.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_STARTING.java deleted file mode 100644 index 980ec17..0000000 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_STARTING.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.nico.ratel.landlords.client.event; - -import org.nico.ratel.landlords.entity.ClientTransferData; -import org.nico.ratel.landlords.entity.Room; -import org.nico.ratel.landlords.print.SimplePrinter; - -import io.netty.channel.Channel; - -public class ClientEventListener_CODE_ROOM_STARTING extends ClientEventListener{ - - @Override - public void call(Channel channel, ClientTransferData clientTransferData) { - SimplePrinter.println(clientTransferData.getData() + " game starting !"); - SimplePrinter.println("Wait for the server to issue the license..."); - } - -} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java index 56fe855..a6ce096 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java @@ -1,7 +1,5 @@ package org.nico.ratel.landlords.client.event; -import org.nico.noson.Noson; -import org.nico.ratel.landlords.entity.ClientTransferData; import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.print.SimplePrinter; import org.nico.ratel.landlords.print.SimpleWriter; @@ -12,7 +10,7 @@ import io.netty.channel.Channel; public class ClientEventListener_CODE_SHOW_OPTIONS extends ClientEventListener{ @Override - public void call(Channel channel, ClientTransferData clientTransferData) { + public void call(Channel channel, String data) { SimplePrinter.println("Options: "); SimplePrinter.println("1. Create Room"); SimplePrinter.println("2. Room List"); @@ -27,18 +25,18 @@ public class ClientEventListener_CODE_SHOW_OPTIONS extends ClientEventListener{ int choose = Integer.valueOf(line); if(choose == 1) { - pushToServer(channel, ServerEventCode.CODE_CREATE_ROOM, null); + pushToServer(channel, ServerEventCode.CODE_ROOM_CREATE, null); }else if(choose == 2){ - pushToServer(channel, ServerEventCode.CODE_GET_ROOM_LIST, null); + pushToServer(channel, ServerEventCode.CODE_ROOM_LIST_GET, null); }else { SimplePrinter.print("Your choose rooms number:"); line = SimpleWriter.write(); int option = OptionsUtils.getOptions(line); if(line == null || option < 1) { SimplePrinter.println("Invalid options, please choose again:"); - call(channel, clientTransferData); + call(channel, data); }else{ - pushToServer(channel, ServerEventCode.CODE_JOIN_ROOM, String.valueOf(option)); + pushToServer(channel, ServerEventCode.CODE_ROOM_JOIN, String.valueOf(option)); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java index b64ab02..32a6382 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java @@ -1,6 +1,11 @@ package org.nico.ratel.landlords.client.event; -import org.nico.ratel.landlords.entity.ClientTransferData; +import java.util.List; + +import org.nico.noson.Noson; +import org.nico.noson.entity.NoType; +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -8,8 +13,9 @@ import io.netty.channel.Channel; public class ClientEventListener_CODE_SHOW_POKERS extends ClientEventListener{ @Override - public void call(Channel channel, ClientTransferData clientTransferData) { - SimplePrinter.println(clientTransferData.getData()); + public void call(Channel channel, String data) { + List landlordPokers = Noson.convert(data, new NoType>() {}); + SimplePrinter.println(PokerHelper.unfoldPoker(landlordPokers, false)); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS_LANDLORD.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS_LANDLORD.java new file mode 100644 index 0000000..3b2aef8 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS_LANDLORD.java @@ -0,0 +1,22 @@ +package org.nico.ratel.landlords.client.event; + +import java.util.List; + +import org.nico.noson.Noson; +import org.nico.noson.entity.NoType; +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.helper.PokerHelper; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_SHOW_POKERS_LANDLORD extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + + List landlordPokers = Noson.convert(data, new NoType>() {}); + SimplePrinter.println(PokerHelper.unfoldPoker(landlordPokers, false)); + } + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java new file mode 100644 index 0000000..9de4819 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java @@ -0,0 +1,36 @@ +package org.nico.ratel.landlords.client.event; + +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentSkipListMap; + +import org.nico.noson.Noson; +import org.nico.noson.entity.NoType; +import org.nico.ratel.landlords.entity.Room; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_SHOW_ROOMS extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + + List> roomList = Noson.convert(data, new NoType>>() {}); + if(roomList != null && ! roomList.isEmpty()){ + SimplePrinter.println("#\tID\t|\tOWNER\t|\tCOUNT\t#"); + for(Map room: roomList) { + SimplePrinter.println("#\t" + room.get("roomId") + "\t|\t" + room.get("roomOwner") + "\t|\t" + room.get("roomClientCount") + "\t#"); + } + get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); + }else { + SimplePrinter.println("No available room, please create a room !"); + get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); + } + } + + + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOM_LIST.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOM_LIST.java deleted file mode 100644 index b801f2c..0000000 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOM_LIST.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.nico.ratel.landlords.client.event; - -import java.util.Map.Entry; -import java.util.concurrent.ConcurrentSkipListMap; - -import org.nico.noson.Noson; -import org.nico.noson.entity.NoType; -import org.nico.ratel.landlords.entity.ClientTransferData; -import org.nico.ratel.landlords.entity.Room; -import org.nico.ratel.landlords.enums.ClientEventCode; -import org.nico.ratel.landlords.print.SimplePrinter; - -import io.netty.channel.Channel; - -public class ClientEventListener_CODE_SHOW_ROOM_LIST extends ClientEventListener{ - - @Override - public void call(Channel channel, ClientTransferData clientTransferData) { - ConcurrentSkipListMap roomMap = Noson.convert(clientTransferData.getData(), new NoType>() {}); - if(roomMap != null && ! roomMap.isEmpty()){ - SimplePrinter.println("Room list: "); - for(Entry roomEntry: roomMap.entrySet()) { - SimplePrinter.println(roomEntry.getKey() + "\t|\t" + roomEntry.getValue()); - } - get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, new ClientTransferData()); - }else { - SimplePrinter.println("No available room, please create a room !"); - get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, new ClientTransferData()); - } - } - - - -} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/DefaultChannelInitializer.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/DefaultChannelInitializer.java index e4f4af4..b5257f5 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/DefaultChannelInitializer.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/DefaultChannelInitializer.java @@ -1,7 +1,6 @@ package org.nico.ratel.landlords.client.handler; import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.serialization.ClassResolvers; import io.netty.handler.codec.serialization.ObjectDecoder; diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java index 4b41fb3..4de443a 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java @@ -4,7 +4,6 @@ import org.nico.ratel.landlords.client.event.ClientEventListener; import org.nico.ratel.landlords.entity.ClientTransferData; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.print.SimplePrinter; -import org.nico.ratel.landlords.transfer.TransferProtocolUtils; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; @@ -24,7 +23,7 @@ public class TransferHandler extends ChannelInboundHandlerAdapter{ ClientEventCode code = clientTransferData.getCode(); if(code != null) { - ClientEventListener.get(code).call(ctx.channel(), clientTransferData); + ClientEventListener.get(code).call(ctx.channel(), clientTransferData.getData()); } } } 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 ef607e1..070a150 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 @@ -6,8 +6,8 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentSkipListMap; +import org.nico.noson.annotations.JsonIgnore; import org.nico.ratel.landlords.enums.RoomStatus; -import org.nico.ratel.landlords.enums.SellType; public class Room implements Serializable{ @@ -15,6 +15,8 @@ public class Room implements Serializable{ private int id; + private String roomOwner; + private RoomStatus status; private Map clientSideMap; @@ -79,6 +81,14 @@ public class Room implements Serializable{ this.landlordPokers = landlordPokers; } + public final String getRoomOwner() { + return roomOwner; + } + + public final void setRoomOwner(String roomOwner) { + this.roomOwner = roomOwner; + } + public final int getId() { return id; } @@ -103,9 +113,4 @@ public class Room implements Serializable{ this.clientSideMap = clientSideMap; } - @Override - public String toString() { - return id + "|" + status.getMsg() + "|" + clientSideMap.size() + " online"; - } - } 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 ff350a7..e7be04e 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 @@ -4,21 +4,37 @@ import java.io.Serializable; public enum ClientEventCode implements Serializable{ - CODE_SET_NICKNAME("设置昵称"), + CODE_CLIENT_NICKNAME_SET("设置昵称"), + + CODE_CLIENT_EXIT("客户端退出"), + + CODE_CLIENT_CONNECT("客户端加入成功"), CODE_SHOW_OPTIONS("展示选项"), - CODE_SHOW_ROOM_LIST("展示房间列表"), - - CODE_JOIN_ROOM_SUCCESS("加入房间成功"), - - CODE_ROOM_STARTING("开始游戏"), + CODE_SHOW_ROOMS("展示房间列表"), CODE_SHOW_POKERS("展示Poker"), - CODE_GRAB_LANDLORD("抢地主"), + CODE_SHOW_POKERS_LANDLORD("展示地主牌"), + + CODE_ROOM_CREATE_SUCCESS("创建房间成功"), - CODE_PLAY_ROUND("出牌回合"), + CODE_ROOM_JOIN_SUCCESS("加入房间成功"), + + CODE_ROOM_JOIN_TRIGGER("加入房间触发"), + + CODE_ROOM_JOIN_FAIL_BY_FULL("房间人数已满"), + + CODE_ROOM_JOIN_FAIL_BY_INEXIST("房间不存在"), + + CODE_GAME_STARTING("开始游戏"), + + CODE_GAME_LANDLORD_ELECT("抢地主"), + + CODE_GAME_LANDLORD_CONFIRM("地主确认"), + + CODE_GAME_POKER_PLAY("出牌回合"), CODE_GAME_OVER("游戏结束"), ; 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 72dfd3a..d436535 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 @@ -4,19 +4,22 @@ import java.io.Serializable; public enum ServerEventCode implements Serializable{ - CODE_SET_NICKNAME("设置昵称"), + CODE_CLIENT_EXIT("玩家退出"), - CODE_CREATE_ROOM("创建房间"), + CODE_CLIENT_NICKNAME_SET("设置昵称"), - CODE_GET_ROOM_LIST("获取房间列表"), + CODE_ROOM_CREATE("创建房间"), - CODE_JOIN_ROOM("加入房间"), + CODE_ROOM_LIST_GET("获取房间列表"), - CODE_GRAB_LANDLORD("抢地主"), + CODE_ROOM_JOIN("加入房间"), - CODE_PLAY_ROUND("出牌环节"), + CODE_GAME_STARTING("游戏开始"), + + CODE_GAME_LANDLORD_ELECT("抢地主"), + + CODE_GAME_POKER_PLAY("出牌环节"), - CODE_PLAYER_EXIT("玩家退出"), ; private String msg; diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/helper/MapHelper.java b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/MapHelper.java new file mode 100644 index 0000000..5c5e72b --- /dev/null +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/MapHelper.java @@ -0,0 +1,37 @@ +package org.nico.ratel.landlords.helper; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.nico.noson.Noson; + +public class MapHelper { + + private Map data; + + private MapHelper() { + this.data = new LinkedHashMap<>(); + } + + public static MapHelper newInstance() { + return new MapHelper(); + } + + public static Map parser(String json) { + return Noson.convert(json, Map.class); + } + + public MapHelper put(String name, Object Object) { + this.data.put(name, Object); + return this; + } + + public String json() { + return Noson.reversal(data); + } + + public Map map() { + return data; + } + +} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/helper/PokerHelper.java b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java similarity index 99% rename from landlords-server/src/main/java/org/nico/ratel/landlords/server/event/helper/PokerHelper.java rename to landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java index 3236e26..54262fd 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/helper/PokerHelper.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java @@ -1,4 +1,4 @@ -package org.nico.ratel.landlords.server.event.helper; +package org.nico.ratel.landlords.helper; import java.util.ArrayList; import java.util.Arrays; diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/helper/RoomHelper.java b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/RoomHelper.java similarity index 100% rename from landlords-server/src/main/java/org/nico/ratel/landlords/server/event/helper/RoomHelper.java rename to landlords-common/src/main/java/org/nico/ratel/landlords/helper/RoomHelper.java diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/helper/TimeHelper.java b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/TimeHelper.java similarity index 81% rename from landlords-server/src/main/java/org/nico/ratel/landlords/server/event/helper/TimeHelper.java rename to landlords-common/src/main/java/org/nico/ratel/landlords/helper/TimeHelper.java index 66bc46a..effbce4 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/helper/TimeHelper.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/TimeHelper.java @@ -1,4 +1,4 @@ -package org.nico.ratel.landlords.server.event.helper; +package org.nico.ratel.landlords.helper; /** * diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_EXIT.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_EXIT.java new file mode 100644 index 0000000..c4a4d3d --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_EXIT.java @@ -0,0 +1,25 @@ +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.ClientEventCode; +import org.nico.ratel.landlords.server.ServerContains; + +public class ServerEventListener_CODE_CLIENT_EXIT implements ServerEventListener{ + + @Override + public void call(ClientSide clientSide, String data) { + + Room room = ServerContains.ROOM_MAP.get(clientSide.getRoomId()); + + if(room != null) { + for(ClientSide client: room.getClientSideList()) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_CLIENT_EXIT, clientSide.getNickname()); + client.init(); + } + ServerContains.ROOM_MAP.remove(room.getId()); + } + } + +} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_SET_NICKNAME.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java similarity index 84% rename from landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_SET_NICKNAME.java rename to landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java index 08a62c0..31157ed 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_SET_NICKNAME.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java @@ -5,7 +5,7 @@ import org.nico.ratel.landlords.entity.ClientSide; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.server.ServerContains; -public class ServerEventListener_CODE_SET_NICKNAME implements ServerEventListener{ +public class ServerEventListener_CODE_CLIENT_NICKNAME_SET implements ServerEventListener{ @Override public void call(ClientSide client, String data) { diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GRAB_LANDLORD.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java similarity index 67% rename from landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GRAB_LANDLORD.java rename to landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java index 1bbcfd0..4d1245c 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GRAB_LANDLORD.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java @@ -1,15 +1,16 @@ package org.nico.ratel.landlords.server.event; +import org.nico.noson.Noson; 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.ServerEventCode; +import org.nico.ratel.landlords.helper.PokerHelper; +import org.nico.ratel.landlords.helper.TimeHelper; import org.nico.ratel.landlords.server.ServerContains; -import org.nico.ratel.landlords.server.event.helper.PokerHelper; -import org.nico.ratel.landlords.server.event.helper.RoomHelper; -import org.nico.ratel.landlords.server.event.helper.TimeHelper; -public class ServerEventListener_CODE_GRAB_LANDLORD implements ServerEventListener{ +public class ServerEventListener_CODE_GAME_LANDLORD_ELECT implements ServerEventListener{ @Override public void call(ClientSide clientSide, String data) { @@ -23,9 +24,14 @@ public class ServerEventListener_CODE_GRAB_LANDLORD implements ServerEventListen room.setLandlordId(clientSide.getId()); for(ClientSide client: room.getClientSideList()){ - ChannelUtils.pushToClient(client.getChannel(), null, null, clientSide.getNickname() + " grabed landlord, he got three pokers:\r\n" + PokerHelper.unfoldPoker(room.getLandlordPokers(), false)); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_CONFIRM, clientSide.getNickname()); } - + + for(ClientSide client: room.getClientSideList()){ + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_POKERS_LANDLORD, Noson.reversal(room.getLandlordPokers())); + } + + //PokerHelper.unfoldPoker(room.getLandlordPokers(), false) TimeHelper.sleep(500); for(ClientSide client: room.getClientSideList()){ @@ -36,13 +42,14 @@ public class ServerEventListener_CODE_GRAB_LANDLORD implements ServerEventListen TimeHelper.sleep(200); - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_PLAY_ROUND, PokerHelper.unfoldPoker(clientSide.getPokers(), true)); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, PokerHelper.unfoldPoker(clientSide.getPokers(), true)); }else{ if(clientSide.getNext().getId() == room.getLandlordId()){ for(ClientSide client: room.getClientSideList()){ ChannelUtils.pushToClient(client.getChannel(), null, null, "No one takes the landowner. Reissue the license"); } - RoomHelper.restartingGame(room); + + ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(room.getId())); }else{ for(ClientSide client: room.getClientSideList()){ if(client.getId() != clientSide.getId()){ @@ -60,7 +67,7 @@ public class ServerEventListener_CODE_GRAB_LANDLORD implements ServerEventListen TimeHelper.sleep(500); - ChannelUtils.pushToClient(currentClient.getChannel(), ClientEventCode.CODE_GRAB_LANDLORD, null); + ChannelUtils.pushToClient(currentClient.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_ELECT, null); } } } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_PLAY_ROUND.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY.java similarity index 79% rename from landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_PLAY_ROUND.java rename to landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY.java index eadd5c1..64602da 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_PLAY_ROUND.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY.java @@ -10,11 +10,11 @@ import org.nico.ratel.landlords.entity.PokerSell; import org.nico.ratel.landlords.entity.Room; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.SellType; +import org.nico.ratel.landlords.helper.PokerHelper; +import org.nico.ratel.landlords.helper.TimeHelper; import org.nico.ratel.landlords.server.ServerContains; -import org.nico.ratel.landlords.server.event.helper.PokerHelper; -import org.nico.ratel.landlords.server.event.helper.TimeHelper; -public class ServerEventListener_CODE_PLAY_ROUND implements ServerEventListener{ +public class ServerEventListener_CODE_GAME_POKER_PLAY implements ServerEventListener{ @Override public void call(ClientSide clientSide, String data) { @@ -28,9 +28,9 @@ public class ServerEventListener_CODE_PLAY_ROUND implements ServerEventListener{ if(room.getLastSellClient() != -1 && room.getLastSellClient() != clientSide.getId() && room.getLastPokerShell() != null){ PokerSell lastPokerShell = room.getLastPokerShell(); if(lastPokerShell.getSellType() != currentPokerShell.getSellType() && currentPokerShell.getSellType() != SellType.BOMB && currentPokerShell.getSellType() != SellType.KING_BOMB) { - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_PLAY_ROUND, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "Your sell type is " + currentPokerShell.getSellType().getMsg() + " ,but last sell type is " + lastPokerShell.getSellType().getMsg()); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "Your sell type is " + currentPokerShell.getSellType().getMsg() + " ,but last sell type is " + lastPokerShell.getSellType().getMsg()); }else if(lastPokerShell.getScore() >= currentPokerShell.getScore()) { - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_PLAY_ROUND, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "It's not as big as the other side"); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "It's not as big as the other side"); }else { sellFlag = true; } @@ -54,7 +54,7 @@ public class ServerEventListener_CODE_PLAY_ROUND implements ServerEventListener{ } }else { ClientSide next = clientSide.getNext(); - ChannelUtils.pushToClient(next.getChannel(), ClientEventCode.CODE_PLAY_ROUND, PokerHelper.unfoldPoker(next.getPokers(), true)); + ChannelUtils.pushToClient(next.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, PokerHelper.unfoldPoker(next.getPokers(), true)); } } }else{ @@ -65,9 +65,9 @@ public class ServerEventListener_CODE_PLAY_ROUND implements ServerEventListener{ TimeHelper.sleep(500); ClientSide next = clientSide.getNext(); - ChannelUtils.pushToClient(next.getChannel(), ClientEventCode.CODE_PLAY_ROUND, PokerHelper.unfoldPoker(next.getPokers(), true)); + ChannelUtils.pushToClient(next.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, PokerHelper.unfoldPoker(next.getPokers(), true)); }else { - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_PLAY_ROUND, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "The draw number is invalid"); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "The draw number is invalid"); } } } 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 new file mode 100644 index 0000000..bded99f --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java @@ -0,0 +1,67 @@ +package org.nico.ratel.landlords.server.event; + +import java.util.LinkedList; +import java.util.List; + +import org.nico.noson.Noson; +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.entity.ClientSide; +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.entity.Room; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ClientType; +import org.nico.ratel.landlords.enums.RoomStatus; +import org.nico.ratel.landlords.helper.PokerHelper; +import org.nico.ratel.landlords.helper.TimeHelper; +import org.nico.ratel.landlords.server.ServerContains; + +public class ServerEventListener_CODE_GAME_STARTING implements ServerEventListener{ + + @Override + public void call(ClientSide clientSide, String data) { + + Room room = ServerContains.ROOM_MAP.get(data); + + LinkedList roomClientList = room.getClientSideList(); + + // Push start game messages + room.setStatus(RoomStatus.STARTING); + for(ClientSide client: roomClientList) { + client.setType(ClientType.PEASANT); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_STARTING, Noson.reversal(room)); + } + + TimeHelper.sleep(500); + + // Send the points of poker + List> pokersList = PokerHelper.distributePoker(); + int cursor = 0; + for(ClientSide client: roomClientList){ + client.setPokers(pokersList.get(cursor ++)); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_POKERS, PokerHelper.unfoldPoker(client.getPokers(), false)); + } + room.setLandlordPokers(pokersList.get(3)); + + TimeHelper.sleep(500); + + // Push information about the robber + int startGrabIndex = (int)(Math.random() * 3); + ClientSide startGrabClient = roomClientList.get(startGrabIndex); + room.setLandlordId(startGrabClient.getId()); + + for(ClientSide client: roomClientList){ + if(client.getId() == startGrabClient.getId()){ + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_ELECT, null); + }else{ + ChannelUtils.pushToClient(client.getChannel(), null, "Wait for " + startGrabClient.getNickname() + " verify local owner"); + } + } + + + } + + + + + +} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOM_LIST.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOM_LIST.java deleted file mode 100644 index d21c74b..0000000 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOM_LIST.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.nico.ratel.landlords.server.event; - -import org.nico.noson.Noson; -import org.nico.ratel.landlords.channel.ChannelUtils; -import org.nico.ratel.landlords.entity.ClientSide; -import org.nico.ratel.landlords.enums.ClientEventCode; -import org.nico.ratel.landlords.server.ServerContains; - -public class ServerEventListener_CODE_GET_ROOM_LIST implements ServerEventListener{ - - @Override - public void call(ClientSide clientSide, String data) { - - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_SHOW_ROOM_LIST, Noson.reversal(ServerContains.ROOM_MAP)); - } - - - - - -} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_JOIN_ROOM.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_JOIN_ROOM.java deleted file mode 100644 index 865520f..0000000 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_JOIN_ROOM.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.nico.ratel.landlords.server.event; - -import java.util.LinkedList; -import java.util.concurrent.ConcurrentSkipListMap; - -import org.nico.noson.Noson; -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.RoomStatus; -import org.nico.ratel.landlords.server.ServerContains; -import org.nico.ratel.landlords.server.event.helper.RoomHelper; - -public class ServerEventListener_CODE_JOIN_ROOM implements ServerEventListener{ - - @Override - public void call(ClientSide clientSide, String data) { - - Room room = ServerContains.ROOM_MAP.get(Integer.valueOf(data)); - - if(room == null) { - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_SHOW_OPTIONS, null, "Room inexistence !"); - }else if(room.getClientSideList().size() == 3) { - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_SHOW_OPTIONS, null, "The room is full !"); - }else { - clientSide.setRoomId(room.getId()); - - ConcurrentSkipListMap roomClientMap = (ConcurrentSkipListMap) room.getClientSideMap(); - LinkedList roomClientList = room.getClientSideList(); - - if(roomClientList.size() > 0){ - ClientSide pre = roomClientList.getLast(); - pre.setNext(clientSide); - clientSide.setPre(pre); - } - - roomClientList.add(clientSide); - roomClientMap.put(clientSide.getId(), clientSide); - - if(roomClientMap.size() == 3) { - clientSide.setNext(roomClientList.getFirst()); - roomClientList.getFirst().setPre(clientSide); - - RoomHelper.restartingGame(room); - }else { - room.setStatus(RoomStatus.WAIT); - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_JOIN_ROOM_SUCCESS, Noson.reversal(room)); - for(ClientSide client: roomClientMap.values()) { - if(client.getId() != clientSide.getId()) { - ChannelUtils.pushToClient(client.getChannel(), null, null, clientSide.getNickname() + " join room !"); - } - } - } - } - } - - - - - -} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_PLAYER_EXIT.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_PLAYER_EXIT.java deleted file mode 100644 index 46fe3ab..0000000 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_PLAYER_EXIT.java +++ /dev/null @@ -1,32 +0,0 @@ -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.entity.ServerTransferData; -import org.nico.ratel.landlords.enums.ClientEventCode; -import org.nico.ratel.landlords.server.ServerContains; - -import io.netty.channel.Channel; - -public class ServerEventListener_CODE_PLAYER_EXIT implements ServerEventListener{ - - @Override - public void call(ClientSide client, String data) { - - Room room = ServerContains.ROOM_MAP.get(client.getRoomId()); - - if(room != null) { - for(ClientSide roomClient: room.getClientSideList()) { - if(client.getId() != roomClient.getId()) { - ChannelUtils.pushToClient(roomClient.getChannel(), ClientEventCode.CODE_SHOW_OPTIONS, null, client.getNickname() + " exited, room dissolve!!"); - }else { - ChannelUtils.pushToClient(roomClient.getChannel(), ClientEventCode.CODE_SHOW_OPTIONS, null, "you exited, room dissolve!!"); - } - roomClient.init(); - } - ServerContains.ROOM_MAP.remove(room.getId()); - } - } - -} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CREATE_ROOM.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java similarity index 82% rename from landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CREATE_ROOM.java rename to landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java index 9573e63..f290265 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CREATE_ROOM.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java @@ -4,25 +4,25 @@ import org.nico.noson.Noson; 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.entity.ServerTransferData; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.RoomStatus; import org.nico.ratel.landlords.server.ServerContains; -public class ServerEventListener_CODE_CREATE_ROOM implements ServerEventListener{ +public class ServerEventListener_CODE_ROOM_CREATE implements ServerEventListener{ @Override public void call(ClientSide clientSide, String data) { Room room = new Room(ServerContains.getServerId()); room.setStatus(RoomStatus.BLANK); + room.setRoomOwner(clientSide.getNickname()); room.getClientSideMap().put(clientSide.getId(), clientSide); room.getClientSideList().add(clientSide); clientSide.setRoomId(room.getId()); ServerContains.ROOM_MAP.put(room.getId(), room); - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_JOIN_ROOM_SUCCESS, Noson.reversal(room)); + 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 new file mode 100644 index 0000000..e5668e2 --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_JOIN.java @@ -0,0 +1,77 @@ +package org.nico.ratel.landlords.server.event; + +import java.util.LinkedList; +import java.util.concurrent.ConcurrentSkipListMap; + +import org.nico.noson.Noson; +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.RoomStatus; +import org.nico.ratel.landlords.enums.ServerEventCode; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.server.ServerContains; + +public class ServerEventListener_CODE_ROOM_JOIN implements ServerEventListener{ + + @Override + public void call(ClientSide clientSide, String data) { + + Room room = ServerContains.ROOM_MAP.get(Integer.valueOf(data)); + + if(room == null) { + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_JOIN_FAIL_BY_INEXIST, data); + }else { + String result = MapHelper.newInstance() + .put("clientId", clientSide.getId()) + .put("clientNickname", clientSide.getNickname()) + .put("roomId", room.getId()) + .put("roomOwner", room.getRoomOwner()) + .put("roomClientCount", room.getClientSideList().size()) + .json(); + + if(room.getClientSideList().size() == 3) { + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_JOIN_FAIL_BY_FULL, Noson.reversal(room)); + }else { + clientSide.setRoomId(room.getId()); + + ConcurrentSkipListMap roomClientMap = (ConcurrentSkipListMap) room.getClientSideMap(); + LinkedList roomClientList = room.getClientSideList(); + + if(roomClientList.size() > 0){ + ClientSide pre = roomClientList.getLast(); + pre.setNext(clientSide); + clientSide.setPre(pre); + } + + roomClientList.add(clientSide); + roomClientMap.put(clientSide.getId(), clientSide); + + if(roomClientMap.size() == 3) { + clientSide.setNext(roomClientList.getFirst()); + roomClientList.getFirst().setPre(clientSide); + + ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(room.getId())); + }else { + room.setStatus(RoomStatus.WAIT); + + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_JOIN_SUCCESS, result); + + for(ClientSide client: roomClientMap.values()) { + if(client.getId() != clientSide.getId()) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_ROOM_JOIN_TRIGGER, result); + } + } + } + } + + + } + } + + + + + +} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_LIST_GET.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_LIST_GET.java new file mode 100644 index 0000000..0dbd0c7 --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_LIST_GET.java @@ -0,0 +1,36 @@ +package org.nico.ratel.landlords.server.event; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.nico.noson.Noson; +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.helper.MapHelper; +import org.nico.ratel.landlords.server.ServerContains; + +public class ServerEventListener_CODE_ROOM_LIST_GET implements ServerEventListener{ + + @Override + public void call(ClientSide clientSide, String data) { + List> roomList = new ArrayList<>(ServerContains.ROOM_MAP.size()); + for(Entry entry: ServerContains.ROOM_MAP.entrySet()) { + Room room = entry.getValue(); + roomList.add(MapHelper.newInstance() + .put("roomId", room.getId()) + .put("roomOwner", room.getRoomOwner()) + .put("roomClientCount", room.getClientSideList().size()) + .map()); + } + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_SHOW_ROOMS, Noson.reversal(roomList)); + } + + + + + +} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java index 0c25ce1..debec44 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java @@ -26,7 +26,7 @@ public class TransferHandler extends ChannelInboundHandlerAdapter{ clientSide.setNickname(String.valueOf(clientSide.getId())); ServerContains.CLIENT_SIDE_MAP.put(clientSide.getId(), clientSide); SimplePrinter.println("A client connects to the server:" + clientSide.getId()); - ChannelUtils.pushToClient(ch, ClientEventCode.CODE_SET_NICKNAME, null); + ChannelUtils.pushToClient(ch, ClientEventCode.CODE_CLIENT_NICKNAME_SET, null); } @Override @@ -52,7 +52,7 @@ public class TransferHandler extends ChannelInboundHandlerAdapter{ ClientSide client = ServerContains.CLIENT_SIDE_MAP.get(((InetSocketAddress)ctx.channel().remoteAddress()).getPort()); if(client != null) { SimplePrinter.println(client.getNickname() + " exit"); - ServerEventListener.get(ServerEventCode.CODE_PLAYER_EXIT).call(client, null); + ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(client, null); } } } From 2e71d6dff14728dfb94daaa18628eaa998e0a586 Mon Sep 17 00:00:00 2001 From: nico Date: Thu, 8 Nov 2018 21:28:01 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B8=8D=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ratel/landlords/helper/RoomHelper.java | 60 ------------------- 1 file changed, 60 deletions(-) delete mode 100644 landlords-common/src/main/java/org/nico/ratel/landlords/helper/RoomHelper.java diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/helper/RoomHelper.java b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/RoomHelper.java deleted file mode 100644 index f1eec7e..0000000 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/helper/RoomHelper.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.nico.ratel.landlords.server.event.helper; - -import java.util.LinkedList; -import java.util.List; - -import org.nico.noson.Noson; -import org.nico.ratel.landlords.channel.ChannelUtils; -import org.nico.ratel.landlords.entity.ClientSide; -import org.nico.ratel.landlords.entity.Poker; -import org.nico.ratel.landlords.entity.Room; -import org.nico.ratel.landlords.enums.ClientEventCode; -import org.nico.ratel.landlords.enums.ClientType; -import org.nico.ratel.landlords.enums.RoomStatus; - -/** - * - * @author nico - */ - -public class RoomHelper { - - public static void restartingGame(Room room){ - LinkedList roomClientList = room.getClientSideList(); - - // Push start game messages - room.setStatus(RoomStatus.STARTING); - for(ClientSide client: roomClientList) { - client.setType(ClientType.PEASANT); - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_ROOM_STARTING, Noson.reversal(room)); - } - - TimeHelper.sleep(500); - - // Send the points of poker - List> pokersList = PokerHelper.distributePoker(); - int cursor = 0; - for(ClientSide client: roomClientList){ - client.setPokers(pokersList.get(cursor ++)); - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_POKERS, PokerHelper.unfoldPoker(client.getPokers(), false)); - } - room.setLandlordPokers(pokersList.get(3)); - - TimeHelper.sleep(500); - - // Push information about the robber - int startGrabIndex = (int)(Math.random() * 3); - ClientSide startGrabClient = roomClientList.get(startGrabIndex); - room.setLandlordId(startGrabClient.getId()); - - for(ClientSide client: roomClientList){ - if(client.getId() == startGrabClient.getId()){ - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GRAB_LANDLORD, null); - }else{ - ChannelUtils.pushToClient(client.getChannel(), null, "Wait for " + startGrabClient.getNickname() + " verify local owner"); - } - } - - } - -} From 6b8167c095605a2a16a4a5b9d337cca2cf09946f Mon Sep 17 00:00:00 2001 From: nico Date: Fri, 9 Nov 2018 17:57:56 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E6=B5=81=E7=A8=8B=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/event/ClientEventListener.java | 5 +- ...ntListener_CODE_GAME_LANDLORD_CONFIRM.java | 24 ++++++- ...ventListener_CODE_GAME_LANDLORD_CYCLE.java | 21 ++++++ ...ventListener_CODE_GAME_LANDLORD_ELECT.java | 30 ++++++--- .../ClientEventListener_CODE_GAME_OVER.java | 5 ++ ...entEventListener_CODE_GAME_POKER_PLAY.java | 12 +++- ...stener_CODE_GAME_POKER_PLAY_DONT_SELL.java | 28 ++++++++ ...Listener_CODE_GAME_POKER_PLAY_INVALID.java | 17 +++++ ...entListener_CODE_GAME_POKER_PLAY_LESS.java | 24 +++++++ ...istener_CODE_GAME_POKER_PLAY_MISMATCH.java | 25 +++++++ ...lientEventListener_CODE_GAME_STARTING.java | 21 +++++- ...tListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java | 2 + ...stener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java | 8 +-- ...ClientEventListener_CODE_SHOW_OPTIONS.java | 2 +- .../ClientEventListener_CODE_SHOW_POKERS.java | 11 ++- ...entListener_CODE_SHOW_POKERS_LANDLORD.java | 22 ------ .../ratel/landlords/channel/ChannelUtils.java | 6 +- .../landlords/enums/ClientEventCode.java | 12 +++- .../landlords/enums/ServerEventCode.java | 6 +- ...ventListener_CODE_GAME_LANDLORD_ELECT.java | 67 ++++++------------- ...rEventListener_CODE_GAME_LANDLORD_GET.java | 39 +++++++++++ ...verEventListener_CODE_GAME_POKER_PLAY.java | 53 +++++++++++---- ...erverEventListener_CODE_GAME_STARTING.java | 50 ++++++-------- .../ServerEventListener_CODE_GET_POKERS.java | 16 +++++ ...> ServerEventListener_CODE_GET_ROOMS.java} | 6 +- .../ServerEventListener_CODE_POKERS_GET.java | 32 +++++++++ .../ServerEventListener_CODE_ROOM_JOIN.java | 2 +- .../server/handler/TransferHandler.java | 2 + 28 files changed, 405 insertions(+), 143 deletions(-) create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CYCLE.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_INVALID.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_LESS.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH.java delete mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS_LANDLORD.java create mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_GET.java create mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_POKERS.java rename landlords-server/src/main/java/org/nico/ratel/landlords/server/event/{ServerEventListener_CODE_ROOM_LIST_GET.java => ServerEventListener_CODE_GET_ROOMS.java} (92%) create mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_POKERS_GET.java diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java index b360028..1f46b0a 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java @@ -8,6 +8,7 @@ import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.ServerEventCode; import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; public abstract class ClientEventListener { @@ -35,7 +36,7 @@ public abstract class ClientEventListener { return listener; } - protected void pushToServer(Channel channel, ServerEventCode code, String datas){ - ChannelUtils.pushToServer(channel, code, datas); + protected ChannelFuture pushToServer(Channel channel, ServerEventCode code, String datas){ + return ChannelUtils.pushToServer(channel, code, datas); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java index c3a4281..c06f4fb 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java @@ -1,5 +1,15 @@ package org.nico.ratel.landlords.client.event; +import java.util.List; +import java.util.Map; + +import org.nico.noson.Noson; +import org.nico.noson.entity.NoType; +import org.nico.ratel.landlords.client.SimpleClient; +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -8,7 +18,19 @@ public class ClientEventListener_CODE_GAME_LANDLORD_CONFIRM extends ClientEventL @Override public void call(Channel channel, String data) { - SimplePrinter.print(data + " grabed landlord, he got three pokers"); + Map map = MapHelper.parser(data); + + String landlordNickname = (String) map.get("landlordNickname"); + + SimplePrinter.println(landlordNickname + " grabed landlord, he got three pokers"); + + List additionalPokers = Noson.convert(map.get("additionalPokers"), new NoType>() {}); + SimplePrinter.println(PokerHelper.unfoldPoker(additionalPokers, false)); + + int landlordId = (int) map.get("landlordId"); + if(SimpleClient.id == landlordId) { + get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); + } } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CYCLE.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CYCLE.java new file mode 100644 index 0000000..0ec28df --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CYCLE.java @@ -0,0 +1,21 @@ +package org.nico.ratel.landlords.client.event; + +import java.util.Map; + +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 io.netty.channel.Channel; + +public class ClientEventListener_CODE_GAME_LANDLORD_CYCLE extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + SimplePrinter.println("No one takes the landowner. Reissue the license"); + + } + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java index 7421def..1994e18 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java @@ -1,6 +1,10 @@ package org.nico.ratel.landlords.client.event; +import java.util.Map; + +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; @@ -10,16 +14,24 @@ public class ClientEventListener_CODE_GAME_LANDLORD_ELECT extends ClientEventLis @Override public void call(Channel channel, String data) { - SimplePrinter.print("It's your turn to rob the landlord[Y/N]:"); - String line = SimpleWriter.write(); - if(line.equalsIgnoreCase("Y")){ - pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "TRUE"); - }else if(line.equalsIgnoreCase("N")){ - pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "FALSE"); - }else{ - SimplePrinter.println("Invalid options"); - call(channel, data); + Map map = MapHelper.parser(data); + int turnClientId = (int) map.get("turnClientId"); + + if(turnClientId == SimpleClient.id) { + SimplePrinter.println("It's your turn to rob the landlord[Y/N]:"); + String line = SimpleWriter.write(); + if(line.equalsIgnoreCase("Y")){ + pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "TRUE"); + }else if(line.equalsIgnoreCase("N")){ + pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "FALSE"); + }else{ + SimplePrinter.println("Invalid options"); + call(channel, data); + } + }else { + SimplePrinter.println("It's turn " + map.get("turnClientNickname") + " to confirm, please wait patiently !"); } + } } 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 50fe7c2..bac4ec6 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 @@ -1,5 +1,8 @@ package org.nico.ratel.landlords.client.event; +import java.util.Map; + +import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -8,6 +11,8 @@ public class ClientEventListener_CODE_GAME_OVER extends ClientEventListener{ @Override public void call(Channel channel, String data) { + Map map = MapHelper.parser(data); + SimplePrinter.println(map.get("winnerNickname") + " win"); SimplePrinter.println("Game over"); } 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 2005ce8..6089418 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 @@ -2,10 +2,15 @@ package org.nico.ratel.landlords.client.event; import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.nico.noson.Noson; +import org.nico.noson.entity.NoType; +import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.enums.PokerLevel; import org.nico.ratel.landlords.enums.ServerEventCode; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.print.SimplePrinter; import org.nico.ratel.landlords.print.SimpleWriter; @@ -15,7 +20,12 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY extends ClientEventListene @Override public void call(Channel channel, String data) { - SimplePrinter.println(data); + Map map = MapHelper.parser(data); + + List pokers = Noson.convert(map.get("pokers"), new NoType>() {}); + SimplePrinter.println(PokerHelper.unfoldPoker(pokers, true)); + + SimplePrinter.println("Please enter the number you want:"); String line = SimpleWriter.write(); diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL.java new file mode 100644 index 0000000..58db039 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL.java @@ -0,0 +1,28 @@ +package org.nico.ratel.landlords.client.event; + +import java.util.Map; + +import org.nico.ratel.landlords.client.SimpleClient; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + Map map = MapHelper.parser(data); + + SimplePrinter.println(map.get("currentClientNickname") + " dont sell"); + SimplePrinter.println("It's turn " + map.get("turnClientNickname")); + + int turnClientId = (int) map.get("turnClientId"); + if(SimpleClient.id == turnClientId) { + get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); + } + } + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_INVALID.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_INVALID.java new file mode 100644 index 0000000..d52f4af --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_INVALID.java @@ -0,0 +1,17 @@ +package org.nico.ratel.landlords.client.event; + +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_GAME_POKER_PLAY_INVALID extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + + SimplePrinter.println("Sell is invalid"); + get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); + } + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_LESS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_LESS.java new file mode 100644 index 0000000..f90d6c7 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_LESS.java @@ -0,0 +1,24 @@ +package org.nico.ratel.landlords.client.event; + +import java.util.Map; + +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_GAME_POKER_PLAY_LESS extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + Map map = MapHelper.parser(data); + + SimplePrinter.println("Your pokers type is less than pre pokers"); + SimplePrinter.println("Could not beat !!"); + + get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); + } + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH.java new file mode 100644 index 0000000..9bc0c76 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH.java @@ -0,0 +1,25 @@ +package org.nico.ratel.landlords.client.event; + +import java.util.Map; + +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + Map map = MapHelper.parser(data); + + SimplePrinter.println("Your pokers type is " + map.get("playType")); + SimplePrinter.println("Pre pokers type is " + map.get("preType")); + SimplePrinter.println("Mismatch !!"); + + get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); + } + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_STARTING.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_STARTING.java index 0a43860..e3a9825 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_STARTING.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_STARTING.java @@ -1,5 +1,14 @@ package org.nico.ratel.landlords.client.event; +import java.util.List; +import java.util.Map; + +import org.nico.noson.Noson; +import org.nico.noson.entity.NoType; +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -8,8 +17,16 @@ public class ClientEventListener_CODE_GAME_STARTING extends ClientEventListener{ @Override public void call(Channel channel, String data) { - SimplePrinter.println(data + " game starting !"); - SimplePrinter.println("Wait for the server to issue the license..."); + + Map map = MapHelper.parser(data); + + SimplePrinter.println(map.get("roomId") + " game starting !"); + + List pokers = Noson.convert(map.get("pokers"), new NoType>() {}); + SimplePrinter.println("Your pokers is:"); + SimplePrinter.println(PokerHelper.unfoldPoker(pokers, false)); + + get(ClientEventCode.CODE_GAME_LANDLORD_ELECT).call(channel, data); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java index bc97f5b..99f0a80 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java @@ -2,6 +2,7 @@ package org.nico.ratel.landlords.client.event; import java.util.Map; +import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; @@ -14,6 +15,7 @@ public class ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL extends ClientEvent Map dataMap = MapHelper.parser(data); SimplePrinter.println("Join fail, room " + dataMap.get("roomId") + " is full!"); + ClientEventListener.get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java index fccc31a..966ff6f 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java @@ -1,8 +1,6 @@ package org.nico.ratel.landlords.client.event; -import java.util.Map; - -import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -11,8 +9,8 @@ public class ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST extends ClientEv @Override public void call(Channel channel, String data) { - - SimplePrinter.println("Join fail, room " + data + " is full!"); + SimplePrinter.println("Join fail, room " + data + " is inexist!"); + ClientEventListener.get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java index a6ce096..0279668 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java @@ -27,7 +27,7 @@ public class ClientEventListener_CODE_SHOW_OPTIONS extends ClientEventListener{ if(choose == 1) { pushToServer(channel, ServerEventCode.CODE_ROOM_CREATE, null); }else if(choose == 2){ - pushToServer(channel, ServerEventCode.CODE_ROOM_LIST_GET, null); + pushToServer(channel, ServerEventCode.CODE_GET_ROOMS, null); }else { SimplePrinter.print("Your choose rooms number:"); line = SimpleWriter.write(); diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java index 32a6382..0884c24 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java @@ -1,10 +1,12 @@ package org.nico.ratel.landlords.client.event; import java.util.List; +import java.util.Map; import org.nico.noson.Noson; import org.nico.noson.entity.NoType; import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.print.SimplePrinter; @@ -14,8 +16,13 @@ public class ClientEventListener_CODE_SHOW_POKERS extends ClientEventListener{ @Override public void call(Channel channel, String data) { - List landlordPokers = Noson.convert(data, new NoType>() {}); - SimplePrinter.println(PokerHelper.unfoldPoker(landlordPokers, false)); + + Map map = MapHelper.parser(data); + + SimplePrinter.println(map.get("currentClientNickname") + " sell:"); + + List pokers = Noson.convert(map.get("pokers"), new NoType>() {}); + SimplePrinter.println(PokerHelper.unfoldPoker(pokers, false)); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS_LANDLORD.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS_LANDLORD.java deleted file mode 100644 index 3b2aef8..0000000 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS_LANDLORD.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.nico.ratel.landlords.client.event; - -import java.util.List; - -import org.nico.noson.Noson; -import org.nico.noson.entity.NoType; -import org.nico.ratel.landlords.entity.Poker; -import org.nico.ratel.landlords.helper.PokerHelper; -import org.nico.ratel.landlords.print.SimplePrinter; - -import io.netty.channel.Channel; - -public class ClientEventListener_CODE_SHOW_POKERS_LANDLORD extends ClientEventListener{ - - @Override - public void call(Channel channel, String data) { - - List landlordPokers = Noson.convert(data, new NoType>() {}); - SimplePrinter.println(PokerHelper.unfoldPoker(landlordPokers, false)); - } - -} diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/channel/ChannelUtils.java b/landlords-common/src/main/java/org/nico/ratel/landlords/channel/ChannelUtils.java index f5d89f1..1c8a815 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/channel/ChannelUtils.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/channel/ChannelUtils.java @@ -1,12 +1,12 @@ package org.nico.ratel.landlords.channel; -import org.nico.noson.Noson; import org.nico.ratel.landlords.entity.ClientTransferData; import org.nico.ratel.landlords.entity.ServerTransferData; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.ServerEventCode; import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; public class ChannelUtils { @@ -19,9 +19,9 @@ public class ChannelUtils { channel.writeAndFlush(clientTransferData); } - public static void pushToServer(Channel channel, ServerEventCode code, String data) { + public static ChannelFuture pushToServer(Channel channel, ServerEventCode code, String data) { ServerTransferData serverTransferData = new ServerTransferData(code, data); - channel.writeAndFlush(serverTransferData); + return channel.writeAndFlush(serverTransferData); } } 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 e7be04e..ba4a8ed 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 @@ -16,8 +16,6 @@ public enum ClientEventCode implements Serializable{ CODE_SHOW_POKERS("展示Poker"), - CODE_SHOW_POKERS_LANDLORD("展示地主牌"), - CODE_ROOM_CREATE_SUCCESS("创建房间成功"), CODE_ROOM_JOIN_SUCCESS("加入房间成功"), @@ -34,8 +32,18 @@ public enum ClientEventCode implements Serializable{ CODE_GAME_LANDLORD_CONFIRM("地主确认"), + CODE_GAME_LANDLORD_CYCLE("地主一轮确认结束"), + CODE_GAME_POKER_PLAY("出牌回合"), + CODE_GAME_POKER_PLAY_MISMATCH("出牌不匹配"), + + CODE_GAME_POKER_PLAY_LESS("出牌太小"), + + CODE_GAME_POKER_PLAY_DONT_SELL("不出"), + + CODE_GAME_POKER_PLAY_INVALID("无效"), + CODE_GAME_OVER("游戏结束"), ; 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 d436535..a4c54fb 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 @@ -10,14 +10,16 @@ public enum ServerEventCode implements Serializable{ CODE_ROOM_CREATE("创建房间"), - CODE_ROOM_LIST_GET("获取房间列表"), - + CODE_GET_ROOMS("获取房间列表"), + CODE_ROOM_JOIN("加入房间"), CODE_GAME_STARTING("游戏开始"), CODE_GAME_LANDLORD_ELECT("抢地主"), + CODE_GAME_LANDLORD_GET("抢地主信息"), + CODE_GAME_POKER_PLAY("出牌环节"), ; diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java index 4d1245c..9701389 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java @@ -1,13 +1,12 @@ package org.nico.ratel.landlords.server.event; -import org.nico.noson.Noson; 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.ServerEventCode; +import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.helper.PokerHelper; -import org.nico.ratel.landlords.helper.TimeHelper; import org.nico.ratel.landlords.server.ServerContains; public class ServerEventListener_CODE_GAME_LANDLORD_ELECT implements ServerEventListener{ @@ -24,56 +23,34 @@ public class ServerEventListener_CODE_GAME_LANDLORD_ELECT implements ServerEvent room.setLandlordId(clientSide.getId()); for(ClientSide client: room.getClientSideList()){ - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_CONFIRM, clientSide.getNickname()); + String result = MapHelper.newInstance() + .put("roomId", room.getId()) + .put("roomOwner", room.getRoomOwner()) + .put("roomClientCount", room.getClientSideList().size()) + .put("landlordNickname", clientSide.getNickname()) + .put("landlordId", clientSide.getId()) + .put("pokers", client.getPokers()) + .put("additionalPokers", room.getLandlordPokers()) + .json(); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_CONFIRM, result); } - - for(ClientSide client: room.getClientSideList()){ - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_POKERS_LANDLORD, Noson.reversal(room.getLandlordPokers())); - } - - //PokerHelper.unfoldPoker(room.getLandlordPokers(), false) - TimeHelper.sleep(500); - - for(ClientSide client: room.getClientSideList()){ - if(client.getId() != clientSide.getId()){ - ChannelUtils.pushToClient(client.getChannel(), null, null, "Please wait for " + clientSide.getNickname() + " to come out"); - } - } - - TimeHelper.sleep(200); - - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, PokerHelper.unfoldPoker(clientSide.getPokers(), true)); }else{ if(clientSide.getNext().getId() == room.getLandlordId()){ for(ClientSide client: room.getClientSideList()){ - ChannelUtils.pushToClient(client.getChannel(), null, null, "No one takes the landowner. Reissue the license"); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_CYCLE, null); } - - ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(room.getId())); + ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, null); }else{ - for(ClientSide client: room.getClientSideList()){ - if(client.getId() != clientSide.getId()){ - ChannelUtils.pushToClient(client.getChannel(), null, null, clientSide.getNickname() + " don't grab"); - } - } - - TimeHelper.sleep(500); - - ClientSide currentClient = clientSide.getNext(); - for(ClientSide client: room.getClientSideList()){ - ChannelUtils.pushToClient(client.getChannel(), null, null, "Turn " + currentClient.getNickname() + " confirm"); - } - - TimeHelper.sleep(500); - - - ChannelUtils.pushToClient(currentClient.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_ELECT, null); + ClientSide turnClientSide = clientSide.getNext(); + String result = MapHelper.newInstance() + .put("roomId", room.getId()) + .put("roomOwner", room.getRoomOwner()) + .put("roomClientCount", room.getClientSideList().size()) + .put("turnClientNickname", turnClientSide.getNickname()) + .put("turnClientId", turnClientSide.getId()) + .json(); + ChannelUtils.pushToClient(turnClientSide.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_ELECT, result); } } } - - - - - } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_GET.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_GET.java new file mode 100644 index 0000000..d06c120 --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_GET.java @@ -0,0 +1,39 @@ +package org.nico.ratel.landlords.server.event; + +import org.nico.noson.Noson; +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.ServerEventCode; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.helper.PokerHelper; +import org.nico.ratel.landlords.helper.TimeHelper; +import org.nico.ratel.landlords.server.ServerContains; + +public class ServerEventListener_CODE_GAME_LANDLORD_GET implements ServerEventListener{ + + @Override + public void call(ClientSide clientSide, String data) { + + Room room = ServerContains.ROOM_MAP.get(clientSide.getRoomId()); + ClientSide startGrabClient = ServerContains.CLIENT_SIDE_MAP.get(room.getLandlordId()); + + String result = MapHelper.newInstance() + .put("roomId", room.getId()) + .put("roomOwner", room.getRoomOwner()) + .put("roomClientCount", room.getClientSideList().size()) + .put("turnClientNickname", startGrabClient.getNickname()) + .put("turnClientId", startGrabClient.getId()) + .json(); + for(ClientSide client: room.getClientSideList()){ + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_ELECT, result); + } + + } + + + + + +} 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 64602da..5b929a0 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 @@ -10,6 +10,7 @@ import org.nico.ratel.landlords.entity.PokerSell; import org.nico.ratel.landlords.entity.Room; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.SellType; +import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.helper.TimeHelper; import org.nico.ratel.landlords.server.ServerContains; @@ -28,9 +29,19 @@ public class ServerEventListener_CODE_GAME_POKER_PLAY implements ServerEventList if(room.getLastSellClient() != -1 && room.getLastSellClient() != clientSide.getId() && room.getLastPokerShell() != null){ PokerSell lastPokerShell = room.getLastPokerShell(); if(lastPokerShell.getSellType() != currentPokerShell.getSellType() && currentPokerShell.getSellType() != SellType.BOMB && currentPokerShell.getSellType() != SellType.KING_BOMB) { - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "Your sell type is " + currentPokerShell.getSellType().getMsg() + " ,but last sell type is " + lastPokerShell.getSellType().getMsg()); + String result = MapHelper.newInstance() + .put("playType", currentPokerShell.getSellType()) + .put("preType", lastPokerShell.getSellType()) + .put("pokers", clientSide.getPokers()) + .json(); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_MISMATCH, result); }else if(lastPokerShell.getScore() >= currentPokerShell.getScore()) { - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "It's not as big as the other side"); + String result = MapHelper.newInstance() + .put("playScore", currentPokerShell.getScore()) + .put("preScore", lastPokerShell.getScore()) + .put("pokers", clientSide.getPokers()) + .json(); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_LESS, result); }else { sellFlag = true; } @@ -41,33 +52,51 @@ public class ServerEventListener_CODE_GAME_POKER_PLAY implements ServerEventList room.setLastSellClient(clientSide.getId()); room.setLastPokerShell(currentPokerShell); clientSide.getPokers().removeAll(currentPokers); - + String result = MapHelper.newInstance() + .put("currentClientId", clientSide.getId()) + .put("currentClientNickname", clientSide.getNickname()) + .put("pokers", currentPokers) + .json(); for(ClientSide client: room.getClientSideList()) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_POKERS, PokerHelper.unfoldPoker(currentPokers, false), clientSide.getNickname() + " sell, turn " + clientSide.getNext().getNickname()); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_POKERS, result); } TimeHelper.sleep(500); if(clientSide.getPokers().isEmpty()) { + result = MapHelper.newInstance() + .put("winnerNickname", clientSide.getNickname()) + .json(); for(ClientSide client: room.getClientSideList()) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, null, clientSide.getNickname() + " win"); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, result); } }else { ClientSide next = clientSide.getNext(); - ChannelUtils.pushToClient(next.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, PokerHelper.unfoldPoker(next.getPokers(), true)); + result = MapHelper.newInstance() + .put("pokers", next.getPokers()) + .json(); + + ChannelUtils.pushToClient(next.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, result); } } }else{ if(options.length > 0 && options[0] == 'p') { + ClientSide turnClient = clientSide.getNext(); for(ClientSide client: room.getClientSideList()) { - ChannelUtils.pushToClient(client.getChannel(), null, null, clientSide.getNickname() + " don't sell, turn " + clientSide.getNext().getNickname()); + String result = MapHelper.newInstance() + .put("currentClientId", clientSide.getId()) + .put("currentClientNickname", clientSide.getNickname()) + .put("turnClientId", turnClient.getId()) + .put("turnClientNickname", turnClient.getNickname()) + .put("pokers", client.getPokers()) + .json(); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_DONT_SELL, result); } - TimeHelper.sleep(500); - - ClientSide next = clientSide.getNext(); - ChannelUtils.pushToClient(next.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, PokerHelper.unfoldPoker(next.getPokers(), true)); }else { - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, PokerHelper.unfoldPoker(clientSide.getPokers(), true), "The draw number is invalid"); + String result = MapHelper.newInstance() + .put("pokers", clientSide.getPokers()) + .json(); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, result); } } } 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 bded99f..71cc806 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 @@ -11,6 +11,7 @@ import org.nico.ratel.landlords.entity.Room; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.ClientType; import org.nico.ratel.landlords.enums.RoomStatus; +import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.helper.TimeHelper; import org.nico.ratel.landlords.server.ServerContains; @@ -19,49 +20,42 @@ public class ServerEventListener_CODE_GAME_STARTING implements ServerEventListen @Override public void call(ClientSide clientSide, String data) { - - Room room = ServerContains.ROOM_MAP.get(data); - + + Room room = ServerContains.ROOM_MAP.get(clientSide.getRoomId()); + LinkedList roomClientList = room.getClientSideList(); - - // Push start game messages - room.setStatus(RoomStatus.STARTING); - for(ClientSide client: roomClientList) { - client.setType(ClientType.PEASANT); - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_STARTING, Noson.reversal(room)); - } - - TimeHelper.sleep(500); - + // Send the points of poker List> pokersList = PokerHelper.distributePoker(); int cursor = 0; for(ClientSide client: roomClientList){ client.setPokers(pokersList.get(cursor ++)); - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_POKERS, PokerHelper.unfoldPoker(client.getPokers(), false)); } room.setLandlordPokers(pokersList.get(3)); - - TimeHelper.sleep(500); - + // Push information about the robber int startGrabIndex = (int)(Math.random() * 3); ClientSide startGrabClient = roomClientList.get(startGrabIndex); room.setLandlordId(startGrabClient.getId()); + + // Push start game messages + room.setStatus(RoomStatus.STARTING); - for(ClientSide client: roomClientList){ - if(client.getId() == startGrabClient.getId()){ - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_ELECT, null); - }else{ - ChannelUtils.pushToClient(client.getChannel(), null, "Wait for " + startGrabClient.getNickname() + " verify local owner"); - } + for(ClientSide client: roomClientList) { + client.setType(ClientType.PEASANT); + + String result = MapHelper.newInstance() + .put("roomId", room.getId()) + .put("roomOwner", room.getRoomOwner()) + .put("roomClientCount", room.getClientSideList().size()) + .put("turnClientNickname", startGrabClient.getNickname()) + .put("turnClientId", startGrabClient.getId()) + .put("pokers", client.getPokers()) + //.put("additionalPokers", room.getLandlordPokers()) + .json(); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_STARTING, result); } - } - - - - } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_POKERS.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_POKERS.java new file mode 100644 index 0000000..412523a --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_POKERS.java @@ -0,0 +1,16 @@ +package org.nico.ratel.landlords.server.event; + +import org.nico.noson.Noson; +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.entity.ClientSide; +import org.nico.ratel.landlords.enums.ClientEventCode; + +public class ServerEventListener_CODE_GET_POKERS implements ServerEventListener{ + + @Override + public void call(ClientSide clientSide, String data) { + + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_SHOW_POKERS, Noson.reversal(clientSide.getPokers())); + } + +} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_LIST_GET.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOMS.java similarity index 92% rename from landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_LIST_GET.java rename to landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOMS.java index 0dbd0c7..10ce00b 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_LIST_GET.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOMS.java @@ -13,7 +13,7 @@ import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.server.ServerContains; -public class ServerEventListener_CODE_ROOM_LIST_GET implements ServerEventListener{ +public class ServerEventListener_CODE_GET_ROOMS implements ServerEventListener{ @Override public void call(ClientSide clientSide, String data) { @@ -29,8 +29,4 @@ public class ServerEventListener_CODE_ROOM_LIST_GET implements ServerEventListen ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_SHOW_ROOMS, Noson.reversal(roomList)); } - - - - } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_POKERS_GET.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_POKERS_GET.java new file mode 100644 index 0000000..878a6a1 --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_POKERS_GET.java @@ -0,0 +1,32 @@ +package org.nico.ratel.landlords.server.event; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.nico.noson.Noson; +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.helper.MapHelper; +import org.nico.ratel.landlords.server.ServerContains; + +public class ServerEventListener_CODE_POKERS_GET implements ServerEventListener{ + + @Override + public void call(ClientSide clientSide, String data) { + List> roomList = new ArrayList<>(ServerContains.ROOM_MAP.size()); + for(Entry entry: ServerContains.ROOM_MAP.entrySet()) { + Room room = entry.getValue(); + roomList.add(MapHelper.newInstance() + .put("roomId", room.getId()) + .put("roomOwner", room.getRoomOwner()) + .put("roomClientCount", room.getClientSideList().size()) + .map()); + } + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_SHOW_ROOMS, Noson.reversal(roomList)); + } + +} 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 e5668e2..b643105 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 @@ -32,7 +32,7 @@ public class ServerEventListener_CODE_ROOM_JOIN implements ServerEventListener{ .json(); if(room.getClientSideList().size() == 3) { - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_JOIN_FAIL_BY_FULL, Noson.reversal(room)); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_JOIN_FAIL_BY_FULL, result); }else { clientSide.setRoomId(room.getId()); diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java index debec44..c44952e 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java @@ -26,6 +26,8 @@ public class TransferHandler extends ChannelInboundHandlerAdapter{ clientSide.setNickname(String.valueOf(clientSide.getId())); ServerContains.CLIENT_SIDE_MAP.put(clientSide.getId(), clientSide); SimplePrinter.println("A client connects to the server:" + clientSide.getId()); + + ChannelUtils.pushToClient(ch, ClientEventCode.CODE_CLIENT_CONNECT, String.valueOf(clientSide.getId())); ChannelUtils.pushToClient(ch, ClientEventCode.CODE_CLIENT_NICKNAME_SET, null); } From 23c63ff3381db0a200777842185dc36ab2e1b326 Mon Sep 17 00:00:00 2001 From: nico Date: Fri, 9 Nov 2018 20:26:44 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- landlords-server/pom.xml | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/landlords-server/pom.xml b/landlords-server/pom.xml index ed6bb5b..fb7c67f 100644 --- a/landlords-server/pom.xml +++ b/landlords-server/pom.xml @@ -10,6 +10,10 @@ 1.0.0 + + org.nico.ratel.landlords.server.SimpleServer + + com.smallnico.ratel @@ -17,4 +21,37 @@ 1.0.0 + + + + + org.springframework.boot + spring-boot-maven-plugin + + ${start-class} + + + + + repackage + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + -parameters + + true + + + + + \ No newline at end of file From 19e1c7bc6228ba95718e7342c0a1f794873e50e1 Mon Sep 17 00:00:00 2001 From: nico Date: Fri, 9 Nov 2018 20:39:07 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E5=8E=BB=E6=8E=89gpg=20plugin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ratel/landlords/client/SimpleClient.java | 2 +- pom.xml | 131 ------------------ 2 files changed, 1 insertion(+), 132 deletions(-) diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java index 7fe548f..c05162d 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java @@ -23,7 +23,7 @@ public class SimpleClient { .group(group) .channel(NioSocketChannel.class) .handler(new DefaultChannelInitializer()); - Channel channel = b.connect("127.0.0.1", 1024).sync().channel(); + Channel channel = b.connect("127.0.0.1", 80).sync().channel(); channel.closeFuture().sync(); } finally { group.shutdownGracefully().sync(); diff --git a/pom.xml b/pom.xml index ff9e121..9b32529 100644 --- a/pom.xml +++ b/pom.xml @@ -10,26 +10,6 @@ pom Ratel Project - - - The Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - - - - - - nico - ainililia@163.com - smallnico - http://smallnico.com - - - - - https://github.com/ainilili/ratel - - 1.8 1.8 @@ -64,106 +44,6 @@ - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - ${java.version.source} - ${java.version.target} - ${java.compiler.argument} - ${java.specification.version} - ${java.specification.version} - ${java.test.compiler.argument} - - - - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.7 - true - - nico - https://oss.sonatype.org/ - true - - - - org.apache.maven.plugins - maven-source-plugin - 2.2.1 - - - attach-sources - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - - attach-javadocs - - jar - - - -Xdoclint:none - - - - - - org.apache.maven.plugins - maven-gpg-plugin - 1.5 - - - sign-artifacts - verify - - sign - - - - - - org.sonatype.plugins - jarjar-maven-plugin - 1.9 - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - - org.sonatype.plugins - nexus-staging-maven-plugin - - - org.apache.maven.plugins - maven-source-plugin - - - org.apache.maven.plugins - maven-javadoc-plugin - - - org.apache.maven.plugins - maven-gpg-plugin - - - landlords-client @@ -171,16 +51,5 @@ landlords-common - - - nico - https://oss.sonatype.org/content/repositories/snapshots/ - - - nico - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - - From 5b24c767bb3dbd4fd38dfa55c8c10f6babf12ff2 Mon Sep 17 00:00:00 2001 From: nico Date: Fri, 9 Nov 2018 20:45:29 +0800 Subject: [PATCH 07/12] compiler --- pom.xml | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pom.xml b/pom.xml index 9b32529..63f3276 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,47 @@ + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + ${java.version.source} + ${java.version.target} + ${java.compiler.argument} + ${java.specification.version} + ${java.specification.version} + ${java.test.compiler.argument} + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + org.sonatype.plugins + nexus-staging-maven-plugin + + + org.apache.maven.plugins + maven-source-plugin + + + org.apache.maven.plugins + maven-javadoc-plugin + + + org.apache.maven.plugins + maven-gpg-plugin + + + landlords-client From 7f7bb02dd5e9407dd6754fb2226fcb483833ec74 Mon Sep 17 00:00:00 2001 From: nico Date: Fri, 9 Nov 2018 21:09:07 +0800 Subject: [PATCH 08/12] IP and port modify --- .../main/java/org/nico/ratel/landlords/client/SimpleClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java index c05162d..7fe548f 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java @@ -23,7 +23,7 @@ public class SimpleClient { .group(group) .channel(NioSocketChannel.class) .handler(new DefaultChannelInitializer()); - Channel channel = b.connect("127.0.0.1", 80).sync().channel(); + Channel channel = b.connect("127.0.0.1", 1024).sync().channel(); channel.closeFuture().sync(); } finally { group.shutdownGracefully().sync(); From e9dddea9581455f087b53361a2e0a825c302706a Mon Sep 17 00:00:00 2001 From: nico Date: Sat, 10 Nov 2018 15:45:34 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ientEventListener_CODE_CLIENT_CONNECT.java | 2 +- .../ClientEventListener_CODE_CLIENT_EXIT.java | 18 ++++++++++- ...ventListener_CODE_CLIENT_NICKNAME_SET.java | 6 ++-- ...ntListener_CODE_GAME_LANDLORD_CONFIRM.java | 5 ++-- ...ventListener_CODE_GAME_LANDLORD_CYCLE.java | 8 +---- ...ventListener_CODE_GAME_LANDLORD_ELECT.java | 8 ++--- .../ClientEventListener_CODE_GAME_OVER.java | 7 +++-- ...entEventListener_CODE_GAME_POKER_PLAY.java | 13 ++++---- ...stener_CODE_GAME_POKER_PLAY_DONT_SELL.java | 4 +-- ...Listener_CODE_GAME_POKER_PLAY_INVALID.java | 2 +- ...entListener_CODE_GAME_POKER_PLAY_LESS.java | 10 +------ ...istener_CODE_GAME_POKER_PLAY_MISMATCH.java | 4 +-- ...lientEventListener_CODE_GAME_STARTING.java | 9 +++--- ...ventListener_CODE_ROOM_CREATE_SUCCESS.java | 7 ++--- ...tListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java | 2 +- ...stener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java | 7 ++++- ...tEventListener_CODE_ROOM_JOIN_SUCCESS.java | 15 ++++++++-- ...tEventListener_CODE_ROOM_JOIN_TRIGGER.java | 22 -------------- ...ClientEventListener_CODE_SHOW_OPTIONS.java | 22 +++++++------- .../ClientEventListener_CODE_SHOW_POKERS.java | 5 ++-- .../ClientEventListener_CODE_SHOW_ROOMS.java | 6 ++-- .../client/handler/TransferHandler.java | 2 +- .../landlords/enums/ClientEventCode.java | 2 -- .../ratel/landlords/helper/PokerHelper.java | 28 ++++++++--------- .../ratel/landlords/print/SimplePrinter.java | 11 +++++-- .../ratel/landlords/print/SimpleWriter.java | 6 +++- .../ServerEventListener_CODE_CLIENT_EXIT.java | 8 ++++- .../ServerEventListener_CODE_ROOM_JOIN.java | 30 ++++++++++--------- .../server/handler/TransferHandler.java | 4 +-- 29 files changed, 138 insertions(+), 135 deletions(-) delete mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_TRIGGER.java diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_CONNECT.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_CONNECT.java index 63088bf..85f7bd9 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_CONNECT.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_CONNECT.java @@ -9,7 +9,7 @@ public class ClientEventListener_CODE_CLIENT_CONNECT extends ClientEventListener @Override public void call(Channel channel, String data) { - SimplePrinter.println("Connect success"); + SimplePrinter.printNotice("Connection to server successful !!"); SimpleClient.id = Integer.parseInt(data); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_EXIT.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_EXIT.java index 514ef45..b22fe2d 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_EXIT.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_EXIT.java @@ -1,5 +1,10 @@ package org.nico.ratel.landlords.client.event; +import java.util.Map; + +import org.nico.ratel.landlords.client.SimpleClient; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -8,7 +13,18 @@ public class ClientEventListener_CODE_CLIENT_EXIT extends ClientEventListener{ @Override public void call(Channel channel, String data) { - SimplePrinter.println(data + " exit, room dissolve!!"); + Map map = MapHelper.parser(data); + + Integer exitClientId = (Integer) map.get("exitClientId"); + + String role = null; + if(exitClientId == SimpleClient.id) { + role = "You"; + }else { + role = (String) map.get("exitClientNickname"); + } + SimplePrinter.printNotice(role + " withdrew from the room, room dissolve!!"); + get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_NICKNAME_SET.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_NICKNAME_SET.java index 96b2d48..97a81c1 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_NICKNAME_SET.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_NICKNAME_SET.java @@ -10,10 +10,8 @@ public class ClientEventListener_CODE_CLIENT_NICKNAME_SET extends ClientEventLis @Override public void call(Channel channel, String data) { - SimplePrinter.println("You has been join Nico-Landlords, please set your nickname: "); - String nickname = SimpleWriter.write(); - - + SimplePrinter.printNotice("Please set your nickname"); + String nickname = SimpleWriter.write("nickname"); pushToServer(channel, ServerEventCode.CODE_CLIENT_NICKNAME_SET, nickname); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java index c06f4fb..23a1928 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java @@ -9,7 +9,6 @@ import org.nico.ratel.landlords.client.SimpleClient; import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.helper.MapHelper; -import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -22,10 +21,10 @@ public class ClientEventListener_CODE_GAME_LANDLORD_CONFIRM extends ClientEventL String landlordNickname = (String) map.get("landlordNickname"); - SimplePrinter.println(landlordNickname + " grabed landlord, he got three pokers"); + SimplePrinter.printNotice(landlordNickname + " grabbed the landlord and got an extra three poker shots"); List additionalPokers = Noson.convert(map.get("additionalPokers"), new NoType>() {}); - SimplePrinter.println(PokerHelper.unfoldPoker(additionalPokers, false)); + SimplePrinter.printPokers(additionalPokers); int landlordId = (int) map.get("landlordId"); if(SimpleClient.id == landlordId) { diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CYCLE.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CYCLE.java index 0ec28df..ef65672 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CYCLE.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CYCLE.java @@ -1,12 +1,6 @@ package org.nico.ratel.landlords.client.event; -import java.util.Map; - -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 io.netty.channel.Channel; @@ -14,7 +8,7 @@ public class ClientEventListener_CODE_GAME_LANDLORD_CYCLE extends ClientEventLis @Override public void call(Channel channel, String data) { - SimplePrinter.println("No one takes the landowner. Reissue the license"); + SimplePrinter.printNotice("No player takes the landowner, resend poker"); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java index 1994e18..67492b5 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java @@ -18,18 +18,18 @@ public class ClientEventListener_CODE_GAME_LANDLORD_ELECT extends ClientEventLis int turnClientId = (int) map.get("turnClientId"); if(turnClientId == SimpleClient.id) { - SimplePrinter.println("It's your turn to rob the landlord[Y/N]:"); - String line = SimpleWriter.write(); + SimplePrinter.printNotice("It's your turn to rob the landlord[Y/N]"); + String line = SimpleWriter.write("Y/N"); if(line.equalsIgnoreCase("Y")){ pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "TRUE"); }else if(line.equalsIgnoreCase("N")){ pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "FALSE"); }else{ - SimplePrinter.println("Invalid options"); + SimplePrinter.printNotice("Invalid options"); call(channel, data); } }else { - SimplePrinter.println("It's turn " + map.get("turnClientNickname") + " to confirm, please wait patiently !"); + SimplePrinter.printNotice("It's turn " + map.get("turnClientNickname") + " to confirm, please wait patiently !"); } } 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 bac4ec6..82b8f1c 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 @@ -2,6 +2,7 @@ package org.nico.ratel.landlords.client.event; import java.util.Map; +import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; @@ -12,9 +13,9 @@ public class ClientEventListener_CODE_GAME_OVER extends ClientEventListener{ @Override public void call(Channel channel, String data) { Map map = MapHelper.parser(data); - SimplePrinter.println(map.get("winnerNickname") + " win"); - SimplePrinter.println("Game over"); - + SimplePrinter.printNotice(map.get("winnerNickname") + " win"); + SimplePrinter.printNotice("Game over"); + get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); } } 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 6089418..43da65b 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 @@ -21,16 +21,17 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY extends ClientEventListene @Override public void call(Channel channel, String data) { Map map = MapHelper.parser(data); - + SimplePrinter.printNotice(""); + SimplePrinter.printNotice("It's your turn to play, your pokers are as follows"); List pokers = Noson.convert(map.get("pokers"), new NoType>() {}); - SimplePrinter.println(PokerHelper.unfoldPoker(pokers, true)); + SimplePrinter.printPokers(pokers); - SimplePrinter.println("Please enter the number you want:"); - String line = SimpleWriter.write(); + SimplePrinter.printNotice("Please enter the card you came up"); + String line = SimpleWriter.write("card"); if(line == null){ - SimplePrinter.println("Invalid input"); + SimplePrinter.printNotice("Invalid enter"); call(channel, data); }else{ if(line.equalsIgnoreCase("PASS")) { @@ -58,7 +59,7 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY extends ClientEventListene if(access){ pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY, Noson.reversal(options.toArray(new Character[] {}))); }else{ - SimplePrinter.println("Invalid input"); + SimplePrinter.printNotice("Invalid enter"); call(channel, data); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL.java index 58db039..a764a8d 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL.java @@ -4,7 +4,6 @@ import java.util.Map; import org.nico.ratel.landlords.client.SimpleClient; import org.nico.ratel.landlords.enums.ClientEventCode; -import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; @@ -16,8 +15,7 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL extends ClientEv public void call(Channel channel, String data) { Map map = MapHelper.parser(data); - SimplePrinter.println(map.get("currentClientNickname") + " dont sell"); - SimplePrinter.println("It's turn " + map.get("turnClientNickname")); + SimplePrinter.printNotice(map.get("currentClientNickname") + " dont out, turn next player " + map.get("turnClientNickname") + " out pokers"); int turnClientId = (int) map.get("turnClientId"); if(SimpleClient.id == turnClientId) { diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_INVALID.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_INVALID.java index d52f4af..d4a3072 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_INVALID.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_INVALID.java @@ -10,7 +10,7 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY_INVALID extends ClientEven @Override public void call(Channel channel, String data) { - SimplePrinter.println("Sell is invalid"); + SimplePrinter.printNotice("Out pokers format is invalid"); get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_LESS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_LESS.java index f90d6c7..d0179a3 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_LESS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_LESS.java @@ -1,10 +1,6 @@ package org.nico.ratel.landlords.client.event; -import java.util.Map; - import org.nico.ratel.landlords.enums.ClientEventCode; -import org.nico.ratel.landlords.enums.ServerEventCode; -import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -13,11 +9,7 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY_LESS extends ClientEventLi @Override public void call(Channel channel, String data) { - Map map = MapHelper.parser(data); - - SimplePrinter.println("Your pokers type is less than pre pokers"); - SimplePrinter.println("Could not beat !!"); - + SimplePrinter.printNotice("Your pokers type is less than pre pokers, could not beat !!"); get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH.java index 9bc0c76..a71bc1c 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH.java @@ -15,9 +15,7 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH extends ClientEve public void call(Channel channel, String data) { Map map = MapHelper.parser(data); - SimplePrinter.println("Your pokers type is " + map.get("playType")); - SimplePrinter.println("Pre pokers type is " + map.get("preType")); - SimplePrinter.println("Mismatch !!"); + SimplePrinter.printNotice("Your pokers type is " + map.get("playType") + " and pre pokers type is " + map.get("preType") + ", mismatch !!"); get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_STARTING.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_STARTING.java index e3a9825..4dc5ad5 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_STARTING.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_STARTING.java @@ -8,7 +8,6 @@ import org.nico.noson.entity.NoType; import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.helper.MapHelper; -import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -20,11 +19,13 @@ public class ClientEventListener_CODE_GAME_STARTING extends ClientEventListener{ Map map = MapHelper.parser(data); - SimplePrinter.println(map.get("roomId") + " game starting !"); + SimplePrinter.printNotice("Game starting !!"); List pokers = Noson.convert(map.get("pokers"), new NoType>() {}); - SimplePrinter.println("Your pokers is:"); - SimplePrinter.println(PokerHelper.unfoldPoker(pokers, false)); + + SimplePrinter.printNotice(""); + SimplePrinter.printNotice("Your pokers is"); + SimplePrinter.printPokers(pokers); get(ClientEventCode.CODE_GAME_LANDLORD_ELECT).call(channel, data); } 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 a879d55..56100fe 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 @@ -13,10 +13,9 @@ public class ClientEventListener_CODE_ROOM_CREATE_SUCCESS extends ClientEventLis Room room = Noson.convert(data, Room.class); - SimplePrinter.println("You has been create room:" + room.getId()); - SimplePrinter.println("Please wait for other players to join !"); + SimplePrinter.printNotice("You has been create room with id " + room.getId()); + SimplePrinter.printNotice("Please wait for other players to join !"); + } - - } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java index 99f0a80..86eb01f 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java @@ -14,7 +14,7 @@ public class ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL extends ClientEvent public void call(Channel channel, String data) { Map dataMap = MapHelper.parser(data); - SimplePrinter.println("Join fail, room " + dataMap.get("roomId") + " is full!"); + SimplePrinter.printNotice("Join room fail, room " + dataMap.get("roomId") + " player count is full!"); ClientEventListener.get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java index 966ff6f..6408e15 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java @@ -1,6 +1,9 @@ package org.nico.ratel.landlords.client.event; +import java.util.Map; + import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -9,7 +12,9 @@ public class ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST extends ClientEv @Override public void call(Channel channel, String data) { - SimplePrinter.println("Join fail, room " + data + " is inexist!"); + Map dataMap = MapHelper.parser(data); + + SimplePrinter.printNotice("Join room fail, room " + dataMap.get("roomId") + " is inexist!"); ClientEventListener.get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); } 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 9b13f73..3734986 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 @@ -2,6 +2,7 @@ package org.nico.ratel.landlords.client.event; import java.util.Map; +import org.nico.ratel.landlords.client.SimpleClient; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; @@ -11,10 +12,18 @@ public class ClientEventListener_CODE_ROOM_JOIN_SUCCESS extends ClientEventListe @Override public void call(Channel channel, String data) { - Map dataMap = MapHelper.parser(data); + Map map = MapHelper.parser(data); + + int joinClientId = (int) map.get("clientId"); + if(SimpleClient.id == joinClientId) { + SimplePrinter.printNotice("You has been join room:" + map.get("roomId") + ", there are " + map.get("roomClientCount") + " player in the room now"); + SimplePrinter.printNotice("Please wait for other players to join, start a good game when the room player reaches three !"); + }else { + SimplePrinter.printNotice(map.get("clientNickname") + " join room, the current number of rooms is " + map.get("roomClientCount")); + } + + - SimplePrinter.println("You has been join room:" + dataMap.get("roomId") + ", There are " + dataMap.get("roomClientCount") + " people in the room now"); - SimplePrinter.println("Please wait for other players to join !"); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_TRIGGER.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_TRIGGER.java deleted file mode 100644 index 57f5748..0000000 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_TRIGGER.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.nico.ratel.landlords.client.event; - -import java.util.Map; - -import org.nico.ratel.landlords.helper.MapHelper; -import org.nico.ratel.landlords.print.SimplePrinter; - -import io.netty.channel.Channel; - -public class ClientEventListener_CODE_ROOM_JOIN_TRIGGER extends ClientEventListener{ - - @Override - public void call(Channel channel, String data) { - Map dataMap = MapHelper.parser(data); - - SimplePrinter.println(dataMap.get("clientNickname") + " has been join room:" + dataMap.get("roomId") + ", There are " + dataMap.get("roomClientCount") + " people in the room now"); - SimplePrinter.println("Please wait for other players to join !"); - } - - - -} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java index 0279668..c1342dc 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java @@ -11,15 +11,15 @@ public class ClientEventListener_CODE_SHOW_OPTIONS extends ClientEventListener{ @Override public void call(Channel channel, String data) { - SimplePrinter.println("Options: "); - SimplePrinter.println("1. Create Room"); - SimplePrinter.println("2. Room List"); - SimplePrinter.println("3. Join Room"); - SimplePrinter.println("Your choose options number:"); - String line = SimpleWriter.write(); + SimplePrinter.printNotice("Options: "); + SimplePrinter.printNotice("1. Create Room"); + SimplePrinter.printNotice("2. Room List"); + SimplePrinter.printNotice("3. Join Room"); + SimplePrinter.printNotice("Please enter the number of options"); + String line = SimpleWriter.write("options"); while(line == null || (! line.equals("1") && ! line.equals("2") && ! line.equals("3"))) { - SimplePrinter.println("Invalid options, please choose again:"); - line = SimpleWriter.write(); + SimplePrinter.printNotice("Invalid options, please choose again:"); + line = SimpleWriter.write("options"); } int choose = Integer.valueOf(line); @@ -29,11 +29,11 @@ public class ClientEventListener_CODE_SHOW_OPTIONS extends ClientEventListener{ }else if(choose == 2){ pushToServer(channel, ServerEventCode.CODE_GET_ROOMS, null); }else { - SimplePrinter.print("Your choose rooms number:"); - line = SimpleWriter.write(); + SimplePrinter.printNotice("Please enter the room id you want to join"); + line = SimpleWriter.write("roomid"); int option = OptionsUtils.getOptions(line); if(line == null || option < 1) { - SimplePrinter.println("Invalid options, please choose again:"); + SimplePrinter.printNotice("Invalid options, please choose again:"); call(channel, data); }else{ pushToServer(channel, ServerEventCode.CODE_ROOM_JOIN, String.valueOf(option)); diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java index 0884c24..ae16345 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java @@ -7,7 +7,6 @@ import org.nico.noson.Noson; import org.nico.noson.entity.NoType; import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.helper.MapHelper; -import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -19,10 +18,10 @@ public class ClientEventListener_CODE_SHOW_POKERS extends ClientEventListener{ Map map = MapHelper.parser(data); - SimplePrinter.println(map.get("currentClientNickname") + " sell:"); + SimplePrinter.printNotice(map.get("currentClientNickname") + " out of the card"); List pokers = Noson.convert(map.get("pokers"), new NoType>() {}); - SimplePrinter.println(PokerHelper.unfoldPoker(pokers, false)); + SimplePrinter.printPokers(pokers); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java index 9de4819..514265b 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java @@ -20,13 +20,13 @@ public class ClientEventListener_CODE_SHOW_ROOMS extends ClientEventListener{ List> roomList = Noson.convert(data, new NoType>>() {}); if(roomList != null && ! roomList.isEmpty()){ - SimplePrinter.println("#\tID\t|\tOWNER\t|\tCOUNT\t#"); + SimplePrinter.printNotice("#\tID\t|\tOWNER\t|\tCOUNT\t#"); for(Map room: roomList) { - SimplePrinter.println("#\t" + room.get("roomId") + "\t|\t" + room.get("roomOwner") + "\t|\t" + room.get("roomClientCount") + "\t#"); + SimplePrinter.printNotice("#\t" + room.get("roomId") + "\t|\t" + room.get("roomOwner") + "\t|\t" + room.get("roomClientCount") + "\t#"); } get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); }else { - SimplePrinter.println("No available room, please create a room !"); + SimplePrinter.printNotice("No available room, please create a room !"); get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java index 4de443a..f8f9c45 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java @@ -17,7 +17,7 @@ public class TransferHandler extends ChannelInboundHandlerAdapter{ ClientTransferData clientTransferData = (ClientTransferData) msg; if(clientTransferData.getInfo() != null) { - SimplePrinter.println(clientTransferData.getInfo()); + SimplePrinter.printNotice(clientTransferData.getInfo()); } ClientEventCode code = clientTransferData.getCode(); 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 ba4a8ed..fc9d4a1 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 @@ -20,8 +20,6 @@ public enum ClientEventCode implements Serializable{ CODE_ROOM_JOIN_SUCCESS("加入房间成功"), - CODE_ROOM_JOIN_TRIGGER("加入房间触发"), - CODE_ROOM_JOIN_FAIL_BY_FULL("房间人数已满"), CODE_ROOM_JOIN_FAIL_BY_INEXIST("房间不存在"), diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java index 54262fd..0fa89b3 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java @@ -254,14 +254,14 @@ public class PokerHelper { return pokersList; } - public static String unfoldPoker(List pokers, boolean serialFlag) { + public static String printPoker(List pokers) { sortPoker(pokers); StringBuilder builder = new StringBuilder(); if(pokers != null && pokers.size() > 0) { for(int index = 0; index < pokers.size(); index ++) { if(index == 0) { - builder.append("Poker: ┌──┐"); + builder.append("┌──┐"); }else if(index == pokers.size() - 1) { builder.append("──┐"); }else { @@ -271,7 +271,7 @@ public class PokerHelper { builder.append(System.lineSeparator()); for(int index = 0; index < pokers.size(); index ++) { if(index == 0) { - builder.append(" │"); + builder.append("│"); } String name = pokers.get(index).getLevel().getName(); builder.append(name + (name.length() == 1 ? " " : "" ) + "|"); @@ -279,29 +279,29 @@ public class PokerHelper { builder.append(System.lineSeparator()); for(int index = 0; index < pokers.size(); index ++) { if(index == 0) { - builder.append(" │"); + builder.append("│"); } builder.append(pokers.get(index).getType().getName() + " |"); } builder.append(System.lineSeparator()); for(int index = 0; index < pokers.size(); index ++) { if(index == 0) { - builder.append(" └──┘"); + builder.append("└──┘"); }else if(index == pokers.size() - 1) { builder.append("──┘"); }else { builder.append("──┘"); } } - if(serialFlag){ - builder.append(System.lineSeparator()); - for(int index = 0; index < pokers.size(); index ++) { - if(index == 0) { - builder.append("Index: │"); - } - builder.append(pokers.get(index).getLevel().getAlias()[0] + " │"); - } - } +// if(serialFlag){ +// builder.append(System.lineSeparator()); +// for(int index = 0; index < pokers.size(); index ++) { +// if(index == 0) { +// builder.append("Index: │"); +// } +// builder.append(pokers.get(index).getLevel().getAlias()[0] + " │"); +// } +// } } return builder.toString(); } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/print/SimplePrinter.java b/landlords-common/src/main/java/org/nico/ratel/landlords/print/SimplePrinter.java index a92b133..f4fe9c2 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/print/SimplePrinter.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/print/SimplePrinter.java @@ -1,12 +1,17 @@ package org.nico.ratel.landlords.print; +import java.util.List; + +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.helper.PokerHelper; + public class SimplePrinter { - public static void print(String msg) { - System.out.print(msg); + public static void printPokers(List pokers) { + System.out.println(PokerHelper.printPoker(pokers)); } - public static void println(String msg) { + public static void printNotice(String msg) { System.out.println(msg); } } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/print/SimpleWriter.java b/landlords-common/src/main/java/org/nico/ratel/landlords/print/SimpleWriter.java index bdb8e6f..24a9bed 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/print/SimpleWriter.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/print/SimpleWriter.java @@ -8,11 +8,15 @@ public class SimpleWriter { private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); - public static String write() { + public static String write(String message) { + System.out.println(); + System.out.print("[ratel@" + message + "]$ "); try { return reader.readLine(); } catch (IOException e) { e.printStackTrace(); + }finally { + System.out.println(); } return null; } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_EXIT.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_EXIT.java index c4a4d3d..b27223f 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_EXIT.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_EXIT.java @@ -4,6 +4,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.helper.MapHelper; import org.nico.ratel.landlords.server.ServerContains; public class ServerEventListener_CODE_CLIENT_EXIT implements ServerEventListener{ @@ -14,8 +15,13 @@ public class ServerEventListener_CODE_CLIENT_EXIT implements ServerEventListener Room room = ServerContains.ROOM_MAP.get(clientSide.getRoomId()); if(room != null) { + String result = MapHelper.newInstance() + .put("roomId", room.getId()) + .put("exitClientId", clientSide.getId()) + .put("exitClientNickname", clientSide.getNickname()) + .json(); for(ClientSide client: room.getClientSideList()) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_CLIENT_EXIT, clientSide.getNickname()); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_CLIENT_EXIT, result); client.init(); } ServerContains.ROOM_MAP.remove(room.getId()); 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 b643105..ffc183b 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 @@ -21,17 +21,16 @@ public class ServerEventListener_CODE_ROOM_JOIN implements ServerEventListener{ Room room = ServerContains.ROOM_MAP.get(Integer.valueOf(data)); if(room == null) { - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_JOIN_FAIL_BY_INEXIST, data); - }else { String result = MapHelper.newInstance() - .put("clientId", clientSide.getId()) - .put("clientNickname", clientSide.getNickname()) - .put("roomId", room.getId()) - .put("roomOwner", room.getRoomOwner()) - .put("roomClientCount", room.getClientSideList().size()) - .json(); - + .put("roomId", data) + .json(); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_JOIN_FAIL_BY_INEXIST, result); + }else { if(room.getClientSideList().size() == 3) { + String result = MapHelper.newInstance() + .put("roomId", room.getId()) + .put("roomOwner", room.getRoomOwner()) + .json(); ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_JOIN_FAIL_BY_FULL, result); }else { clientSide.setRoomId(room.getId()); @@ -56,12 +55,15 @@ public class ServerEventListener_CODE_ROOM_JOIN implements ServerEventListener{ }else { room.setStatus(RoomStatus.WAIT); - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_JOIN_SUCCESS, result); - + String result = MapHelper.newInstance() + .put("clientId", clientSide.getId()) + .put("clientNickname", clientSide.getNickname()) + .put("roomId", room.getId()) + .put("roomOwner", room.getRoomOwner()) + .put("roomClientCount", room.getClientSideList().size()) + .json(); for(ClientSide client: roomClientMap.values()) { - if(client.getId() != clientSide.getId()) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_ROOM_JOIN_TRIGGER, result); - } + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_ROOM_JOIN_SUCCESS, result); } } } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java index c44952e..fcc814b 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java @@ -25,7 +25,7 @@ public class TransferHandler extends ChannelInboundHandlerAdapter{ ClientSide clientSide = new ClientSide(((InetSocketAddress)ch.remoteAddress()).getPort(), ClientStatus.TO_CHOOSE, ch); clientSide.setNickname(String.valueOf(clientSide.getId())); ServerContains.CLIENT_SIDE_MAP.put(clientSide.getId(), clientSide); - SimplePrinter.println("A client connects to the server:" + clientSide.getId()); + SimplePrinter.printNotice("A client connects to the server:" + clientSide.getId()); ChannelUtils.pushToClient(ch, ClientEventCode.CODE_CLIENT_CONNECT, String.valueOf(clientSide.getId())); ChannelUtils.pushToClient(ch, ClientEventCode.CODE_CLIENT_NICKNAME_SET, null); @@ -53,7 +53,7 @@ public class TransferHandler extends ChannelInboundHandlerAdapter{ if(cause instanceof java.io.IOException) { ClientSide client = ServerContains.CLIENT_SIDE_MAP.get(((InetSocketAddress)ctx.channel().remoteAddress()).getPort()); if(client != null) { - SimplePrinter.println(client.getNickname() + " exit"); + SimplePrinter.printNotice(client.getNickname() + " exit"); ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(client, null); } } From fdd48fa3adea164fec8019f1bd4413b1fb03fca8 Mon Sep 17 00:00:00 2001 From: nico Date: Sat, 10 Nov 2018 17:37:35 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E5=AE=89=E5=85=A8=E5=92=8C=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E6=98=BE=E7=A4=BA=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/event/ClientEventListener.java | 4 + ...ientEventListener_CODE_CLIENT_CONNECT.java | 2 +- ...ventListener_CODE_GAME_LANDLORD_ELECT.java | 4 +- ...entEventListener_CODE_GAME_POKER_PLAY.java | 7 +- ...stener_CODE_GAME_POKER_PLAY_CANT_PASS.java | 16 +++ ...ener_CODE_GAME_POKER_PLAY_ORDER_ERROR.java | 15 ++ ...ntListener_CODE_GAME_POKER_PLAY_PASS.java} | 6 +- ...ventListener_CODE_ROOM_CREATE_SUCCESS.java | 1 - .../ClientEventListener_CODE_SHOW_POKERS.java | 2 +- .../ClientEventListener_CODE_SHOW_ROOMS.java | 1 + .../org/nico/ratel/landlords/entity/Room.java | 10 ++ .../landlords/enums/ClientEventCode.java | 6 +- .../landlords/enums/ServerEventCode.java | 4 +- ...ventListener_CODE_GAME_LANDLORD_ELECT.java | 10 +- ...rEventListener_CODE_GAME_LANDLORD_GET.java | 39 ----- ...verEventListener_CODE_GAME_POKER_PLAY.java | 136 +++++++++--------- ...entListener_CODE_GAME_POKER_PLAY_PASS.java | 43 ++++++ ...erverEventListener_CODE_GAME_STARTING.java | 4 +- .../ServerEventListener_CODE_GET_POKERS.java | 16 --- .../ServerEventListener_CODE_POKERS_GET.java | 32 ----- 20 files changed, 181 insertions(+), 177 deletions(-) create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_CANT_PASS.java create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_ORDER_ERROR.java rename landlords-client/src/main/java/org/nico/ratel/landlords/client/event/{ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL.java => ClientEventListener_CODE_GAME_POKER_PLAY_PASS.java} (65%) delete mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_GET.java create mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY_PASS.java delete mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_POKERS.java delete mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_POKERS_GET.java diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java index 1f46b0a..37faebe 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java @@ -39,4 +39,8 @@ public abstract class ClientEventListener { protected ChannelFuture pushToServer(Channel channel, ServerEventCode code, String datas){ return ChannelUtils.pushToServer(channel, code, datas); } + + protected ChannelFuture pushToServer(Channel channel, ServerEventCode code){ + return pushToServer(channel, code, null); + } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_CONNECT.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_CONNECT.java index 85f7bd9..3530c3b 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_CONNECT.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_CONNECT.java @@ -9,7 +9,7 @@ public class ClientEventListener_CODE_CLIENT_CONNECT extends ClientEventListener @Override public void call(Channel channel, String data) { - SimplePrinter.printNotice("Connection to server successful !!"); + SimplePrinter.printNotice("Connection to server successful, welcome to ratel !!"); SimpleClient.id = Integer.parseInt(data); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java index 67492b5..5ef29f0 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java @@ -15,7 +15,7 @@ public class ClientEventListener_CODE_GAME_LANDLORD_ELECT extends ClientEventLis @Override public void call(Channel channel, String data) { Map map = MapHelper.parser(data); - int turnClientId = (int) map.get("turnClientId"); + int turnClientId = (int) map.get("nextClientId"); if(turnClientId == SimpleClient.id) { SimplePrinter.printNotice("It's your turn to rob the landlord[Y/N]"); @@ -29,7 +29,7 @@ public class ClientEventListener_CODE_GAME_LANDLORD_ELECT extends ClientEventLis call(channel, data); } }else { - SimplePrinter.printNotice("It's turn " + map.get("turnClientNickname") + " to confirm, please wait patiently !"); + SimplePrinter.printNotice("It's turn " + map.get("nextClientNickname") + " to confirm, please wait patiently !"); } } 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 43da65b..147e1a9 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 @@ -10,7 +10,6 @@ import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.enums.PokerLevel; import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.helper.MapHelper; -import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.print.SimplePrinter; import org.nico.ratel.landlords.print.SimpleWriter; @@ -27,7 +26,7 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY extends ClientEventListene SimplePrinter.printPokers(pokers); - SimplePrinter.printNotice("Please enter the card you came up"); + SimplePrinter.printNotice("Please enter the card you came up (enter [exit] to exit current room, enter [pass] jump current round)"); String line = SimpleWriter.write("card"); if(line == null){ @@ -35,9 +34,9 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY extends ClientEventListene call(channel, data); }else{ if(line.equalsIgnoreCase("PASS")) { - pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY, Noson.reversal(new Character[] {'p'})); + pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_PASS); }else if(line.equalsIgnoreCase("EXIT")){ - pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT, null); + pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT); }else { String[] strs = line.split(" "); List options = new ArrayList<>(); diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_CANT_PASS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_CANT_PASS.java new file mode 100644 index 0000000..4ddf680 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_CANT_PASS.java @@ -0,0 +1,16 @@ +package org.nico.ratel.landlords.client.event; + +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_GAME_POKER_PLAY_CANT_PASS extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + SimplePrinter.printNotice("The last time you play the card, you can't pass"); + get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); + } + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_ORDER_ERROR.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_ORDER_ERROR.java new file mode 100644 index 0000000..d415ed8 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_ORDER_ERROR.java @@ -0,0 +1,15 @@ +package org.nico.ratel.landlords.client.event; + +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_GAME_POKER_PLAY_ORDER_ERROR extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + + SimplePrinter.printNotice("Not turn you to operate, please wait other player !!"); + } + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_PASS.java similarity index 65% rename from landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL.java rename to landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_PASS.java index a764a8d..aeaf8ad 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_PASS.java @@ -9,15 +9,15 @@ import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; -public class ClientEventListener_CODE_GAME_POKER_PLAY_DONT_SELL extends ClientEventListener{ +public class ClientEventListener_CODE_GAME_POKER_PLAY_PASS extends ClientEventListener{ @Override public void call(Channel channel, String data) { Map map = MapHelper.parser(data); - SimplePrinter.printNotice(map.get("currentClientNickname") + " dont out, turn next player " + map.get("turnClientNickname") + " out pokers"); + SimplePrinter.printNotice(map.get("clientNickname") + " dont out, turn next player " + map.get("nextClientNickname") + " out pokers"); - int turnClientId = (int) map.get("turnClientId"); + int turnClientId = (int) map.get("nextClientId"); if(SimpleClient.id == turnClientId) { get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); } 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 56100fe..8be85d5 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 @@ -15,7 +15,6 @@ public class ClientEventListener_CODE_ROOM_CREATE_SUCCESS extends ClientEventLis SimplePrinter.printNotice("You has been create room with id " + room.getId()); SimplePrinter.printNotice("Please wait for other players to join !"); - } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java index ae16345..806ea43 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java @@ -18,7 +18,7 @@ public class ClientEventListener_CODE_SHOW_POKERS extends ClientEventListener{ Map map = MapHelper.parser(data); - SimplePrinter.printNotice(map.get("currentClientNickname") + " out of the card"); + SimplePrinter.printNotice(map.get("clientNickname") + " out of the card"); List pokers = Noson.convert(map.get("pokers"), new NoType>() {}); SimplePrinter.printPokers(pokers); diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java index 514265b..390d988 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java @@ -29,6 +29,7 @@ public class ClientEventListener_CODE_SHOW_ROOMS extends ClientEventListener{ SimplePrinter.printNotice("No available room, please create a room !"); get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); } + SimplePrinter.printNotice(""); } 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 070a150..b47ccb4 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 @@ -30,6 +30,8 @@ public class Room implements Serializable{ private PokerSell lastPokerShell; private int lastSellClient = -1; + + private int currentSellClient = -1; public Room() { } @@ -49,6 +51,14 @@ public class Room implements Serializable{ this.lastPokerShell = lastPokerShell; } + public final int getCurrentSellClient() { + return currentSellClient; + } + + public final void setCurrentSellClient(int currentSellClient) { + this.currentSellClient = currentSellClient; + } + public int getLastSellClient() { return lastSellClient; } 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 fc9d4a1..78af344 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 @@ -38,10 +38,14 @@ public enum ClientEventCode implements Serializable{ CODE_GAME_POKER_PLAY_LESS("出牌太小"), - CODE_GAME_POKER_PLAY_DONT_SELL("不出"), + CODE_GAME_POKER_PLAY_PASS("不出"), + + CODE_GAME_POKER_PLAY_CANT_PASS("不允许不出"), CODE_GAME_POKER_PLAY_INVALID("无效"), + CODE_GAME_POKER_PLAY_ORDER_ERROR("顺序错误"), + CODE_GAME_OVER("游戏结束"), ; 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 a4c54fb..1d5141d 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 @@ -18,10 +18,10 @@ public enum ServerEventCode implements Serializable{ CODE_GAME_LANDLORD_ELECT("抢地主"), - CODE_GAME_LANDLORD_GET("抢地主信息"), - CODE_GAME_POKER_PLAY("出牌环节"), + CODE_GAME_POKER_PLAY_PASS("不出"), + ; private String msg; diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java index 9701389..2a8702b 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java @@ -21,7 +21,11 @@ public class ServerEventListener_CODE_GAME_LANDLORD_ELECT implements ServerEvent clientSide.getPokers().addAll(room.getLandlordPokers()); PokerHelper.sortPoker(clientSide.getPokers()); - room.setLandlordId(clientSide.getId()); + int currentClientId = clientSide.getId(); + room.setLandlordId(currentClientId); + room.setLastSellClient(currentClientId); + room.setCurrentSellClient(currentClientId); + for(ClientSide client: room.getClientSideList()){ String result = MapHelper.newInstance() .put("roomId", room.getId()) @@ -46,8 +50,8 @@ public class ServerEventListener_CODE_GAME_LANDLORD_ELECT implements ServerEvent .put("roomId", room.getId()) .put("roomOwner", room.getRoomOwner()) .put("roomClientCount", room.getClientSideList().size()) - .put("turnClientNickname", turnClientSide.getNickname()) - .put("turnClientId", turnClientSide.getId()) + .put("nextClientNickname", turnClientSide.getNickname()) + .put("nextClientId", turnClientSide.getId()) .json(); ChannelUtils.pushToClient(turnClientSide.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_ELECT, result); } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_GET.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_GET.java deleted file mode 100644 index d06c120..0000000 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_GET.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.nico.ratel.landlords.server.event; - -import org.nico.noson.Noson; -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.ServerEventCode; -import org.nico.ratel.landlords.helper.MapHelper; -import org.nico.ratel.landlords.helper.PokerHelper; -import org.nico.ratel.landlords.helper.TimeHelper; -import org.nico.ratel.landlords.server.ServerContains; - -public class ServerEventListener_CODE_GAME_LANDLORD_GET implements ServerEventListener{ - - @Override - public void call(ClientSide clientSide, String data) { - - Room room = ServerContains.ROOM_MAP.get(clientSide.getRoomId()); - ClientSide startGrabClient = ServerContains.CLIENT_SIDE_MAP.get(room.getLandlordId()); - - String result = MapHelper.newInstance() - .put("roomId", room.getId()) - .put("roomOwner", room.getRoomOwner()) - .put("roomClientCount", room.getClientSideList().size()) - .put("turnClientNickname", startGrabClient.getNickname()) - .put("turnClientId", startGrabClient.getId()) - .json(); - for(ClientSide client: room.getClientSideList()){ - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_ELECT, result); - } - - } - - - - - -} 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 5b929a0..a03e1f4 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 @@ -20,84 +20,80 @@ public class ServerEventListener_CODE_GAME_POKER_PLAY implements ServerEventList @Override public void call(ClientSide clientSide, String data) { Room room = ServerContains.ROOM_MAP.get(clientSide.getRoomId()); - Character[] options = Noson.convert(data, Character[].class); - int[] indexes = PokerHelper.getIndexes(options, clientSide.getPokers()); - if(PokerHelper.checkPokerIndex(indexes, clientSide.getPokers())){ - boolean sellFlag = false; - List currentPokers = PokerHelper.getPoker(indexes, clientSide.getPokers()); - PokerSell currentPokerShell = PokerHelper.checkPokerType(currentPokers); - if(room.getLastSellClient() != -1 && room.getLastSellClient() != clientSide.getId() && room.getLastPokerShell() != null){ - PokerSell lastPokerShell = room.getLastPokerShell(); - if(lastPokerShell.getSellType() != currentPokerShell.getSellType() && currentPokerShell.getSellType() != SellType.BOMB && currentPokerShell.getSellType() != SellType.KING_BOMB) { + + if(room.getCurrentSellClient() == clientSide.getId()) { + Character[] options = Noson.convert(data, Character[].class); + int[] indexes = PokerHelper.getIndexes(options, clientSide.getPokers()); + if(PokerHelper.checkPokerIndex(indexes, clientSide.getPokers())){ + boolean sellFlag = true; + + List currentPokers = PokerHelper.getPoker(indexes, clientSide.getPokers()); + PokerSell currentPokerShell = PokerHelper.checkPokerType(currentPokers); + if(room.getLastSellClient() != clientSide.getId() && room.getLastPokerShell() != null){ + PokerSell lastPokerShell = room.getLastPokerShell(); + + if(lastPokerShell.getSellType() != currentPokerShell.getSellType() && currentPokerShell.getSellType() != SellType.BOMB && currentPokerShell.getSellType() != SellType.KING_BOMB) { + String result = MapHelper.newInstance() + .put("playType", currentPokerShell.getSellType()) + .put("preType", lastPokerShell.getSellType()) + .put("pokers", clientSide.getPokers()) + .json(); + sellFlag = false; + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_MISMATCH, result); + }else if(lastPokerShell.getScore() >= currentPokerShell.getScore()) { + String result = MapHelper.newInstance() + .put("playScore", currentPokerShell.getScore()) + .put("preScore", lastPokerShell.getScore()) + .put("pokers", clientSide.getPokers()) + .json(); + sellFlag = false; + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_LESS, result); + } + } + if(sellFlag) { + ClientSide next = clientSide.getNext(); + + room.setLastSellClient(clientSide.getId()); + room.setLastPokerShell(currentPokerShell); + room.setCurrentSellClient(next.getId()); + + clientSide.getPokers().removeAll(currentPokers); String result = MapHelper.newInstance() - .put("playType", currentPokerShell.getSellType()) - .put("preType", lastPokerShell.getSellType()) - .put("pokers", clientSide.getPokers()) - .json(); - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_MISMATCH, result); - }else if(lastPokerShell.getScore() >= currentPokerShell.getScore()) { - String result = MapHelper.newInstance() - .put("playScore", currentPokerShell.getScore()) - .put("preScore", lastPokerShell.getScore()) - .put("pokers", clientSide.getPokers()) + .put("clientId", clientSide.getId()) + .put("clientNickname", clientSide.getNickname()) + .put("pokers", currentPokers) .json(); - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_LESS, result); - }else { - sellFlag = true; + for(ClientSide client: room.getClientSideList()) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_POKERS, result); + } + + TimeHelper.sleep(500); + + if(clientSide.getPokers().isEmpty()) { + result = MapHelper.newInstance() + .put("winnerNickname", clientSide.getNickname()) + .json(); + + for(ClientSide client: room.getClientSideList()) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, result); + } + }else { + result = MapHelper.newInstance() + .put("pokers", next.getPokers()) + .json(); + + ChannelUtils.pushToClient(next.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, result); + } } }else{ - sellFlag = true; - } - if(sellFlag) { - room.setLastSellClient(clientSide.getId()); - room.setLastPokerShell(currentPokerShell); - clientSide.getPokers().removeAll(currentPokers); - String result = MapHelper.newInstance() - .put("currentClientId", clientSide.getId()) - .put("currentClientNickname", clientSide.getNickname()) - .put("pokers", currentPokers) - .json(); - for(ClientSide client: room.getClientSideList()) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_POKERS, result); - } - - TimeHelper.sleep(500); - - if(clientSide.getPokers().isEmpty()) { - result = MapHelper.newInstance() - .put("winnerNickname", clientSide.getNickname()) - .json(); - for(ClientSide client: room.getClientSideList()) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, result); - } - }else { - ClientSide next = clientSide.getNext(); - result = MapHelper.newInstance() - .put("pokers", next.getPokers()) - .json(); - - ChannelUtils.pushToClient(next.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, result); - } - } - }else{ - if(options.length > 0 && options[0] == 'p') { - ClientSide turnClient = clientSide.getNext(); - for(ClientSide client: room.getClientSideList()) { - String result = MapHelper.newInstance() - .put("currentClientId", clientSide.getId()) - .put("currentClientNickname", clientSide.getNickname()) - .put("turnClientId", turnClient.getId()) - .put("turnClientNickname", turnClient.getNickname()) - .put("pokers", client.getPokers()) - .json(); - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_DONT_SELL, result); - } - }else { String result = MapHelper.newInstance() .put("pokers", clientSide.getPokers()) .json(); - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, result); + + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_INVALID, result); } + }else { + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_ORDER_ERROR, null); } } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY_PASS.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY_PASS.java new file mode 100644 index 0000000..4e2d9d9 --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY_PASS.java @@ -0,0 +1,43 @@ +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.ClientEventCode; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.server.ServerContains; + +public class ServerEventListener_CODE_GAME_POKER_PLAY_PASS implements ServerEventListener{ + + @Override + public void call(ClientSide clientSide, String data) { + Room room = ServerContains.ROOM_MAP.get(clientSide.getRoomId()); + + if(room.getCurrentSellClient() == clientSide.getId()) { + if(clientSide.getId() != room.getLastSellClient()) { + ClientSide turnClient = clientSide.getNext(); + + room.setCurrentSellClient(turnClient.getId()); + + for(ClientSide client: room.getClientSideList()) { + String result = MapHelper.newInstance() + .put("clientId", clientSide.getId()) + .put("clientNickname", clientSide.getNickname()) + .put("nextClientId", turnClient.getId()) + .put("nextClientNickname", turnClient.getNickname()) + .put("pokers", client.getPokers()) + .json(); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_PASS, result); + } + }else { + String result = MapHelper.newInstance() + .put("pokers", clientSide.getPokers()) + .json(); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_CANT_PASS, result); + } + }else { + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_ORDER_ERROR, null); + } + } + +} 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 71cc806..ac04d77 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 @@ -48,8 +48,8 @@ public class ServerEventListener_CODE_GAME_STARTING implements ServerEventListen .put("roomId", room.getId()) .put("roomOwner", room.getRoomOwner()) .put("roomClientCount", room.getClientSideList().size()) - .put("turnClientNickname", startGrabClient.getNickname()) - .put("turnClientId", startGrabClient.getId()) + .put("nextClientNickname", startGrabClient.getNickname()) + .put("nextClientId", startGrabClient.getId()) .put("pokers", client.getPokers()) //.put("additionalPokers", room.getLandlordPokers()) .json(); diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_POKERS.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_POKERS.java deleted file mode 100644 index 412523a..0000000 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_POKERS.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.nico.ratel.landlords.server.event; - -import org.nico.noson.Noson; -import org.nico.ratel.landlords.channel.ChannelUtils; -import org.nico.ratel.landlords.entity.ClientSide; -import org.nico.ratel.landlords.enums.ClientEventCode; - -public class ServerEventListener_CODE_GET_POKERS implements ServerEventListener{ - - @Override - public void call(ClientSide clientSide, String data) { - - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_SHOW_POKERS, Noson.reversal(clientSide.getPokers())); - } - -} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_POKERS_GET.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_POKERS_GET.java deleted file mode 100644 index 878a6a1..0000000 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_POKERS_GET.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.nico.ratel.landlords.server.event; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -import org.nico.noson.Noson; -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.helper.MapHelper; -import org.nico.ratel.landlords.server.ServerContains; - -public class ServerEventListener_CODE_POKERS_GET implements ServerEventListener{ - - @Override - public void call(ClientSide clientSide, String data) { - List> roomList = new ArrayList<>(ServerContains.ROOM_MAP.size()); - for(Entry entry: ServerContains.ROOM_MAP.entrySet()) { - Room room = entry.getValue(); - roomList.add(MapHelper.newInstance() - .put("roomId", room.getId()) - .put("roomOwner", room.getRoomOwner()) - .put("roomClientCount", room.getClientSideList().size()) - .map()); - } - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_SHOW_ROOMS, Noson.reversal(roomList)); - } - -} From 5a1c54dd752caa774058fd8a71f955851b07ca69 Mon Sep 17 00:00:00 2001 From: nico Date: Sat, 10 Nov 2018 18:59:52 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E6=B5=81=E7=A8=8B=E5=8F=8A=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ntListener_CODE_GAME_LANDLORD_CONFIRM.java | 7 +-- ...ventListener_CODE_GAME_LANDLORD_ELECT.java | 6 ++- .../ClientEventListener_CODE_GAME_OVER.java | 4 +- ...entEventListener_CODE_GAME_POKER_PLAY.java | 4 +- ...stener_CODE_GAME_POKER_PLAY_CANT_PASS.java | 4 +- ...Listener_CODE_GAME_POKER_PLAY_INVALID.java | 4 +- ...entListener_CODE_GAME_POKER_PLAY_LESS.java | 4 +- ...istener_CODE_GAME_POKER_PLAY_MISMATCH.java | 3 +- ...entListener_CODE_GAME_POKER_PLAY_PASS.java | 4 +- ...istener_CODE_GAME_POKER_PLAY_REDIRECT.java | 38 ++++++++++++++++ ...ClientEventListener_CODE_SHOW_OPTIONS.java | 17 ++++--- .../ClientEventListener_CODE_SHOW_POKERS.java | 6 ++- .../ratel/landlords/entity/ClientSide.java | 5 +-- .../nico/ratel/landlords/entity/Poker.java | 6 +-- .../org/nico/ratel/landlords/entity/Room.java | 6 +-- .../landlords/enums/ClientEventCode.java | 2 + .../ratel/landlords/enums/ClientStatus.java | 2 +- .../ratel/landlords/enums/ClientType.java | 2 +- .../ratel/landlords/enums/PokerLevel.java | 2 +- .../nico/ratel/landlords/enums/PokerType.java | 2 +- .../ratel/landlords/enums/RoomStatus.java | 2 +- .../nico/ratel/landlords/enums/SellType.java | 2 +- .../landlords/enums/ServerEventCode.java | 2 + .../ratel/landlords/helper/PokerHelper.java | 45 ------------------- ...ventListener_CODE_GAME_LANDLORD_ELECT.java | 7 ++- ...verEventListener_CODE_GAME_POKER_PLAY.java | 17 +++---- ...entListener_CODE_GAME_POKER_PLAY_PASS.java | 6 +-- ...istener_CODE_GAME_POKER_PLAY_REDIRECT.java | 41 +++++++++++++++++ ...erverEventListener_CODE_GAME_STARTING.java | 2 +- 29 files changed, 140 insertions(+), 112 deletions(-) create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_REDIRECT.java create mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY_REDIRECT.java diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java index 23a1928..73275f4 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CONFIRM.java @@ -7,7 +7,7 @@ import org.nico.noson.Noson; import org.nico.noson.entity.NoType; import org.nico.ratel.landlords.client.SimpleClient; import org.nico.ratel.landlords.entity.Poker; -import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; @@ -26,10 +26,7 @@ public class ClientEventListener_CODE_GAME_LANDLORD_CONFIRM extends ClientEventL List additionalPokers = Noson.convert(map.get("additionalPokers"), new NoType>() {}); SimplePrinter.printPokers(additionalPokers); - int landlordId = (int) map.get("landlordId"); - if(SimpleClient.id == landlordId) { - get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); - } + pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java index 5ef29f0..1d94509 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_ELECT.java @@ -18,9 +18,11 @@ public class ClientEventListener_CODE_GAME_LANDLORD_ELECT extends ClientEventLis int turnClientId = (int) map.get("nextClientId"); if(turnClientId == SimpleClient.id) { - SimplePrinter.printNotice("It's your turn to rob the landlord[Y/N]"); + SimplePrinter.printNotice("It's your turn to rob the landlord[Y/N] (enter [EXIT] to exit current room)"); String line = SimpleWriter.write("Y/N"); - if(line.equalsIgnoreCase("Y")){ + if(line.equalsIgnoreCase("EXIT")) { + pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT); + }else if(line.equalsIgnoreCase("Y")){ pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "TRUE"); }else if(line.equalsIgnoreCase("N")){ pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "FALSE"); 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 82b8f1c..7b8553c 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 @@ -2,7 +2,7 @@ package org.nico.ratel.landlords.client.event; import java.util.Map; -import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; @@ -15,7 +15,7 @@ public class ClientEventListener_CODE_GAME_OVER extends ClientEventListener{ Map map = MapHelper.parser(data); SimplePrinter.printNotice(map.get("winnerNickname") + " win"); SimplePrinter.printNotice("Game over"); - get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); + pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT); } } 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 147e1a9..19fbe10 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 @@ -20,13 +20,13 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY extends ClientEventListene @Override public void call(Channel channel, String data) { Map map = MapHelper.parser(data); - SimplePrinter.printNotice(""); + SimplePrinter.printNotice("It's your turn to play, your pokers are as follows"); List pokers = Noson.convert(map.get("pokers"), new NoType>() {}); SimplePrinter.printPokers(pokers); - SimplePrinter.printNotice("Please enter the card you came up (enter [exit] to exit current room, enter [pass] jump current round)"); + SimplePrinter.printNotice("Please enter the card you came up (enter [EXIT] to exit current room, enter [PASS] jump current round)"); String line = SimpleWriter.write("card"); if(line == null){ diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_CANT_PASS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_CANT_PASS.java index 4ddf680..3cdffdb 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_CANT_PASS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_CANT_PASS.java @@ -1,6 +1,6 @@ package org.nico.ratel.landlords.client.event; -import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -10,7 +10,7 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY_CANT_PASS extends ClientEv @Override public void call(Channel channel, String data) { SimplePrinter.printNotice("The last time you play the card, you can't pass"); - get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); + pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_INVALID.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_INVALID.java index d4a3072..e394961 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_INVALID.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_INVALID.java @@ -1,6 +1,6 @@ package org.nico.ratel.landlords.client.event; -import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -11,7 +11,7 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY_INVALID extends ClientEven public void call(Channel channel, String data) { SimplePrinter.printNotice("Out pokers format is invalid"); - get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); + pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_LESS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_LESS.java index d0179a3..3ca4564 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_LESS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_LESS.java @@ -1,6 +1,6 @@ package org.nico.ratel.landlords.client.event; -import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; @@ -10,7 +10,7 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY_LESS extends ClientEventLi @Override public void call(Channel channel, String data) { SimplePrinter.printNotice("Your pokers type is less than pre pokers, could not beat !!"); - get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); + pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH.java index a71bc1c..976479c 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH.java @@ -2,7 +2,6 @@ package org.nico.ratel.landlords.client.event; import java.util.Map; -import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; @@ -17,7 +16,7 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH extends ClientEve SimplePrinter.printNotice("Your pokers type is " + map.get("playType") + " and pre pokers type is " + map.get("preType") + ", mismatch !!"); - get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); + pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_PASS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_PASS.java index aeaf8ad..5741896 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_PASS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_PASS.java @@ -3,7 +3,7 @@ package org.nico.ratel.landlords.client.event; import java.util.Map; import org.nico.ratel.landlords.client.SimpleClient; -import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; @@ -19,7 +19,7 @@ public class ClientEventListener_CODE_GAME_POKER_PLAY_PASS extends ClientEventLi int turnClientId = (int) map.get("nextClientId"); if(SimpleClient.id == turnClientId) { - get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); + pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_REDIRECT.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_REDIRECT.java new file mode 100644 index 0000000..8e77df0 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_REDIRECT.java @@ -0,0 +1,38 @@ +package org.nico.ratel.landlords.client.event; + +import java.util.List; +import java.util.Map; + +import org.nico.ratel.landlords.client.SimpleClient; +import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.print.SimplePrinter; + +import io.netty.channel.Channel; + +public class ClientEventListener_CODE_GAME_POKER_PLAY_REDIRECT extends ClientEventListener{ + + @Override + public void call(Channel channel, String data) { + + Map map = MapHelper.parser(data); + + int sellClientId = (int) map.get("sellClientId"); + + SimplePrinter.printNotice(""); + SimplePrinter.printNotice("Everyone's current situation: "); + SimplePrinter.printNotice(""); + List> clientInfos = (List>) map.get("clientInfos"); + for(Map clientInfo: clientInfos) { + SimplePrinter.printNotice(clientInfo.get("clientNickname") + "\t[" + clientInfo.get("type") + "] surplus " + clientInfo.get("surplus") + " piece of poker"); + } + SimplePrinter.printNotice(""); + + if(sellClientId == SimpleClient.id) { + get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); + }else { + SimplePrinter.printNotice("Next out player is " + map.get("sellClinetNickname") + ", please wait for him to out his poker"); + } + } + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java index c1342dc..f192202 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java @@ -29,14 +29,19 @@ public class ClientEventListener_CODE_SHOW_OPTIONS extends ClientEventListener{ }else if(choose == 2){ pushToServer(channel, ServerEventCode.CODE_GET_ROOMS, null); }else { - SimplePrinter.printNotice("Please enter the room id you want to join"); + SimplePrinter.printNotice("Please enter the room id you want to join (enter [BACK] return options list)"); line = SimpleWriter.write("roomid"); - int option = OptionsUtils.getOptions(line); - if(line == null || option < 1) { - SimplePrinter.printNotice("Invalid options, please choose again:"); + + if(line.equalsIgnoreCase("BACK")) { call(channel, data); - }else{ - pushToServer(channel, ServerEventCode.CODE_ROOM_JOIN, String.valueOf(option)); + }else { + int option = OptionsUtils.getOptions(line); + if(line == null || option < 1) { + SimplePrinter.printNotice("Invalid options, please choose again:"); + call(channel, data); + }else{ + pushToServer(channel, ServerEventCode.CODE_ROOM_JOIN, String.valueOf(option)); + } } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java index 806ea43..0a89a46 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_POKERS.java @@ -18,10 +18,14 @@ public class ClientEventListener_CODE_SHOW_POKERS extends ClientEventListener{ Map map = MapHelper.parser(data); - SimplePrinter.printNotice(map.get("clientNickname") + " out of the card"); + SimplePrinter.printNotice(map.get("clientNickname") + " out of the poker"); List pokers = Noson.convert(map.get("pokers"), new NoType>() {}); SimplePrinter.printPokers(pokers); + + if(map.containsKey("sellClinetNickname")) { + SimplePrinter.printNotice("Next out player is " + map.get("sellClinetNickname") + ", please wait for him to out his poker"); + } } } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientSide.java b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientSide.java index 0022176..4d72f74 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientSide.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientSide.java @@ -1,6 +1,5 @@ package org.nico.ratel.landlords.entity; -import java.io.Serializable; import java.util.List; import org.nico.ratel.landlords.enums.ClientStatus; @@ -8,9 +7,7 @@ import org.nico.ratel.landlords.enums.ClientType; import io.netty.channel.Channel; -public class ClientSide implements Serializable{ - - private static final long serialVersionUID = -1208782543528394339L; +public class ClientSide{ private int id; diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Poker.java b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Poker.java index 4c441ce..38e505d 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Poker.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Poker.java @@ -1,7 +1,5 @@ package org.nico.ratel.landlords.entity; -import java.io.Serializable; - import org.nico.ratel.landlords.enums.PokerLevel; import org.nico.ratel.landlords.enums.PokerType; @@ -10,10 +8,8 @@ import org.nico.ratel.landlords.enums.PokerType; * * @author nico */ -public class Poker implements Serializable{ +public class Poker{ - private static final long serialVersionUID = -3830261356192537124L; - private PokerLevel level; private PokerType type; 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 b47ccb4..b8b4bf2 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 @@ -1,17 +1,13 @@ package org.nico.ratel.landlords.entity; -import java.io.Serializable; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentSkipListMap; -import org.nico.noson.annotations.JsonIgnore; import org.nico.ratel.landlords.enums.RoomStatus; -public class Room implements Serializable{ - - private static final long serialVersionUID = -9182226630057841379L; +public class Room{ private int id; 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 78af344..3ec3ba6 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 @@ -34,6 +34,8 @@ public enum ClientEventCode implements Serializable{ CODE_GAME_POKER_PLAY("出牌回合"), + CODE_GAME_POKER_PLAY_REDIRECT("出牌重定向"), + CODE_GAME_POKER_PLAY_MISMATCH("出牌不匹配"), CODE_GAME_POKER_PLAY_LESS("出牌太小"), 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 ecdeb52..7066b9d 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 @@ -2,7 +2,7 @@ package org.nico.ratel.landlords.enums; import java.io.Serializable; -public enum ClientStatus implements Serializable{ +public enum ClientStatus{ TO_CHOOSE, diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientType.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientType.java index cdfb8e5..e75a66f 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientType.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientType.java @@ -2,7 +2,7 @@ package org.nico.ratel.landlords.enums; import java.io.Serializable; -public enum ClientType implements Serializable{ +public enum ClientType{ LANDLORD, diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerLevel.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerLevel.java index 2840fb4..1d96a51 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerLevel.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerLevel.java @@ -10,7 +10,7 @@ import java.util.Set; * * @author nico */ -public enum PokerLevel implements Serializable{ +public enum PokerLevel{ LEVEL_3(3, "3", new Character[]{'3'}), diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerType.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerType.java index cd48b14..6b70ebf 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerType.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerType.java @@ -7,7 +7,7 @@ import java.io.Serializable; * * @author nico */ -public enum PokerType implements Serializable{ +public enum PokerType{ BLANK(" "), diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomStatus.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomStatus.java index 0b1ace3..6073f2f 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomStatus.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomStatus.java @@ -2,7 +2,7 @@ package org.nico.ratel.landlords.enums; import java.io.Serializable; -public enum RoomStatus implements Serializable{ +public enum RoomStatus{ BLANK("空闲"), diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/SellType.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/SellType.java index 6156354..18b6834 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/SellType.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/SellType.java @@ -2,7 +2,7 @@ package org.nico.ratel.landlords.enums; import java.io.Serializable; -public enum SellType implements Serializable{ +public enum SellType { ILLEGAL("非合法"), 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 1d5141d..cb3217c 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 @@ -20,6 +20,8 @@ public enum ServerEventCode implements Serializable{ CODE_GAME_POKER_PLAY("出牌环节"), + CODE_GAME_POKER_PLAY_REDIRECT("出牌重定向"), + CODE_GAME_POKER_PLAY_PASS("不出"), ; diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java index 0fa89b3..e933774 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/PokerHelper.java @@ -293,52 +293,7 @@ public class PokerHelper { builder.append("──┘"); } } -// if(serialFlag){ -// builder.append(System.lineSeparator()); -// for(int index = 0; index < pokers.size(); index ++) { -// if(index == 0) { -// builder.append("Index: │"); -// } -// builder.append(pokers.get(index).getLevel().getAlias()[0] + " │"); -// } -// } } return builder.toString(); } - - public static void main(String[] args) { - List pokers = new ArrayList<>(); -// pokers.add(new Poker(PokerLevel.LEVEL_3, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_4, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_3, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_4, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_3, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_4, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - -// pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_J, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_J, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_J, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_J, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_Q, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_Q, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_Q, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_Q, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_BIG_KING, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_SMALL_KING, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_K, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_A, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_Q, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_K, PokerType.HEART)); -// pokers.add(new Poker(PokerLevel.LEVEL_A, PokerType.HEART)); - - System.out.println(checkPokerType(pokers)); - } } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java index 2a8702b..8b0a6f5 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_LANDLORD_ELECT.java @@ -1,9 +1,14 @@ package org.nico.ratel.landlords.server.event; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + 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.ClientType; import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.helper.PokerHelper; @@ -25,6 +30,7 @@ public class ServerEventListener_CODE_GAME_LANDLORD_ELECT implements ServerEvent room.setLandlordId(currentClientId); room.setLastSellClient(currentClientId); room.setCurrentSellClient(currentClientId); + clientSide.setType(ClientType.LANDLORD); for(ClientSide client: room.getClientSideList()){ String result = MapHelper.newInstance() @@ -33,7 +39,6 @@ public class ServerEventListener_CODE_GAME_LANDLORD_ELECT implements ServerEvent .put("roomClientCount", room.getClientSideList().size()) .put("landlordNickname", clientSide.getNickname()) .put("landlordId", clientSide.getId()) - .put("pokers", client.getPokers()) .put("additionalPokers", room.getLandlordPokers()) .json(); ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_CONFIRM, result); 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 a03e1f4..716a910 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 @@ -1,6 +1,7 @@ package org.nico.ratel.landlords.server.event; import java.util.List; +import java.util.Map; import org.nico.noson.Noson; import org.nico.ratel.landlords.channel.ChannelUtils; @@ -10,6 +11,7 @@ import org.nico.ratel.landlords.entity.PokerSell; import org.nico.ratel.landlords.entity.Room; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.SellType; +import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.helper.TimeHelper; @@ -36,7 +38,6 @@ public class ServerEventListener_CODE_GAME_POKER_PLAY implements ServerEventList String result = MapHelper.newInstance() .put("playType", currentPokerShell.getSellType()) .put("preType", lastPokerShell.getSellType()) - .put("pokers", clientSide.getPokers()) .json(); sellFlag = false; ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_MISMATCH, result); @@ -44,7 +45,6 @@ public class ServerEventListener_CODE_GAME_POKER_PLAY implements ServerEventList String result = MapHelper.newInstance() .put("playScore", currentPokerShell.getScore()) .put("preScore", lastPokerShell.getScore()) - .put("pokers", clientSide.getPokers()) .json(); sellFlag = false; ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_LESS, result); @@ -62,6 +62,7 @@ public class ServerEventListener_CODE_GAME_POKER_PLAY implements ServerEventList .put("clientId", clientSide.getId()) .put("clientNickname", clientSide.getNickname()) .put("pokers", currentPokers) + .put("sellClinetNickname", next.getNickname()) .json(); for(ClientSide client: room.getClientSideList()) { ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_POKERS, result); @@ -78,19 +79,11 @@ public class ServerEventListener_CODE_GAME_POKER_PLAY implements ServerEventList ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, result); } }else { - result = MapHelper.newInstance() - .put("pokers", next.getPokers()) - .json(); - - ChannelUtils.pushToClient(next.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY, result); + ServerEventListener.get(ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT).call(next, data); } } }else{ - String result = MapHelper.newInstance() - .put("pokers", clientSide.getPokers()) - .json(); - - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_INVALID, result); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_INVALID, null); } }else { ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_ORDER_ERROR, null); diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY_PASS.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY_PASS.java index 4e2d9d9..846ec36 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY_PASS.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY_PASS.java @@ -25,15 +25,11 @@ public class ServerEventListener_CODE_GAME_POKER_PLAY_PASS implements ServerEven .put("clientNickname", clientSide.getNickname()) .put("nextClientId", turnClient.getId()) .put("nextClientNickname", turnClient.getNickname()) - .put("pokers", client.getPokers()) .json(); ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_PASS, result); } }else { - String result = MapHelper.newInstance() - .put("pokers", clientSide.getPokers()) - .json(); - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_CANT_PASS, result); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_CANT_PASS, null); } }else { ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_ORDER_ERROR, null); diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY_REDIRECT.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY_REDIRECT.java new file mode 100644 index 0000000..60729fe --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY_REDIRECT.java @@ -0,0 +1,41 @@ +package org.nico.ratel.landlords.server.event; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +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.helper.MapHelper; +import org.nico.ratel.landlords.helper.PokerHelper; +import org.nico.ratel.landlords.server.ServerContains; + +public class ServerEventListener_CODE_GAME_POKER_PLAY_REDIRECT implements ServerEventListener{ + + @Override + public void call(ClientSide clientSide, String data) { + Room room = ServerContains.ROOM_MAP.get(clientSide.getRoomId()); + + List> clientInfos = new ArrayList>(3); + for(ClientSide client: room.getClientSideList()){ + clientInfos.add(MapHelper.newInstance() + .put("clientId", client.getId()) + .put("clientNickname", client.getNickname()) + .put("type", client.getType()) + .put("surplus", client.getPokers().size()) + .map()); + } + + String result = MapHelper.newInstance() + .put("pokers", clientSide.getPokers()) + .put("clientInfos", clientInfos) + .put("sellClientId", room.getCurrentSellClient()) + .put("sellClinetNickname", ServerContains.CLIENT_SIDE_MAP.get(room.getCurrentSellClient()).getNickname()) + .json(); + + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_REDIRECT, result); + } + +} 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 ac04d77..d1c049c 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 @@ -51,8 +51,8 @@ public class ServerEventListener_CODE_GAME_STARTING implements ServerEventListen .put("nextClientNickname", startGrabClient.getNickname()) .put("nextClientId", startGrabClient.getId()) .put("pokers", client.getPokers()) - //.put("additionalPokers", room.getLandlordPokers()) .json(); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_STARTING, result); } From cf16e26bf8dae766a0a43f9a8d36a20c2f913e73 Mon Sep 17 00:00:00 2001 From: nico Date: Mon, 12 Nov 2018 09:20:00 +0800 Subject: [PATCH 12/12] =?UTF-8?q?server=20=E7=89=8C=E5=9E=8B=E7=B1=BB?= =?UTF-8?q?=E5=9E=8Bbug=20fixed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ClientEventListener_CODE_SHOW_ROOMS.java | 3 +- ...verEventListener_CODE_GAME_POKER_PLAY.java | 41 +++++++++++-------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java index 390d988..2332686 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_ROOMS.java @@ -24,12 +24,13 @@ public class ClientEventListener_CODE_SHOW_ROOMS extends ClientEventListener{ for(Map room: roomList) { SimplePrinter.printNotice("#\t" + room.get("roomId") + "\t|\t" + room.get("roomOwner") + "\t|\t" + room.get("roomClientCount") + "\t#"); } + SimplePrinter.printNotice(""); get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); }else { SimplePrinter.printNotice("No available room, please create a room !"); get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); } - SimplePrinter.printNotice(""); + } 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 716a910..f413c94 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 @@ -31,25 +31,32 @@ public class ServerEventListener_CODE_GAME_POKER_PLAY implements ServerEventList List currentPokers = PokerHelper.getPoker(indexes, clientSide.getPokers()); PokerSell currentPokerShell = PokerHelper.checkPokerType(currentPokers); - if(room.getLastSellClient() != clientSide.getId() && room.getLastPokerShell() != null){ - PokerSell lastPokerShell = room.getLastPokerShell(); - - if(lastPokerShell.getSellType() != currentPokerShell.getSellType() && currentPokerShell.getSellType() != SellType.BOMB && currentPokerShell.getSellType() != SellType.KING_BOMB) { - String result = MapHelper.newInstance() - .put("playType", currentPokerShell.getSellType()) - .put("preType", lastPokerShell.getSellType()) - .json(); - sellFlag = false; - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_MISMATCH, result); - }else if(lastPokerShell.getScore() >= currentPokerShell.getScore()) { - String result = MapHelper.newInstance() - .put("playScore", currentPokerShell.getScore()) - .put("preScore", lastPokerShell.getScore()) - .json(); - sellFlag = false; - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_LESS, result); + + if(currentPokerShell.getSellType() != SellType.ILLEGAL) { + if(room.getLastSellClient() != clientSide.getId() && room.getLastPokerShell() != null){ + PokerSell lastPokerShell = room.getLastPokerShell(); + + if(lastPokerShell.getSellType() != currentPokerShell.getSellType() && currentPokerShell.getSellType() != SellType.BOMB && currentPokerShell.getSellType() != SellType.KING_BOMB) { + String result = MapHelper.newInstance() + .put("playType", currentPokerShell.getSellType()) + .put("preType", lastPokerShell.getSellType()) + .json(); + sellFlag = false; + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_MISMATCH, result); + }else if(lastPokerShell.getScore() >= currentPokerShell.getScore()) { + String result = MapHelper.newInstance() + .put("playScore", currentPokerShell.getScore()) + .put("preScore", lastPokerShell.getScore()) + .json(); + sellFlag = false; + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_LESS, result); + } } + }else { + sellFlag = false; + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_INVALID, null); } + if(sellFlag) { ClientSide next = clientSide.getNext();