From f53553f8446aef9a097864fe95e6d280db22e0b5 Mon Sep 17 00:00:00 2001 From: doveeeee Date: Tue, 13 Jul 2021 17:52:40 +0800 Subject: [PATCH 01/11] reformat code --- .../ratel/landlords/client/SimpleClient.java | 40 +- .../client/event/ClientEventListener.java | 28 +- ...ientEventListener_CODE_CLIENT_CONNECT.java | 2 +- .../ClientEventListener_CODE_CLIENT_EXIT.java | 13 +- .../ClientEventListener_CODE_CLIENT_KICK.java | 6 +- ...ventListener_CODE_CLIENT_NICKNAME_SET.java | 11 +- ...ntListener_CODE_GAME_LANDLORD_CONFIRM.java | 10 +- ...ventListener_CODE_GAME_LANDLORD_CYCLE.java | 4 +- ...ventListener_CODE_GAME_LANDLORD_ELECT.java | 20 +- .../ClientEventListener_CODE_GAME_OVER.java | 2 +- ...entEventListener_CODE_GAME_POKER_PLAY.java | 88 +- ...stener_CODE_GAME_POKER_PLAY_CANT_PASS.java | 2 +- ...Listener_CODE_GAME_POKER_PLAY_INVALID.java | 6 +- ...entListener_CODE_GAME_POKER_PLAY_LESS.java | 6 +- ...istener_CODE_GAME_POKER_PLAY_MISMATCH.java | 8 +- ...ener_CODE_GAME_POKER_PLAY_ORDER_ERROR.java | 4 +- ...entListener_CODE_GAME_POKER_PLAY_PASS.java | 8 +- ...istener_CODE_GAME_POKER_PLAY_REDIRECT.java | 37 +- ...lientEventListener_CODE_GAME_STARTING.java | 19 +- ...tener_CODE_PVE_DIFFICULTY_NOT_SUPPORT.java | 3 +- ...ventListener_CODE_ROOM_CREATE_SUCCESS.java | 8 +- ...tListener_CODE_ROOM_JOIN_FAIL_BY_FULL.java | 5 +- ...stener_CODE_ROOM_JOIN_FAIL_BY_INEXIST.java | 7 +- ...tEventListener_CODE_ROOM_JOIN_SUCCESS.java | 15 +- ...stener_CODE_ROOM_PLAY_FAIL_BY_INEXIST.java | 5 +- ...ClientEventListener_CODE_SHOW_OPTIONS.java | 19 +- ...entListener_CODE_SHOW_OPTIONS_SETTING.java | 13 +- .../ClientEventListener_CODE_SHOW_POKERS.java | 14 +- .../ClientEventListener_CODE_SHOW_ROOMS.java | 13 +- .../ratel/landlords/channel/ChannelUtils.java | 32 +- .../ratel/landlords/entity/ClientSide.java | 32 +- .../nico/ratel/landlords/entity/Poker.java | 16 +- .../ratel/landlords/entity/PokerSell.java | 16 +- .../org/nico/ratel/landlords/entity/Room.java | 28 +- .../landlords/entity/ServerTransferData.java | 1813 +++++++++-------- .../landlords/enums/ClientEventCode.java | 62 +- .../ratel/landlords/enums/ClientRole.java | 6 +- .../ratel/landlords/enums/ClientStatus.java | 16 +- .../ratel/landlords/enums/ClientType.java | 6 +- .../ratel/landlords/enums/PokerLevel.java | 46 +- .../nico/ratel/landlords/enums/PokerType.java | 21 +- .../ratel/landlords/enums/RoomStatus.java | 16 +- .../nico/ratel/landlords/enums/RoomType.java | 10 +- .../nico/ratel/landlords/enums/SellType.java | 40 +- .../landlords/enums/ServerEventCode.java | 30 +- .../exception/LandlordException.java | 2 +- .../landlords/handler/DefaultDecoder.java | 12 +- .../ratel/landlords/helper/MapHelper.java | 14 +- .../ratel/landlords/helper/PokerHelper.java | 23 +- .../ratel/landlords/helper/TimeHelper.java | 4 +- .../ratel/landlords/print/SimplePrinter.java | 12 +- .../ratel/landlords/print/SimpleWriter.java | 6 +- .../robot/AbstractRobotDecisionMakers.java | 2 +- .../robot/EasyRobotDecisionMakers.java | 33 +- .../robot/MediumRobotDecisionMakers.java | 4 +- .../landlords/robot/RobotDecisionMakers.java | 4 +- .../ratel/landlords/transfer/ByteKit.java | 28 +- .../ratel/landlords/transfer/ByteLink.java | 54 +- .../transfer/TransferProtocolUtils.java | 28 +- .../nico/ratel/landlords/utils/ListUtils.java | 17 +- .../ratel/landlords/utils/OptionsUtils.java | 4 +- .../nico/ratel/landlords/utils/RegxUtils.java | 2 +- .../ratel/landlords/utils/StreamUtils.java | 31 +- .../landlords/server/ServerContains.java | 40 +- .../ratel/landlords/server/SimpleServer.java | 4 +- .../server/event/ServerEventListener.java | 22 +- .../ServerEventListener_CODE_CLIENT_EXIT.java | 2 +- ...ventListener_CODE_CLIENT_NICKNAME_SET.java | 4 +- ...rverEventListener_CODE_CLIENT_OFFLINE.java | 4 +- ...entListener_CODE_GAME_POKER_PLAY_PASS.java | 2 +- ...istener_CODE_GAME_POKER_PLAY_REDIRECT.java | 10 +- ...erverEventListener_CODE_GAME_STARTING.java | 18 +- .../ServerEventListener_CODE_GET_ROOMS.java | 4 +- .../ServerEventListener_CODE_ROOM_CREATE.java | 6 +- .../handler/ProtobufTransferHandler.java | 18 +- .../server/handler/SecondProtobufCodec.java | 4 +- .../server/robot/RobotEventListener.java | 21 +- ...ventListener_CODE_GAME_LANDLORD_ELECT.java | 4 +- ...botEventListener_CODE_GAME_POKER_PLAY.java | 20 +- 79 files changed, 1548 insertions(+), 1521 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 b82287f..3d0e292 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 @@ -24,64 +24,64 @@ public class SimpleClient { public static String protocol = "pb"; - private static String[] serverAddressSource = new String[] { + private static final String[] serverAddressSource = new String[]{ "https://raw.githubusercontent.com/ainilili/ratel/master/serverlist.json", "https://gitee.com/ainilili/ratel/raw/master/serverlist.json" }; public static void main(String[] args) throws InterruptedException, IOException, URISyntaxException { - if(args != null && args.length > 0) { - for(int index = 0; index < args.length; index = index + 2) { - if(index + 1 < args.length) { - if(args[index].equalsIgnoreCase("-p") || args[index].equalsIgnoreCase("-port")) { + if (args != null && args.length > 0) { + for (int index = 0; index < args.length; index = index + 2) { + if (index + 1 < args.length) { + if (args[index].equalsIgnoreCase("-p") || args[index].equalsIgnoreCase("-port")) { port = Integer.parseInt(args[index + 1]); } - if(args[index].equalsIgnoreCase("-h") || args[index].equalsIgnoreCase("-host")) { + if (args[index].equalsIgnoreCase("-h") || args[index].equalsIgnoreCase("-host")) { serverAddress = args[index + 1]; } - if(args[index].equalsIgnoreCase("-ptl") || args[index].equalsIgnoreCase("-protocol")) { + if (args[index].equalsIgnoreCase("-ptl") || args[index].equalsIgnoreCase("-protocol")) { protocol = args[index + 1]; } } } } - if(serverAddress == null){ + if (serverAddress == null) { List serverAddressList = getServerAddressList(); - if(serverAddressList == null || serverAddressList.size() == 0) { + if (serverAddressList == null || serverAddressList.size() == 0) { throw new RuntimeException("Please use '-host' to setting server address."); } SimplePrinter.printNotice("Please select a server:"); - for(int i = 0; i < serverAddressList.size(); i++) { - SimplePrinter.printNotice((i+1) + ". " + serverAddressList.get(i)); + for (int i = 0; i < serverAddressList.size(); i++) { + SimplePrinter.printNotice((i + 1) + ". " + serverAddressList.get(i)); } int serverPick = Integer.parseInt(SimpleWriter.write("option")); - while(serverPick < 1 || serverPick > serverAddressList.size()){ + while (serverPick < 1 || serverPick > serverAddressList.size()) { try { SimplePrinter.printNotice("The server address does not exist!"); serverPick = Integer.parseInt(SimpleWriter.write("option")); - }catch(NumberFormatException e){} + } catch (NumberFormatException ignore) {} } - serverAddress = serverAddressList.get(serverPick-1); + serverAddress = serverAddressList.get(serverPick - 1); String[] elements = serverAddress.split(":"); serverAddress = elements[0]; port = Integer.parseInt(elements[1]); } - if (Objects.equals(protocol, "pb")){ + if (Objects.equals(protocol, "pb")) { new ProtobufProxy().connect(serverAddress, port); - }else if (Objects.equals(protocol, "ws")){ + } else if (Objects.equals(protocol, "ws")) { new WebsocketProxy().connect(serverAddress, port + 1); - }else{ + } else { throw new UnsupportedOperationException("Unsupported protocol " + protocol); } } - private static List getServerAddressList(){ - for(String serverAddressSource: serverAddressSource) { + private static List getServerAddressList() { + for (String serverAddressSource : serverAddressSource) { try { String serverInfo = StreamUtils.convertToString(new URL(serverAddressSource)); - return Noson.convert(serverInfo, new NoType>() {}); + return Noson.convert(serverInfo, new NoType>() {}); } catch (IOException e) { SimplePrinter.printNotice("Try connected " + serverAddressSource + " failed: " + e.getMessage()); } 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 98cf7e3..5cb2525 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 @@ -17,43 +17,43 @@ public abstract class ClientEventListener { public abstract void call(Channel channel, String data); public final static Map LISTENER_MAP = new HashMap<>(); - + private final static String LISTENER_PREFIX = "org.nico.ratel.landlords.client.event.ClientEventListener_"; - + protected static List lastPokers = null; protected static String lastSellClientNickname = null; protected static String lastSellClientType = null; - + protected static void initLastSellInfo() { lastPokers = null; lastSellClientNickname = null; lastSellClientType = null; } - + @SuppressWarnings("unchecked") - public static ClientEventListener get(ClientEventCode code){ - ClientEventListener listener = null; + public static ClientEventListener get(ClientEventCode code) { + ClientEventListener listener; try { - if(ClientEventListener.LISTENER_MAP.containsKey(code)){ + if (ClientEventListener.LISTENER_MAP.containsKey(code)) { listener = ClientEventListener.LISTENER_MAP.get(code); - }else{ + } else { String eventListener = LISTENER_PREFIX + code.name(); Class listenerClass = (Class) Class.forName(eventListener); listener = listenerClass.newInstance(); ClientEventListener.LISTENER_MAP.put(code, listener); } return listener; - }catch(ClassNotFoundException | InstantiationException | IllegalAccessException e) { + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { e.printStackTrace(); } - return listener; + return null; } - - protected ChannelFuture pushToServer(Channel channel, ServerEventCode code, String datas){ + + protected ChannelFuture pushToServer(Channel channel, ServerEventCode code, String datas) { return ChannelUtils.pushToServer(channel, code, datas); } - - protected ChannelFuture pushToServer(Channel channel, ServerEventCode code){ + + 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 8b39393..30da56d 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 @@ -5,7 +5,7 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_CLIENT_CONNECT extends ClientEventListener{ +public class ClientEventListener_CODE_CLIENT_CONNECT extends ClientEventListener { @Override public void call(Channel channel, String 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 d8bf3f1..5f2f241 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 @@ -9,23 +9,22 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_CLIENT_EXIT extends ClientEventListener{ +public class ClientEventListener_CODE_CLIENT_EXIT extends ClientEventListener { @Override public void call(Channel channel, String data) { Map map = MapHelper.parser(data); - + Integer exitClientId = (Integer) map.get("exitClientId"); - - String role = null; - if(exitClientId == SimpleClient.id) { + + String role; + if (exitClientId == SimpleClient.id) { role = "You"; - }else { + } else { role = String.valueOf(map.get("exitClientNickname")); } SimplePrinter.printNotice(role + " left the room. Room disbanded!\n"); 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_KICK.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_KICK.java index 8962492..c72f768 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_KICK.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_KICK.java @@ -5,13 +5,13 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_CLIENT_KICK extends ClientEventListener{ +public class ClientEventListener_CODE_CLIENT_KICK extends ClientEventListener { @Override public void call(Channel channel, String data) { - + SimplePrinter.printNotice("You have been kicked from the room for being idle.\n"); - + 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 9b40bf1..41e8198 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 @@ -11,13 +11,13 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_CLIENT_NICKNAME_SET extends ClientEventListener{ +public class ClientEventListener_CODE_CLIENT_NICKNAME_SET extends ClientEventListener { public static final int NICKNAME_MAX_LENGTH = 10; - + @Override public void call(Channel channel, String data) { - + // If it is not the first time that the user is prompted to enter nickname // If first time, data = null or "" otherwise not empty if (StringUtils.isNotBlank(data)) { @@ -28,16 +28,15 @@ public void call(Channel channel, String data) { } SimplePrinter.printNotice("Please set your nickname (upto " + NICKNAME_MAX_LENGTH + " characters)"); String nickname = SimpleWriter.write("nickname"); - + // If the length of nickname is more that NICKNAME_MAX_LENGTH if (nickname.trim().length() > NICKNAME_MAX_LENGTH) { String result = MapHelper.newInstance().put("invalidLength", nickname.trim().length()).json(); get(ClientEventCode.CODE_CLIENT_NICKNAME_SET).call(channel, result); - }else{ + } else { 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 6341ed2..c6098be 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 @@ -12,19 +12,19 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_GAME_LANDLORD_CONFIRM extends ClientEventListener{ +public class ClientEventListener_CODE_GAME_LANDLORD_CONFIRM extends ClientEventListener { @Override public void call(Channel channel, String data) { Map map = MapHelper.parser(data); - + String landlordNickname = String.valueOf(map.get("landlordNickname")); - + SimplePrinter.printNotice(landlordNickname + " has become the landlord and gotten three extra cards"); - + List additionalPokers = Noson.convert(map.get("additionalPokers"), new NoType>() {}); SimplePrinter.printPokers(additionalPokers); - + 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_CYCLE.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_LANDLORD_CYCLE.java index c86e28a..e34e21d 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 @@ -4,12 +4,12 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_GAME_LANDLORD_CYCLE extends ClientEventListener{ +public class ClientEventListener_CODE_GAME_LANDLORD_CYCLE extends ClientEventListener { @Override public void call(Channel channel, String data) { SimplePrinter.printNotice("No player takes the landlord, so redealing cards."); - + } } 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 08ecdd1..1752498 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 @@ -10,34 +10,34 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_GAME_LANDLORD_ELECT extends ClientEventListener{ +public class ClientEventListener_CODE_GAME_LANDLORD_ELECT extends ClientEventListener { @Override public void call(Channel channel, String data) { Map map = MapHelper.parser(data); int turnClientId = (int) map.get("nextClientId"); - - if(map.containsKey("preClientNickname")) { + + if (map.containsKey("preClientNickname")) { SimplePrinter.printNotice(map.get("preClientNickname") + " don't rob the landlord!"); } - + if(turnClientId == SimpleClient.id) { SimplePrinter.printNotice("It's your turn. Do you want to rob the landlord? [Y/N] (enter [exit|e] to exit current room)"); String line = SimpleWriter.write("Y/N"); - if(line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("e")) { + if (line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("e")) { pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT); - }else if(line.equalsIgnoreCase("Y")){ + } else if (line.equalsIgnoreCase("Y")) { pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "TRUE"); - }else if(line.equalsIgnoreCase("N")){ + } else if (line.equalsIgnoreCase("N")) { pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "FALSE"); - }else{ + } else { SimplePrinter.printNotice("Invalid options"); call(channel, data); } - }else { + } else { SimplePrinter.printNotice("It's " + map.get("nextClientNickname") + "'s turn. Please wait patiently for his/her confirmation !"); } - + } } 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 9b7659f..4136ca5 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 @@ -7,7 +7,7 @@ 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, String 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 00d304c..9136c0d 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 @@ -1,9 +1,6 @@ package org.nico.ratel.landlords.client.event; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - +import io.netty.channel.Channel; import org.nico.noson.Noson; import org.nico.noson.entity.NoType; import org.nico.ratel.landlords.client.SimpleClient; @@ -16,113 +13,116 @@ import org.nico.ratel.landlords.print.SimplePrinter; import org.nico.ratel.landlords.print.SimpleWriter; -import io.netty.channel.Channel; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; -public class ClientEventListener_CODE_GAME_POKER_PLAY extends ClientEventListener{ +public class ClientEventListener_CODE_GAME_POKER_PLAY extends ClientEventListener { @Override public void call(Channel channel, String data) { Map map = MapHelper.parser(data); - + SimplePrinter.printNotice("It's your turn to play, your cards are as follows: "); - List pokers = Noson.convert(map.get("pokers"), new NoType>() {}); + List pokers = Noson.convert(map.get("pokers"), new NoType>() { + }); SimplePrinter.printPokers(pokers); - - + SimplePrinter.printNotice("Please enter the combination you came up with (enter [exit|e] to exit current room, enter [pass|p] to jump current round, enter [view|v] to show all valid combinations.)"); String line = SimpleWriter.write("combination"); - if(line == null){ + if (line == null) { SimplePrinter.printNotice("Invalid enter"); call(channel, data); - }else{ - if(line.equalsIgnoreCase("pass") || line.equalsIgnoreCase("p")) { + } else { + if (line.equalsIgnoreCase("pass") || line.equalsIgnoreCase("p")) { pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_PASS); - }else if(line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("e")){ + } else if (line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("e")) { pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT); - }else if(line.equalsIgnoreCase("view") || line.equalsIgnoreCase("v")){ - if(! map.containsKey("lastSellPokers") || ! map.containsKey("lastSellClientId")) { + } else if (line.equalsIgnoreCase("view") || line.equalsIgnoreCase("v")) { + if (!map.containsKey("lastSellPokers") || !map.containsKey("lastSellClientId")) { SimplePrinter.printNotice("Current server version unsupport this feature, need more than v1.2.4."); call(channel, data); return; } Object lastSellPokersObj = map.get("lastSellPokers"); - if(lastSellPokersObj == null || Integer.valueOf(SimpleClient.id).equals(map.get("lastSellClientId"))) { + if (lastSellPokersObj == null || Integer.valueOf(SimpleClient.id).equals(map.get("lastSellClientId"))) { SimplePrinter.printNotice("Up to you !"); call(channel, data); return; - }else { - List lastSellPokers = Noson.convert(lastSellPokersObj, new NoType>() {}); + } else { + List lastSellPokers = Noson.convert(lastSellPokersObj, new NoType>() { + }); List sells = PokerHelper.validSells(PokerHelper.checkPokerType(lastSellPokers), pokers); - if(sells == null || sells.size() == 0) { + if (sells == null || sells.size() == 0) { SimplePrinter.printNotice("It is a pity that, there is no winning combination..."); call(channel, data); return; } - for(int i = 0; i < sells.size(); i ++) { + for (int i = 0; i < sells.size(); i++) { SimplePrinter.printNotice(i + 1 + ". " + PokerHelper.textOnlyNoType(sells.get(i).getSellPokers())); } - while(true){ + while (true) { SimplePrinter.printNotice("You can enter index to choose anyone.(enter [back|b] to go back.)"); line = SimpleWriter.write("choose"); - if(line.equalsIgnoreCase("back") || line.equalsIgnoreCase("b")) { + if (line.equalsIgnoreCase("back") || line.equalsIgnoreCase("b")) { call(channel, data); return; - }else { + } else { try { int choose = Integer.valueOf(line); - if(choose < 1 || choose > sells.size()) { + if (choose < 1 || choose > sells.size()) { SimplePrinter.printNotice("The input number must be in the range of 1 to " + sells.size() + "."); - }else { + } else { List choosePokers = sells.get(choose - 1).getSellPokers(); List options = new ArrayList<>(); - for(Poker poker: choosePokers) { + for (Poker poker : choosePokers) { options.add(poker.getLevel().getAlias()[0]); } - pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY, Noson.reversal(options.toArray(new Character[] {}))); + pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY, Noson.reversal(options.toArray(new Character[]{}))); break; } - }catch(NumberFormatException e) { + } catch (NumberFormatException e) { SimplePrinter.printNotice("Please input a number."); } } } } - + // PokerHelper.validSells(lastPokerSell, pokers); - }else { + } else { String[] strs = line.split(" "); List options = new ArrayList<>(); boolean access = true; - for(int index = 0; index < strs.length; index ++){ + for (int index = 0; index < strs.length; index++) { String str = strs[index]; - for(char c: str.toCharArray()) { - if(c == ' ' || c == '\t') { - }else { - if(! PokerLevel.aliasContains(c)) { + for (char c : str.toCharArray()) { + if (c == ' ' || c == '\t') { + } else { + if (!PokerLevel.aliasContains(c)) { access = false; break; - }else { + } else { options.add(c); } } } } - if(access){ - pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY, Noson.reversal(options.toArray(new Character[] {}))); - }else{ + if (access) { + pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY, Noson.reversal(options.toArray(new Character[]{}))); + } else { SimplePrinter.printNotice("Invalid enter"); - - if(lastPokers != null) { + + if (lastPokers != null) { SimplePrinter.printNotice(lastSellClientNickname + "[" + lastSellClientType + "] played:"); SimplePrinter.printPokers(lastPokers); } - + call(channel, data); } } } - + } } 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 032b197..826f674 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 @@ -5,7 +5,7 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_GAME_POKER_PLAY_CANT_PASS extends ClientEventListener{ +public class ClientEventListener_CODE_GAME_POKER_PLAY_CANT_PASS extends ClientEventListener { @Override public void call(Channel channel, String 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 index cac25bf..9a69aaa 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 @@ -5,13 +5,13 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_GAME_POKER_PLAY_INVALID extends ClientEventListener{ +public class ClientEventListener_CODE_GAME_POKER_PLAY_INVALID extends ClientEventListener { @Override public void call(Channel channel, String data) { - + SimplePrinter.printNotice("This combination is invalid."); - + if(lastPokers != null) { SimplePrinter.printNotice(lastSellClientNickname + "[" + lastSellClientType + "] played:"); SimplePrinter.printPokers(lastPokers); 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 be27951..4013c2d 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 @@ -5,17 +5,17 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_GAME_POKER_PLAY_LESS extends ClientEventListener{ +public class ClientEventListener_CODE_GAME_POKER_PLAY_LESS extends ClientEventListener { @Override public void call(Channel channel, String data) { SimplePrinter.printNotice("Your combination has lower rank than the previous. You cannot play this combination!"); - + if(lastPokers != null) { SimplePrinter.printNotice(lastSellClientNickname + "[" + lastSellClientType + "] played:"); SimplePrinter.printPokers(lastPokers); } - + 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 3689f11..06bb1cb 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 @@ -8,19 +8,19 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH extends ClientEventListener{ +public class ClientEventListener_CODE_GAME_POKER_PLAY_MISMATCH extends ClientEventListener { @Override public void call(Channel channel, String data) { Map map = MapHelper.parser(data); - + SimplePrinter.printNotice("Your combination is " + map.get("playType") + " (" + map.get("playCount") + "), but the previous combination is " + map.get("preType") + " (" + map.get("preCount") + "). Mismatch!"); - + if(lastPokers != null) { SimplePrinter.printNotice(lastSellClientNickname + "[" + lastSellClientType + "] played:"); SimplePrinter.printPokers(lastPokers); } - + 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_ORDER_ERROR.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY_ORDER_ERROR.java index 0b3b45d..8c698a0 100644 --- 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 @@ -4,11 +4,11 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_GAME_POKER_PLAY_ORDER_ERROR extends ClientEventListener{ +public class ClientEventListener_CODE_GAME_POKER_PLAY_ORDER_ERROR extends ClientEventListener { @Override public void call(Channel channel, String data) { - + SimplePrinter.printNotice("It is not your turn yet. Please wait for other players!"); } 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 1226604..c77db21 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 @@ -9,16 +9,16 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_GAME_POKER_PLAY_PASS 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("clientNickname") + " passed. It is now " + map.get("nextClientNickname") + "'s turn."); - + int turnClientId = (int) map.get("nextClientId"); - if(SimpleClient.id == turnClientId) { + if (SimpleClient.id == turnClientId) { 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 index 9a4431c..3f25bfa 100644 --- 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 @@ -1,47 +1,46 @@ package org.nico.ratel.landlords.client.event; -import static org.nico.ratel.landlords.client.event.ClientEventListener_CODE_CLIENT_NICKNAME_SET.NICKNAME_MAX_LENGTH; - -import java.util.List; -import java.util.Map; - +import io.netty.channel.Channel; 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.FormatPrinter; import org.nico.ratel.landlords.print.SimplePrinter; -import io.netty.channel.Channel; +import java.util.List; +import java.util.Map; -public class ClientEventListener_CODE_GAME_POKER_PLAY_REDIRECT extends ClientEventListener{ +import static org.nico.ratel.landlords.client.event.ClientEventListener_CODE_CLIENT_NICKNAME_SET.NICKNAME_MAX_LENGTH; + +public class ClientEventListener_CODE_GAME_POKER_PLAY_REDIRECT extends ClientEventListener { private static String[] choose = new String[]{"UP", "DOWN"}; - + private static String format = "\n[%-4s] %-" + NICKNAME_MAX_LENGTH + "s surplus %-2s [%-8s]"; - + @SuppressWarnings("unchecked") @Override public void call(Channel channel, String data) { - + Map map = MapHelper.parser(data); - + int sellClientId = (int) map.get("sellClientId"); - + List> clientInfos = (List>) map.get("clientInfos"); - - for(int index = 0; index < 2; index ++){ - for(Map clientInfo: clientInfos) { + + for (int index = 0; index < 2; index++) { + for (Map clientInfo : clientInfos) { String position = (String) clientInfo.get("position"); - if(position.equalsIgnoreCase(choose[index])){ + if (position.equalsIgnoreCase(choose[index])) { FormatPrinter.printNotice(format, clientInfo.get("position"), clientInfo.get("clientNickname"), clientInfo.get("surplus"), clientInfo.get("type")); } } } SimplePrinter.printNotice(""); - - if(sellClientId == SimpleClient.id) { + + if (sellClientId == SimpleClient.id) { get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); - }else { + } else { SimplePrinter.printNotice("It is " + map.get("sellClinetNickname") + "'s turn. Please wait for him to play his cards."); } } 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 fe4082b..e080380 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,8 +1,6 @@ package org.nico.ratel.landlords.client.event; -import java.util.List; -import java.util.Map; - +import io.netty.channel.Channel; import org.nico.noson.Noson; import org.nico.noson.entity.NoType; import org.nico.ratel.landlords.entity.Poker; @@ -10,23 +8,24 @@ import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; -import io.netty.channel.Channel; +import java.util.List; +import java.util.Map; -public class ClientEventListener_CODE_GAME_STARTING extends ClientEventListener{ +public class ClientEventListener_CODE_GAME_STARTING extends ClientEventListener { @Override public void call(Channel channel, String data) { - + Map map = MapHelper.parser(data); - + SimplePrinter.printNotice("Game starting!"); - + List pokers = Noson.convert(map.get("pokers"), new NoType>() {}); - + SimplePrinter.printNotice(""); SimplePrinter.printNotice("Your cards are"); 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_PVE_DIFFICULTY_NOT_SUPPORT.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_PVE_DIFFICULTY_NOT_SUPPORT.java index 5a1039f..d7d0b74 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_PVE_DIFFICULTY_NOT_SUPPORT.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_PVE_DIFFICULTY_NOT_SUPPORT.java @@ -5,7 +5,7 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_PVE_DIFFICULTY_NOT_SUPPORT extends ClientEventListener{ +public class ClientEventListener_CODE_PVE_DIFFICULTY_NOT_SUPPORT extends ClientEventListener { @Override public void call(Channel channel, String data) { @@ -14,5 +14,4 @@ public void call(Channel channel, String 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 60a8150..024ec65 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 @@ -6,15 +6,15 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_ROOM_CREATE_SUCCESS extends ClientEventListener{ +public class ClientEventListener_CODE_ROOM_CREATE_SUCCESS extends ClientEventListener { @Override public void call(Channel channel, String data) { - + Room room = Noson.convert(data, Room.class); - + initLastSellInfo(); - + SimplePrinter.printNotice("You have created a 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 291afe6..bf5668f 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 @@ -8,16 +8,15 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_FULL extends ClientEventListener{ +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.printNotice("Join room failed. 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 1932d1d..c501153 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 @@ -8,16 +8,13 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST extends ClientEventListener{ +public class ClientEventListener_CODE_ROOM_JOIN_FAIL_BY_INEXIST extends ClientEventListener { @Override public void call(Channel channel, String data) { Map dataMap = MapHelper.parser(data); - + SimplePrinter.printNotice("Join room failed. Room " + dataMap.get("roomId") + " doesn't exist!"); 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 ae3f667..3c4509d 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 @@ -8,24 +8,21 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_ROOM_JOIN_SUCCESS extends ClientEventListener{ +public class ClientEventListener_CODE_ROOM_JOIN_SUCCESS extends ClientEventListener { @Override public void call(Channel channel, String data) { Map map = MapHelper.parser(data); - + initLastSellInfo(); - + int joinClientId = (int) map.get("clientId"); - if(SimpleClient.id == joinClientId) { + if (SimpleClient.id == joinClientId) { SimplePrinter.printNotice("You have joined room:" + map.get("roomId") + ". There are " + map.get("roomClientCount") + " players in the room now."); SimplePrinter.printNotice("Please wait for other players to join. The game would start at three players!"); - }else { + } else { SimplePrinter.printNotice(map.get("clientNickname") + " joined room, there are currently " + map.get("roomClientCount") + " in the room."); } - - } - - + } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_PLAY_FAIL_BY_INEXIST.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_PLAY_FAIL_BY_INEXIST.java index 689f279..97d994a 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_PLAY_FAIL_BY_INEXIST.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_PLAY_FAIL_BY_INEXIST.java @@ -5,15 +5,14 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_ROOM_PLAY_FAIL_BY_INEXIST extends ClientEventListener{ +public class ClientEventListener_CODE_ROOM_PLAY_FAIL_BY_INEXIST extends ClientEventListener { @Override public void call(Channel channel, String data) { - + SimplePrinter.printNotice("Play failed. Room already disbanded!"); 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 1eec368..527d963 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 @@ -7,7 +7,7 @@ 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, String data) { @@ -17,25 +17,22 @@ public void call(Channel channel, String data) { SimplePrinter.printNotice("3. Settings"); SimplePrinter.printNotice("Please select an option above (enter [exit|e] to log out)"); String line = SimpleWriter.write("selection"); - + if(line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("e")) { System.exit(0); - }else { + } else { int choose = OptionsUtils.getOptions(line); - - if(choose == 1) { + + if (choose == 1) { get(ClientEventCode.CODE_SHOW_OPTIONS_PVP).call(channel, data); - }else if(choose == 2){ + } else if (choose == 2) { get(ClientEventCode.CODE_SHOW_OPTIONS_PVE).call(channel, data); - }else if(choose == 3){ + } else if (choose == 3) { get(ClientEventCode.CODE_SHOW_OPTIONS_SETTING).call(channel, data); - }else { + } else { SimplePrinter.printNotice("Invalid option, please choose again:"); call(channel, data); } } } - - - } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_SETTING.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_SETTING.java index 3f627ab..4fac6bc 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_SETTING.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_SETTING.java @@ -8,7 +8,7 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_SHOW_OPTIONS_SETTING extends ClientEventListener{ +public class ClientEventListener_CODE_SHOW_OPTIONS_SETTING extends ClientEventListener { @Override public void call(Channel channel, String data) { @@ -21,13 +21,13 @@ public void call(Channel channel, String data) { SimplePrinter.printNotice("Please select an option above (enter [BACK] to return to options list)"); String line = SimpleWriter.write("setting"); - - if(line.equalsIgnoreCase("BACK")) { + + if (line.equalsIgnoreCase("BACK")) { get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); - }else { + } else { int choose = OptionsUtils.getOptions(line); - - if(choose >=1 && choose <= 5){ + + if (choose >= 1 && choose <= 5) { PokerHelper.pokerPrinterType = choose - 1; get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); } else { @@ -38,5 +38,4 @@ public void call(Channel channel, String data) { } - } 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 9de4d63..9459523 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 @@ -11,22 +11,22 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_SHOW_POKERS extends ClientEventListener{ +public class ClientEventListener_CODE_SHOW_POKERS extends ClientEventListener { @Override public void call(Channel channel, String data) { - + Map map = MapHelper.parser(data); - + lastSellClientNickname = (String) map.get("clientNickname"); lastSellClientType = (String) map.get("clientType"); - + SimplePrinter.printNotice(lastSellClientNickname + "[" + lastSellClientType + "] played:"); lastPokers = Noson.convert(map.get("pokers"), new NoType>() {}); SimplePrinter.printPokers(lastPokers); - - if(map.containsKey("sellClinetNickname")) { - SimplePrinter.printNotice("Next player is " + map.get("sellClinetNickname") + ". Please wait for him to play his combination."); + + if (map.containsKey("sellClientNickname")) { + SimplePrinter.printNotice("Next player is " + map.get("sellClientNickname") + ". Please wait for him to play his combination."); } } 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 f4876b3..29490ea 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 @@ -12,27 +12,24 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_SHOW_ROOMS extends ClientEventListener{ +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()){ + if (roomList != null && !roomList.isEmpty()) { // "COUNT" begins after NICKNAME_MAX_LENGTH characters. The dash means that the string is left-justified. String format = "#\t%s\t|\t%-" + NICKNAME_MAX_LENGTH + "s\t|\t%-6s\t|\t%-6s\t#\n"; FormatPrinter.printNotice(format, "ID", "OWNER", "COUNT", "TYPE"); - for(Map room: roomList) { + for (Map room : roomList) { FormatPrinter.printNotice(format, room.get("roomId"), room.get("roomOwner"), room.get("roomClientCount"), room.get("roomType")); } SimplePrinter.printNotice(""); get(ClientEventCode.CODE_SHOW_OPTIONS_PVP).call(channel, data); - }else { + } else { SimplePrinter.printNotice("No available room. Please create a room!"); get(ClientEventCode.CODE_SHOW_OPTIONS_PVP).call(channel, data); } } - - - } 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 31af4f9..33f5679 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,29 +1,25 @@ package org.nico.ratel.landlords.channel; -import com.google.gson.Gson; -import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import org.nico.ratel.landlords.entity.ClientTransferData; import org.nico.ratel.landlords.entity.Msg; import org.nico.ratel.landlords.entity.ServerTransferData; import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.ServerEventCode; +import org.nico.ratel.landlords.utils.JsonUtils; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; -import org.nico.ratel.landlords.utils.JsonUtils; - -import java.util.HashMap; -import java.util.Map; +import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; public class ChannelUtils { public static void pushToClient(Channel channel, ClientEventCode code, String data) { pushToClient(channel, code, data, null); } - + public static void pushToClient(Channel channel, ClientEventCode code, String data, String info) { - if(channel != null) { - if (channel.pipeline().get("ws") != null){ + if (channel != null) { + if (channel.pipeline().get("ws") != null) { Msg msg = new Msg(); msg.setCode(code.toString()); msg.setData(data); @@ -31,36 +27,36 @@ public static void pushToClient(Channel channel, ClientEventCode code, String da channel.writeAndFlush(new TextWebSocketFrame(JsonUtils.toJson(msg))); } else { ClientTransferData.ClientTransferDataProtoc.Builder clientTransferData = ClientTransferData.ClientTransferDataProtoc.newBuilder(); - if(code != null) { + if (code != null) { clientTransferData.setCode(code.toString()); } - if(data != null) { + if (data != null) { clientTransferData.setData(data); } - if(info != null) { + if (info != null) { clientTransferData.setInfo(info); } channel.writeAndFlush(clientTransferData); } } } - + public static ChannelFuture pushToServer(Channel channel, ServerEventCode code, String data) { - if (channel.pipeline().get("ws") != null){ + if (channel.pipeline().get("ws") != null) { Msg msg = new Msg(); msg.setCode(code.toString()); msg.setData(data); return channel.writeAndFlush(new TextWebSocketFrame(JsonUtils.toJson(msg))); - }else{ + } else { ServerTransferData.ServerTransferDataProtoc.Builder serverTransferData = ServerTransferData.ServerTransferDataProtoc.newBuilder(); - if(code != null) { + if (code != null) { serverTransferData.setCode(code.toString()); } - if(data != null) { + if (data != null) { serverTransferData.setData(data); } return channel.writeAndFlush(serverTransferData); } } - + } 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 23cc02f..b9ed205 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 @@ -8,28 +8,32 @@ import io.netty.channel.Channel; -public class ClientSide{ +public class ClientSide { private int id; - + private int roomId; - + + private int score; + + private int scoreInc; + private String nickname; - + private List pokers; - + private ClientStatus status; - + private ClientRole role; - + private ClientType type; - + private ClientSide next; - + private ClientSide pre; - + private transient Channel channel; - + public ClientSide() {} public ClientSide(int id, ClientStatus status, Channel channel) { @@ -37,7 +41,7 @@ public ClientSide(int id, ClientStatus status, Channel channel) { this.status = status; this.channel = channel; } - + public void init() { roomId = 0; pokers = null; @@ -144,9 +148,7 @@ public boolean equals(Object obj) { if (getClass() != obj.getClass()) return false; ClientSide other = (ClientSide) obj; - if (id != other.id) - return false; - return true; + return id == other.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 98d72fd..2133bd6 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 @@ -5,13 +5,13 @@ /** * Poke, with {@link PokerLevel} and {@link PokerType} - * + * * @author nico */ -public class Poker{ - +public class Poker { + private PokerLevel level; - + private PokerType type; public Poker() { @@ -59,14 +59,12 @@ public boolean equals(Object obj) { Poker other = (Poker) obj; if (level != other.level) return false; - if (type != other.type) - return false; - return true; + return type == other.type; } @Override public String toString() { - return String.valueOf(level.getLevel()) + " "; + return level.getLevel() + " "; } - + } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/PokerSell.java b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/PokerSell.java index 6130b0a..f3a8d60 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/PokerSell.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/PokerSell.java @@ -1,27 +1,27 @@ package org.nico.ratel.landlords.entity; -import java.util.List; - import org.nico.ratel.landlords.enums.SellType; import org.nico.ratel.landlords.helper.PokerHelper; +import java.util.List; + public class PokerSell { private int score; - + private SellType sellType; - + private List sellPokers; - + private int coreLevel; - + public PokerSell(SellType sellType, List sellPokers, int coreLevel) { this.score = PokerHelper.parseScore(sellType, coreLevel); this.sellType = sellType; this.sellPokers = sellPokers; this.coreLevel = coreLevel; } - + public final int getCoreLevel() { return coreLevel; } @@ -58,5 +58,5 @@ public final void setSellPokers(List sellPokers) { public String toString() { return sellType + "\t| " + score + "\t|" + sellPokers; } - + } 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 b749bab..751aebc 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 @@ -9,34 +9,34 @@ import org.nico.ratel.landlords.enums.RoomStatus; import org.nico.ratel.landlords.enums.RoomType; -public class Room{ +public class Room { private int id; - + private String roomOwner; - + private RoomStatus status; - + private RoomType type; - + private Map clientSideMap; - + private LinkedList clientSideList; - + private int landlordId = -1; - + private List landlordPokers; - + private PokerSell lastPokerShell; - + private int lastSellClient = -1; - + private int currentSellClient = -1; - + private int difficultyCoefficient; - + private long lastFlushTime; - + private long createTime; private int firstSellClient; 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 edcb171..45ccce3 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,879 +4,942 @@ package org.nico.ratel.landlords.entity; public final class ServerTransferData { - private ServerTransferData() {} - public static void registerAllExtensions( - com.google.protobuf.ExtensionRegistryLite registry) { - } - - public static void registerAllExtensions( - com.google.protobuf.ExtensionRegistry registry) { - registerAllExtensions( - (com.google.protobuf.ExtensionRegistryLite) registry); - } - public interface ServerTransferDataProtocOrBuilder extends - // @@protoc_insertion_point(interface_extends:org.nico.ratel.landlords.entity.ServerTransferDataProtoc) - com.google.protobuf.MessageOrBuilder { - - /** - * string code = 1; - */ - java.lang.String getCode(); - /** - * string code = 1; - */ - com.google.protobuf.ByteString - getCodeBytes(); - - /** - * string data = 2; - */ - java.lang.String getData(); - /** - * string data = 2; - */ - com.google.protobuf.ByteString - getDataBytes(); - - /** - * string info = 3; - */ - java.lang.String getInfo(); - /** - * string info = 3; - */ - com.google.protobuf.ByteString - getInfoBytes(); - } - /** - * Protobuf type {@code org.nico.ratel.landlords.entity.ServerTransferDataProtoc} - */ - public static final class ServerTransferDataProtoc extends - com.google.protobuf.GeneratedMessageV3 implements - // @@protoc_insertion_point(message_implements:org.nico.ratel.landlords.entity.ServerTransferDataProtoc) - ServerTransferDataProtocOrBuilder { - private static final long serialVersionUID = 0L; - // Use ServerTransferDataProtoc.newBuilder() to construct. - private ServerTransferDataProtoc(com.google.protobuf.GeneratedMessageV3.Builder builder) { - super(builder); - } - private ServerTransferDataProtoc() { - code_ = ""; - data_ = ""; - info_ = ""; - } - - @java.lang.Override - public final com.google.protobuf.UnknownFieldSet - getUnknownFields() { - return this.unknownFields; - } - private ServerTransferDataProtoc( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - this(); - if (extensionRegistry == null) { - throw new java.lang.NullPointerException(); - } - int mutable_bitField0_ = 0; - com.google.protobuf.UnknownFieldSet.Builder unknownFields = - com.google.protobuf.UnknownFieldSet.newBuilder(); - try { - boolean done = false; - while (!done) { - int tag = input.readTag(); - switch (tag) { - case 0: - done = true; - break; - case 10: { - java.lang.String s = input.readStringRequireUtf8(); - - code_ = s; - break; - } - case 18: { - java.lang.String s = input.readStringRequireUtf8(); - - data_ = s; - break; - } - case 26: { - java.lang.String s = input.readStringRequireUtf8(); - - info_ = s; - break; - } - default: { - if (!parseUnknownFieldProto3( - input, unknownFields, extensionRegistry, tag)) { - done = true; - } - break; - } - } - } - } catch (com.google.protobuf.InvalidProtocolBufferException e) { - throw e.setUnfinishedMessage(this); - } catch (java.io.IOException e) { - throw new com.google.protobuf.InvalidProtocolBufferException( - e).setUnfinishedMessage(this); - } finally { - this.unknownFields = unknownFields.build(); - makeExtensionsImmutable(); - } - } - public static final com.google.protobuf.Descriptors.Descriptor - getDescriptor() { - return org.nico.ratel.landlords.entity.ServerTransferData.internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_descriptor; - } - - @java.lang.Override - protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable - internalGetFieldAccessorTable() { - return org.nico.ratel.landlords.entity.ServerTransferData.internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_fieldAccessorTable - .ensureFieldAccessorsInitialized( - org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc.class, org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc.Builder.class); - } - - public static final int CODE_FIELD_NUMBER = 1; - private volatile java.lang.Object code_; - /** - * string code = 1; - */ - public java.lang.String getCode() { - java.lang.Object ref = code_; - if (ref instanceof java.lang.String) { - return (java.lang.String) ref; - } else { - com.google.protobuf.ByteString bs = - (com.google.protobuf.ByteString) ref; - java.lang.String s = bs.toStringUtf8(); - code_ = s; - return s; - } - } - /** - * string code = 1; - */ - public com.google.protobuf.ByteString - getCodeBytes() { - java.lang.Object ref = code_; - if (ref instanceof java.lang.String) { - com.google.protobuf.ByteString b = - com.google.protobuf.ByteString.copyFromUtf8( - (java.lang.String) ref); - code_ = b; - return b; - } else { - return (com.google.protobuf.ByteString) ref; - } - } - - public static final int DATA_FIELD_NUMBER = 2; - private volatile java.lang.Object data_; - /** - * string data = 2; - */ - public java.lang.String getData() { - java.lang.Object ref = data_; - if (ref instanceof java.lang.String) { - return (java.lang.String) ref; - } else { - com.google.protobuf.ByteString bs = - (com.google.protobuf.ByteString) ref; - java.lang.String s = bs.toStringUtf8(); - data_ = s; - return s; - } - } - /** - * string data = 2; - */ - public com.google.protobuf.ByteString - getDataBytes() { - java.lang.Object ref = data_; - if (ref instanceof java.lang.String) { - com.google.protobuf.ByteString b = - com.google.protobuf.ByteString.copyFromUtf8( - (java.lang.String) ref); - data_ = b; - return b; - } else { - return (com.google.protobuf.ByteString) ref; - } - } - - public static final int INFO_FIELD_NUMBER = 3; - private volatile java.lang.Object info_; - /** - * string info = 3; - */ - public java.lang.String getInfo() { - java.lang.Object ref = info_; - if (ref instanceof java.lang.String) { - return (java.lang.String) ref; - } else { - com.google.protobuf.ByteString bs = - (com.google.protobuf.ByteString) ref; - java.lang.String s = bs.toStringUtf8(); - info_ = s; - return s; - } - } - /** - * string info = 3; - */ - public com.google.protobuf.ByteString - getInfoBytes() { - java.lang.Object ref = info_; - if (ref instanceof java.lang.String) { - com.google.protobuf.ByteString b = - com.google.protobuf.ByteString.copyFromUtf8( - (java.lang.String) ref); - info_ = b; - return b; - } else { - return (com.google.protobuf.ByteString) ref; - } - } - - private byte memoizedIsInitialized = -1; - @java.lang.Override - public final boolean isInitialized() { - byte isInitialized = memoizedIsInitialized; - if (isInitialized == 1) return true; - if (isInitialized == 0) return false; - - memoizedIsInitialized = 1; - return true; - } - - @java.lang.Override - public void writeTo(com.google.protobuf.CodedOutputStream output) - throws java.io.IOException { - if (!getCodeBytes().isEmpty()) { - com.google.protobuf.GeneratedMessageV3.writeString(output, 1, code_); - } - if (!getDataBytes().isEmpty()) { - com.google.protobuf.GeneratedMessageV3.writeString(output, 2, data_); - } - if (!getInfoBytes().isEmpty()) { - com.google.protobuf.GeneratedMessageV3.writeString(output, 3, info_); - } - unknownFields.writeTo(output); - } - - @java.lang.Override - public int getSerializedSize() { - int size = memoizedSize; - if (size != -1) return size; - - size = 0; - if (!getCodeBytes().isEmpty()) { - size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, code_); - } - if (!getDataBytes().isEmpty()) { - size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, data_); - } - if (!getInfoBytes().isEmpty()) { - size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, info_); - } - size += unknownFields.getSerializedSize(); - memoizedSize = size; - return size; - } - - @java.lang.Override - public boolean equals(final java.lang.Object obj) { - if (obj == this) { - return true; - } - if (!(obj instanceof org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc)) { - return super.equals(obj); - } - org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc other = (org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc) obj; - - boolean result = true; - result = result && getCode() - .equals(other.getCode()); - result = result && getData() - .equals(other.getData()); - result = result && getInfo() - .equals(other.getInfo()); - result = result && unknownFields.equals(other.unknownFields); - return result; - } - - @java.lang.Override - public int hashCode() { - if (memoizedHashCode != 0) { - return memoizedHashCode; - } - int hash = 41; - hash = (19 * hash) + getDescriptor().hashCode(); - hash = (37 * hash) + CODE_FIELD_NUMBER; - hash = (53 * hash) + getCode().hashCode(); - hash = (37 * hash) + DATA_FIELD_NUMBER; - hash = (53 * hash) + getData().hashCode(); - hash = (37 * hash) + INFO_FIELD_NUMBER; - hash = (53 * hash) + getInfo().hashCode(); - hash = (29 * hash) + unknownFields.hashCode(); - memoizedHashCode = hash; - return hash; - } - - public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( - java.nio.ByteBuffer data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( - java.nio.ByteBuffer data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( - com.google.protobuf.ByteString data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( - com.google.protobuf.ByteString data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom(byte[] data) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( - byte[] data, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom(java.io.InputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input); - } - public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( - java.io.InputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input, extensionRegistry); - } - public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseDelimitedFrom(java.io.InputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseDelimitedWithIOException(PARSER, input); - } - public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseDelimitedFrom( - java.io.InputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseDelimitedWithIOException(PARSER, input, extensionRegistry); - } - public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( - com.google.protobuf.CodedInputStream input) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input); - } - public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - return com.google.protobuf.GeneratedMessageV3 - .parseWithIOException(PARSER, input, extensionRegistry); - } - - @java.lang.Override - public Builder newBuilderForType() { return newBuilder(); } - public static Builder newBuilder() { - return DEFAULT_INSTANCE.toBuilder(); - } - public static Builder newBuilder(org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc prototype) { - return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); - } - @java.lang.Override - public Builder toBuilder() { - return this == DEFAULT_INSTANCE - ? new Builder() : new Builder().mergeFrom(this); - } - - @java.lang.Override - protected Builder newBuilderForType( - com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { - Builder builder = new Builder(parent); - return builder; - } - /** - * Protobuf type {@code org.nico.ratel.landlords.entity.ServerTransferDataProtoc} - */ - public static final class Builder extends - com.google.protobuf.GeneratedMessageV3.Builder implements - // @@protoc_insertion_point(builder_implements:org.nico.ratel.landlords.entity.ServerTransferDataProtoc) - org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtocOrBuilder { - public static final com.google.protobuf.Descriptors.Descriptor - getDescriptor() { - return org.nico.ratel.landlords.entity.ServerTransferData.internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_descriptor; - } - - @java.lang.Override - protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable - internalGetFieldAccessorTable() { - return org.nico.ratel.landlords.entity.ServerTransferData.internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_fieldAccessorTable - .ensureFieldAccessorsInitialized( - org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc.class, org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc.Builder.class); - } - - // Construct using org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc.newBuilder() - private Builder() { - maybeForceBuilderInitialization(); - } - - private Builder( - com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { - super(parent); - maybeForceBuilderInitialization(); - } - private void maybeForceBuilderInitialization() { - if (com.google.protobuf.GeneratedMessageV3 - .alwaysUseFieldBuilders) { - } - } - @java.lang.Override - public Builder clear() { - super.clear(); - code_ = ""; - - data_ = ""; - - info_ = ""; - - return this; - } - - @java.lang.Override - public com.google.protobuf.Descriptors.Descriptor - getDescriptorForType() { - return org.nico.ratel.landlords.entity.ServerTransferData.internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_descriptor; - } - - @java.lang.Override - public org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc getDefaultInstanceForType() { - return org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc.getDefaultInstance(); - } - - @java.lang.Override - public org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc build() { - org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc result = buildPartial(); - if (!result.isInitialized()) { - throw newUninitializedMessageException(result); - } - return result; - } - - @java.lang.Override - public org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc buildPartial() { - org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc result = new org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc(this); - result.code_ = code_; - result.data_ = data_; - result.info_ = info_; - onBuilt(); - return result; - } - - @java.lang.Override - public Builder clone() { - return (Builder) super.clone(); - } - @java.lang.Override - public Builder setField( - com.google.protobuf.Descriptors.FieldDescriptor field, - java.lang.Object value) { - return (Builder) super.setField(field, value); - } - @java.lang.Override - public Builder clearField( - com.google.protobuf.Descriptors.FieldDescriptor field) { - return (Builder) super.clearField(field); - } - @java.lang.Override - public Builder clearOneof( - com.google.protobuf.Descriptors.OneofDescriptor oneof) { - return (Builder) super.clearOneof(oneof); - } - @java.lang.Override - public Builder setRepeatedField( - com.google.protobuf.Descriptors.FieldDescriptor field, - int index, java.lang.Object value) { - return (Builder) super.setRepeatedField(field, index, value); - } - @java.lang.Override - public Builder addRepeatedField( - com.google.protobuf.Descriptors.FieldDescriptor field, - java.lang.Object value) { - return (Builder) super.addRepeatedField(field, value); - } - @java.lang.Override - public Builder mergeFrom(com.google.protobuf.Message other) { - if (other instanceof org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc) { - return mergeFrom((org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc)other); - } else { - super.mergeFrom(other); - return this; - } - } - - public Builder mergeFrom(org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc other) { - if (other == org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc.getDefaultInstance()) return this; - if (!other.getCode().isEmpty()) { - code_ = other.code_; - onChanged(); - } - if (!other.getData().isEmpty()) { - data_ = other.data_; - onChanged(); - } - if (!other.getInfo().isEmpty()) { - info_ = other.info_; - onChanged(); - } - this.mergeUnknownFields(other.unknownFields); - onChanged(); - return this; - } - - @java.lang.Override - public final boolean isInitialized() { - return true; - } - - @java.lang.Override - public Builder mergeFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws java.io.IOException { - org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parsedMessage = null; - try { - parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); - } catch (com.google.protobuf.InvalidProtocolBufferException e) { - parsedMessage = (org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc) e.getUnfinishedMessage(); - throw e.unwrapIOException(); - } finally { - if (parsedMessage != null) { - mergeFrom(parsedMessage); - } - } - return this; - } - - private java.lang.Object code_ = ""; - /** - * string code = 1; - */ - public java.lang.String getCode() { - java.lang.Object ref = code_; - if (!(ref instanceof java.lang.String)) { - com.google.protobuf.ByteString bs = - (com.google.protobuf.ByteString) ref; - java.lang.String s = bs.toStringUtf8(); - code_ = s; - return s; - } else { - return (java.lang.String) ref; - } - } - /** - * string code = 1; - */ - public com.google.protobuf.ByteString - getCodeBytes() { - java.lang.Object ref = code_; - if (ref instanceof String) { - com.google.protobuf.ByteString b = - com.google.protobuf.ByteString.copyFromUtf8( - (java.lang.String) ref); - code_ = b; - return b; - } else { - return (com.google.protobuf.ByteString) ref; - } - } - /** - * string code = 1; - */ - public Builder setCode( - java.lang.String value) { - if (value == null) { - throw new NullPointerException(); - } - - code_ = value; - onChanged(); - return this; - } - /** - * string code = 1; - */ - public Builder clearCode() { - - code_ = getDefaultInstance().getCode(); - onChanged(); - return this; - } - /** - * string code = 1; - */ - public Builder setCodeBytes( - com.google.protobuf.ByteString value) { - if (value == null) { - throw new NullPointerException(); - } - checkByteStringIsUtf8(value); - - code_ = value; - onChanged(); - return this; - } - - private java.lang.Object data_ = ""; - /** - * string data = 2; - */ - public java.lang.String getData() { - java.lang.Object ref = data_; - if (!(ref instanceof java.lang.String)) { - com.google.protobuf.ByteString bs = - (com.google.protobuf.ByteString) ref; - java.lang.String s = bs.toStringUtf8(); - data_ = s; - return s; - } else { - return (java.lang.String) ref; - } - } - /** - * string data = 2; - */ - public com.google.protobuf.ByteString - getDataBytes() { - java.lang.Object ref = data_; - if (ref instanceof String) { - com.google.protobuf.ByteString b = - com.google.protobuf.ByteString.copyFromUtf8( - (java.lang.String) ref); - data_ = b; - return b; - } else { - return (com.google.protobuf.ByteString) ref; - } - } - /** - * string data = 2; - */ - public Builder setData( - java.lang.String value) { - if (value == null) { - throw new NullPointerException(); - } - - data_ = value; - onChanged(); - return this; - } - /** - * string data = 2; - */ - public Builder clearData() { - - data_ = getDefaultInstance().getData(); - onChanged(); - return this; - } - /** - * string data = 2; - */ - public Builder setDataBytes( - com.google.protobuf.ByteString value) { - if (value == null) { - throw new NullPointerException(); - } - checkByteStringIsUtf8(value); - - data_ = value; - onChanged(); - return this; - } - - private java.lang.Object info_ = ""; - /** - * string info = 3; - */ - public java.lang.String getInfo() { - java.lang.Object ref = info_; - if (!(ref instanceof java.lang.String)) { - com.google.protobuf.ByteString bs = - (com.google.protobuf.ByteString) ref; - java.lang.String s = bs.toStringUtf8(); - info_ = s; - return s; - } else { - return (java.lang.String) ref; - } - } - /** - * string info = 3; - */ - public com.google.protobuf.ByteString - getInfoBytes() { - java.lang.Object ref = info_; - if (ref instanceof String) { - com.google.protobuf.ByteString b = - com.google.protobuf.ByteString.copyFromUtf8( - (java.lang.String) ref); - info_ = b; - return b; - } else { - return (com.google.protobuf.ByteString) ref; - } - } - /** - * string info = 3; - */ - public Builder setInfo( - java.lang.String value) { - if (value == null) { - throw new NullPointerException(); - } - - info_ = value; - onChanged(); - return this; - } - /** - * string info = 3; - */ - public Builder clearInfo() { - - info_ = getDefaultInstance().getInfo(); - onChanged(); - return this; - } - /** - * string info = 3; - */ - public Builder setInfoBytes( - com.google.protobuf.ByteString value) { - if (value == null) { - throw new NullPointerException(); - } - checkByteStringIsUtf8(value); - - info_ = value; - onChanged(); - return this; - } - @java.lang.Override - public final Builder setUnknownFields( - final com.google.protobuf.UnknownFieldSet unknownFields) { - return super.setUnknownFieldsProto3(unknownFields); - } - - @java.lang.Override - public final Builder mergeUnknownFields( - final com.google.protobuf.UnknownFieldSet unknownFields) { - return super.mergeUnknownFields(unknownFields); - } - - - // @@protoc_insertion_point(builder_scope:org.nico.ratel.landlords.entity.ServerTransferDataProtoc) - } - - // @@protoc_insertion_point(class_scope:org.nico.ratel.landlords.entity.ServerTransferDataProtoc) - private static final org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc DEFAULT_INSTANCE; - static { - DEFAULT_INSTANCE = new org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc(); - } - - public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc getDefaultInstance() { - return DEFAULT_INSTANCE; - } - - private static final com.google.protobuf.Parser - PARSER = new com.google.protobuf.AbstractParser() { - @java.lang.Override - public ServerTransferDataProtoc parsePartialFrom( - com.google.protobuf.CodedInputStream input, - com.google.protobuf.ExtensionRegistryLite extensionRegistry) - throws com.google.protobuf.InvalidProtocolBufferException { - return new ServerTransferDataProtoc(input, extensionRegistry); - } - }; - - public static com.google.protobuf.Parser parser() { - return PARSER; - } - - @java.lang.Override - public com.google.protobuf.Parser getParserForType() { - return PARSER; - } - - @java.lang.Override - public org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc getDefaultInstanceForType() { - return DEFAULT_INSTANCE; - } - - } - - private static final com.google.protobuf.Descriptors.Descriptor - internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_descriptor; - private static final - com.google.protobuf.GeneratedMessageV3.FieldAccessorTable - internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_fieldAccessorTable; - - public static com.google.protobuf.Descriptors.FileDescriptor - getDescriptor() { - return descriptor; - } - private static com.google.protobuf.Descriptors.FileDescriptor - descriptor; - static { - java.lang.String[] descriptorData = { - "\n\036ServerTransferDataProtoc.proto\022\037org.ni" + - "co.ratel.landlords.entity\"D\n\030ServerTrans" + - "ferDataProtoc\022\014\n\004code\030\001 \001(\t\022\014\n\004data\030\002 \001(" + - "\t\022\014\n\004info\030\003 \001(\tB5\n\037org.nico.ratel.landlo" + - "rds.entityB\022ServerTransferDatab\006proto3" - }; - com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = - new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() { - public com.google.protobuf.ExtensionRegistry assignDescriptors( - com.google.protobuf.Descriptors.FileDescriptor root) { - descriptor = root; - return null; - } - }; - com.google.protobuf.Descriptors.FileDescriptor - .internalBuildGeneratedFileFrom(descriptorData, - new com.google.protobuf.Descriptors.FileDescriptor[] { - }, assigner); - internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_descriptor = - getDescriptor().getMessageTypes().get(0); - internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_fieldAccessorTable = new - com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( - internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_descriptor, - new java.lang.String[] { "Code", "Data", "Info", }); - } - - // @@protoc_insertion_point(outer_class_scope) + private ServerTransferData() { + } + + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistryLite registry) { + } + + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistry registry) { + registerAllExtensions( + (com.google.protobuf.ExtensionRegistryLite) registry); + } + + public interface ServerTransferDataProtocOrBuilder extends + // @@protoc_insertion_point(interface_extends:org.nico.ratel.landlords.entity.ServerTransferDataProtoc) + com.google.protobuf.MessageOrBuilder { + + /** + * string code = 1; + */ + java.lang.String getCode(); + + /** + * string code = 1; + */ + com.google.protobuf.ByteString + getCodeBytes(); + + /** + * string data = 2; + */ + java.lang.String getData(); + + /** + * string data = 2; + */ + com.google.protobuf.ByteString + getDataBytes(); + + /** + * string info = 3; + */ + java.lang.String getInfo(); + + /** + * string info = 3; + */ + com.google.protobuf.ByteString + getInfoBytes(); + } + + /** + * Protobuf type {@code org.nico.ratel.landlords.entity.ServerTransferDataProtoc} + */ + public static final class ServerTransferDataProtoc extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:org.nico.ratel.landlords.entity.ServerTransferDataProtoc) + ServerTransferDataProtocOrBuilder { + private static final long serialVersionUID = 0L; + + // Use ServerTransferDataProtoc.newBuilder() to construct. + private ServerTransferDataProtoc(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + + private ServerTransferDataProtoc() { + code_ = ""; + data_ = ""; + info_ = ""; + } + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + + private ServerTransferDataProtoc( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + if (extensionRegistry == null) { + throw new java.lang.NullPointerException(); + } + int mutable_bitField0_ = 0; + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + case 10: { + java.lang.String s = input.readStringRequireUtf8(); + + code_ = s; + break; + } + case 18: { + java.lang.String s = input.readStringRequireUtf8(); + + data_ = s; + break; + } + case 26: { + java.lang.String s = input.readStringRequireUtf8(); + + info_ = s; + break; + } + default: { + if (!parseUnknownFieldProto3( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return org.nico.ratel.landlords.entity.ServerTransferData.internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return org.nico.ratel.landlords.entity.ServerTransferData.internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_fieldAccessorTable + .ensureFieldAccessorsInitialized( + org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc.class, org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc.Builder.class); + } + + public static final int CODE_FIELD_NUMBER = 1; + private volatile java.lang.Object code_; + + /** + * string code = 1; + */ + public java.lang.String getCode() { + java.lang.Object ref = code_; + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + code_ = s; + return s; + } + } + + /** + * string code = 1; + */ + public com.google.protobuf.ByteString + getCodeBytes() { + java.lang.Object ref = code_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + code_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + public static final int DATA_FIELD_NUMBER = 2; + private volatile java.lang.Object data_; + + /** + * string data = 2; + */ + public java.lang.String getData() { + java.lang.Object ref = data_; + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + data_ = s; + return s; + } + } + + /** + * string data = 2; + */ + public com.google.protobuf.ByteString + getDataBytes() { + java.lang.Object ref = data_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + data_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + public static final int INFO_FIELD_NUMBER = 3; + private volatile java.lang.Object info_; + + /** + * string info = 3; + */ + public java.lang.String getInfo() { + java.lang.Object ref = info_; + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + info_ = s; + return s; + } + } + + /** + * string info = 3; + */ + public com.google.protobuf.ByteString + getInfoBytes() { + java.lang.Object ref = info_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + info_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + private byte memoizedIsInitialized = -1; + + @java.lang.Override + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + @java.lang.Override + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (!getCodeBytes().isEmpty()) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 1, code_); + } + if (!getDataBytes().isEmpty()) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 2, data_); + } + if (!getInfoBytes().isEmpty()) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 3, info_); + } + unknownFields.writeTo(output); + } + + @java.lang.Override + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (!getCodeBytes().isEmpty()) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, code_); + } + if (!getDataBytes().isEmpty()) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, data_); + } + if (!getInfoBytes().isEmpty()) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, info_); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @java.lang.Override + public boolean equals(final java.lang.Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc)) { + return super.equals(obj); + } + org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc other = (org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc) obj; + + boolean result = true; + result = result && getCode() + .equals(other.getCode()); + result = result && getData() + .equals(other.getData()); + result = result && getInfo() + .equals(other.getInfo()); + result = result && unknownFields.equals(other.unknownFields); + return result; + } + + @java.lang.Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + hash = (37 * hash) + CODE_FIELD_NUMBER; + hash = (53 * hash) + getCode().hashCode(); + hash = (37 * hash) + DATA_FIELD_NUMBER; + hash = (53 * hash) + getData().hashCode(); + hash = (37 * hash) + INFO_FIELD_NUMBER; + hash = (53 * hash) + getInfo().hashCode(); + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + + public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + + public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + + public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + + public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + + public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + + public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + + public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + + public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + + public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + + public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + @java.lang.Override + public Builder newBuilderForType() { + return newBuilder(); + } + + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + + public static Builder newBuilder(org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + + @java.lang.Override + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + + /** + * Protobuf type {@code org.nico.ratel.landlords.entity.ServerTransferDataProtoc} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:org.nico.ratel.landlords.entity.ServerTransferDataProtoc) + org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtocOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return org.nico.ratel.landlords.entity.ServerTransferData.internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_descriptor; + } + + @java.lang.Override + protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internalGetFieldAccessorTable() { + return org.nico.ratel.landlords.entity.ServerTransferData.internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_fieldAccessorTable + .ensureFieldAccessorsInitialized( + org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc.class, org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc.Builder.class); + } + + // Construct using org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + } + } + + @java.lang.Override + public Builder clear() { + super.clear(); + code_ = ""; + + data_ = ""; + + info_ = ""; + + return this; + } + + @java.lang.Override + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return org.nico.ratel.landlords.entity.ServerTransferData.internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_descriptor; + } + + @java.lang.Override + public org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc getDefaultInstanceForType() { + return org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc.getDefaultInstance(); + } + + @java.lang.Override + public org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc build() { + org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + @java.lang.Override + public org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc buildPartial() { + org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc result = new org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc(this); + result.code_ = code_; + result.data_ = data_; + result.info_ = info_; + onBuilt(); + return result; + } + + @java.lang.Override + public Builder clone() { + return (Builder) super.clone(); + } + + @java.lang.Override + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return (Builder) super.setField(field, value); + } + + @java.lang.Override + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return (Builder) super.clearField(field); + } + + @java.lang.Override + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return (Builder) super.clearOneof(oneof); + } + + @java.lang.Override + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, java.lang.Object value) { + return (Builder) super.setRepeatedField(field, index, value); + } + + @java.lang.Override + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + java.lang.Object value) { + return (Builder) super.addRepeatedField(field, value); + } + + @java.lang.Override + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc) { + return mergeFrom((org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc) other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc other) { + if (other == org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc.getDefaultInstance()) + return this; + if (!other.getCode().isEmpty()) { + code_ = other.code_; + onChanged(); + } + if (!other.getData().isEmpty()) { + data_ = other.data_; + onChanged(); + } + if (!other.getInfo().isEmpty()) { + info_ = other.info_; + onChanged(); + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + @java.lang.Override + public final boolean isInitialized() { + return true; + } + + @java.lang.Override + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private java.lang.Object code_ = ""; + + /** + * string code = 1; + */ + public java.lang.String getCode() { + java.lang.Object ref = code_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + code_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + + /** + * string code = 1; + */ + public com.google.protobuf.ByteString + getCodeBytes() { + java.lang.Object ref = code_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + code_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + /** + * string code = 1; + */ + public Builder setCode( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + code_ = value; + onChanged(); + return this; + } + + /** + * string code = 1; + */ + public Builder clearCode() { + + code_ = getDefaultInstance().getCode(); + onChanged(); + return this; + } + + /** + * string code = 1; + */ + public Builder setCodeBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + code_ = value; + onChanged(); + return this; + } + + private java.lang.Object data_ = ""; + + /** + * string data = 2; + */ + public java.lang.String getData() { + java.lang.Object ref = data_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + data_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + + /** + * string data = 2; + */ + public com.google.protobuf.ByteString + getDataBytes() { + java.lang.Object ref = data_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + data_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + /** + * string data = 2; + */ + public Builder setData( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + data_ = value; + onChanged(); + return this; + } + + /** + * string data = 2; + */ + public Builder clearData() { + + data_ = getDefaultInstance().getData(); + onChanged(); + return this; + } + + /** + * string data = 2; + */ + public Builder setDataBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + data_ = value; + onChanged(); + return this; + } + + private java.lang.Object info_ = ""; + + /** + * string info = 3; + */ + public java.lang.String getInfo() { + java.lang.Object ref = info_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + info_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + + /** + * string info = 3; + */ + public com.google.protobuf.ByteString + getInfoBytes() { + java.lang.Object ref = info_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + info_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + /** + * string info = 3; + */ + public Builder setInfo( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + info_ = value; + onChanged(); + return this; + } + + /** + * string info = 3; + */ + public Builder clearInfo() { + + info_ = getDefaultInstance().getInfo(); + onChanged(); + return this; + } + + /** + * string info = 3; + */ + public Builder setInfoBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + info_ = value; + onChanged(); + return this; + } + + @java.lang.Override + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFieldsProto3(unknownFields); + } + + @java.lang.Override + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:org.nico.ratel.landlords.entity.ServerTransferDataProtoc) + } + + // @@protoc_insertion_point(class_scope:org.nico.ratel.landlords.entity.ServerTransferDataProtoc) + private static final org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc DEFAULT_INSTANCE; + + static { + DEFAULT_INSTANCE = new org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc(); + } + + public static org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + @java.lang.Override + public ServerTransferDataProtoc parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new ServerTransferDataProtoc(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + @java.lang.Override + public org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + + } + + private static final com.google.protobuf.Descriptors.Descriptor + internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_descriptor; + private static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_fieldAccessorTable; + + public static com.google.protobuf.Descriptors.FileDescriptor + getDescriptor() { + return descriptor; + } + + private static com.google.protobuf.Descriptors.FileDescriptor + descriptor; + + static { + java.lang.String[] descriptorData = { + "\n\036ServerTransferDataProtoc.proto\022\037org.ni" + + "co.ratel.landlords.entity\"D\n\030ServerTrans" + + "ferDataProtoc\022\014\n\004code\030\001 \001(\t\022\014\n\004data\030\002 \001(" + + "\t\022\014\n\004info\030\003 \001(\tB5\n\037org.nico.ratel.landlo" + + "rds.entityB\022ServerTransferDatab\006proto3" + }; + com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = + new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() { + public com.google.protobuf.ExtensionRegistry assignDescriptors( + com.google.protobuf.Descriptors.FileDescriptor root) { + descriptor = root; + return null; + } + }; + com.google.protobuf.Descriptors.FileDescriptor + .internalBuildGeneratedFileFrom(descriptorData, + new com.google.protobuf.Descriptors.FileDescriptor[]{ + }, assigner); + internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_descriptor = + getDescriptor().getMessageTypes().get(0); + internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_org_nico_ratel_landlords_entity_ServerTransferDataProtoc_descriptor, + new java.lang.String[]{"Code", "Data", "Info",}); + } + + // @@protoc_insertion_point(outer_class_scope) } 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 93dd6ec..9df5fa5 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 @@ -2,70 +2,70 @@ import java.io.Serializable; -public enum ClientEventCode implements Serializable{ +public enum ClientEventCode implements Serializable { CODE_CLIENT_NICKNAME_SET("设置昵称"), - + CODE_CLIENT_EXIT("客户端退出"), - + CODE_CLIENT_KICK("客户端被踢出"), - + CODE_CLIENT_CONNECT("客户端加入成功"), - + CODE_SHOW_OPTIONS("全局选项列表"), - + CODE_SHOW_OPTIONS_SETTING("设置选项"), - + CODE_SHOW_OPTIONS_PVP("玩家对战选项"), - + CODE_SHOW_OPTIONS_PVE("人机对战选项"), - + CODE_SHOW_ROOMS("展示房间列表"), - + CODE_SHOW_POKERS("展示Poker"), - + CODE_ROOM_CREATE_SUCCESS("创建房间成功"), - + CODE_ROOM_JOIN_SUCCESS("加入房间成功"), - + CODE_ROOM_JOIN_FAIL_BY_FULL("房间人数已满"), - + CODE_ROOM_JOIN_FAIL_BY_INEXIST("加入-房间不存在"), - + CODE_ROOM_PLAY_FAIL_BY_INEXIST1("出牌-房间不存在"), - + CODE_GAME_STARTING("开始游戏"), - + CODE_GAME_LANDLORD_ELECT("抢地主"), - + CODE_GAME_LANDLORD_CONFIRM("地主确认"), - + CODE_GAME_LANDLORD_CYCLE("地主一轮确认结束"), - + CODE_GAME_POKER_PLAY("出牌回合"), - + CODE_GAME_POKER_PLAY_REDIRECT("出牌重定向"), - + CODE_GAME_POKER_PLAY_MISMATCH("出牌不匹配"), - + CODE_GAME_POKER_PLAY_LESS("出牌太小"), - + 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("游戏结束"), - + CODE_PVE_DIFFICULTY_NOT_SUPPORT("人机难度不支持"), CODE_GAME_WATCH("观战"), CODE_GAME_WATCH_SUCCESSFUL("观战成功"); - + private String msg; private ClientEventCode(String msg) { @@ -79,5 +79,5 @@ public final String getMsg() { public final void setMsg(String msg) { this.msg = msg; } - + } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientRole.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientRole.java index d25ce81..38ba214 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientRole.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientRole.java @@ -1,9 +1,9 @@ package org.nico.ratel.landlords.enums; -public enum ClientRole{ +public enum ClientRole { PLAYER, - + ROBOT - + } 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 9b70da8..efbf92e 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 @@ -1,18 +1,18 @@ package org.nico.ratel.landlords.enums; -public enum ClientStatus{ +public enum ClientStatus { TO_CHOOSE, - + NO_READY, - + READY, - + WAIT, - + CALL_LANDLORD, - + PLAYING - - + + } 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 241f11b..868a901 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 @@ -1,9 +1,9 @@ package org.nico.ratel.landlords.enums; -public enum ClientType{ +public enum ClientType { LANDLORD, - + PEASANT - + } 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 f1cbb35..e2f96a4 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 @@ -6,56 +6,56 @@ /** * Poker level - * + * * @author nico */ -public enum PokerLevel{ +public enum PokerLevel { LEVEL_3(3, "3", new Character[]{'3'}), - + LEVEL_4(4, "4", new Character[]{'4'}), - + LEVEL_5(5, "5", new Character[]{'5'}), - + LEVEL_6(6, "6", new Character[]{'6'}), - + LEVEL_7(7, "7", new Character[]{'7'}), - + LEVEL_8(8, "8", new Character[]{'8'}), - + LEVEL_9(9, "9", new Character[]{'9'}), - + LEVEL_10(10, "10", new Character[]{'T', 't', '0'}), - + LEVEL_J(11, "J", new Character[]{'J', 'j'}), - + LEVEL_Q(12, "Q", new Character[]{'Q', 'q'}), - + LEVEL_K(13, "K", new Character[]{'K', 'k'}), - + LEVEL_A(14, "A", new Character[]{'A', 'a', '1'}), - + LEVEL_2(15, "2", new Character[]{'2'}), - + LEVEL_SMALL_KING(16, "S", new Character[]{'S', 's'}), - + LEVEL_BIG_KING(17, "X", new Character[]{'X', 'x'}), ; - + private int level; - + private String name; - + private Character[] alias; - + private static Set aliasSet = new HashSet<>(); - + static { - for(PokerLevel level: PokerLevel.values()) { + for (PokerLevel level : PokerLevel.values()) { PokerLevel.aliasSet.addAll(Arrays.asList(level.getAlias())); } } - + private PokerLevel(int level, String name, Character[] alias) { this.level = level; this.name = name; 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 a3a7f97..fd6dc6a 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 @@ -2,25 +2,24 @@ /** * Poker type Spade、 Heart、 Diamond、 Club - * + * * @author nico */ -public enum PokerType{ +public enum PokerType { BLANK(" "), - + DIAMOND("♦"), - + CLUB("♣"), - + SPADE("♠"), - - HEART("♥") - ; - - private String name; - private PokerType(String name) { + HEART("♥"); + + private final String name; + + PokerType(String name) { this.name = name; } 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 486c05c..68f5b2a 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 @@ -1,19 +1,19 @@ package org.nico.ratel.landlords.enums; -public enum RoomStatus{ +public enum RoomStatus { BLANK("空闲"), - + WAIT("等待"), - + STARTING("开始"), - - + + ; - + private String msg; - private RoomStatus(String msg) { + RoomStatus(String msg) { this.msg = msg; } @@ -24,5 +24,5 @@ public final String getMsg() { public final void setMsg(String msg) { this.msg = msg; } - + } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomType.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomType.java index badd5f7..601aa73 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomType.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomType.java @@ -1,15 +1,15 @@ package org.nico.ratel.landlords.enums; -public enum RoomType{ +public enum RoomType { PVP("玩家对战"), - + PVE("人机对战"), - + ; private String msg; - private RoomType(String msg) { + RoomType(String msg) { this.msg = msg; } @@ -20,5 +20,5 @@ public final String getMsg() { public final void setMsg(String msg) { this.msg = msg; } - + } 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 6d19efb..fc41b35 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 @@ -3,45 +3,45 @@ public enum SellType { ILLEGAL("非合法"), - + BOMB("炸弹"), - + KING_BOMB("王炸"), - + SINGLE("单个牌"), - + DOUBLE("对子牌"), - + THREE("三张牌"), - + THREE_ZONES_SINGLE("三带单"), - + THREE_ZONES_DOUBLE("三带对"), - + FOUR_ZONES_SINGLE("四带单"), - + FOUR_ZONES_DOUBLE("四带对"), - + SINGLE_STRAIGHT("单顺子"), - + DOUBLE_STRAIGHT("双顺子"), - + THREE_STRAIGHT("三顺子"), - + FOUR_STRAIGHT("四顺子"), - + THREE_STRAIGHT_WITH_SINGLE("飞机带单牌"), - + THREE_STRAIGHT_WITH_DOUBLE("飞机带对牌"), - + FOUR_STRAIGHT_WITH_SINGLE("四顺子带单"), - + FOUR_STRAIGHT_WITH_DOUBLE("四顺子带对"), ; - + private String msg; - private SellType(String msg) { + SellType(String msg) { this.msg = msg; } @@ -52,5 +52,5 @@ public final String getMsg() { public final void setMsg(String msg) { this.msg = msg; } - + } 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 6f72e71..2d3dc8c 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 @@ -2,41 +2,41 @@ import java.io.Serializable; -public enum ServerEventCode implements Serializable{ +public enum ServerEventCode implements Serializable { CODE_CLIENT_EXIT("玩家退出"), - + CODE_CLIENT_OFFLINE("玩家离线"), - + CODE_CLIENT_NICKNAME_SET("设置昵称"), - + CODE_CLIENT_HEAD_BEAT("不出"), - + CODE_ROOM_CREATE("创建PVP房间"), - + CODE_ROOM_CREATE_PVE("创建PVE房间"), - + CODE_GET_ROOMS("获取房间列表"), CODE_ROOM_JOIN("加入房间"), - + CODE_GAME_STARTING("游戏开始"), - + CODE_GAME_LANDLORD_ELECT("抢地主"), - + CODE_GAME_POKER_PLAY("出牌环节"), - + CODE_GAME_POKER_PLAY_REDIRECT("出牌重定向"), - + CODE_GAME_POKER_PLAY_PASS("不出"), CODE_GAME_WATCH("观战"), CODE_GAME_WATCH_EXIT("退出观战"); - + private String msg; - private ServerEventCode(String msg) { + ServerEventCode(String msg) { this.msg = msg; } @@ -47,5 +47,5 @@ public final String getMsg() { public final void setMsg(String msg) { this.msg = msg; } - + } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/exception/LandlordException.java b/landlords-common/src/main/java/org/nico/ratel/landlords/exception/LandlordException.java index 20a7eb3..2643a2a 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/exception/LandlordException.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/exception/LandlordException.java @@ -1,6 +1,6 @@ package org.nico.ratel.landlords.exception; -public class LandlordException extends RuntimeException{ +public class LandlordException extends RuntimeException { private static final long serialVersionUID = -5643145833569293539L; diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/handler/DefaultDecoder.java b/landlords-common/src/main/java/org/nico/ratel/landlords/handler/DefaultDecoder.java index 3d0b8e6..60e2ef1 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/handler/DefaultDecoder.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/handler/DefaultDecoder.java @@ -8,15 +8,15 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; -public class DefaultDecoder extends ByteToMessageDecoder{ +public class DefaultDecoder extends ByteToMessageDecoder { @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { - int startIndex = -1; - int endIndex = -1; - if((startIndex = in.indexOf(in.readerIndex(), in.writerIndex(), TransferProtocolUtils.PROTOCOL_HAED)) != -1 && + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) { + int startIndex; + int endIndex; + if ((startIndex = in.indexOf(in.readerIndex(), in.writerIndex(), TransferProtocolUtils.PROTOCOL_HAED)) != -1 && (endIndex = in.indexOf(startIndex + 1, in.writerIndex(), TransferProtocolUtils.PROTOCOL_TAIL)) != -1) { - endIndex ++; + endIndex++; byte[] bytes = new byte[endIndex - startIndex]; in.skipBytes(startIndex - in.readerIndex()); in.readBytes(bytes, 0, bytes.length); 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 index 5c5e72b..84ce706 100644 --- 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 @@ -7,20 +7,20 @@ public class MapHelper { - private Map data; - + private final 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; @@ -29,9 +29,9 @@ public MapHelper put(String name, Object Object) { public String json() { return Noson.reversal(data); } - + public Map map() { return data; } - + } 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 ca68994..74c3a89 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 @@ -18,39 +18,34 @@ public class PokerHelper { /** - * Print the type of poker style + * Print the type of poker style */ public static int pokerPrinterType = 0; public static int totalPrinters = 5; - + /** * The list of all pokers, by 54 */ - private static List basePokers = new ArrayList(54); + private static final List basePokers = new ArrayList<>(54); - private static Comparator pokerComparator = new Comparator() { - @Override - public int compare(Poker o1, Poker o2) { - return o1.getLevel().getLevel() - o2.getLevel().getLevel(); - } - }; + private static final Comparator pokerComparator = (o1, o2) -> o1.getLevel().getLevel() - o2.getLevel().getLevel(); static { PokerLevel[] pokerLevels = PokerLevel.values(); PokerType[] pokerTypes = PokerType.values(); - for(PokerLevel level: pokerLevels) { - if(level == PokerLevel.LEVEL_BIG_KING) { + for (PokerLevel level : pokerLevels) { + if (level == PokerLevel.LEVEL_BIG_KING) { basePokers.add(new Poker(level, PokerType.BLANK)); continue; } - if(level == PokerLevel.LEVEL_SMALL_KING) { + if (level == PokerLevel.LEVEL_SMALL_KING) { basePokers.add(new Poker(level, PokerType.BLANK)); continue; } - for(PokerType type: pokerTypes) { - if(type == PokerType.BLANK) { + for (PokerType type : pokerTypes) { + if (type == PokerType.BLANK) { continue; } basePokers.add(new Poker(level, type)); diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/helper/TimeHelper.java b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/TimeHelper.java index effbce4..fa076ab 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/helper/TimeHelper.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/helper/TimeHelper.java @@ -8,10 +8,10 @@ public class TimeHelper { - public static void sleep(long millis){ + public static void sleep(long millis) { try { Thread.sleep(millis); - } catch (InterruptedException e) { + } catch (InterruptedException ignored) { } } } 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 9a77a1b..5127458 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 @@ -11,21 +11,21 @@ public class SimplePrinter { - private final static SimpleDateFormat FORMAT = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); + private final static SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static int pokerDisplayFormat = 0; - + public static void printPokers(List pokers) { System.out.println(PokerHelper.printPoker(pokers)); } - + public static void printNotice(String msg) { System.out.println(msg); } - + public static void printNotice(String msgKey, String locale) { //TODO : read locale - Map> map = new HashMap>(); - map.put("english", new HashMap()); + Map> map = new HashMap<>(); + map.put("english", new HashMap<>()); map.get("eng").put("caterpillar", "caterpillar's message!!"); System.out.println(map.get(locale).get(msgKey)); 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 1d6b7de..68d41b7 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 @@ -6,14 +6,14 @@ public class SimpleWriter { - private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); - + private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + public static String write(String message) { System.out.println(); System.out.print("[ratel@" + message + "]$ "); try { return write(); - }finally { + } finally { System.out.println(); } } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/AbstractRobotDecisionMakers.java b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/AbstractRobotDecisionMakers.java index 62cb0b3..316a2c5 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/AbstractRobotDecisionMakers.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/AbstractRobotDecisionMakers.java @@ -12,7 +12,7 @@ * @version createTime:2018年11月15日 上午12:12:15 */ -public abstract class AbstractRobotDecisionMakers { +public abstract class AbstractRobotDecisionMakers { public abstract PokerSell howToPlayPokers(PokerSell lastPokerSell, ClientSide robot); diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/EasyRobotDecisionMakers.java b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/EasyRobotDecisionMakers.java index ac7da58..3bc18d5 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/EasyRobotDecisionMakers.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/EasyRobotDecisionMakers.java @@ -9,42 +9,41 @@ import org.nico.ratel.landlords.enums.SellType; import org.nico.ratel.landlords.helper.PokerHelper; -/** - * +/** * @author nico * @date 2018-11-15 12:13:49 */ -public class EasyRobotDecisionMakers extends AbstractRobotDecisionMakers{ +public class EasyRobotDecisionMakers extends AbstractRobotDecisionMakers { private static Random random = new Random(); - + @Override public PokerSell howToPlayPokers(PokerSell lastPokerSell, ClientSide robot) { - if(lastPokerSell != null && lastPokerSell.getSellType() == SellType.KING_BOMB) { + if (lastPokerSell != null && lastPokerSell.getSellType() == SellType.KING_BOMB) { return null; } - + List sells = PokerHelper.parsePokerSells(robot.getPokers()); - if(lastPokerSell == null) { + if (lastPokerSell == null) { return sells.get(random.nextInt(sells.size())); } - - for(PokerSell sell: sells) { - if(sell.getSellType() == lastPokerSell.getSellType()) { - if(sell.getScore() > lastPokerSell.getScore() && sell.getSellPokers().size() == lastPokerSell.getSellPokers().size()) { + + for (PokerSell sell : sells) { + if (sell.getSellType() == lastPokerSell.getSellType()) { + if (sell.getScore() > lastPokerSell.getScore() && sell.getSellPokers().size() == lastPokerSell.getSellPokers().size()) { return sell; } } } - if(lastPokerSell.getSellType() != SellType.BOMB) { - for(PokerSell sell: sells) { - if(sell.getSellType() == SellType.BOMB) { + if (lastPokerSell.getSellType() != SellType.BOMB) { + for (PokerSell sell : sells) { + if (sell.getSellType() == SellType.BOMB) { return sell; } } } - for(PokerSell sell: sells) { - if(sell.getSellType() == SellType.KING_BOMB) { + for (PokerSell sell : sells) { + if (sell.getSellType() == SellType.KING_BOMB) { return sell; } } @@ -58,6 +57,4 @@ public boolean howToChooseLandlord(List leftPokers, List rightPoke List rightSells = PokerHelper.parsePokerSells(rightPokers); return mySells.size() > leftSells.size() && mySells.size() > rightSells.size(); } - - } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/MediumRobotDecisionMakers.java b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/MediumRobotDecisionMakers.java index e2627e1..959f497 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/MediumRobotDecisionMakers.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/MediumRobotDecisionMakers.java @@ -17,7 +17,7 @@ * @author nico * @date 2020-12-19 16:36 */ -public class MediumRobotDecisionMakers extends AbstractRobotDecisionMakers{ +public class MediumRobotDecisionMakers extends AbstractRobotDecisionMakers { private static final Long DEDUCE_LIMIT = 100 * 3L; @@ -133,7 +133,7 @@ public boolean howToChooseLandlord(List leftPokers, List rightPoke int leftScore = PokerHelper.parsePokerColligationScore(leftPokers); int rightScore = PokerHelper.parsePokerColligationScore(rightPokers); int myScore = PokerHelper.parsePokerColligationScore(myPokers); - return myScore >= (leftScore + rightScore)/2; + return myScore >= (leftScore + rightScore) / 2; } } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/RobotDecisionMakers.java b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/RobotDecisionMakers.java index 936c996..d96d167 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/RobotDecisionMakers.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/RobotDecisionMakers.java @@ -29,9 +29,9 @@ public static boolean contains(int difficultyCoefficient) { public static PokerSell howToPlayPokers(int difficultyCoefficient, PokerSell lastPokerSell, ClientSide robot){ return decisionMakersMap.get(difficultyCoefficient).howToPlayPokers(lastPokerSell, robot); } - + public static boolean howToChooseLandlord(int difficultyCoefficient, List leftPokers, List rightPokers, List myPokers) { return decisionMakersMap.get(difficultyCoefficient).howToChooseLandlord(leftPokers, rightPokers, myPokers); } - + } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/ByteKit.java b/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/ByteKit.java index 5fc301d..df827ad 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/ByteKit.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/ByteKit.java @@ -2,7 +2,7 @@ /** * Byte manipulation tool - * + * * @author nico */ public class ByteKit { @@ -18,27 +18,27 @@ public ByteKit(byte[] bytes) { /** * Gets the index of the incoming array in the target array, not matched to return -1 - * - * @param bs Incoming array + * + * @param bs Incoming array * @param start Matching start index * @return Match index, not match to return -1 */ public int indexOf(byte[] bs, int start) { int targetIndex = -1; - if(bs == null) { - return targetIndex; + if (bs == null) { + return targetIndex; } - for(int index = start; index < bytes.length; index ++) { + for (int index = start; index < bytes.length; index++) { byte cbyte = bytes[index]; - if(bs[0] == cbyte) { + if (bs[0] == cbyte) { boolean isEquals = true; - for(int sindex = 1; sindex < bs.length; sindex ++) { - if(index + sindex >= bytes.length || bs[sindex] != bytes[index + sindex]) { + for (int sindex = 1; sindex < bs.length; sindex++) { + if (index + sindex >= bytes.length || bs[sindex] != bytes[index + sindex]) { isEquals = false; break; } } - if(isEquals) { + if (isEquals) { targetIndex = index; break; } @@ -46,15 +46,15 @@ public int indexOf(byte[] bs, int start) { } return targetIndex; } - + /** * Gets the position of the byte byte in the byte array - * - * @param b Byte + * + * @param b Byte * @param start Matching start index * @return Match index, not match to return -1 */ public int indexOf(byte b, int start) { - return indexOf(new byte[] {b}, start); + return indexOf(new byte[]{b}, start); } } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/ByteLink.java b/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/ByteLink.java index 06df462..d069672 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/ByteLink.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/ByteLink.java @@ -1,63 +1,63 @@ package org.nico.ratel.landlords.transfer; -public class ByteLink{ - +public class ByteLink { + private ByteNode start; - + private ByteNode current; - + private int size; - - public void append(byte b){ - if(start == null){ + + public void append(byte b) { + if (start == null) { start = new ByteNode(b); current = start; - }else{ + } else { ByteNode node = new ByteNode(b); current.setNext(node); current = node; } - size ++; + size++; } - - public void append(byte[] bs){ - if(bs != null) { - for(byte b: bs) { + + public void append(byte[] bs) { + if (bs != null) { + for (byte b : bs) { append(b); } } } - - public byte[] toArray(){ - if(size == 0){ + + public byte[] toArray() { + if (size == 0) { return null; } byte[] bytes = new byte[size]; int index = 0; ByteNode s = start.clone(); - while(s != null){ - bytes[index ++] = s.getB(); + while (s != null) { + bytes[index++] = s.getB(); s = s.getNext(); } return bytes; } - - public static class ByteNode{ - + + public static class ByteNode { + private byte b; - + private ByteNode next; - + public ByteNode(byte b) { this.b = b; } - + public ByteNode(byte b, ByteNode next) { this.b = b; this.next = next; } - - protected ByteNode clone(){ + + protected ByteNode clone() { return new ByteNode(b, next); } @@ -76,6 +76,6 @@ public ByteNode getNext() { public void setNext(ByteNode next) { this.next = next; } - + } } \ No newline at end of file diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/TransferProtocolUtils.java b/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/TransferProtocolUtils.java index b255f39..ec07a5c 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/TransferProtocolUtils.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/TransferProtocolUtils.java @@ -5,13 +5,13 @@ /** * Protocol transport related tools - * + * * @author nico * @time 2018-11-01 20:43 */ public class TransferProtocolUtils { - - + + /** * A protocol header that represents the beginning of an available stream of data */ @@ -24,8 +24,8 @@ public class TransferProtocolUtils { /** * Serialize the poker list to transportable bytes - * - * @param pokers Poker list + * + * @param obj Poker list * @return Transportable byte array */ public static byte[] serialize(Object obj) { @@ -38,7 +38,7 @@ public static byte[] serialize(Object obj) { /** * Deserialize the byte stream as an object - * + * * @param bytes Byte array * @return Genericity */ @@ -46,20 +46,20 @@ public static T unserialize(byte[] bytes, Class clazz) { ByteKit bk = new ByteKit(bytes); int start = -1; int end = -1; - + int index = bk.indexOf(PROTOCOL_HAED, 0); - if(index != -1) start = index + 1; - + if (index != -1) start = index + 1; + index = bk.indexOf(PROTOCOL_TAIL, 0); - if(index != -1) end = index; - - if(start != -1 && end != -1 && start > end) { + if (index != -1) end = index; + + if (start != -1 && end != -1 && start > end) { throw new LandlordException("Message format error, head and tail error."); - }else { + } else { byte[] content = new byte[end - start]; System.arraycopy(bytes, start, content, 0, content.length); return Noson.convert(new String(content), clazz); } } - + } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/utils/ListUtils.java b/landlords-common/src/main/java/org/nico/ratel/landlords/utils/ListUtils.java index 5e0a47f..35ae406 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/utils/ListUtils.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/utils/ListUtils.java @@ -1,27 +1,26 @@ package org.nico.ratel.landlords.utils; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class ListUtils { - public static List getList(T[] array){ + public static List getList(T[] array) { List list = new ArrayList<>(array.length); - for(T t: array) { - list.add(t); - } + Collections.addAll(list, array); return list; } - - public static List getList(List[] array){ + + public static List getList(List[] array) { List list = new ArrayList<>(array.length); - for(List t: array) { + for (List t : array) { list.addAll(t); } return list; } - - public static List getList(List source){ + + public static List getList(List source) { List list = new ArrayList<>(source.size()); list.addAll(source); return list; diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/utils/OptionsUtils.java b/landlords-common/src/main/java/org/nico/ratel/landlords/utils/OptionsUtils.java index d3c4403..20fc172 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/utils/OptionsUtils.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/utils/OptionsUtils.java @@ -5,8 +5,8 @@ public class OptionsUtils { public static int getOptions(String line) { int option = -1; try { - option = Integer.valueOf(line); - }catch(Exception e) {} + option = Integer.parseInt(line); + } catch (Exception ignored) {} return option; } } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/utils/RegxUtils.java b/landlords-common/src/main/java/org/nico/ratel/landlords/utils/RegxUtils.java index 1cb54f7..88aed6a 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/utils/RegxUtils.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/utils/RegxUtils.java @@ -3,7 +3,7 @@ public class RegxUtils { // private final static String POKER_SELECT_REGX = "^[2-9]" - + // public static isRegularPokerSelect(String line) { // 123456789t // } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/utils/StreamUtils.java b/landlords-common/src/main/java/org/nico/ratel/landlords/utils/StreamUtils.java index c3db2dc..a389a3b 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/utils/StreamUtils.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/utils/StreamUtils.java @@ -8,27 +8,26 @@ import java.net.URLConnection; public class StreamUtils { - + /** * Convert input stream to string - * + * * @param inStream Input stream * @return {@link String} - * @throws IOException If an I/O error occurs */ - public static String convertToString(InputStream inStream){ - BufferedReader br = new BufferedReader(new InputStreamReader(inStream)); - StringBuilder reqStr = new StringBuilder(); - char[] buf = new char[2048]; - int len = -1; + public static String convertToString(InputStream inStream) { + BufferedReader br = new BufferedReader(new InputStreamReader(inStream)); + StringBuilder reqStr = new StringBuilder(); + char[] buf = new char[2048]; + int len; try { - while ((len = br.read(buf)) != -1) { - reqStr.append(new String(buf, 0, len)); - } + while ((len = br.read(buf)) != -1) { + reqStr.append(new String(buf, 0, len)); + } br.close(); - }catch(IOException e) { + } catch (IOException e) { return null; - }finally { + } finally { try { br.close(); } catch (IOException e) { @@ -37,13 +36,13 @@ public static String convertToString(InputStream inStream){ } return reqStr.toString(); } - + public static String convertToString(URL url) throws IOException { URLConnection con = url.openConnection(); con.setUseCaches(false); con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36"); return convertToString(con.getInputStream()); } - - + + } 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 e982123..5b8191b 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 @@ -12,62 +12,62 @@ import org.nico.ratel.landlords.entity.Room; public class ServerContains { - + /** * Server port */ public static int port = 1024; - + /** * The map of server side */ private final static Map ROOM_MAP = new ConcurrentSkipListMap<>(); - + /** * The list of client side */ public final static Map CLIENT_SIDE_MAP = new ConcurrentSkipListMap<>(); - + public final static Map CHANNEL_ID_MAP = new ConcurrentHashMap<>(); - + private final static AtomicInteger CLIENT_ATOMIC_ID = new AtomicInteger(1); - + private final static AtomicInteger SERVER_ATOMIC_ID = new AtomicInteger(1); - + public final static int getClientId() { return CLIENT_ATOMIC_ID.getAndIncrement(); } - + public final static int getServerId() { return SERVER_ATOMIC_ID.getAndIncrement(); } - + public final static ThreadPoolExecutor THREAD_EXCUTER = new ThreadPoolExecutor(500, 500, 0, TimeUnit.MILLISECONDS, - new LinkedBlockingQueue()); - + new LinkedBlockingQueue()); + /** * Get room by id, with flush time - * + * * @param id room id * @return */ - public final static Room getRoom(int id){ + public static Room getRoom(int id) { Room room = ROOM_MAP.get(id); - if(room != null){ + if (room != null) { room.setLastFlushTime(System.currentTimeMillis()); } return room; } - - public final static Map getRoomMap(){ + + public static Map getRoomMap() { return ROOM_MAP; } - - public final static Room removeRoom(int id){ + + public static Room removeRoom(int id) { return ROOM_MAP.remove(id); } - - public final static Room addRoom(Room room){ + + public static Room addRoom(Room room) { return ROOM_MAP.put(room.getId(), room); } } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/SimpleServer.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/SimpleServer.java index 1cb389b..7b41e21 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/SimpleServer.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/SimpleServer.java @@ -6,8 +6,8 @@ public class SimpleServer { public static void main(String[] args) throws InterruptedException { - if(args != null && args.length > 1) { - if(args[0].equalsIgnoreCase("-p") || args[0].equalsIgnoreCase("-port")) { + if (args != null && args.length > 1) { + if (args[0].equalsIgnoreCase("-p") || args[0].equalsIgnoreCase("-port")) { ServerContains.port = Integer.parseInt(args[1]); } } 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 322fdd1..8892b28 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 @@ -9,19 +9,19 @@ public interface ServerEventListener { - public void call(ClientSide client, String data); + void call(ClientSide client, String data); + + Map LISTENER_MAP = new HashMap<>(); + + String LISTENER_PREFIX = "org.nico.ratel.landlords.server.event.ServerEventListener_"; - public final static Map LISTENER_MAP = new HashMap<>(); - - final static String LISTENER_PREFIX = "org.nico.ratel.landlords.server.event.ServerEventListener_"; - @SuppressWarnings("unchecked") - public static ServerEventListener get(ServerEventCode code){ + static ServerEventListener get(ServerEventCode code) { ServerEventListener listener = null; try { - if(ServerEventListener.LISTENER_MAP.containsKey(code)){ + if (ServerEventListener.LISTENER_MAP.containsKey(code)) { listener = ServerEventListener.LISTENER_MAP.get(code); - }else{ + } else { String eventListener = LISTENER_PREFIX + code.name(); Class listenerClass = (Class) Class.forName(eventListener); try { @@ -32,10 +32,10 @@ public static ServerEventListener get(ServerEventCode code){ ServerEventListener.LISTENER_MAP.put(code, listener); } return listener; - }catch(ClassNotFoundException | InstantiationException | IllegalAccessException e) { + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { e.printStackTrace(); } - return listener; + 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 badc3c5..3a7f4b0 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 @@ -8,7 +8,7 @@ import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.server.ServerContains; -public class ServerEventListener_CODE_CLIENT_EXIT implements ServerEventListener{ +public class ServerEventListener_CODE_CLIENT_EXIT implements ServerEventListener { @Override public void call(ClientSide clientSide, String data) { diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java index 3c459b6..1161020 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java @@ -6,10 +6,10 @@ import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.server.ServerContains; -public class ServerEventListener_CODE_CLIENT_NICKNAME_SET implements ServerEventListener{ +public class ServerEventListener_CODE_CLIENT_NICKNAME_SET implements ServerEventListener { public static final int NICKNAME_MAX_LENGTH = 10; - + @Override public void call(ClientSide client, String nickname) { diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_OFFLINE.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_OFFLINE.java index 1864e12..38ca36a 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_OFFLINE.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_OFFLINE.java @@ -8,11 +8,11 @@ import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.server.ServerContains; -public class ServerEventListener_CODE_CLIENT_OFFLINE implements ServerEventListener{ +public class ServerEventListener_CODE_CLIENT_OFFLINE implements ServerEventListener { @Override public void call(ClientSide clientSide, String data) { - + Room room = ServerContains.getRoom(clientSide.getRoomId()); if(room != 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 1050f15..0d23fb4 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 @@ -9,7 +9,7 @@ import org.nico.ratel.landlords.server.ServerContains; import org.nico.ratel.landlords.server.robot.RobotEventListener; -public class ServerEventListener_CODE_GAME_POKER_PLAY_PASS implements ServerEventListener{ +public class ServerEventListener_CODE_GAME_POKER_PLAY_PASS implements ServerEventListener { @Override public void call(ClientSide clientSide, String data) { 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 index bd9442a..3158a0f 100644 --- 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 @@ -23,10 +23,10 @@ public void call(ClientSide clientSide, String data) { if(StringUtils.isNotBlank(data)) { datas = Noson.parseMap(data); } - + List> clientInfos = new ArrayList>(3); - for(ClientSide client: room.getClientSideList()){ - if(clientSide.getId() != client.getId()){ + for(ClientSide client : room.getClientSideList()) { + if(clientSide.getId() != client.getId()) { clientInfos.add(MapHelper.newInstance() .put("clientId", client.getId()) .put("clientNickname", client.getNickname()) @@ -36,7 +36,7 @@ public void call(ClientSide clientSide, String data) { .map()); } } - + String result = MapHelper.newInstance() .put("pokers", clientSide.getPokers()) .put("lastSellPokers", datas.get("lastSellPokers")) @@ -45,7 +45,7 @@ public void call(ClientSide clientSide, String data) { .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 0cae0eb..4e7cc71 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 @@ -13,7 +13,7 @@ import org.nico.ratel.landlords.server.ServerContains; import org.nico.ratel.landlords.server.robot.RobotEventListener; -public class ServerEventListener_CODE_GAME_STARTING implements ServerEventListener{ +public class ServerEventListener_CODE_GAME_STARTING implements ServerEventListener { @Override public void call(ClientSide clientSide, String data) { @@ -25,23 +25,23 @@ public void call(ClientSide clientSide, String data) { // Send the points of poker List> pokersList = PokerHelper.distributePoker(); int cursor = 0; - for(ClientSide client: roomClientList){ - client.setPokers(pokersList.get(cursor ++)); + for (ClientSide client : roomClientList) { + client.setPokers(pokersList.get(cursor++)); } room.setLandlordPokers(pokersList.get(3)); // Push information about the robber - int startGrabIndex = (int)(Math.random() * 3); + int startGrabIndex = (int) (Math.random() * 3); ClientSide startGrabClient = roomClientList.get(startGrabIndex); room.setCurrentSellClient(startGrabClient.getId()); - + // Push start game messages room.setStatus(RoomStatus.STARTING); // Record the first speaker room.setFirstSellClient(startGrabClient.getId()); - for(ClientSide client: roomClientList) { + for (ClientSide client : roomClientList) { client.setType(ClientType.PEASANT); String result = MapHelper.newInstance() @@ -55,10 +55,10 @@ public void call(ClientSide clientSide, String data) { .put("clientOrderList", roomClientList) .json(); - if(client.getRole() == ClientRole.PLAYER) { + if (client.getRole() == ClientRole.PLAYER) { ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_STARTING, result); - }else { - if(startGrabClient.getId() == client.getId()) { + } else { + if (startGrabClient.getId() == client.getId()) { RobotEventListener.get(ClientEventCode.CODE_GAME_LANDLORD_ELECT).call(client, result); } } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOMS.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOMS.java index a1919a9..611fdc0 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOMS.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GET_ROOMS.java @@ -13,12 +13,12 @@ import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.server.ServerContains; -public class ServerEventListener_CODE_GET_ROOMS implements ServerEventListener{ +public class ServerEventListener_CODE_GET_ROOMS implements ServerEventListener { @Override public void call(ClientSide clientSide, String data) { List> roomList = new ArrayList<>(ServerContains.getRoomMap().size()); - for(Entry entry: ServerContains.getRoomMap().entrySet()) { + for (Entry entry : ServerContains.getRoomMap().entrySet()) { Room room = entry.getValue(); roomList.add(MapHelper.newInstance() .put("roomId", room.getId()) diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java index f95d846..ecb4f02 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java @@ -9,11 +9,11 @@ import org.nico.ratel.landlords.enums.RoomType; import org.nico.ratel.landlords.server.ServerContains; -public class ServerEventListener_CODE_ROOM_CREATE 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.setType(RoomType.PVP); @@ -23,7 +23,7 @@ public void call(ClientSide clientSide, String data) { room.setCurrentSellClient(clientSide.getId()); room.setCreateTime(System.currentTimeMillis()); room.setLastFlushTime(System.currentTimeMillis()); - + clientSide.setRoomId(room.getId()); ServerContains.addRoom(room); diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java index 0f63285..012522b 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java @@ -17,31 +17,31 @@ import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleStateEvent; -public class ProtobufTransferHandler extends ChannelInboundHandlerAdapter{ +public class ProtobufTransferHandler extends ChannelInboundHandlerAdapter { @Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { Channel ch = ctx.channel(); - + //init client info ClientSide clientSide = new ClientSide(getId(ctx.channel()), ClientStatus.TO_CHOOSE, ch); clientSide.setNickname(String.valueOf(clientSide.getId())); clientSide.setRole(ClientRole.PLAYER); - + ServerContains.CLIENT_SIDE_MAP.put(clientSide.getId(), clientSide); SimplePrinter.serverLog("Has client connect 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); } - + @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - if(msg instanceof ServerTransferDataProtoc) { + if (msg instanceof ServerTransferDataProtoc) { ServerTransferDataProtoc serverTransferData = (ServerTransferDataProtoc) msg; ServerEventCode code = ServerEventCode.valueOf(serverTransferData.getCode()); - if(code != null && code != ServerEventCode.CODE_CLIENT_HEAD_BEAT) { + if (code != ServerEventCode.CODE_CLIENT_HEAD_BEAT) { ClientSide client = ServerContains.CLIENT_SIDE_MAP.get(getId(ctx.channel())); SimplePrinter.serverLog(client.getId() + " | " + client.getNickname() + " do:" + code.getMsg()); ServerEventListener.get(code).call(client, serverTransferData.getData()); @@ -51,9 +51,9 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if(cause instanceof java.io.IOException) { + if (cause instanceof java.io.IOException) { clientOfflineEvent(ctx.channel()); - }else { + } else { SimplePrinter.serverLog("ERROR:" + cause.getMessage()); cause.printStackTrace(); } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/SecondProtobufCodec.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/SecondProtobufCodec.java index 723729b..77884ab 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/SecondProtobufCodec.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/SecondProtobufCodec.java @@ -12,12 +12,12 @@ public class SecondProtobufCodec extends MessageToMessageCodec { @Override - protected void encode(ChannelHandlerContext ctx, MessageLite msg, List out) throws Exception { + protected void encode(ChannelHandlerContext ctx, MessageLite msg, List out) { out.add(msg); } @Override - protected void decode(ChannelHandlerContext ctx, ServerTransferDataProtoc msg, List out) throws Exception { + protected void decode(ChannelHandlerContext ctx, ServerTransferDataProtoc msg, List out) { out.add(msg); } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener.java index cb2079d..3985452 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener.java @@ -9,19 +9,19 @@ public interface RobotEventListener { - final static String LISTENER_PREFIX = "org.nico.ratel.landlords.server.robot.RobotEventListener_"; - - public final static Map LISTENER_MAP = new HashMap<>(); - - public void call(ClientSide robot, String data); + String LISTENER_PREFIX = "org.nico.ratel.landlords.server.robot.RobotEventListener_"; + + Map LISTENER_MAP = new HashMap<>(); + + void call(ClientSide robot, String data); @SuppressWarnings("unchecked") - public static RobotEventListener get(ClientEventCode code) { + static RobotEventListener get(ClientEventCode code) { RobotEventListener listener = null; try { - if(RobotEventListener.LISTENER_MAP.containsKey(code)){ + if (RobotEventListener.LISTENER_MAP.containsKey(code)) { listener = RobotEventListener.LISTENER_MAP.get(code); - }else{ + } else { String eventListener = LISTENER_PREFIX + code.name(); Class listenerClass = (Class) Class.forName(eventListener); try { @@ -32,10 +32,9 @@ public static RobotEventListener get(ClientEventCode code) { RobotEventListener.LISTENER_MAP.put(code, listener); } return listener; - }catch(ClassNotFoundException | InstantiationException | IllegalAccessException e) { + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { e.printStackTrace(); } - return listener; + return null; } - } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_LANDLORD_ELECT.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_LANDLORD_ELECT.java index be3bdf0..73ea3c5 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_LANDLORD_ELECT.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_LANDLORD_ELECT.java @@ -13,7 +13,7 @@ import org.nico.ratel.landlords.server.ServerContains; import org.nico.ratel.landlords.server.event.ServerEventListener; -public class RobotEventListener_CODE_GAME_LANDLORD_ELECT implements RobotEventListener{ +public class RobotEventListener_CODE_GAME_LANDLORD_ELECT implements RobotEventListener { @Override public void call(ClientSide robot, String data) { @@ -35,7 +35,7 @@ public void call(ClientSide robot, String data) { PokerHelper.sortPoker(rightPokers); TimeHelper.sleep(300); - + ServerEventListener.get(ServerEventCode.CODE_GAME_LANDLORD_ELECT).call(robot, String.valueOf(RobotDecisionMakers.howToChooseLandlord(room.getDifficultyCoefficient(), leftPokers, rightPokers, landlordPokers))); }); } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_POKER_PLAY.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_POKER_PLAY.java index d4ba2ce..7a98f92 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_POKER_PLAY.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_POKER_PLAY.java @@ -13,7 +13,7 @@ import org.nico.ratel.landlords.server.ServerContains; import org.nico.ratel.landlords.server.event.ServerEventListener; -public class RobotEventListener_CODE_GAME_POKER_PLAY implements RobotEventListener{ +public class RobotEventListener_CODE_GAME_POKER_PLAY implements RobotEventListener { @Override public void call(ClientSide robot, String data) { @@ -22,27 +22,27 @@ public void call(ClientSide robot, String data) { PokerSell lastPokerSell = null; PokerSell pokerSell = null; - if(room.getLastSellClient() != robot.getId()) { + if (room.getLastSellClient() != robot.getId()) { lastPokerSell = room.getLastPokerShell(); pokerSell = RobotDecisionMakers.howToPlayPokers(room.getDifficultyCoefficient(), lastPokerSell, robot); - }else { + } else { pokerSell = RobotDecisionMakers.howToPlayPokers(room.getDifficultyCoefficient(), null, robot); } - - if(pokerSell != null && lastPokerSell != null) { + + if (pokerSell != null && lastPokerSell != null) { SimplePrinter.serverLog("Robot monitoring[room:" + room.getId() + "]"); SimplePrinter.serverLog("last sell -> " + lastPokerSell.toString()); SimplePrinter.serverLog("robot sell -> " + pokerSell.toString()); SimplePrinter.serverLog("robot poker -> " + PokerHelper.textOnlyNoType(robot.getPokers())); } - + TimeHelper.sleep(300); - - if(pokerSell == null || pokerSell.getSellType() == SellType.ILLEGAL) { + + if (pokerSell == null || pokerSell.getSellType() == SellType.ILLEGAL) { ServerEventListener.get(ServerEventCode.CODE_GAME_POKER_PLAY_PASS).call(robot, data); - }else { + } else { Character[] cs = new Character[pokerSell.getSellPokers().size()]; - for(int index = 0; index < cs.length; index ++) { + for (int index = 0; index < cs.length; index++) { cs[index] = pokerSell.getSellPokers().get(index).getLevel().getAlias()[0]; } ServerEventListener.get(ServerEventCode.CODE_GAME_POKER_PLAY).call(robot, Noson.reversal(cs)); From e11b976bbd93d419551bd0d8efebd31ae57105e2 Mon Sep 17 00:00:00 2001 From: doveeeee Date: Tue, 13 Jul 2021 19:23:46 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=95=B4=E4=BD=93?= =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ntEventListener_CODE_SHOW_OPTIONS_PVE.java | 27 +- ...ntEventListener_CODE_SHOW_OPTIONS_PVP.java | 95 +-- .../handler/ProtobufTransferHandler.java | 48 +- .../client/handler/SecondProtobufCodec.java | 4 +- .../ratel/landlords/helper/PokerHelper.java | 547 +++++++++--------- .../helper/tests/PokerHelperTest.java | 411 ++++++------- .../ServerEventListener_CODE_CLIENT_EXIT.java | 29 +- ...ventListener_CODE_CLIENT_NICKNAME_SET.java | 10 +- ...rverEventListener_CODE_CLIENT_OFFLINE.java | 35 +- ...ventListener_CODE_GAME_LANDLORD_ELECT.java | 135 ++--- ...verEventListener_CODE_GAME_POKER_PLAY.java | 192 +++--- .../ServerEventListener_CODE_ROOM_CREATE.java | 7 +- ...verEventListener_CODE_ROOM_CREATE_PVE.java | 83 ++- .../ServerEventListener_CODE_ROOM_JOIN.java | 89 +-- .../handler/ProtobufTransferHandler.java | 64 +- .../landlords/server/timer/RoomClearTask.java | 189 +++--- 16 files changed, 970 insertions(+), 995 deletions(-) diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVE.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVE.java index d293609..50aa9d7 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVE.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVE.java @@ -8,7 +8,7 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_SHOW_OPTIONS_PVE extends ClientEventListener{ +public class ClientEventListener_CODE_SHOW_OPTIONS_PVE extends ClientEventListener { @Override public void call(Channel channel, String data) { @@ -18,23 +18,20 @@ public void call(Channel channel, String data) { SimplePrinter.printNotice("3. Hard Mode"); SimplePrinter.printNotice("Please select an option above (enter [back|b] to return to options list)"); String line = SimpleWriter.write("pve"); - + if(line.equalsIgnoreCase("back") || line.equalsIgnoreCase("b")) { get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); - }else { - int choose = OptionsUtils.getOptions(line); - - if(0 < choose && choose < 4) { - initLastSellInfo(); - pushToServer(channel, ServerEventCode.CODE_ROOM_CREATE_PVE, String.valueOf(choose)); - }else { - SimplePrinter.printNotice("Invalid option, please choose again:"); - call(channel, data); - } + return; } - - } - + int choose = OptionsUtils.getOptions(line); + if (choose < 0 || choose >= 4) { + SimplePrinter.printNotice("Invalid option, please choose again:"); + call(channel, data); + return; + } + initLastSellInfo(); + pushToServer(channel, ServerEventCode.CODE_ROOM_CREATE_PVE, String.valueOf(choose)); + } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVP.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVP.java index 572e378..06a6f46 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVP.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVP.java @@ -8,7 +8,7 @@ import io.netty.channel.Channel; -public class ClientEventListener_CODE_SHOW_OPTIONS_PVP extends ClientEventListener{ +public class ClientEventListener_CODE_SHOW_OPTIONS_PVP extends ClientEventListener { @Override public void call(Channel channel, String data) { @@ -19,54 +19,67 @@ public void call(Channel channel, String data) { SimplePrinter.printNotice("4. Spectate Game"); SimplePrinter.printNotice("Please select an option above (enter [back|b] to return to options list)"); String line = SimpleWriter.write("pvp"); - - if(line.equalsIgnoreCase("back") || line.equalsIgnoreCase("b")) { + if (line == null) { + SimplePrinter.printNotice("Invalid options, please choose again:"); + call(channel, data); + return; + } + + if (line.equalsIgnoreCase("BACK") || line.equalsIgnoreCase("b")) { get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data); - }else { - int choose = OptionsUtils.getOptions(line); - - if(choose == 1) { + return; + } + + int choose = OptionsUtils.getOptions(line); + switch (choose) { + case 1: pushToServer(channel, ServerEventCode.CODE_ROOM_CREATE, null); - }else if(choose == 2){ + break; + case 2: pushToServer(channel, ServerEventCode.CODE_GET_ROOMS, null); - }else if(choose == 3){ - SimplePrinter.printNotice("Please enter the room id you wish to join (enter [back|b] to return to options list)"); - line = SimpleWriter.write("roomid"); - - if(line.equalsIgnoreCase("back") || line.equalsIgnoreCase("b")) { - call(channel, data); - }else { - int option = OptionsUtils.getOptions(line); - if(line == null || option < 1) { - SimplePrinter.printNotice("Invalid option, please choose again:"); - call(channel, data); - }else{ - pushToServer(channel, ServerEventCode.CODE_ROOM_JOIN, String.valueOf(option)); - } - } - } else if (choose == 4) { - SimplePrinter.printNotice("Please enter the room id you want to spectate (enter [back] to return to options list)"); - line = SimpleWriter.write("roomid"); - - if(line.equalsIgnoreCase("back") || line.equalsIgnoreCase("b")) { - call(channel, data); - }else { - int option = OptionsUtils.getOptions(line); - if(line == null || option < 1) { - SimplePrinter.printNotice("Invalid option, please choose again:"); - call(channel, data); - }else{ - pushToServer(channel, ServerEventCode.CODE_GAME_WATCH, String.valueOf(option)); - } - } - } else { + break; + case 3: + handleJoinRoom(channel, data); + break; + case 4: + handleJoinRoom(channel, data, true); + break; + default: SimplePrinter.printNotice("Invalid option, please choose again:"); call(channel, data); - } } - } + private void parseInvalid(Channel channel, String data) { + SimplePrinter.printNotice("Invalid options, please choose again:"); + call(channel, data); + } + + private void handleJoinRoom(Channel channel, String data) { + handleJoinRoom(channel, data, false); + } + private void handleJoinRoom(Channel channel, String data, Boolean watchMode) { + String notice = String.format("Please enter the room id you want to %s (enter [back|b] return options list)", watchMode ? "spectate" : "join"); + SimplePrinter.printNotice(notice); + String line = SimpleWriter.write("roomid"); + if (line == null) { + parseInvalid(channel, data); + return; + } + + if (line.equalsIgnoreCase("BACK") || line.equalsIgnoreCase("b")) { + call(channel, data); + return; + } + + int option = OptionsUtils.getOptions(line); + if (option < 1) { + parseInvalid(channel, data); + return; + } + + pushToServer(channel, watchMode? ServerEventCode.CODE_GAME_WATCH : ServerEventCode.CODE_ROOM_JOIN, String.valueOf(option)); + } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/ProtobufTransferHandler.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/ProtobufTransferHandler.java index 6230725..c392920 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/ProtobufTransferHandler.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/ProtobufTransferHandler.java @@ -17,44 +17,42 @@ import java.util.HashMap; import java.util.Map; -public class ProtobufTransferHandler extends ChannelInboundHandlerAdapter{ +public class ProtobufTransferHandler extends ChannelInboundHandlerAdapter { @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + public void channelRead(ChannelHandlerContext ctx, Object msg) { - if(msg instanceof ClientTransferDataProtoc) { + if (msg instanceof ClientTransferDataProtoc) { ClientTransferDataProtoc clientTransferData = (ClientTransferDataProtoc) msg; - if(clientTransferData.getInfo() != null && ! clientTransferData.getInfo().isEmpty()) { + if (!clientTransferData.getInfo().isEmpty()) { SimplePrinter.printNotice(clientTransferData.getInfo()); } ClientEventCode code = ClientEventCode.valueOf(clientTransferData.getCode()); - if(code != null) { - if (User.INSTANCE.isWatching()) { - Map wrapMap = new HashMap<>(3); - wrapMap.put("code", code); - wrapMap.put("data", clientTransferData.getData()); - - ClientEventListener.get(ClientEventCode.CODE_GAME_WATCH).call(ctx.channel(), Noson.reversal(wrapMap)); - } else { - ClientEventListener.get(code).call(ctx.channel(), clientTransferData.getData()); - } + if (User.INSTANCE.isWatching()) { + Map wrapMap = new HashMap<>(3); + wrapMap.put("code", code); + wrapMap.put("data", clientTransferData.getData()); + + ClientEventListener.get(ClientEventCode.CODE_GAME_WATCH).call(ctx.channel(), Noson.reversal(wrapMap)); + } else { + ClientEventListener.get(code).call(ctx.channel(), clientTransferData.getData()); } } } - @Override - public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { - if (evt instanceof IdleStateEvent) { - IdleStateEvent event = (IdleStateEvent) evt; - if (event.state() == IdleState.WRITER_IDLE) { + @Override + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { + if (evt instanceof IdleStateEvent) { + IdleStateEvent event = (IdleStateEvent) evt; + if (event.state() == IdleState.WRITER_IDLE) { ChannelUtils.pushToServer(ctx.channel(), ServerEventCode.CODE_CLIENT_HEAD_BEAT, "heartbeat"); - } - } - } - + } + } + } + @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if(cause instanceof java.io.IOException) { + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + if (cause instanceof java.io.IOException) { SimplePrinter.printNotice("The network is not good or did not operate for a long time, has been offline"); System.exit(0); } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/SecondProtobufCodec.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/SecondProtobufCodec.java index 26a3fa4..7f2c968 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/SecondProtobufCodec.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/SecondProtobufCodec.java @@ -12,12 +12,12 @@ public class SecondProtobufCodec extends MessageToMessageCodec { @Override - protected void encode(ChannelHandlerContext ctx, MessageLite msg, List out) throws Exception { + protected void encode(ChannelHandlerContext ctx, MessageLite msg, List out) { out.add(msg); } @Override - protected void decode(ChannelHandlerContext ctx, ClientTransferDataProtoc msg, List out) throws Exception { + protected void decode(ChannelHandlerContext ctx, ClientTransferDataProtoc msg, List out) { out.add(msg); } 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 74c3a89..edeb36d 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 @@ -53,10 +53,10 @@ public class PokerHelper { } } - public static void sortPoker(List pokers){ - Collections.sort(pokers, pokerComparator); + public static void sortPoker(List pokers) { + pokers.sort(pokerComparator); } - + public static List clonePokers(List pokers){ List newPokers = new ArrayList(pokers.size()); for(Poker poker: pokers) { @@ -64,7 +64,7 @@ public static List clonePokers(List pokers){ } return newPokers; } - + public static List validSells(PokerSell lastPokerSell, List pokers) { List sells = PokerHelper.parsePokerSells(pokers); if(lastPokerSell == null) { @@ -96,22 +96,23 @@ public static int[] getIndexes(Character[] options, List pokers) { List copyList = new ArrayList<>(pokers.size()); copyList.addAll(pokers); int[] indexes = new int[options.length]; - for(int index = 0; index < options.length; index ++) { + for (int index = 0; index < options.length; index++) { char option = options[index]; boolean isTarget = false; - for(int pi = 0; pi < copyList.size(); pi ++) { + for (int pi = 0; pi < copyList.size(); pi++) { Poker poker = copyList.get(pi); - if(poker != null) { - if(Arrays.asList(poker.getLevel().getAlias()).contains(option)) { - isTarget = true; - //Index start from 1, not 0 - indexes[index] = pi + 1; - copyList.set(pi, null); - break; - } + if (poker == null) { + continue; + } + if (Arrays.asList(poker.getLevel().getAlias()).contains(option)) { + isTarget = true; + //Index start from 1, not 0 + indexes[index] = pi + 1; + copyList.set(pi, null); + break; } } - if(! isTarget) { + if (!isTarget) { return null; } } @@ -119,136 +120,131 @@ public static int[] getIndexes(Character[] options, List pokers) { return indexes; } - public static boolean checkPokerIndex(int[] indexes, List pokers){ - boolean access = true; - if(indexes == null || indexes.length == 0) { - access = false; - }else { - for(int index: indexes){ - if(index > pokers.size() || index < 1){ - access = false; - } + public static boolean checkPokerIndex(int[] indexes, List pokers) { + if (indexes == null || indexes.length == 0) { + return false; + } + for (int index : indexes) { + if (index > pokers.size() || index < 1) { + return false; } } - return access; + return true; } public static PokerSell checkPokerType(List pokers) { + if (pokers == null || pokers.isEmpty()) { + return new PokerSell(SellType.ILLEGAL, null, -1); + } + sortPoker(pokers); - if(pokers != null && ! pokers.isEmpty()) { - sortPoker(pokers); - - int[] levelTable = new int[20]; - for(Poker poker: pokers) { - levelTable[poker.getLevel().getLevel()] ++; - } + int[] levelTable = new int[20]; + for (Poker poker : pokers) { + levelTable[poker.getLevel().getLevel()]++; + } - int startIndex = -1; - int endIndex = -1; - int count = 0; + int startIndex = -1; + int endIndex = -1; + int count = 0; - int singleCount = 0; - int doubleCount = 0; - int threeCount = 0; - int threeStartIndex = -1; - int threeEndIndex = -1; - int fourCount = 0; - int fourStartIndex = -1; - int fourEndIndex = -1; - for(int index = 0; index < levelTable.length; index ++) { - int value = levelTable[index]; - if(value != 0) { - endIndex = index; - count ++; - if(startIndex == -1) { - startIndex = index; - } - if(value == 1) { - singleCount ++; - }else if(value == 2) { - doubleCount ++; - }else if(value == 3) { - if(threeStartIndex == -1) { - threeStartIndex = index; - } - threeEndIndex = index; - threeCount ++; - }else if(value == 4) { - if(fourStartIndex == -1) { - fourStartIndex = index; - } - fourEndIndex = index; - fourCount ++; - } + int singleCount = 0; + int doubleCount = 0; + int threeCount = 0; + int threeStartIndex = -1; + int threeEndIndex = -1; + int fourCount = 0; + int fourStartIndex = -1; + int fourEndIndex = -1; + for (int index = 0; index < levelTable.length; index++) { + int value = levelTable[index]; + if (value == 0) { + continue; + } + endIndex = index; + count++; + if (startIndex == -1) { + startIndex = index; + } + if (value == 1) { + singleCount++; + } else if (value == 2) { + doubleCount++; + } else if (value == 3) { + if (threeStartIndex == -1) { + threeStartIndex = index; + } + threeEndIndex = index; + threeCount++; + } else if (value == 4) { + if (fourStartIndex == -1) { + fourStartIndex = index; } + fourEndIndex = index; + fourCount++; } + } - if(singleCount == doubleCount && singleCount == threeCount && singleCount == 0 && fourCount == 1) { - return new PokerSell(SellType.BOMB, pokers, startIndex); - } + if (singleCount == doubleCount && singleCount == threeCount && singleCount == 0 && fourCount == 1) { + return new PokerSell(SellType.BOMB, pokers, startIndex); + } - if(singleCount == 2 && startIndex == PokerLevel.LEVEL_SMALL_KING.getLevel() && endIndex == PokerLevel.LEVEL_BIG_KING.getLevel()) { - return new PokerSell(SellType.KING_BOMB, pokers, PokerLevel.LEVEL_SMALL_KING.getLevel()); - } + if (singleCount == 2 && startIndex == PokerLevel.LEVEL_SMALL_KING.getLevel() && endIndex == PokerLevel.LEVEL_BIG_KING.getLevel()) { + return new PokerSell(SellType.KING_BOMB, pokers, PokerLevel.LEVEL_SMALL_KING.getLevel()); + } - if(startIndex == endIndex) { - if(levelTable[startIndex] == 1) { - return new PokerSell(SellType.SINGLE, pokers, startIndex); - }else if(levelTable[startIndex] == 2) { - return new PokerSell(SellType.DOUBLE, pokers, startIndex); - }else if(levelTable[startIndex] == 3) { - return new PokerSell(SellType.THREE, pokers, startIndex); - } + if (startIndex == endIndex) { + if (levelTable[startIndex] == 1) { + return new PokerSell(SellType.SINGLE, pokers, startIndex); + } else if (levelTable[startIndex] == 2) { + return new PokerSell(SellType.DOUBLE, pokers, startIndex); + } else if (levelTable[startIndex] == 3) { + return new PokerSell(SellType.THREE, pokers, startIndex); } - if(endIndex - startIndex == count - 1 && endIndex < PokerLevel.LEVEL_2.getLevel()) { - if(levelTable[startIndex] == 1 && singleCount > 4 && doubleCount + threeCount + fourCount == 0) { - return new PokerSell(SellType.SINGLE_STRAIGHT, pokers, endIndex); - }else if(levelTable[startIndex] == 2 && doubleCount > 2 && singleCount + threeCount + fourCount == 0) { - return new PokerSell(SellType.DOUBLE_STRAIGHT, pokers, endIndex); - }else if(levelTable[startIndex] == 3 && threeCount > 1 && doubleCount + singleCount + fourCount == 0) { - return new PokerSell(SellType.THREE_STRAIGHT, pokers, endIndex); - }else if(levelTable[startIndex] == 4 && fourCount > 1 && doubleCount + threeCount + singleCount == 0) { - return new PokerSell(SellType.FOUR_STRAIGHT, pokers, endIndex); - } + } + if (endIndex - startIndex == count - 1 && endIndex < PokerLevel.LEVEL_2.getLevel()) { + if (levelTable[startIndex] == 1 && singleCount > 4 && doubleCount + threeCount + fourCount == 0) { + return new PokerSell(SellType.SINGLE_STRAIGHT, pokers, endIndex); + } else if (levelTable[startIndex] == 2 && doubleCount > 2 && singleCount + threeCount + fourCount == 0) { + return new PokerSell(SellType.DOUBLE_STRAIGHT, pokers, endIndex); + } else if (levelTable[startIndex] == 3 && threeCount > 1 && doubleCount + singleCount + fourCount == 0) { + return new PokerSell(SellType.THREE_STRAIGHT, pokers, endIndex); + } else if (levelTable[startIndex] == 4 && fourCount > 1 && doubleCount + threeCount + singleCount == 0) { + return new PokerSell(SellType.FOUR_STRAIGHT, pokers, endIndex); } + } - if(threeCount != 0) { - if(singleCount != 0 && singleCount == threeCount && doubleCount == 0 && fourCount == 0) { - if(threeCount == 1) { - return new PokerSell(SellType.THREE_ZONES_SINGLE, pokers, threeEndIndex); - }else { - if(threeEndIndex - threeStartIndex + 1 == threeCount && threeEndIndex < PokerLevel.LEVEL_2.getLevel()) { - return new PokerSell(SellType.THREE_STRAIGHT_WITH_SINGLE, pokers, threeEndIndex); - } - } - }else if(doubleCount != 0 && doubleCount == threeCount && singleCount == 0 && fourCount == 0) { - if(threeCount == 1) { - return new PokerSell(SellType.THREE_ZONES_DOUBLE, pokers, threeEndIndex); - }else { - if(threeEndIndex - threeStartIndex + 1 == threeCount && threeEndIndex < PokerLevel.LEVEL_2.getLevel()) { - return new PokerSell(SellType.FOUR_STRAIGHT_WITH_DOUBLE, pokers, threeEndIndex); - } - } + if (threeCount != 0) { + if (singleCount != 0 && singleCount == threeCount && doubleCount == 0 && fourCount == 0) { + if (threeCount == 1) { + return new PokerSell(SellType.THREE_ZONES_SINGLE, pokers, threeEndIndex); + } + if (threeEndIndex - threeStartIndex + 1 == threeCount && threeEndIndex < PokerLevel.LEVEL_2.getLevel()) { + return new PokerSell(SellType.THREE_STRAIGHT_WITH_SINGLE, pokers, threeEndIndex); + } + } else if (doubleCount != 0 && doubleCount == threeCount && singleCount == 0 && fourCount == 0) { + if (threeCount == 1) { + return new PokerSell(SellType.THREE_ZONES_DOUBLE, pokers, threeEndIndex); + } + if (threeEndIndex - threeStartIndex + 1 == threeCount && threeEndIndex < PokerLevel.LEVEL_2.getLevel()) { + return new PokerSell(SellType.FOUR_STRAIGHT_WITH_DOUBLE, pokers, threeEndIndex); } } + } - if(fourCount != 0) { - if(singleCount != 0 && singleCount == fourCount * 2 && doubleCount == 0 && threeCount == 0) { - if(fourCount == 1) { - return new PokerSell(SellType.FOUR_ZONES_SINGLE, pokers, fourEndIndex); - }else { - if(fourEndIndex - fourStartIndex + 1 == fourCount && fourEndIndex < PokerLevel.LEVEL_2.getLevel()) { - return new PokerSell(SellType.FOUR_STRAIGHT_WITH_SINGLE, pokers, fourEndIndex); - } - } - }else if(doubleCount != 0 && doubleCount == fourCount * 2 && singleCount == 0 && threeCount == 0) { - if(fourCount == 1) { - return new PokerSell(SellType.FOUR_ZONES_DOUBLE, pokers, fourEndIndex); - }else { - if(fourEndIndex - fourStartIndex + 1 == fourCount && fourEndIndex < PokerLevel.LEVEL_2.getLevel()) { - return new PokerSell(SellType.FOUR_STRAIGHT_WITH_DOUBLE, pokers, fourEndIndex); - } - } + if (fourCount != 0) { + if (singleCount != 0 && singleCount == fourCount * 2 && doubleCount == 0 && threeCount == 0) { + if (fourCount == 1) { + return new PokerSell(SellType.FOUR_ZONES_SINGLE, pokers, fourEndIndex); + } + if (fourEndIndex - fourStartIndex + 1 == fourCount && fourEndIndex < PokerLevel.LEVEL_2.getLevel()) { + return new PokerSell(SellType.FOUR_STRAIGHT_WITH_SINGLE, pokers, fourEndIndex); + } + } else if (doubleCount != 0 && doubleCount == fourCount * 2 && singleCount == 0 && threeCount == 0) { + if (fourCount == 1) { + return new PokerSell(SellType.FOUR_ZONES_DOUBLE, pokers, fourEndIndex); + } + if (fourEndIndex - fourStartIndex + 1 == fourCount && fourEndIndex < PokerLevel.LEVEL_2.getLevel()) { + return new PokerSell(SellType.FOUR_STRAIGHT_WITH_DOUBLE, pokers, fourEndIndex); } } } @@ -256,37 +252,37 @@ public static PokerSell checkPokerType(List pokers) { } public static int parseScore(SellType sellType, int level) { - if(sellType == SellType.BOMB) { + if (sellType == SellType.BOMB) { return level * 4 + 999; - }else if(sellType == SellType.KING_BOMB) { + } else if (sellType == SellType.KING_BOMB) { return Integer.MAX_VALUE; - }else if(sellType == SellType.SINGLE || sellType == SellType.DOUBLE || sellType == SellType.THREE) { + } else if (sellType == SellType.SINGLE || sellType == SellType.DOUBLE || sellType == SellType.THREE) { return level; - }else if(sellType == SellType.SINGLE_STRAIGHT || sellType == SellType.DOUBLE_STRAIGHT || sellType == SellType.THREE_STRAIGHT || sellType == SellType.FOUR_STRAIGHT) { + } else if (sellType == SellType.SINGLE_STRAIGHT || sellType == SellType.DOUBLE_STRAIGHT || sellType == SellType.THREE_STRAIGHT || sellType == SellType.FOUR_STRAIGHT) { return level; - }else if(sellType == SellType.THREE_ZONES_SINGLE || sellType == SellType.THREE_STRAIGHT_WITH_SINGLE || sellType == SellType.THREE_ZONES_DOUBLE || sellType == SellType.FOUR_STRAIGHT_WITH_DOUBLE) { + } else if (sellType == SellType.THREE_ZONES_SINGLE || sellType == SellType.THREE_STRAIGHT_WITH_SINGLE || sellType == SellType.THREE_ZONES_DOUBLE || sellType == SellType.FOUR_STRAIGHT_WITH_DOUBLE) { return level; - }else if(sellType == SellType.FOUR_ZONES_SINGLE || sellType == SellType.FOUR_STRAIGHT_WITH_SINGLE || sellType == SellType.FOUR_ZONES_DOUBLE || sellType == SellType.FOUR_STRAIGHT_WITH_DOUBLE) { + } else if (sellType == SellType.FOUR_ZONES_SINGLE || sellType == SellType.FOUR_STRAIGHT_WITH_SINGLE || sellType == SellType.FOUR_ZONES_DOUBLE || sellType == SellType.FOUR_STRAIGHT_WITH_DOUBLE) { return level; } return -1; } - public static List getPoker(int[] indexes, List pokers){ + public static List getPoker(int[] indexes, List pokers) { List resultPokers = new ArrayList<>(indexes.length); - for(int index: indexes){ + for (int index : indexes) { resultPokers.add(pokers.get(index - 1)); } sortPoker(resultPokers); return resultPokers; } - public static boolean comparePoker(List pres, List currents){ + public static boolean comparePoker(List pres, List currents) { return true; } - public static List> distributePoker(){ + public static List> distributePoker() { Collections.shuffle(basePokers); List> pokersList = new ArrayList>(); List pokers1 = new ArrayList<>(17); @@ -301,7 +297,7 @@ public static List> distributePoker(){ pokersList.add(pokers2); pokersList.add(pokers3); pokersList.add(pokers4); - for(List pokers: pokersList) { + for (List pokers : pokersList) { sortPoker(pokers); } return pokersList; @@ -309,50 +305,51 @@ public static List> distributePoker(){ public static String printPoker(List pokers) { sortPoker(pokers); - switch(pokerPrinterType){ - case 0: - return buildHandStringSharp(pokers); - case 1: - return buildHandStringRounded(pokers); - case 2: - return textOnly(pokers); - case 3: - return textOnlyNoType(pokers); - default: - return buildHandStringSharp(pokers); + switch (pokerPrinterType) { + case 0: + return buildHandStringSharp(pokers); + case 1: + return buildHandStringRounded(pokers); + case 2: + return textOnly(pokers); + case 3: + return textOnlyNoType(pokers); + default: + return buildHandStringSharp(pokers); } } - private static String buildHandStringSharp(List pokers){ + + private static String buildHandStringSharp(List pokers) { StringBuilder builder = new StringBuilder(); - if(pokers != null && pokers.size() > 0) { + if (pokers != null && pokers.size() > 0) { - for(int index = 0; index < pokers.size(); index ++) { - if(index == 0) { + for (int index = 0; index < pokers.size(); index++) { + if (index == 0) { builder.append("┌──┐"); - }else { + } else { builder.append("──┐"); } } builder.append(System.lineSeparator()); - for(int index = 0; index < pokers.size(); index ++) { - if(index == 0) { + for (int index = 0; index < pokers.size(); index++) { + if (index == 0) { builder.append("│"); } String name = pokers.get(index).getLevel().getName(); - builder.append(name + (name.length() == 1 ? " " : "" ) + "|"); + builder.append(name).append(name.length() == 1 ? " " : "").append("|"); } builder.append(System.lineSeparator()); - for(int index = 0; index < pokers.size(); index ++) { - if(index == 0) { + for (int index = 0; index < pokers.size(); index++) { + if (index == 0) { builder.append("│"); } - builder.append(pokers.get(index).getType().getName() + " |"); + builder.append(pokers.get(index).getType().getName()).append(" |"); } builder.append(System.lineSeparator()); - for(int index = 0; index < pokers.size(); index ++) { - if(index == 0) { + for (int index = 0; index < pokers.size(); index++) { + if (index == 0) { builder.append("└──┘"); } else { builder.append("──┘"); @@ -361,35 +358,36 @@ private static String buildHandStringSharp(List pokers){ } return builder.toString(); } - private static String buildHandStringRounded(List pokers){ + + private static String buildHandStringRounded(List pokers) { StringBuilder builder = new StringBuilder(); - if(pokers != null && pokers.size() > 0) { + if (pokers != null && pokers.size() > 0) { - for(int index = 0; index < pokers.size(); index ++) { - if(index == 0) { + for (int index = 0; index < pokers.size(); index++) { + if (index == 0) { builder.append("┌──╮"); - }else { + } else { builder.append("──╮"); } } builder.append(System.lineSeparator()); - for(int index = 0; index < pokers.size(); index ++) { - if(index == 0) { + for (int index = 0; index < pokers.size(); index++) { + if (index == 0) { builder.append("│"); } String name = pokers.get(index).getLevel().getName(); - builder.append(name + (name.length() == 1 ? " " : "" ) + "|"); + builder.append(name).append(name.length() == 1 ? " " : "").append("|"); } builder.append(System.lineSeparator()); - for(int index = 0; index < pokers.size(); index ++) { - if(index == 0) { + for (int index = 0; index < pokers.size(); index++) { + if (index == 0) { builder.append("│"); } - builder.append(pokers.get(index).getType().getName() + " |"); + builder.append(pokers.get(index).getType().getName()).append(" |"); } builder.append(System.lineSeparator()); - for(int index = 0; index < pokers.size(); index ++) { - if(index == 0) { + for (int index = 0; index < pokers.size(); index++) { + if (index == 0) { builder.append("└──╯"); } else { builder.append("──╯"); @@ -399,59 +397,60 @@ private static String buildHandStringRounded(List pokers){ return builder.toString(); } - private static String textOnly(List pokers){ + private static String textOnly(List pokers) { StringBuilder builder = new StringBuilder(); - if(pokers != null && pokers.size() > 0) { - for(int index = 0; index < pokers.size(); index ++) { - String name = pokers.get(index).getLevel().getName(); - String type = pokers.get(index).getType().getName(); + if (pokers != null && pokers.size() > 0) { + for (Poker poker : pokers) { + String name = poker.getLevel().getName(); + String type = poker.getType().getName(); - builder.append(name + type); + builder.append(name).append(type); } } return builder.toString(); } - public static String textOnlyNoType(List pokers){ + + public static String textOnlyNoType(List pokers) { StringBuilder builder = new StringBuilder(); - if(pokers != null && pokers.size() > 0) { - for(int index = 0; index < pokers.size(); index ++) { - String name = pokers.get(index).getLevel().getName(); - builder.append(name + " "); + if (pokers != null && pokers.size() > 0) { + for (Poker poker : pokers) { + String name = poker.getLevel().getName(); + builder.append(name).append(" "); } } return builder.toString(); } - public static int parsePokerColligationScore(List pokers){ + public static int parsePokerColligationScore(List pokers) { int score = 0; int count = 0; int increase = 0; int lastLevel = -1; - if(pokers != null && ! pokers.isEmpty()){ - for(int index = 0; index < pokers.size(); index ++){ + if (pokers != null && !pokers.isEmpty()) { + for (int index = 0; index < pokers.size(); index++) { int level = pokers.get(index).getLevel().getLevel(); - if(lastLevel == -1){ - increase ++; - count ++; + if (lastLevel == -1) { + increase++; + count++; score += lastLevel; - }else{ - if(level == lastLevel){ - ++ count; - }else{ + } else { + if (level == lastLevel) { + ++count; + } else { count = 1; } - if(level < PokerLevel.LEVEL_2.getLevel() && level - 1 == lastLevel){ - ++ increase; - }else{ + if (level < PokerLevel.LEVEL_2.getLevel() && level - 1 == lastLevel) { + ++increase; + } else { increase = 1; } score += (count + (increase > 4 ? increase : 0)) * level; } - if(level == PokerLevel.LEVEL_2.getLevel()){ + if (level == PokerLevel.LEVEL_2.getLevel()) { score += level * 2; - }else if(level > PokerLevel.LEVEL_2.getLevel()){ + } else if (level > PokerLevel.LEVEL_2.getLevel()) { score += level * 3; } lastLevel = level; @@ -460,7 +459,7 @@ public static int parsePokerColligationScore(List pokers){ return score; } - public static List parsePokerSells(List pokers){ + public static List parsePokerSells(List pokers) { List pokerSells = new ArrayList<>(); int size = pokers.size(); @@ -469,27 +468,26 @@ public static List parsePokerSells(List pokers){ int count = 0; int lastLevel = -1; List sellPokers = new ArrayList<>(4); - for(int index = 0; index < pokers.size(); index ++) { - Poker poker = pokers.get(index); + for (Poker poker : pokers) { int level = poker.getLevel().getLevel(); - if(lastLevel == -1) { - ++ count; - }else { - if(level == lastLevel) { - ++ count; - }else { + if (lastLevel == -1) { + ++count; + } else { + if (level == lastLevel) { + ++count; + } else { count = 1; sellPokers.clear(); } } sellPokers.add(poker); - if(count == 1) { + if (count == 1) { pokerSells.add(new PokerSell(SellType.SINGLE, ListUtils.getList(sellPokers), poker.getLevel().getLevel())); - }else if(count == 2){ + } else if (count == 2) { pokerSells.add(new PokerSell(SellType.DOUBLE, ListUtils.getList(sellPokers), poker.getLevel().getLevel())); - }else if(count == 3){ + } else if (count == 3) { pokerSells.add(new PokerSell(SellType.THREE, ListUtils.getList(sellPokers), poker.getLevel().getLevel())); - }else if(count == 4){ + } else if (count == 4) { pokerSells.add(new PokerSell(SellType.BOMB, ListUtils.getList(sellPokers), poker.getLevel().getLevel())); } @@ -503,63 +501,62 @@ public static List parsePokerSells(List pokers){ parsePokerSellStraight(pokerSells, SellType.THREE); parsePokerSellStraight(pokerSells, SellType.BOMB); } - + //Shunzi with args { - for(int index = 0; index < pokerSells.size(); index ++) { + for (int index = 0; index < pokerSells.size(); index++) { PokerSell sell = pokerSells.get(index); - if(sell.getSellType() == SellType.THREE) { + if (sell.getSellType() == SellType.THREE) { parseArgs(pokerSells, sell, 1, SellType.SINGLE, SellType.THREE_ZONES_SINGLE); parseArgs(pokerSells, sell, 1, SellType.DOUBLE, SellType.THREE_ZONES_DOUBLE); - }else if(sell.getSellType() == SellType.BOMB) { + } else if (sell.getSellType() == SellType.BOMB) { parseArgs(pokerSells, sell, 2, SellType.SINGLE, SellType.FOUR_ZONES_SINGLE); parseArgs(pokerSells, sell, 2, SellType.DOUBLE, SellType.FOUR_ZONES_DOUBLE); - }else if(sell.getSellType() == SellType.THREE_STRAIGHT) { + } else if (sell.getSellType() == SellType.THREE_STRAIGHT) { int count = sell.getSellPokers().size() / 3; parseArgs(pokerSells, sell, count, SellType.SINGLE, SellType.THREE_STRAIGHT_WITH_SINGLE); parseArgs(pokerSells, sell, count, SellType.DOUBLE, SellType.THREE_STRAIGHT_WITH_DOUBLE); - }else if(sell.getSellType() == SellType.FOUR_STRAIGHT) { + } else if (sell.getSellType() == SellType.FOUR_STRAIGHT) { int count = (sell.getSellPokers().size() / 4) * 2; parseArgs(pokerSells, sell, count, SellType.SINGLE, SellType.FOUR_STRAIGHT_WITH_SINGLE); parseArgs(pokerSells, sell, count, SellType.DOUBLE, SellType.FOUR_STRAIGHT_WITH_DOUBLE); } } } - + //king boom { - if(size > 1) { - if(pokers.get(size - 1).getLevel() == PokerLevel.LEVEL_BIG_KING && pokers.get(size - 2).getLevel() == PokerLevel.LEVEL_SMALL_KING) { - pokerSells.add(new PokerSell(SellType.KING_BOMB, ListUtils.getList(new Poker[] {pokers.get(size - 2), pokers.get(size - 1)}), PokerLevel.LEVEL_BIG_KING.getLevel())); + if (size > 1) { + if (pokers.get(size - 1).getLevel() == PokerLevel.LEVEL_BIG_KING && pokers.get(size - 2).getLevel() == PokerLevel.LEVEL_SMALL_KING) { + pokerSells.add(new PokerSell(SellType.KING_BOMB, ListUtils.getList(new Poker[]{pokers.get(size - 2), pokers.get(size - 1)}), PokerLevel.LEVEL_BIG_KING.getLevel())); } } } - + return pokerSells; } - + private static void parseArgs(List pokerSells, PokerSell pokerSell, int deep, SellType sellType, SellType targetSellType) { Set existLevelSet = new HashSet<>(); - for(Poker p: pokerSell.getSellPokers()) { + for (Poker p : pokerSell.getSellPokers()) { existLevelSet.add(p.getLevel().getLevel()); } parseArgs(existLevelSet, pokerSells, new HashSet<>(), pokerSell, deep, sellType, targetSellType); } - + private static void parseArgs(Set existLevelSet, List pokerSells, Set> pokersList, PokerSell pokerSell, int deep, SellType sellType, SellType targetSellType) { - if(deep == 0) { - List allPokers = new ArrayList<>(); - allPokers.addAll(pokerSell.getSellPokers()); - for(List ps: pokersList) { + if (deep == 0) { + List allPokers = new ArrayList<>(pokerSell.getSellPokers()); + for (List ps : pokersList) { allPokers.addAll(ps); } pokerSells.add(new PokerSell(targetSellType, allPokers, pokerSell.getCoreLevel())); return; } - - for(int index = 0; index < pokerSells.size(); index ++) { + + for (int index = 0; index < pokerSells.size(); index++) { PokerSell subSell = pokerSells.get(index); - if(subSell.getSellType() == sellType && ! existLevelSet.contains(subSell.getCoreLevel())) { + if (subSell.getSellType() == sellType && !existLevelSet.contains(subSell.getCoreLevel())) { pokersList.add(subSell.getSellPokers()); existLevelSet.add(subSell.getCoreLevel()); parseArgs(existLevelSet, pokerSells, pokersList, pokerSell, deep - 1, sellType, targetSellType); @@ -568,25 +565,25 @@ private static void parseArgs(Set existLevelSet, List pokerS } } } - + private static void parsePokerSellStraight(List pokerSells, SellType sellType) { - int minLenght = -1; + int minLength = -1; int width = -1; SellType targetSellType = null; - if(sellType == SellType.SINGLE) { - minLenght = 5; + if (sellType == SellType.SINGLE) { + minLength = 5; width = 1; targetSellType = SellType.SINGLE_STRAIGHT; - }else if(sellType == SellType.DOUBLE) { - minLenght = 3; + } else if (sellType == SellType.DOUBLE) { + minLength = 3; width = 2; targetSellType = SellType.DOUBLE_STRAIGHT; - }else if(sellType == SellType.THREE) { - minLenght = 2; + } else if (sellType == SellType.THREE) { + minLength = 2; width = 3; targetSellType = SellType.THREE_STRAIGHT; - }else if(sellType == SellType.BOMB) { - minLenght = 2; + } else if (sellType == SellType.BOMB) { + minLength = 2; width = 4; targetSellType = SellType.FOUR_STRAIGHT; } @@ -594,48 +591,40 @@ private static void parsePokerSellStraight(List pokerSells, SellType int increase_1 = 0; int lastLevel_1 = -1; List sellPokers_1 = new ArrayList<>(4); - for(int index = 0; index < pokerSells.size(); index ++) { + for (int index = 0; index < pokerSells.size(); index++) { PokerSell sell = pokerSells.get(index); - if(sell.getSellType() == sellType) { - int level = sell.getCoreLevel(); - if(lastLevel_1 == -1) { - ++ increase_1; - sellPokers_1.addAll(sell.getSellPokers()); - }else { - if(level - 1 == lastLevel_1 && level != PokerLevel.LEVEL_2.getLevel()) { - ++ increase_1; - sellPokers_1.addAll(sell.getSellPokers()); - }else{ - if(increase_1 >= minLenght) { - for(int s = 0; s <= increase_1 - minLenght; s ++) { - int len = minLenght + s; - for(int subIndex = 0; subIndex <= increase_1 - len; subIndex ++) { - List pokers = ListUtils.getList(sellPokers_1.subList(subIndex * width, (subIndex + len) * width)); - pokerSells.add(new PokerSell(targetSellType, pokers, pokers.get(pokers.size() - 1).getLevel().getLevel())); - } - } - } - increase_1 = 1; - sellPokers_1.clear(); - sellPokers_1.addAll(sell.getSellPokers()); - } + if (sell.getSellType() != sellType) { + continue; + } + int level = sell.getCoreLevel(); + if (lastLevel_1 == -1) { + ++increase_1; + } else { + if (level - 1 == lastLevel_1 && level != PokerLevel.LEVEL_2.getLevel()) { + ++increase_1; + } else { + addPokers(pokerSells, minLength, width, targetSellType, increase_1, sellPokers_1); + + increase_1 = 1; } - lastLevel_1 = level; } + sellPokers_1.addAll(sell.getSellPokers()); + lastLevel_1 = level; } - if(sellPokers_1 != null) { - if(increase_1 >= minLenght) { - for(int s = 0; s <= increase_1 - minLenght; s ++) { - int len = minLenght + s; - for(int subIndex = 0; subIndex <= increase_1 - len; subIndex ++) { - List pokers = ListUtils.getList(sellPokers_1.subList(subIndex * width, (subIndex + len) * width)); - pokerSells.add(new PokerSell(targetSellType, pokers, pokers.get(pokers.size() - 1).getLevel().getLevel())); - } + addPokers(pokerSells, minLength, width, targetSellType, increase_1, sellPokers_1); + } + + private static void addPokers(List pokerSells, int minLenght, int width, SellType targetSellType, int increase_1, List sellPokers_1) { + if (increase_1 >= minLenght) { + for (int s = 0; s <= increase_1 - minLenght; s++) { + int len = minLenght + s; + for (int subIndex = 0; subIndex <= increase_1 - len; subIndex++) { + List pokers = ListUtils.getList(sellPokers_1.subList(subIndex * width, (subIndex + len) * width)); + pokerSells.add(new PokerSell(targetSellType, pokers, pokers.get(pokers.size() - 1).getLevel().getLevel())); } } - increase_1 = 0; - sellPokers_1.clear(); } + sellPokers_1.clear(); } } diff --git a/landlords-common/src/test/java/org/nico/ratel/landlords/helper/tests/PokerHelperTest.java b/landlords-common/src/test/java/org/nico/ratel/landlords/helper/tests/PokerHelperTest.java index d4566dc..36952c0 100644 --- a/landlords-common/src/test/java/org/nico/ratel/landlords/helper/tests/PokerHelperTest.java +++ b/landlords-common/src/test/java/org/nico/ratel/landlords/helper/tests/PokerHelperTest.java @@ -4,6 +4,7 @@ import org.junit.Before; import org.junit.Test; import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.entity.PokerSell; import org.nico.ratel.landlords.enums.PokerLevel; import org.nico.ratel.landlords.enums.PokerType; import org.nico.ratel.landlords.enums.SellType; @@ -13,209 +14,209 @@ public class PokerHelperTest { - private ArrayList pokers = new ArrayList<>(); - - @Before - public void setUp() { - pokers.add(new Poker(PokerLevel.LEVEL_3, PokerType.BLANK)); - pokers.add(new Poker(PokerLevel.LEVEL_4, PokerType.DIAMOND)); - pokers.add(new Poker(PokerLevel.LEVEL_4, PokerType.DIAMOND)); - pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.CLUB)); - pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.CLUB)); - pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.CLUB)); - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.SPADE)); - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - } - - @Test - public void testComparePoker() { - Assert.assertTrue(PokerHelper.comparePoker(new ArrayList<>(), new ArrayList<>())); - } - - @Test - public void testCheckPokerIndex() { - Assert.assertFalse(PokerHelper.checkPokerIndex(new int[]{}, new ArrayList<>())); - Assert.assertFalse(PokerHelper.checkPokerIndex(new int[]{2, -4_194_302}, new ArrayList<>())); - } - - @Test - public void testGetIndexes() { - Assert.assertNull(PokerHelper.getIndexes(new Character[]{'3', '4', '5', '6', '7', '8'}, pokers)); - Assert.assertNotNull(PokerHelper.getIndexes(new Character[]{}, new ArrayList<>())); - Assert.assertEquals(0,PokerHelper.getIndexes(new Character[]{}, new ArrayList<>()).length); - } - - @Test - public void testGetPoker() { - Assert.assertEquals(PokerLevel.LEVEL_3,PokerHelper.getPoker(new int[]{1, 2}, pokers).get(0).getLevel()); - Assert.assertEquals(PokerType.BLANK,PokerHelper.getPoker(new int[]{1, 2}, pokers).get(0).getType()); - Assert.assertEquals(PokerLevel.LEVEL_4,PokerHelper.getPoker(new int[]{1, 2}, pokers).get(1).getLevel()); - Assert.assertEquals(PokerType.DIAMOND,PokerHelper.getPoker(new int[]{1, 2}, pokers).get(1).getType()); - } - - @Test - public void testPrintPoker() { - Assert.assertNotNull(PokerHelper.printPoker(pokers)); - } - - @Test - public void testDistributePoker() { - Assert.assertNotNull(PokerHelper.distributePoker()); - } - - @Test - public void testCheckPokerType1() { - pokers.clear(); - Assert.assertNull(PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(-1,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.ILLEGAL,PokerHelper.checkPokerType(pokers).getSellType()); - - pokers.add(new Poker(PokerLevel.LEVEL_SMALL_KING, PokerType.BLANK)); - pokers.add(new Poker(PokerLevel.LEVEL_BIG_KING, PokerType.DIAMOND)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(2147483647,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.KING_BOMB,PokerHelper.checkPokerType(pokers).getSellType()); - } - - @Test - public void testCheckPokerType2() { - pokers.clear(); - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(7,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.SINGLE,PokerHelper.checkPokerType(pokers).getSellType()); - - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(7,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.DOUBLE,PokerHelper.checkPokerType(pokers).getSellType()); - - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(7,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.THREE,PokerHelper.checkPokerType(pokers).getSellType()); - - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(1027,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.BOMB,PokerHelper.checkPokerType(pokers).getSellType()); - - pokers.remove(pokers.size()-1); - pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.BLANK)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(7,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.THREE_ZONES_SINGLE,PokerHelper.checkPokerType(pokers).getSellType()); - - pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.BLANK)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(7,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.THREE_ZONES_DOUBLE,PokerHelper.checkPokerType(pokers).getSellType()); - - pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.BLANK)); - pokers.add(new Poker(PokerLevel.LEVEL_9, PokerType.BLANK)); - pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(8,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.THREE_STRAIGHT_WITH_SINGLE,PokerHelper.checkPokerType(pokers).getSellType()); - - pokers.add(new Poker(PokerLevel.LEVEL_9, PokerType.BLANK)); - pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(8,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.FOUR_STRAIGHT_WITH_DOUBLE,PokerHelper.checkPokerType(pokers).getSellType()); - } - - @Test - public void testCheckPokerType3() { - pokers.clear(); - pokers.add(new Poker(PokerLevel.LEVEL_6, 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_9, PokerType.BLANK)); - pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(10,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.SINGLE_STRAIGHT,PokerHelper.checkPokerType(pokers).getSellType()); - - pokers.add(new Poker(PokerLevel.LEVEL_6, 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_9, PokerType.BLANK)); - pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(10,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.DOUBLE_STRAIGHT,PokerHelper.checkPokerType(pokers).getSellType()); - - pokers.add(new Poker(PokerLevel.LEVEL_6, 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_9, PokerType.BLANK)); - pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(10,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.THREE_STRAIGHT,PokerHelper.checkPokerType(pokers).getSellType()); - - pokers.add(new Poker(PokerLevel.LEVEL_6, 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_9, PokerType.BLANK)); - pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(10,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.FOUR_STRAIGHT,PokerHelper.checkPokerType(pokers).getSellType()); - } - - @Test - public void testCheckPokerType4() { - pokers.clear(); - pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_9, PokerType.BLANK)); - pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(8,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.FOUR_ZONES_SINGLE,PokerHelper.checkPokerType(pokers).getSellType()); - - pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_6, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(8,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.FOUR_STRAIGHT_WITH_SINGLE,PokerHelper.checkPokerType(pokers).getSellType()); - } - - @Test - public void testCheckPokerType5() { - pokers.clear(); - pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_9, PokerType.BLANK)); - pokers.add(new Poker(PokerLevel.LEVEL_9, PokerType.BLANK)); - pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); - pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(8,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.FOUR_ZONES_DOUBLE,PokerHelper.checkPokerType(pokers).getSellType()); - - pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_6, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_6, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); - Assert.assertEquals(pokers,PokerHelper.checkPokerType(pokers).getSellPokers()); - Assert.assertEquals(8,PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.FOUR_STRAIGHT_WITH_DOUBLE,PokerHelper.checkPokerType(pokers).getSellType()); - } + private ArrayList pokers = new ArrayList<>(); + + @Before + public void setUp() { + pokers.add(new Poker(PokerLevel.LEVEL_3, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_4, PokerType.DIAMOND)); + pokers.add(new Poker(PokerLevel.LEVEL_4, PokerType.DIAMOND)); + pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.CLUB)); + pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.CLUB)); + pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.CLUB)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.SPADE)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + } + + @Test + public void testComparePoker() { + Assert.assertTrue(PokerHelper.comparePoker(new ArrayList<>(), new ArrayList<>())); + } + + @Test + public void testCheckPokerIndex() { + Assert.assertFalse(PokerHelper.checkPokerIndex(new int[]{}, new ArrayList<>())); + Assert.assertFalse(PokerHelper.checkPokerIndex(new int[]{2, -4_194_302}, new ArrayList<>())); + } + + @Test + public void testGetIndexes() { + Assert.assertNull(PokerHelper.getIndexes(new Character[]{'3', '4', '5', '6', '7', '8'}, pokers)); + Assert.assertNotNull(PokerHelper.getIndexes(new Character[]{}, new ArrayList<>())); + Assert.assertEquals(0, PokerHelper.getIndexes(new Character[]{}, new ArrayList<>()).length); + } + + @Test + public void testGetPoker() { + Assert.assertEquals(PokerLevel.LEVEL_3, PokerHelper.getPoker(new int[]{1, 2}, pokers).get(0).getLevel()); + Assert.assertEquals(PokerType.BLANK, PokerHelper.getPoker(new int[]{1, 2}, pokers).get(0).getType()); + Assert.assertEquals(PokerLevel.LEVEL_4, PokerHelper.getPoker(new int[]{1, 2}, pokers).get(1).getLevel()); + Assert.assertEquals(PokerType.DIAMOND, PokerHelper.getPoker(new int[]{1, 2}, pokers).get(1).getType()); + } + + @Test + public void testPrintPoker() { + Assert.assertNotNull(PokerHelper.printPoker(pokers)); + } + + @Test + public void testDistributePoker() { + Assert.assertNotNull(PokerHelper.distributePoker()); + } + + @Test + public void testCheckPokerType1() { + pokers.clear(); + Assert.assertNull(PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(-1, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.ILLEGAL, PokerHelper.checkPokerType(pokers).getSellType()); + + pokers.add(new Poker(PokerLevel.LEVEL_SMALL_KING, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_BIG_KING, PokerType.DIAMOND)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(2147483647, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.KING_BOMB, PokerHelper.checkPokerType(pokers).getSellType()); + } + + @Test + public void testCheckPokerType2() { + pokers.clear(); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(7, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.SINGLE, PokerHelper.checkPokerType(pokers).getSellType()); + + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(7, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.DOUBLE, PokerHelper.checkPokerType(pokers).getSellType()); + + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(7, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.THREE, PokerHelper.checkPokerType(pokers).getSellType()); + + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(1027, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.BOMB, PokerHelper.checkPokerType(pokers).getSellType()); + + pokers.remove(pokers.size() - 1); + pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.BLANK)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(7, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.THREE_ZONES_SINGLE, PokerHelper.checkPokerType(pokers).getSellType()); + + pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.BLANK)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(7, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.THREE_ZONES_DOUBLE, PokerHelper.checkPokerType(pokers).getSellType()); + + pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_9, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(8, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.THREE_STRAIGHT_WITH_SINGLE, PokerHelper.checkPokerType(pokers).getSellType()); + + pokers.add(new Poker(PokerLevel.LEVEL_9, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(8, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.FOUR_STRAIGHT_WITH_DOUBLE, PokerHelper.checkPokerType(pokers).getSellType()); + } + + @Test + public void testCheckPokerType3() { + pokers.clear(); + pokers.add(new Poker(PokerLevel.LEVEL_6, 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_9, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(10, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.SINGLE_STRAIGHT, PokerHelper.checkPokerType(pokers).getSellType()); + + pokers.add(new Poker(PokerLevel.LEVEL_6, 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_9, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(10, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.DOUBLE_STRAIGHT, PokerHelper.checkPokerType(pokers).getSellType()); + + pokers.add(new Poker(PokerLevel.LEVEL_6, 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_9, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(10, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.THREE_STRAIGHT, PokerHelper.checkPokerType(pokers).getSellType()); + + pokers.add(new Poker(PokerLevel.LEVEL_6, 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_9, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(10, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.FOUR_STRAIGHT, PokerHelper.checkPokerType(pokers).getSellType()); + } + + @Test + public void testCheckPokerType4() { + pokers.clear(); + pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_9, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(8, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.FOUR_ZONES_SINGLE, PokerHelper.checkPokerType(pokers).getSellType()); + + pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_6, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(8, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.FOUR_STRAIGHT_WITH_SINGLE, PokerHelper.checkPokerType(pokers).getSellType()); + } + + @Test + public void testCheckPokerType5() { + pokers.clear(); + pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_9, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_9, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(8, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.FOUR_ZONES_DOUBLE, PokerHelper.checkPokerType(pokers).getSellType()); + + pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_6, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_6, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.HEART)); + Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); + Assert.assertEquals(8, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.FOUR_STRAIGHT_WITH_DOUBLE, PokerHelper.checkPokerType(pokers).getSellType()); + } } 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 3a7f4b0..009eaac 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 @@ -15,23 +15,24 @@ public void call(ClientSide clientSide, String data) { Room room = ServerContains.getRoom(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()) { - if(client.getRole() == ClientRole.PLAYER){ - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_CLIENT_EXIT, result); - client.init(); - } + if (room == null) { + return; + } + String result = MapHelper.newInstance() + .put("roomId", room.getId()) + .put("exitClientId", clientSide.getId()) + .put("exitClientNickname", clientSide.getNickname()) + .json(); + for (ClientSide client : room.getClientSideList()) { + if (client.getRole() == ClientRole.PLAYER) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_CLIENT_EXIT, result); + client.init(); } + } - notifyWatcherClientExit(room, clientSide); + notifyWatcherClientExit(room, clientSide); - ServerContains.removeRoom(room.getId()); - } + ServerContains.removeRoom(room.getId()); } /** diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java index 1161020..1bc0475 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java @@ -12,14 +12,14 @@ public class ServerEventListener_CODE_CLIENT_NICKNAME_SET implements ServerEvent @Override public void call(ClientSide client, String nickname) { - - if (nickname.trim().length() > NICKNAME_MAX_LENGTH) { + + if (nickname.trim().length() > NICKNAME_MAX_LENGTH || nickname.trim().isEmpty()) { String result = MapHelper.newInstance().put("invalidLength", nickname.trim().length()).json(); ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_CLIENT_NICKNAME_SET, result); - }else{ - ServerContains.CLIENT_SIDE_MAP.get(client.getId()).setNickname(nickname); - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_OPTIONS, null); + return; } + ServerContains.CLIENT_SIDE_MAP.get(client.getId()).setNickname(nickname); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_OPTIONS, null); } } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_OFFLINE.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_OFFLINE.java index 38ca36a..d17ac5b 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_OFFLINE.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_OFFLINE.java @@ -15,24 +15,25 @@ public void call(ClientSide clientSide, String data) { Room room = ServerContains.getRoom(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()) { - if(client.getRole() == ClientRole.PLAYER){ - if(client.getId() != clientSide.getId()){ - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_CLIENT_EXIT, result); - client.init(); - } - } + if (room == null) { + ServerContains.CLIENT_SIDE_MAP.remove(clientSide.getId()); + return; + } + + String result = MapHelper.newInstance() + .put("roomId", room.getId()) + .put("exitClientId", clientSide.getId()) + .put("exitClientNickname", clientSide.getNickname()) + .json(); + for (ClientSide client : room.getClientSideList()) { + if (client.getRole() != ClientRole.PLAYER) { + continue; + } + if (client.getId() != clientSide.getId()) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_CLIENT_EXIT, result); + client.init(); } - ServerContains.removeRoom(room.getId()); } - - ServerContains.CLIENT_SIDE_MAP.remove(clientSide.getId()); + ServerContains.removeRoom(room.getId()); } - } 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 d7afa6f..d33141f 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 @@ -15,82 +15,83 @@ import java.util.Map; -public class ServerEventListener_CODE_GAME_LANDLORD_ELECT implements ServerEventListener{ +public class ServerEventListener_CODE_GAME_LANDLORD_ELECT implements ServerEventListener { @Override public void call(ClientSide clientSide, String data) { Room room = ServerContains.getRoom(clientSide.getRoomId()); - - if(room != null) { - boolean isY = Boolean.valueOf(data); - if(isY){ - clientSide.getPokers().addAll(room.getLandlordPokers()); - PokerHelper.sortPoker(clientSide.getPokers()); - - int currentClientId = clientSide.getId(); - room.setLandlordId(currentClientId); - room.setLastSellClient(currentClientId); - room.setCurrentSellClient(currentClientId); - clientSide.setType(ClientType.LANDLORD); - - for(ClientSide client: room.getClientSideList()){ - 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("additionalPokers", room.getLandlordPokers()) - .json(); - - if(client.getRole() == ClientRole.PLAYER) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_CONFIRM, result); - }else { - if(currentClientId == client.getId()) { - RobotEventListener.get(ClientEventCode.CODE_GAME_POKER_PLAY).call(client, result); - } - } - } - notifyWatcherConfirmLandlord(room, clientSide); - }else{ - if(clientSide.getNext().getId() == room.getFirstSellClient()){ - for(ClientSide client: room.getClientSideList()){ - if(client.getRole() == ClientRole.PLAYER) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_CYCLE, null); - } - } - - ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, null); - }else{ - ClientSide turnClientSide = clientSide.getNext(); - room.setCurrentSellClient(turnClientSide.getId()); - String result = MapHelper.newInstance() - .put("roomId", room.getId()) - .put("roomOwner", room.getRoomOwner()) - .put("roomClientCount", room.getClientSideList().size()) - .put("preClientNickname", clientSide.getNickname()) - .put("nextClientNickname", turnClientSide.getNickname()) - .put("nextClientId", turnClientSide.getId()) - .json(); - - for(ClientSide client: room.getClientSideList()) { - if(client.getRole() == ClientRole.PLAYER) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_ELECT, result); - }else { - if(client.getId() == turnClientSide.getId()) { - RobotEventListener.get(ClientEventCode.CODE_GAME_LANDLORD_ELECT).call(client, result); - } - } - } - - notifyWatcherRobLandlord(room, clientSide); + if (room == null) { + return; + } + if (Boolean.parseBoolean(data)) { + confirmLandlord(clientSide, room); + return; + } + if (clientSide.getNext().getId() == room.getFirstSellClient()) { + for (ClientSide client : room.getClientSideList()) { + if (client.getRole() == ClientRole.PLAYER) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_CYCLE, null); } } - }else { -// ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_PLAY_FAIL_BY_INEXIST, null); + ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, null); + return; + } + ClientSide turnClientSide = clientSide.getNext(); + room.setCurrentSellClient(turnClientSide.getId()); + String result = MapHelper.newInstance() + .put("roomId", room.getId()) + .put("roomOwner", room.getRoomOwner()) + .put("roomClientCount", room.getClientSideList().size()) + .put("preClientNickname", clientSide.getNickname()) + .put("nextClientNickname", turnClientSide.getNickname()) + .put("nextClientId", turnClientSide.getId()) + .json(); + + for (ClientSide client : room.getClientSideList()) { + if (client.getRole() == ClientRole.PLAYER) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_ELECT, result); + continue; + } + if (client.getId() == turnClientSide.getId()) { + RobotEventListener.get(ClientEventCode.CODE_GAME_LANDLORD_ELECT).call(client, result); + } } + notifyWatcherRobLandlord(room, clientSide); + } + + public void confirmLandlord(ClientSide clientSide, Room room) { + clientSide.getPokers().addAll(room.getLandlordPokers()); + PokerHelper.sortPoker(clientSide.getPokers()); + + int currentClientId = clientSide.getId(); + room.setLandlordId(currentClientId); + room.setLastSellClient(currentClientId); + room.setCurrentSellClient(currentClientId); + clientSide.setType(ClientType.LANDLORD); + + for (ClientSide client : room.getClientSideList()) { + 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("additionalPokers", room.getLandlordPokers()) + .json(); + + if (client.getRole() == ClientRole.PLAYER) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_LANDLORD_CONFIRM, result); + continue; + } + + if (currentClientId == client.getId()) { + RobotEventListener.get(ClientEventCode.CODE_GAME_POKER_PLAY).call(client, result); + } + } + + notifyWatcherConfirmLandlord(room, clientSide); } /** 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 6d9840c..83060cb 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 @@ -8,117 +8,111 @@ 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.enums.ClientEventCode; -import org.nico.ratel.landlords.enums.ClientRole; -import org.nico.ratel.landlords.enums.SellType; -import org.nico.ratel.landlords.enums.ServerEventCode; +import org.nico.ratel.landlords.enums.*; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.server.ServerContains; import org.nico.ratel.landlords.server.robot.RobotEventListener; -public class ServerEventListener_CODE_GAME_POKER_PLAY implements ServerEventListener{ +public class ServerEventListener_CODE_GAME_POKER_PLAY implements ServerEventListener { @Override public void call(ClientSide clientSide, String data) { Room room = ServerContains.getRoom(clientSide.getRoomId()); - if(room != null) { - 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(currentPokerShell.getSellType() != SellType.ILLEGAL) { - if(room.getLastSellClient() != clientSide.getId() && room.getLastPokerShell() != null){ - PokerSell lastPokerShell = room.getLastPokerShell(); - - if((lastPokerShell.getSellType() != currentPokerShell.getSellType() || lastPokerShell.getSellPokers().size() != currentPokerShell.getSellPokers().size()) && currentPokerShell.getSellType() != SellType.BOMB && currentPokerShell.getSellType() != SellType.KING_BOMB) { - String result = MapHelper.newInstance() - .put("playType", currentPokerShell.getSellType()) - .put("playCount", currentPokerShell.getSellPokers().size()) - .put("preType", lastPokerShell.getSellType()) - .put("preCount", lastPokerShell.getSellPokers().size()) - .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(); - - room.setLastSellClient(clientSide.getId()); - room.setLastPokerShell(currentPokerShell); - room.setCurrentSellClient(next.getId()); - - clientSide.getPokers().removeAll(currentPokers); - MapHelper mapHelper = MapHelper.newInstance() - .put("clientId", clientSide.getId()) - .put("clientNickname", clientSide.getNickname()) - .put("clientType", clientSide.getType()) - .put("pokers", currentPokers) - .put("lastSellClientId", clientSide.getId()) - .put("lastSellPokers", currentPokers); - - if(! clientSide.getPokers().isEmpty()) { - mapHelper.put("sellClinetNickname", next.getNickname()); - } - - String result = mapHelper.json(); - - for(ClientSide client: room.getClientSideList()) { - if(client.getRole() == ClientRole.PLAYER) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_POKERS, result); - } - } - - notifyWatcherPlayPoker(room, result); - - if(clientSide.getPokers().isEmpty()) { - result = MapHelper.newInstance() - .put("winnerNickname", clientSide.getNickname()) - .put("winnerType", clientSide.getType()) - .json(); - - for(ClientSide client: room.getClientSideList()) { - if(client.getRole() == ClientRole.PLAYER) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, result); - } - } - - notifyWatcherGameOver(room, result); - - ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(clientSide, data); - }else { - if(next.getRole() == ClientRole.PLAYER) { - ServerEventListener.get(ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT).call(next, result); - }else { - RobotEventListener.get(ClientEventCode.CODE_GAME_POKER_PLAY).call(next, data); - } - } - } - }else{ - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_INVALID, null); + if (room == null) { + return; + } + if (room.getCurrentSellClient() != clientSide.getId()) { + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_ORDER_ERROR, null); + return; + } + Character[] options = Noson.convert(data, Character[].class); + int[] indexes = PokerHelper.getIndexes(options, clientSide.getPokers()); + if (!PokerHelper.checkPokerIndex(indexes, clientSide.getPokers())) { + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_INVALID, null); + return; + } + + boolean sellFlag = true; + List currentPokers = PokerHelper.getPoker(indexes, clientSide.getPokers()); + PokerSell currentPokerSell = PokerHelper.checkPokerType(currentPokers); + if (currentPokerSell.getSellType() == SellType.ILLEGAL) { + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_INVALID, null); + return; + } + if (room.getLastSellClient() != clientSide.getId() && room.getLastPokerShell() != null) { + PokerSell lastPokerSell = room.getLastPokerShell(); + + if ((lastPokerSell.getSellType() != currentPokerSell.getSellType() || lastPokerSell.getSellPokers().size() != currentPokerSell.getSellPokers().size()) && currentPokerSell.getSellType() != SellType.BOMB && currentPokerSell.getSellType() != SellType.KING_BOMB) { + String result = MapHelper.newInstance() + .put("playType", currentPokerSell.getSellType()) + .put("playCount", currentPokerSell.getSellPokers().size()) + .put("preType", lastPokerSell.getSellType()) + .put("preCount", lastPokerSell.getSellPokers().size()) + .json(); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_MISMATCH, result); + return; + } + if (lastPokerSell.getScore() >= currentPokerSell.getScore()) { + String result = MapHelper.newInstance() + .put("playScore", currentPokerSell.getScore()) + .put("preScore", lastPokerSell.getScore()) + .json(); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_LESS, result); + return; + } + } + + ClientSide next = clientSide.getNext(); + + room.setLastSellClient(clientSide.getId()); + room.setLastPokerShell(currentPokerSell); + room.setCurrentSellClient(next.getId()); + + clientSide.getPokers().removeAll(currentPokers); + MapHelper mapHelper = MapHelper.newInstance() + .put("clientId", clientSide.getId()) + .put("clientNickname", clientSide.getNickname()) + .put("clientType", clientSide.getType()) + .put("pokers", currentPokers) + .put("lastSellClientId", clientSide.getId()) + .put("lastSellPokers", currentPokers); + + if (!clientSide.getPokers().isEmpty()) { + mapHelper.put("sellClinetNickname", next.getNickname()); + } + + String result = mapHelper.json(); + + for (ClientSide client : room.getClientSideList()) { + if (client.getRole() == ClientRole.PLAYER) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_SHOW_POKERS, result); + } + } + + notifyWatcherPlayPoker(room, result); + + if (clientSide.getPokers().isEmpty()) { + result = MapHelper.newInstance() + .put("winnerNickname", clientSide.getNickname()) + .put("winnerType", clientSide.getType()) + .json(); + + for(ClientSide client: room.getClientSideList()) { + if(client.getRole() == ClientRole.PLAYER) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, result); } + } + + notifyWatcherGameOver(room, result); + + ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(clientSide, data); + } else { + if(next.getRole() == ClientRole.PLAYER) { + ServerEventListener.get(ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT).call(next, result); }else { - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_ORDER_ERROR, null); + RobotEventListener.get(ClientEventCode.CODE_GAME_POKER_PLAY).call(next, data); } - }else { -// ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_PLAY_FAIL_BY_INEXIST, null); } } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java index ecb4f02..274ffd7 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java @@ -26,12 +26,7 @@ public void call(ClientSide clientSide, String data) { clientSide.setRoomId(room.getId()); ServerContains.addRoom(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_CREATE_PVE.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE_PVE.java index 27576c1..586c589 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE_PVE.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE_PVE.java @@ -3,63 +3,54 @@ 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.ClientRole; -import org.nico.ratel.landlords.enums.ClientStatus; -import org.nico.ratel.landlords.enums.RoomStatus; -import org.nico.ratel.landlords.enums.RoomType; -import org.nico.ratel.landlords.enums.ServerEventCode; +import org.nico.ratel.landlords.enums.*; import org.nico.ratel.landlords.robot.RobotDecisionMakers; import org.nico.ratel.landlords.server.ServerContains; -public class ServerEventListener_CODE_ROOM_CREATE_PVE implements ServerEventListener{ +public class ServerEventListener_CODE_ROOM_CREATE_PVE implements ServerEventListener { @Override public void call(ClientSide clientSide, String data) { - - int difficultyCoefficient = Integer.valueOf(data); - if(RobotDecisionMakers.contains(difficultyCoefficient)) { - Room room = new Room(ServerContains.getServerId()); - room.setType(RoomType.PVE); - room.setStatus(RoomStatus.BLANK); - room.setRoomOwner(clientSide.getNickname()); - room.getClientSideMap().put(clientSide.getId(), clientSide); - room.getClientSideList().add(clientSide); - room.setCurrentSellClient(clientSide.getId()); - room.setCreateTime(System.currentTimeMillis()); - room.setDifficultyCoefficient(difficultyCoefficient); - - clientSide.setRoomId(room.getId()); - ServerContains.addRoom(room); - - ClientSide preClient = clientSide; - //Add robots - for(int index = 1; index < 3; index ++) { - ClientSide robot = new ClientSide(- ServerContains.getClientId(), ClientStatus.PLAYING, null); - robot.setNickname("robot_" + index); - robot.setRole(ClientRole.ROBOT); - preClient.setNext(robot); - robot.setPre(preClient); - robot.setRoomId(room.getId()); - room.getClientSideMap().put(robot.getId(), robot); - room.getClientSideList().add(robot); - - preClient = robot; - ServerContains.CLIENT_SIDE_MAP.put(robot.getId(), robot); - } - preClient.setNext(clientSide); - clientSide.setPre(preClient); - - ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(room.getId())); - }else { + int difficultyCoefficient = Integer.parseInt(data); + if (!RobotDecisionMakers.contains(difficultyCoefficient)) { ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_PVE_DIFFICULTY_NOT_SUPPORT, null); + return; } - - } - + Room room = new Room(ServerContains.getServerId()); + room.setType(RoomType.PVE); + room.setStatus(RoomStatus.BLANK); + room.setRoomOwner(clientSide.getNickname()); + room.getClientSideMap().put(clientSide.getId(), clientSide); + room.getClientSideList().add(clientSide); + room.setCurrentSellClient(clientSide.getId()); + room.setCreateTime(System.currentTimeMillis()); + room.setDifficultyCoefficient(difficultyCoefficient); + + clientSide.setRoomId(room.getId()); + ServerContains.addRoom(room); + + ClientSide preClient = clientSide; + //Add robots + for (int index = 1; index < 3; index++) { + ClientSide robot = new ClientSide(-ServerContains.getClientId(), ClientStatus.PLAYING, null); + robot.setNickname("robot_" + index); + robot.setRole(ClientRole.ROBOT); + preClient.setNext(robot); + robot.setPre(preClient); + robot.setRoomId(room.getId()); + room.getClientSideMap().put(robot.getId(), robot); + room.getClientSideList().add(robot); + + preClient = robot; + ServerContains.CLIENT_SIDE_MAP.put(robot.getId(), robot); + } + preClient.setNext(clientSide); + clientSide.setPre(preClient); + ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(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 90fd764..ffddc41 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 @@ -7,68 +7,69 @@ import org.nico.ratel.landlords.entity.ClientSide; import org.nico.ratel.landlords.entity.Room; import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ClientStatus; import org.nico.ratel.landlords.enums.RoomStatus; import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.server.ServerContains; -public class ServerEventListener_CODE_ROOM_JOIN implements ServerEventListener{ +public class ServerEventListener_CODE_ROOM_JOIN implements ServerEventListener { @Override public void call(ClientSide clientSide, String data) { - Room room = ServerContains.getRoom(Integer.valueOf(data)); - - if(room == null) { + Room room = ServerContains.getRoom(Integer.parseInt(data)); + + if (room == null) { String result = MapHelper.newInstance() - .put("roomId", data) - .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()); - - ConcurrentSkipListMap roomClientMap = (ConcurrentSkipListMap) room.getClientSideMap(); - LinkedList roomClientList = room.getClientSideList(); + return; + } + 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); + return; + } + clientSide.setRoomId(room.getId()); - if(roomClientList.size() > 0){ - ClientSide pre = roomClientList.getLast(); - pre.setNext(clientSide); - clientSide.setPre(pre); - } + ConcurrentSkipListMap roomClientMap = (ConcurrentSkipListMap) room.getClientSideMap(); + LinkedList roomClientList = room.getClientSideList(); - roomClientList.add(clientSide); - roomClientMap.put(clientSide.getId(), clientSide); + if (roomClientList.size() > 0) { + ClientSide pre = roomClientList.getLast(); + pre.setNext(clientSide); + clientSide.setPre(pre); + } - if(roomClientMap.size() == 3) { - clientSide.setNext(roomClientList.getFirst()); - roomClientList.getFirst().setPre(clientSide); + roomClientList.add(clientSide); + roomClientMap.put(clientSide.getId(), clientSide); - ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(room.getId())); - }else { - room.setStatus(RoomStatus.WAIT); + if (roomClientMap.size() == 3) { + clientSide.setNext(roomClientList.getFirst()); + roomClientList.getFirst().setPre(clientSide); + ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(room.getId())); + return; + } - 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()) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_ROOM_JOIN_SUCCESS, result); - } + room.setStatus(RoomStatus.WAIT); - notifyWatcherJoinRoom(room, clientSide); - } - } + 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()) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_ROOM_JOIN_SUCCESS, result); } + + notifyWatcherJoinRoom(room, clientSide); } /** diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java index 012522b..008e3db 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java @@ -58,39 +58,39 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E cause.printStackTrace(); } } - - @Override - public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { - if (evt instanceof IdleStateEvent) { - IdleStateEvent event = (IdleStateEvent) evt; - if (event.state() == IdleState.READER_IDLE) { - try{ - clientOfflineEvent(ctx.channel()); - ctx.channel().close(); - }catch(Exception e){ - } - } - } else { - super.userEventTriggered(ctx, evt); - } - } - - private int getId(Channel channel){ - String longId = channel.id().asLongText(); - Integer clientId = ServerContains.CHANNEL_ID_MAP.get(longId); - if(null == clientId){ - clientId = ServerContains.getClientId(); - ServerContains.CHANNEL_ID_MAP.put(longId, clientId); - } - return clientId; - } - - private void clientOfflineEvent(Channel channel){ - int clientId = getId(channel); - ClientSide client = ServerContains.CLIENT_SIDE_MAP.get(clientId); - if(client != null) { + + @Override + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { + if (evt instanceof IdleStateEvent) { + IdleStateEvent event = (IdleStateEvent) evt; + if (event.state() == IdleState.READER_IDLE) { + try { + clientOfflineEvent(ctx.channel()); + ctx.channel().close(); + } catch (Exception ignore) { + } + } + } else { + super.userEventTriggered(ctx, evt); + } + } + + private int getId(Channel channel) { + String longId = channel.id().asLongText(); + Integer clientId = ServerContains.CHANNEL_ID_MAP.get(longId); + if (null == clientId) { + clientId = ServerContains.getClientId(); + ServerContains.CHANNEL_ID_MAP.put(longId, clientId); + } + return clientId; + } + + private void clientOfflineEvent(Channel channel) { + int clientId = getId(channel); + ClientSide client = ServerContains.CLIENT_SIDE_MAP.get(clientId); + if (client != null) { SimplePrinter.serverLog("Has client exit to the server:" + clientId + " | " + client.getNickname()); ServerEventListener.get(ServerEventCode.CODE_CLIENT_OFFLINE).call(client, null); } - } + } } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/timer/RoomClearTask.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/timer/RoomClearTask.java index f355cb4..cfd44ed 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/timer/RoomClearTask.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/timer/RoomClearTask.java @@ -17,124 +17,117 @@ import org.nico.ratel.landlords.server.event.ServerEventListener; import org.nico.ratel.landlords.server.robot.RobotEventListener; -/** - * +/** + * * @author nico */ -public class RoomClearTask extends TimerTask{ +public class RoomClearTask extends TimerTask { //The room wait time of after create is 100s - private static long waitingStatusInterval = 1000 * 100; + private static final long waitingStatusInterval = 1000 * 100; //The room starting destroy time is 100s - private static long startingStatusInterval = 1000 * 100; + private static final long startingStatusInterval = 1000 * 300; //The room live time is 600s - private static long liveTime = 1000 * 60 * 20; - + private static final long liveTime = 1000 * 60 * 20; + @Override public void run() { try { doing(); - }catch(Exception e) { + } catch (Exception e) { SimplePrinter.serverLog(e.getMessage()); } - + } - + public void doing() { Map rooms = ServerContains.getRoomMap(); - if(rooms != null && ! rooms.isEmpty()){ - long now = System.currentTimeMillis(); - for(Room room: rooms.values()){ - long alreadyLiveTime = System.currentTimeMillis() - room.getCreateTime(); - SimplePrinter.serverLog("room " + room.getId() + " already live " + alreadyLiveTime + "ms"); - if(alreadyLiveTime > liveTime) { - SimplePrinter.serverLog("room " + room.getId() + " live time overflow " + liveTime + ", closed!"); - ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(room.getClientSideList().get(0), null); - }else { - long diff = now - room.getLastFlushTime(); - - if(room.getStatus() != RoomStatus.STARTING){ - long interval = waitingStatusInterval; - - if(diff > interval) { - SimplePrinter.serverLog("room " + room.getId() + " starting waiting time overflow " + waitingStatusInterval + ", closed!"); - - ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(room.getClientSideList().get(0), null); - } - }else{ - if(room.getType() == RoomType.PVP) { - long interval = startingStatusInterval; - - if(diff > interval){ - boolean allRobots = true; - for(ClientSide client: room.getClientSideList()) { - if(client.getId() != room.getCurrentSellClient() && client.getRole() == ClientRole.PLAYER) { - allRobots = false; - break; - } - } - - ClientSide currentPlayer = room.getClientSideMap().get(room.getCurrentSellClient()); - - if(allRobots) { - SimplePrinter.serverLog("room " + room.getId() + " all is robots, closed!"); - - ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(currentPlayer, null); - }else { - //kick this client - ChannelUtils.pushToClient(currentPlayer.getChannel(), ClientEventCode.CODE_CLIENT_KICK, null); - - notifyWatcherClientKick(room, currentPlayer); - - //client current player - room.getClientSideMap().remove(currentPlayer.getId()); - room.getClientSideList().remove(currentPlayer); - - ClientSide robot = new ClientSide(- ServerContains.getClientId(), ClientStatus.PLAYING, null); - robot.setNickname(currentPlayer.getNickname()); - robot.setRole(ClientRole.ROBOT); - robot.setRoomId(room.getId()); - robot.setNext(currentPlayer.getNext()); - robot.setPre(currentPlayer.getPre()); - robot.getNext().setPre(robot); - robot.getPre().setNext(robot); - robot.setPokers(currentPlayer.getPokers()); - robot.setType(currentPlayer.getType()); - - room.getClientSideMap().put(robot.getId(), robot); - room.getClientSideList().add(robot); - room.setCurrentSellClient(robot.getId()); - - //If last sell client is current client, replace it to robot id - if(room.getLastSellClient() == currentPlayer.getId()) { - room.setLastSellClient(robot.getId()); - } - - - //set robot difficulty -> simple - room.setDifficultyCoefficient(1); - - ServerContains.CLIENT_SIDE_MAP.put(robot.getId(), robot); - - //init client - currentPlayer.init(); - - SimplePrinter.serverLog("room " + room.getId() + " player " + currentPlayer.getNickname() + " " + startingStatusInterval + "ms not operating, automatic custody!"); - - if(room.getLandlordId() == -1){ - RobotEventListener.get(ClientEventCode.CODE_GAME_LANDLORD_ELECT).call(robot, null); - }else{ - RobotEventListener.get(ClientEventCode.CODE_GAME_POKER_PLAY).call(robot, null); - } - } - } - } - } + if (rooms == null || rooms.isEmpty()) { + return; + } + + long now = System.currentTimeMillis(); + for (Room room : rooms.values()) { + long alreadyLiveTime = System.currentTimeMillis() - room.getCreateTime(); + SimplePrinter.serverLog("room " + room.getId() + " already live " + alreadyLiveTime + "ms"); + if (alreadyLiveTime > liveTime) { + SimplePrinter.serverLog("room " + room.getId() + " live time overflow " + liveTime + ", closed!"); + ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(room.getClientSideList().get(0), null); + return; + } + + long diff = now - room.getLastFlushTime(); + if (room.getStatus() != RoomStatus.STARTING && diff > waitingStatusInterval) { + SimplePrinter.serverLog("room " + room.getId() + " starting waiting time overflow " + waitingStatusInterval + ", closed!"); + ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(room.getClientSideList().get(0), null); + return; + } + if (room.getType() != RoomType.PVP) { + return; + } + + if (diff <= startingStatusInterval) { + return; + } + + boolean allRobots = true; + for (ClientSide client : room.getClientSideList()) { + if (client.getId() != room.getCurrentSellClient() && client.getRole() == ClientRole.PLAYER) { + allRobots = false; + break; } } + + ClientSide currentPlayer = room.getClientSideMap().get(room.getCurrentSellClient()); + + if (allRobots) { + SimplePrinter.serverLog("room " + room.getId() + " all is robots, closed!"); + ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(currentPlayer, null); + return; + } + //kick this client + ChannelUtils.pushToClient(currentPlayer.getChannel(), ClientEventCode.CODE_CLIENT_KICK, null); + + notifyWatcherClientKick(room, currentPlayer); + + //client current player + room.getClientSideMap().remove(currentPlayer.getId()); + room.getClientSideList().remove(currentPlayer); + + ClientSide robot = new ClientSide(-ServerContains.getClientId(), ClientStatus.PLAYING, null); + robot.setNickname(currentPlayer.getNickname()); + robot.setRole(ClientRole.ROBOT); + robot.setRoomId(room.getId()); + robot.setNext(currentPlayer.getNext()); + robot.setPre(currentPlayer.getPre()); + robot.getNext().setPre(robot); + robot.getPre().setNext(robot); + robot.setPokers(currentPlayer.getPokers()); + robot.setType(currentPlayer.getType()); + + room.getClientSideMap().put(robot.getId(), robot); + room.getClientSideList().add(robot); + room.setCurrentSellClient(robot.getId()); + + //If last sell client is current client, replace it to robot id + if (room.getLastSellClient() == currentPlayer.getId()) { + room.setLastSellClient(robot.getId()); + } + + //set robot difficulty -> simple + room.setDifficultyCoefficient(1); + + ServerContains.CLIENT_SIDE_MAP.put(robot.getId(), robot); + + //init client + currentPlayer.init(); + + SimplePrinter.serverLog("room " + room.getId() + " player " + currentPlayer.getNickname() + " " + startingStatusInterval + "ms not operating, automatic custody!"); + + RobotEventListener.get(room.getLandlordId() == -1 ? ClientEventCode.CODE_GAME_LANDLORD_ELECT : ClientEventCode.CODE_GAME_POKER_PLAY).call(robot, null); } } From 737fea270050f956df214517d6f58a572ba5e10a Mon Sep 17 00:00:00 2001 From: doveeeee Date: Tue, 13 Jul 2021 20:11:54 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=BA=9Bbug=20=20=20=20=201.=20333444555667=E5=87=BA=E7=89=8C?= =?UTF-8?q?=E5=9C=BA=E6=99=AF=E4=B8=AD667=E4=BC=9A=E8=A2=AB=E8=AE=A4?= =?UTF-8?q?=E4=B8=BA=E6=98=AF=E4=B8=80=E4=B8=AA=E5=AF=B9=E5=AD=90+?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=8D=95=E6=8E=92=EF=BC=8C=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E5=87=BA=E7=89=8C=20=20=20=20=202.=20?= =?UTF-8?q?=E9=A3=9E=E6=9C=BA=E5=B8=A6=E5=AF=B9=E4=BC=9A=E8=A2=AB=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E4=B8=BA=E5=9B=9B=E9=A1=BA=E5=AD=90=E5=B8=A6=E5=AF=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...istener_CODE_GAME_POKER_PLAY_REDIRECT.java | 2 +- .../ratel/landlords/helper/PokerHelper.java | 6 +++-- .../helper/tests/PokerHelperTest.java | 23 ++++++++++++++++++- ...verEventListener_CODE_GAME_POKER_PLAY.java | 2 +- ...istener_CODE_GAME_POKER_PLAY_REDIRECT.java | 2 +- 5 files changed, 29 insertions(+), 6 deletions(-) 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 index 3f25bfa..8cdafc3 100644 --- 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 @@ -41,7 +41,7 @@ public void call(Channel channel, String data) { if (sellClientId == SimpleClient.id) { get(ClientEventCode.CODE_GAME_POKER_PLAY).call(channel, data); } else { - SimplePrinter.printNotice("It is " + map.get("sellClinetNickname") + "'s turn. Please wait for him to play his cards."); + SimplePrinter.printNotice("It is " + map.get("sellClientNickname") + "'s turn. Please wait for him to play his cards."); } } 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 edeb36d..55eaf36 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 @@ -226,8 +226,10 @@ public static PokerSell checkPokerType(List pokers) { return new PokerSell(SellType.THREE_ZONES_DOUBLE, pokers, threeEndIndex); } if (threeEndIndex - threeStartIndex + 1 == threeCount && threeEndIndex < PokerLevel.LEVEL_2.getLevel()) { - return new PokerSell(SellType.FOUR_STRAIGHT_WITH_DOUBLE, pokers, threeEndIndex); + return new PokerSell(SellType.THREE_STRAIGHT_WITH_DOUBLE, pokers, threeEndIndex); } + } else if (singleCount + doubleCount * 2 == threeCount && fourCount == 0) { + return new PokerSell(SellType.THREE_STRAIGHT_WITH_SINGLE, pokers, threeEndIndex); } } @@ -260,7 +262,7 @@ public static int parseScore(SellType sellType, int level) { return level; } else if (sellType == SellType.SINGLE_STRAIGHT || sellType == SellType.DOUBLE_STRAIGHT || sellType == SellType.THREE_STRAIGHT || sellType == SellType.FOUR_STRAIGHT) { return level; - } else if (sellType == SellType.THREE_ZONES_SINGLE || sellType == SellType.THREE_STRAIGHT_WITH_SINGLE || sellType == SellType.THREE_ZONES_DOUBLE || sellType == SellType.FOUR_STRAIGHT_WITH_DOUBLE) { + } else if (sellType == SellType.THREE_ZONES_SINGLE || sellType == SellType.THREE_STRAIGHT_WITH_SINGLE || sellType == SellType.THREE_ZONES_DOUBLE || sellType == SellType.THREE_STRAIGHT_WITH_DOUBLE) { return level; } else if (sellType == SellType.FOUR_ZONES_SINGLE || sellType == SellType.FOUR_STRAIGHT_WITH_SINGLE || sellType == SellType.FOUR_ZONES_DOUBLE || sellType == SellType.FOUR_STRAIGHT_WITH_DOUBLE) { return level; diff --git a/landlords-common/src/test/java/org/nico/ratel/landlords/helper/tests/PokerHelperTest.java b/landlords-common/src/test/java/org/nico/ratel/landlords/helper/tests/PokerHelperTest.java index 36952c0..f84b3fc 100644 --- a/landlords-common/src/test/java/org/nico/ratel/landlords/helper/tests/PokerHelperTest.java +++ b/landlords-common/src/test/java/org/nico/ratel/landlords/helper/tests/PokerHelperTest.java @@ -125,7 +125,7 @@ public void testCheckPokerType2() { pokers.add(new Poker(PokerLevel.LEVEL_10, PokerType.BLANK)); Assert.assertEquals(pokers, PokerHelper.checkPokerType(pokers).getSellPokers()); Assert.assertEquals(8, PokerHelper.checkPokerType(pokers).getScore()); - Assert.assertEquals(SellType.FOUR_STRAIGHT_WITH_DOUBLE, PokerHelper.checkPokerType(pokers).getSellType()); + Assert.assertEquals(SellType.THREE_STRAIGHT_WITH_DOUBLE, PokerHelper.checkPokerType(pokers).getSellType()); } @Test @@ -219,4 +219,25 @@ public void testCheckPokerType5() { Assert.assertEquals(8, PokerHelper.checkPokerType(pokers).getScore()); Assert.assertEquals(SellType.FOUR_STRAIGHT_WITH_DOUBLE, PokerHelper.checkPokerType(pokers).getSellType()); } + + @Test + public void testCheckPokerType6() { + pokers.clear(); + pokers.add(new Poker(PokerLevel.LEVEL_6, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_6, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_6, PokerType.HEART)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_7, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_8, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_4, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_4, PokerType.BLANK)); + pokers.add(new Poker(PokerLevel.LEVEL_5, PokerType.BLANK)); + PokerSell pokerSell = PokerHelper.checkPokerType(pokers); + Assert.assertEquals(pokers, pokerSell.getSellPokers()); + Assert.assertEquals(8, PokerHelper.checkPokerType(pokers).getScore()); + Assert.assertEquals(SellType.THREE_STRAIGHT_WITH_SINGLE, pokerSell.getSellType()); + } } 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 83060cb..92dba8b 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 @@ -79,7 +79,7 @@ public void call(ClientSide clientSide, String data) { .put("lastSellPokers", currentPokers); if (!clientSide.getPokers().isEmpty()) { - mapHelper.put("sellClinetNickname", next.getNickname()); + mapHelper.put("sellClientNickname", next.getNickname()); } String result = mapHelper.json(); 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 index 3158a0f..d156ff4 100644 --- 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 @@ -43,7 +43,7 @@ public void call(ClientSide clientSide, String data) { .put("lastSellClientId", datas.get("lastSellClientId")) .put("clientInfos", clientInfos) .put("sellClientId", room.getCurrentSellClient()) - .put("sellClinetNickname", ServerContains.CLIENT_SIDE_MAP.get(room.getCurrentSellClient()).getNickname()) + .put("sellClientNickname", ServerContains.CLIENT_SIDE_MAP.get(room.getCurrentSellClient()).getNickname()) .json(); ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_POKER_PLAY_REDIRECT, result); From bb9500f4191c1f7f206abe3005d9774a53e83e95 Mon Sep 17 00:00:00 2001 From: doveeeee Date: Tue, 13 Jul 2021 20:22:40 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E4=BC=98?= =?UTF-8?q?=E9=9B=85=E9=80=80=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../landlords/server/handler/ProtobufTransferHandler.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java index 008e3db..a1647b3 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java @@ -19,6 +19,14 @@ public class ProtobufTransferHandler extends ChannelInboundHandlerAdapter { + @Override + public void handlerRemoved(ChannelHandlerContext ctx) { + ClientSide client = ServerContains.CLIENT_SIDE_MAP.get(getId(ctx.channel())); + SimplePrinter.serverLog("client " + client.getId() + "(" + client.getNickname() + ") disconnected"); + clientOfflineEvent(ctx.channel()); + ctx.channel().close(); + } + @Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { Channel ch = ctx.channel(); From d8bbde8e63fc6542b6e2ff808a635e262eee0a8b Mon Sep 17 00:00:00 2001 From: doveeeee Date: Wed, 14 Jul 2021 18:42:01 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=87=86=E5=A4=87?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=20=E6=B8=B8=E6=88=8F=E7=BB=93=E6=9D=9F?= =?UTF-8?q?=E5=90=8E=E4=B8=8D=E5=86=8D=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA?= =?UTF-8?q?=E6=88=BF=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ClientEventListener_CODE_GAME_OVER.java | 3 +- ...entEventListener_CODE_GAME_POKER_PLAY.java | 2 +- .../ClientEventListener_CODE_GAME_READY.java | 37 +++++++++++++ .../ClientEventListener_CODE_GAME_WATCH.java | 1 - ...ventListener_CODE_ROOM_CREATE_SUCCESS.java | 2 + ...tEventListener_CODE_ROOM_JOIN_SUCCESS.java | 1 + .../org/nico/ratel/landlords/entity/Room.java | 2 +- .../landlords/enums/ClientEventCode.java | 2 + .../ratel/landlords/enums/ClientStatus.java | 2 +- .../landlords/enums/ServerEventCode.java | 2 + ...verEventListener_CODE_GAME_POKER_PLAY.java | 4 +- .../ServerEventListener_CODE_GAME_READY.java | 54 +++++++++++++++++++ ...erverEventListener_CODE_GAME_STARTING.java | 1 + .../ServerEventListener_CODE_ROOM_CREATE.java | 5 +- .../ServerEventListener_CODE_ROOM_JOIN.java | 5 +- 15 files changed, 110 insertions(+), 13 deletions(-) create mode 100644 landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_READY.java create mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_OVER.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_OVER.java index 4136ca5..a75e0e7 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_OVER.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_OVER.java @@ -13,7 +13,6 @@ public class ClientEventListener_CODE_GAME_OVER extends ClientEventListener { public void call(Channel channel, String data) { Map map = MapHelper.parser(data); SimplePrinter.printNotice("\nPlayer " + map.get("winnerNickname") + "[" + map.get("winnerType") + "]" + " won the game"); - SimplePrinter.printNotice("Game over, friendship first, competition second\n"); + ClientEventListener_CODE_GAME_READY.gameReady(channel); } - } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY.java index 9136c0d..cbb2427 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY.java @@ -54,7 +54,7 @@ public void call(Channel channel, String data) { List lastSellPokers = Noson.convert(lastSellPokersObj, new NoType>() { }); List sells = PokerHelper.validSells(PokerHelper.checkPokerType(lastSellPokers), pokers); - if (sells == null || sells.size() == 0) { + if (sells.size() == 0) { SimplePrinter.printNotice("It is a pity that, there is no winning combination..."); call(channel, data); return; diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_READY.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_READY.java new file mode 100644 index 0000000..6ebfc89 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_READY.java @@ -0,0 +1,37 @@ +package org.nico.ratel.landlords.client.event; + +import io.netty.channel.Channel; +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.client.SimpleClient; +import org.nico.ratel.landlords.enums.ServerEventCode; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.print.SimplePrinter; +import org.nico.ratel.landlords.print.SimpleWriter; + +import java.util.Map; + +public class ClientEventListener_CODE_GAME_READY extends ClientEventListener { + @Override + public void call(Channel channel, String data) { + Map map = MapHelper.parser(data); + if (SimpleClient.id == (int) map.get("clientId")) { + SimplePrinter.printNotice("you are ready to play game."); + return; + } + SimplePrinter.printNotice(map.get("clientNickName").toString() + " is ready to play game."); + } + + static void gameReady(Channel channel) { + SimplePrinter.printNotice("\nPress any key to get ready(Press [exit|e] to exit the room)"); + String line = SimpleWriter.write("notReady"); + if (line.equals("")) { + gameReady(channel); + return; + } + if (line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("e")) { + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT, ""); + return; + } + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_READY, ""); + } +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_WATCH.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_WATCH.java index 1a65451..54dc969 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_WATCH.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_WATCH.java @@ -135,7 +135,6 @@ private void printGameResult(Object rawData, Channel channel) { Map map = MapHelper.parser(rawData.toString()); printNoticeWithTime("Player [" + map.get("winnerNickname") + "](" + map.get("winnerType") + ") won the game."); - quitWatch(channel); } private void printKickInfo(Object rawData) { diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_CREATE_SUCCESS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_CREATE_SUCCESS.java index 024ec65..d3dfa44 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_CREATE_SUCCESS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_CREATE_SUCCESS.java @@ -17,6 +17,8 @@ public void call(Channel channel, String data) { SimplePrinter.printNotice("You have created a room with id " + room.getId()); SimplePrinter.printNotice("Please wait for other players to join !"); + + ClientEventListener_CODE_GAME_READY.gameReady(channel); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_SUCCESS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_SUCCESS.java index 3c4509d..090a4ad 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_SUCCESS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_SUCCESS.java @@ -20,6 +20,7 @@ public void call(Channel channel, String data) { if (SimpleClient.id == joinClientId) { SimplePrinter.printNotice("You have joined room:" + map.get("roomId") + ". There are " + map.get("roomClientCount") + " players in the room now."); SimplePrinter.printNotice("Please wait for other players to join. The game would start at three players!"); + ClientEventListener_CODE_GAME_READY.gameReady(channel); } else { SimplePrinter.printNotice(map.get("clientNickname") + " joined room, there are currently " + map.get("roomClientCount") + " in the room."); } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java index 751aebc..64c565a 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java @@ -51,7 +51,7 @@ public Room(int id) { this.id = id; this.clientSideMap = new ConcurrentSkipListMap<>(); this.clientSideList = new LinkedList<>(); - this.status = RoomStatus.BLANK; + this.status = RoomStatus.WAIT; } public final long getCreateTime() { diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java index 9df5fa5..972b4de 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java @@ -62,6 +62,8 @@ public enum ClientEventCode implements Serializable { CODE_PVE_DIFFICULTY_NOT_SUPPORT("人机难度不支持"), + CODE_GAME_READY("准备开始游戏"), + CODE_GAME_WATCH("观战"), CODE_GAME_WATCH_SUCCESSFUL("观战成功"); diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java index efbf92e..fed401f 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java @@ -4,7 +4,7 @@ public enum ClientStatus { TO_CHOOSE, - NO_READY, + NOT_READY, READY, diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerEventCode.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerEventCode.java index 2d3dc8c..0ad997e 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerEventCode.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerEventCode.java @@ -22,6 +22,8 @@ public enum ServerEventCode implements Serializable { CODE_GAME_STARTING("游戏开始"), + CODE_GAME_READY("玩家准备"), + CODE_GAME_LANDLORD_ELECT("抢地主"), CODE_GAME_POKER_PLAY("出牌环节"), diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY.java index 92dba8b..62d2f8b 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_POKER_PLAY.java @@ -101,12 +101,12 @@ public void call(ClientSide clientSide, String data) { for(ClientSide client: room.getClientSideList()) { if(client.getRole() == ClientRole.PLAYER) { ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, result); + client.setStatus(ClientStatus.NOT_READY); } } + room.setStatus(RoomStatus.WAIT); notifyWatcherGameOver(room, result); - - ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(clientSide, data); } else { if(next.getRole() == ClientRole.PLAYER) { ServerEventListener.get(ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT).call(next, result); diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java new file mode 100644 index 0000000..6868de7 --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java @@ -0,0 +1,54 @@ +package org.nico.ratel.landlords.server.event; + +import org.nico.ratel.landlords.channel.ChannelUtils; +import org.nico.ratel.landlords.entity.ClientSide; +import org.nico.ratel.landlords.entity.Room; +import org.nico.ratel.landlords.enums.*; +import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.print.SimplePrinter; +import org.nico.ratel.landlords.server.ServerContains; + +import java.util.concurrent.ConcurrentSkipListMap; + +public class ServerEventListener_CODE_GAME_READY implements ServerEventListener { + @Override + public void call(ClientSide clientSide, String data) { + Room room = ServerContains.getRoom(clientSide.getRoomId()); + SimplePrinter.serverLog("房间状态:" + room.getStatus()); + SimplePrinter.serverLog("玩家状态:" + clientSide.getStatus()); + if (room.getStatus() == RoomStatus.STARTING) { + return; + } + if (clientSide.getStatus() == ClientStatus.PLAYING || clientSide.getStatus() == ClientStatus.TO_CHOOSE || clientSide.getStatus() == ClientStatus.CALL_LANDLORD) { + return; + } + clientSide.setStatus(clientSide.getStatus() == ClientStatus.READY ? ClientStatus.NOT_READY : ClientStatus.READY); + String result = MapHelper.newInstance() + .put("clientNickName", clientSide.getNickname()) + .put("status", clientSide.getStatus()) + .put("clientId", clientSide.getId()) + .json(); + boolean allReady = true; + ConcurrentSkipListMap roomClientMap = (ConcurrentSkipListMap) room.getClientSideMap(); + if (roomClientMap.size() < 3) { + allReady = false; + } else { + for (ClientSide client : room.getClientSideList()) { + if (client.getRole() == ClientRole.PLAYER && client.getStatus() != ClientStatus.READY) { + allReady = false; + break; + } + } + } + + for (ClientSide client : room.getClientSideList()) { + if (client.getRole() == ClientRole.PLAYER) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_READY, result); + } + } + + if (allReady) { + ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, data); + } + } +} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java index 4e7cc71..cd32d9d 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java @@ -43,6 +43,7 @@ public void call(ClientSide clientSide, String data) { for (ClientSide client : roomClientList) { client.setType(ClientType.PEASANT); + client.setStatus(ClientStatus.PLAYING); String result = MapHelper.newInstance() .put("roomId", room.getId()) diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java index 274ffd7..77e8b1c 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java @@ -5,6 +5,7 @@ import org.nico.ratel.landlords.entity.ClientSide; import org.nico.ratel.landlords.entity.Room; import org.nico.ratel.landlords.enums.ClientEventCode; +import org.nico.ratel.landlords.enums.ClientStatus; import org.nico.ratel.landlords.enums.RoomStatus; import org.nico.ratel.landlords.enums.RoomType; import org.nico.ratel.landlords.server.ServerContains; @@ -15,7 +16,7 @@ public class ServerEventListener_CODE_ROOM_CREATE implements ServerEventListener public void call(ClientSide clientSide, String data) { Room room = new Room(ServerContains.getServerId()); - room.setStatus(RoomStatus.BLANK); + room.setStatus(RoomStatus.WAIT); room.setType(RoomType.PVP); room.setRoomOwner(clientSide.getNickname()); room.getClientSideMap().put(clientSide.getId(), clientSide); @@ -27,6 +28,8 @@ public void call(ClientSide clientSide, String data) { clientSide.setRoomId(room.getId()); ServerContains.addRoom(room); + clientSide.setStatus(ClientStatus.NOT_READY); + ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_CREATE_SUCCESS, Noson.reversal(room)); } } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_JOIN.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_JOIN.java index ffddc41..ce7655d 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_JOIN.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_JOIN.java @@ -9,7 +9,6 @@ import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.ClientStatus; import org.nico.ratel.landlords.enums.RoomStatus; -import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.server.ServerContains; @@ -52,12 +51,10 @@ public void call(ClientSide clientSide, String data) { if (roomClientMap.size() == 3) { clientSide.setNext(roomClientList.getFirst()); roomClientList.getFirst().setPre(clientSide); - ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(room.getId())); - return; } room.setStatus(RoomStatus.WAIT); - + clientSide.setStatus(ClientStatus.NOT_READY); String result = MapHelper.newInstance() .put("clientId", clientSide.getId()) .put("clientNickname", clientSide.getNickname()) From 0b8e04a1e7c75a36c979bbb7c11722bd8c9b0418 Mon Sep 17 00:00:00 2001 From: doveeeee Date: Wed, 14 Jul 2021 20:21:31 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=A7=AF=E5=88=86?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ClientEventListener_CODE_GAME_OVER.java | 27 +++++ ...entEventListener_CODE_GAME_POKER_PLAY.java | 3 +- .../ratel/landlords/entity/ClientSide.java | 22 ++++ .../org/nico/ratel/landlords/entity/Room.java | 35 +++++- ...verEventListener_CODE_GAME_POKER_PLAY.java | 103 +++++++++++++++--- .../ServerEventListener_CODE_GAME_READY.java | 3 + 6 files changed, 173 insertions(+), 20 deletions(-) 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 a75e0e7..5abd90d 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,7 +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.client.SimpleClient; +import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; @@ -13,6 +18,28 @@ public class ClientEventListener_CODE_GAME_OVER extends ClientEventListener { public void call(Channel channel, String data) { Map map = MapHelper.parser(data); SimplePrinter.printNotice("\nPlayer " + map.get("winnerNickname") + "[" + map.get("winnerType") + "]" + " won the game"); + + List> scores = Noson.convert(map.get("scores"), new NoType>>() {}); + + for (Map score : scores) { + if (SimpleClient.id != (int) score.get("clientId")) { + SimplePrinter.printNotice(score.get("nickName").toString() + "'s rest poker is:"); + SimplePrinter.printPokers(Noson.convert(score.get("pokers"), new NoType>() {})); + } + } + SimplePrinter.printNotice("\n"); + + // print score + for (Map score : scores) { + String scoreInc = score.get("scoreInc").toString(); + String scoreTotal = score.get("score").toString(); + if (SimpleClient.id != (int) score.get("clientId")) { + SimplePrinter.printNotice(score.get("nickName").toString() + "'s score is " + scoreInc + ", total score is " + scoreTotal); + } else { + SimplePrinter.printNotice("your score is " + scoreInc + ", total score is " + scoreTotal); + } + } + ClientEventListener_CODE_GAME_READY.gameReady(channel); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_POKER_PLAY.java index cbb2427..1a05e77 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 @@ -51,8 +51,7 @@ public void call(Channel channel, String data) { call(channel, data); return; } else { - List lastSellPokers = Noson.convert(lastSellPokersObj, new NoType>() { - }); + List lastSellPokers = Noson.convert(lastSellPokersObj, new NoType>() {}); List sells = PokerHelper.validSells(PokerHelper.checkPokerType(lastSellPokers), pokers); if (sells.size() == 0) { SimplePrinter.printNotice("It is a pity that, there is no winning combination..."); 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 b9ed205..0efda35 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 @@ -49,6 +49,7 @@ public void init() { type = null; next = null; pre = null; + score = 0; } public final ClientRole getRole() { @@ -91,6 +92,27 @@ public final void setPokers(List pokers) { this.pokers = pokers; } + public final int getScore() { + return score; + } + + public final void setScore(int score) { + this.score = score; + } + + public final void addScore(int score) { + this.score += score; + this.scoreInc = score; + } + + public final void setScoreInc(int scoreInc) { + this.scoreInc = scoreInc; + } + + public final int getScoreInc() { + return this.scoreInc; + } + public final ClientStatus getStatus() { return status; } 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 64c565a..657acfd 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 @@ -43,7 +43,11 @@ public class Room { /** 观战者列表 */ private List watcherList = new ArrayList<>(5); - + + private int scoreRate = 1; + + private int baseScore = 3; + public Room() { } @@ -52,6 +56,35 @@ public Room(int id) { this.clientSideMap = new ConcurrentSkipListMap<>(); this.clientSideList = new LinkedList<>(); this.status = RoomStatus.WAIT; + this.createTime = System.currentTimeMillis(); + } + + public int getScore() { + return this.baseScore * this.scoreRate; + } + + public int getBaseScore() { + return this.baseScore; + } + + public void setBaseScore(int baseScore) { + this.baseScore = baseScore; + } + + public int getScoreRate() { + return this.scoreRate; + } + + public void setScoreRate(int scoreRate) { + this.scoreRate = scoreRate; + } + + public void initScoreRate() { + this.scoreRate = 1; + } + + public void increaseRate() { + this.scoreRate *= 2; } public final long getCreateTime() { 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 62d2f8b..82d0cea 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,5 +1,6 @@ package org.nico.ratel.landlords.server.event; +import java.util.ArrayList; import java.util.List; import org.nico.noson.Noson; @@ -11,6 +12,7 @@ import org.nico.ratel.landlords.enums.*; 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.server.ServerContains; import org.nico.ratel.landlords.server.robot.RobotEventListener; @@ -65,6 +67,11 @@ public void call(ClientSide clientSide, String data) { ClientSide next = clientSide.getNext(); + if (currentPokerSell.getSellType() == SellType.BOMB || currentPokerSell.getSellType() == SellType.KING_BOMB) { + // 炸弹积分翻倍 + room.increaseRate(); + } + room.setLastSellClient(clientSide.getId()); room.setLastPokerShell(currentPokerSell); room.setCurrentSellClient(next.getId()); @@ -92,28 +99,90 @@ public void call(ClientSide clientSide, String data) { notifyWatcherPlayPoker(room, result); - if (clientSide.getPokers().isEmpty()) { - result = MapHelper.newInstance() - .put("winnerNickname", clientSide.getNickname()) - .put("winnerType", clientSide.getType()) - .json(); - - for(ClientSide client: room.getClientSideList()) { - if(client.getRole() == ClientRole.PLAYER) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, result); - client.setStatus(ClientStatus.NOT_READY); - } + if (!clientSide.getPokers().isEmpty()) { + if (next.getRole() == ClientRole.ROBOT) { + RobotEventListener.get(ClientEventCode.CODE_GAME_POKER_PLAY).call(next, data); + } else { + ServerEventListener.get(ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT).call(next, result); } - room.setStatus(RoomStatus.WAIT); + return; + } + + gameOver(clientSide, room); +// ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, data); + } - notifyWatcherGameOver(room, result); + private void setRoomClientScore(Room room, ClientType winnerType) { + int landLordScore = room.getScore() * 2; + int peasantScore = room.getScore(); + // 输的一方分数为负 + if (winnerType == ClientType.LANDLORD) { + peasantScore = -peasantScore; } else { - if(next.getRole() == ClientRole.PLAYER) { - ServerEventListener.get(ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT).call(next, result); - }else { - RobotEventListener.get(ClientEventCode.CODE_GAME_POKER_PLAY).call(next, data); + landLordScore = -landLordScore; + } + for (ClientSide client : room.getClientSideList()) { + if (client.getType() == ClientType.LANDLORD) { + client.addScore(landLordScore); + } else { + client.addScore(peasantScore); + } + } + } + + private void gameOver(ClientSide winner, Room room) { + ClientType winnerType = winner.getType(); + if (isSpring(winner, room)) { + room.increaseRate(); + } + + setRoomClientScore(room, winnerType); + + ArrayList clientScores = new ArrayList<>(); + for (ClientSide client : room.getClientSideList()) { + MapHelper score = MapHelper.newInstance() + .put("clientId", client.getId()) + .put("nickName", client.getNickname()) + .put("score", client.getScore()) + .put("scoreInc", client.getScoreInc()) + .put("pokers", client.getPokers()); + clientScores.add(score.map()); + } + + SimplePrinter.serverLog(clientScores.toString()); + String result = MapHelper.newInstance() + .put("winnerNickname", winner.getNickname()) + .put("winnerType", winner.getType()) + .put("scores", clientScores) + .json(); + + for (ClientSide client : room.getClientSideList()) { + if (client.getRole() == ClientRole.ROBOT) { + continue; + } + + client.setStatus(ClientStatus.NOT_READY); + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, result); + } + room.setStatus(RoomStatus.WAIT); + room.initScoreRate(); + notifyWatcherGameOver(room, result); + } + + private boolean isSpring(ClientSide winner, Room room) { + boolean isSpring = true; + for (ClientSide client: room.getClientSideList()) { + if (client.getId() == winner.getId()) { + continue; + } + if (client.getType() == ClientType.PEASANT && client.getPokers().size() < 17) { + isSpring = false; + } + if (client.getType() == ClientType.LANDLORD && client.getPokers().size() < 20) { + isSpring = false; } } + return isSpring; } /** diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java index 6868de7..f41764f 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java @@ -14,6 +14,9 @@ public class ServerEventListener_CODE_GAME_READY implements ServerEventListener @Override public void call(ClientSide clientSide, String data) { Room room = ServerContains.getRoom(clientSide.getRoomId()); + if (room == null) { + return; + } SimplePrinter.serverLog("房间状态:" + room.getStatus()); SimplePrinter.serverLog("玩家状态:" + clientSide.getStatus()); if (room.getStatus() == RoomStatus.STARTING) { From 5bf65d262a97230a1889421416c8249baf463817 Mon Sep 17 00:00:00 2001 From: doveeeee Date: Wed, 14 Jul 2021 21:41:36 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/handler/ProtobufTransferHandler.java | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java index a1647b3..294630e 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ProtobufTransferHandler.java @@ -37,7 +37,7 @@ public void channelRegistered(ChannelHandlerContext ctx) throws Exception { clientSide.setRole(ClientRole.PLAYER); ServerContains.CLIENT_SIDE_MAP.put(clientSide.getId(), clientSide); - SimplePrinter.serverLog("Has client connect to the server:" + clientSide.getId()); + SimplePrinter.serverLog("Has client connect 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); @@ -57,16 +57,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } } - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if (cause instanceof java.io.IOException) { - clientOfflineEvent(ctx.channel()); - } else { - SimplePrinter.serverLog("ERROR:" + cause.getMessage()); - cause.printStackTrace(); - } - } - @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { From 4d5de613fd06ac1d52b9b5a1114a63befe3fb476 Mon Sep 17 00:00:00 2001 From: Nico Date: Thu, 15 Jul 2021 14:49:50 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E9=9A=90=E6=82=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ClientEventListener_CODE_GAME_OVER.java | 38 +++++++++---------- ...erverEventListener_CODE_GAME_STARTING.java | 16 +++----- 2 files changed, 24 insertions(+), 30 deletions(-) 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 5abd90d..b267655 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 @@ import java.util.List; import java.util.Map; +import java.util.Objects; import org.nico.noson.Noson; import org.nico.noson.entity.NoType; @@ -19,27 +20,26 @@ public void call(Channel channel, String data) { Map map = MapHelper.parser(data); SimplePrinter.printNotice("\nPlayer " + map.get("winnerNickname") + "[" + map.get("winnerType") + "]" + " won the game"); - List> scores = Noson.convert(map.get("scores"), new NoType>>() {}); - - for (Map score : scores) { - if (SimpleClient.id != (int) score.get("clientId")) { - SimplePrinter.printNotice(score.get("nickName").toString() + "'s rest poker is:"); - SimplePrinter.printPokers(Noson.convert(score.get("pokers"), new NoType>() {})); + if (map.containsKey("scores")){ + List> scores = Noson.convert(map.get("scores"), new NoType>>() {}); + for (Map score : scores) { + if (! Objects.equals(score.get("clientId"), SimpleClient.id)) { + SimplePrinter.printNotice(score.get("nickName").toString() + "'s rest poker is:"); + SimplePrinter.printPokers(Noson.convert(score.get("pokers"), new NoType>() {})); + } } - } - SimplePrinter.printNotice("\n"); - - // print score - for (Map score : scores) { - String scoreInc = score.get("scoreInc").toString(); - String scoreTotal = score.get("score").toString(); - if (SimpleClient.id != (int) score.get("clientId")) { - SimplePrinter.printNotice(score.get("nickName").toString() + "'s score is " + scoreInc + ", total score is " + scoreTotal); - } else { - SimplePrinter.printNotice("your score is " + scoreInc + ", total score is " + scoreTotal); + SimplePrinter.printNotice("\n"); + // print score + for (Map score : scores) { + String scoreInc = score.get("scoreInc").toString(); + String scoreTotal = score.get("score").toString(); + if (SimpleClient.id != (int) score.get("clientId")) { + SimplePrinter.printNotice(score.get("nickName").toString() + "'s score is " + scoreInc + ", total score is " + scoreTotal); + } else { + SimplePrinter.printNotice("your score is " + scoreInc + ", total score is " + scoreTotal); + } } + ClientEventListener_CODE_GAME_READY.gameReady(channel); } - - ClientEventListener_CODE_GAME_READY.gameReady(channel); } } 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 cd32d9d..5f0f493 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 @@ -52,8 +52,6 @@ public void call(ClientSide clientSide, String data) { .put("nextClientNickname", startGrabClient.getNickname()) .put("nextClientId", startGrabClient.getId()) .put("pokers", client.getPokers()) - // this key-value use to client order to show - .put("clientOrderList", roomClientList) .json(); if (client.getRole() == ClientRole.PLAYER) { @@ -76,16 +74,12 @@ public void call(ClientSide clientSide, String data) { * @param room 房间 */ private void notifyWatcherGameStart(Room room) { + String result = MapHelper.newInstance() + .put("player1", room.getClientSideList().getFirst().getNickname()) + .put("player2", room.getClientSideList().getFirst().getNext().getNickname()) + .put("player3", room.getClientSideList().getLast().getNickname()) + .json(); for (ClientSide clientSide : room.getWatcherList()) { - String result = MapHelper.newInstance() - .put("player1", room.getClientSideList().getFirst().getNickname()) - .put("pokers1", room.getClientSideList().getFirst().getPokers()) - .put("player2", room.getClientSideList().getFirst().getNext().getNickname()) - .put("pokers2", room.getClientSideList().getFirst().getNext().getPokers()) - .put("player3", room.getClientSideList().getLast().getNickname()) - .put("pokers3", room.getClientSideList().getLast().getPokers()) - .json(); - ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_GAME_STARTING, result); } } From d451030c54dd1454a2f930bcf2e282d6d6eddb75 Mon Sep 17 00:00:00 2001 From: Nico Date: Thu, 15 Jul 2021 18:17:49 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E5=90=91=E4=B8=8B=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=EF=BC=8C=E7=AC=AC=E4=B8=80=E5=B1=80=E7=BB=93=E6=9D=9F=E5=90=8E?= =?UTF-8?q?=E8=BF=9B=E5=85=A5=E5=87=86=E5=A4=87=E9=98=B6=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- landlords-client/pom.xml | 4 +- .../ratel/landlords/client/SimpleClient.java | 5 ++- ...ientEventListener_CODE_CLIENT_CONNECT.java | 12 ++++++ .../ClientEventListener_CODE_GAME_READY.java | 12 ++---- ...ventListener_CODE_ROOM_CREATE_SUCCESS.java | 5 --- ...tEventListener_CODE_ROOM_JOIN_SUCCESS.java | 1 - ...ClientEventListener_CODE_SHOW_OPTIONS.java | 1 - landlords-common/pom.xml | 2 +- .../ratel/landlords/entity/ClientSide.java | 10 +++++ .../org/nico/ratel/landlords/entity/Room.java | 2 + .../ratel/landlords/enums/ClientStatus.java | 2 +- .../landlords/enums/ServerEventCode.java | 3 ++ .../ratel/landlords/features/Features.java | 23 +++++++++++ landlords-server/pom.xml | 4 +- .../ServerEventListener_CODE_CLIENT_EXIT.java | 39 ++++++++++--------- ...verEventListener_CODE_CLIENT_INFO_SET.java | 22 +++++++++++ ...ventListener_CODE_CLIENT_NICKNAME_SET.java | 1 - ...verEventListener_CODE_GAME_POKER_PLAY.java | 21 ++++++---- .../ServerEventListener_CODE_GAME_READY.java | 2 +- .../ServerEventListener_CODE_GAME_WATCH.java | 2 +- .../ServerEventListener_CODE_ROOM_CREATE.java | 2 +- .../ServerEventListener_CODE_ROOM_JOIN.java | 8 +++- .../server/proxy/WebsocketProxy.java | 5 --- .../landlords/server/timer/RoomClearTask.java | 10 ++--- pom.xml | 2 +- 25 files changed, 135 insertions(+), 65 deletions(-) create mode 100644 landlords-common/src/main/java/org/nico/ratel/landlords/features/Features.java create mode 100644 landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_INFO_SET.java diff --git a/landlords-client/pom.xml b/landlords-client/pom.xml index 7686cf4..ce66f39 100644 --- a/landlords-client/pom.xml +++ b/landlords-client/pom.xml @@ -13,14 +13,14 @@ com.smallnico.ratel landlords - 1.2.7 + 1.3.0 com.smallnico.ratel landlords-common - 1.2.7 + 1.3.0 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 3d0e292..0b8cb0d 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 @@ -10,6 +10,7 @@ import org.nico.noson.entity.NoType; import org.nico.ratel.landlords.client.proxy.ProtobufProxy; import org.nico.ratel.landlords.client.proxy.WebsocketProxy; +import org.nico.ratel.landlords.features.Features; import org.nico.ratel.landlords.print.SimplePrinter; import org.nico.ratel.landlords.print.SimpleWriter; import org.nico.ratel.landlords.utils.StreamUtils; @@ -18,13 +19,15 @@ public class SimpleClient { public static int id = -1; + public final static String VERSION = Features.VERSION_1_3_0; + public static String serverAddress; public static int port = 1024; public static String protocol = "pb"; - private static final String[] serverAddressSource = new String[]{ + private final static String[] serverAddressSource = new String[]{ "https://raw.githubusercontent.com/ainilili/ratel/master/serverlist.json", "https://gitee.com/ainilili/ratel/raw/master/serverlist.json" }; 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 30da56d..8d630da 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 @@ -1,9 +1,17 @@ package org.nico.ratel.landlords.client.event; +import org.nico.noson.Noson; import org.nico.ratel.landlords.client.SimpleClient; +import org.nico.ratel.landlords.enums.ServerEventCode; +import org.nico.ratel.landlords.features.Features; +import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.print.SimplePrinter; import io.netty.channel.Channel; +import org.nico.ratel.landlords.utils.JsonUtils; + +import java.util.HashMap; +import java.util.Map; public class ClientEventListener_CODE_CLIENT_CONNECT extends ClientEventListener { @@ -11,6 +19,10 @@ public class ClientEventListener_CODE_CLIENT_CONNECT extends ClientEventListener public void call(Channel channel, String data) { SimplePrinter.printNotice("Connected to server. Welcome to ratel!"); SimpleClient.id = Integer.parseInt(data); + + Map infos = new HashMap<>(); + infos.put("version", SimpleClient.VERSION); + pushToServer(channel, ServerEventCode.CODE_CLIENT_INFO_SET, Noson.reversal(infos)); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_READY.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_READY.java index 6ebfc89..4efe9a7 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_READY.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_GAME_READY.java @@ -22,16 +22,12 @@ public void call(Channel channel, String data) { } static void gameReady(Channel channel) { - SimplePrinter.printNotice("\nPress any key to get ready(Press [exit|e] to exit the room)"); + SimplePrinter.printNotice("\nDo you want to continue the game? [Y/N]"); String line = SimpleWriter.write("notReady"); - if (line.equals("")) { - gameReady(channel); + if (line.equals("Y") || line.equals("y")) { + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_READY, ""); return; } - if (line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("e")) { - ChannelUtils.pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT, ""); - return; - } - ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_READY, ""); + ChannelUtils.pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT, ""); } } 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 d3dfa44..6bd8b6e 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 @@ -10,15 +10,10 @@ public class ClientEventListener_CODE_ROOM_CREATE_SUCCESS extends ClientEventLis @Override public void call(Channel channel, String data) { - Room room = Noson.convert(data, Room.class); - initLastSellInfo(); - SimplePrinter.printNotice("You have created a room with id " + room.getId()); SimplePrinter.printNotice("Please wait for other players to join !"); - - ClientEventListener_CODE_GAME_READY.gameReady(channel); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_SUCCESS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_SUCCESS.java index 090a4ad..3c4509d 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_SUCCESS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_JOIN_SUCCESS.java @@ -20,7 +20,6 @@ public void call(Channel channel, String data) { if (SimpleClient.id == joinClientId) { SimplePrinter.printNotice("You have joined room:" + map.get("roomId") + ". There are " + map.get("roomClientCount") + " players in the room now."); SimplePrinter.printNotice("Please wait for other players to join. The game would start at three players!"); - ClientEventListener_CODE_GAME_READY.gameReady(channel); } else { SimplePrinter.printNotice(map.get("clientNickname") + " joined room, there are currently " + map.get("roomClientCount") + " in the room."); } diff --git a/landlords-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 527d963..5a92b4f 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 @@ -22,7 +22,6 @@ public void call(Channel channel, String data) { System.exit(0); } else { int choose = OptionsUtils.getOptions(line); - if (choose == 1) { get(ClientEventCode.CODE_SHOW_OPTIONS_PVP).call(channel, data); } else if (choose == 2) { diff --git a/landlords-common/pom.xml b/landlords-common/pom.xml index 16fccd5..120ad2b 100644 --- a/landlords-common/pom.xml +++ b/landlords-common/pom.xml @@ -10,7 +10,7 @@ com.smallnico.ratel landlords - 1.2.7 + 1.3.0 \ No newline at end of file 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 0efda35..e587d02 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 @@ -34,6 +34,8 @@ public class ClientSide { private transient Channel channel; + private String version; + public ClientSide() {} public ClientSide(int id, ClientStatus status, Channel channel) { @@ -153,6 +155,14 @@ public final void setPre(ClientSide pre) { this.pre = pre; } + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + @Override public int hashCode() { final int prime = 31; 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 657acfd..4bb54c0 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 @@ -5,7 +5,9 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentSkipListMap; +import java.util.concurrent.locks.ReentrantLock; +import org.nico.noson.annotations.JsonIgnore; import org.nico.ratel.landlords.enums.RoomStatus; import org.nico.ratel.landlords.enums.RoomType; 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 fed401f..efbf92e 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java @@ -4,7 +4,7 @@ public enum ClientStatus { TO_CHOOSE, - NOT_READY, + NO_READY, READY, diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerEventCode.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerEventCode.java index 0ad997e..b478890 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 @@ -8,6 +8,8 @@ public enum ServerEventCode implements Serializable { CODE_CLIENT_OFFLINE("玩家离线"), + CODE_CLIENT_INFO_SET("设置客户端信息"), + CODE_CLIENT_NICKNAME_SET("设置昵称"), CODE_CLIENT_HEAD_BEAT("不出"), @@ -36,6 +38,7 @@ public enum ServerEventCode implements Serializable { CODE_GAME_WATCH_EXIT("退出观战"); + private String msg; ServerEventCode(String msg) { diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/features/Features.java b/landlords-common/src/main/java/org/nico/ratel/landlords/features/Features.java new file mode 100644 index 0000000..40b1db7 --- /dev/null +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/features/Features.java @@ -0,0 +1,23 @@ +package org.nico.ratel.landlords.features; + +import java.util.*; + +public class Features { + + public final static String VERSION_1_3_0 = "v1.3.0"; + public final static String READY = "READY"; + private final static Map> FEATURES = new HashMap<>(); + + static{ + FEATURES.put(VERSION_1_3_0, Collections.singletonList(READY)); + } + + public static boolean supported(String clientVersion, String feature){ + List features = FEATURES.get(clientVersion); + if (Objects.isNull(features) || Objects.isNull(feature)){ + return false; + } + return features.contains(feature.toUpperCase(Locale.ROOT)); + } + +} diff --git a/landlords-server/pom.xml b/landlords-server/pom.xml index 8a110dc..020251c 100644 --- a/landlords-server/pom.xml +++ b/landlords-server/pom.xml @@ -9,7 +9,7 @@ com.smallnico.ratel landlords - 1.2.7 + 1.3.0 @@ -20,7 +20,7 @@ com.smallnico.ratel landlords-common - 1.2.7 + 1.3.0 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 009eaac..71f25d1 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 @@ -10,29 +10,30 @@ public class ServerEventListener_CODE_CLIENT_EXIT implements ServerEventListener { + private static final Object locked = new Object(); + @Override public void call(ClientSide clientSide, String data) { - - Room room = ServerContains.getRoom(clientSide.getRoomId()); - - if (room == null) { - return; - } - String result = MapHelper.newInstance() - .put("roomId", room.getId()) - .put("exitClientId", clientSide.getId()) - .put("exitClientNickname", clientSide.getNickname()) - .json(); - for (ClientSide client : room.getClientSideList()) { - if (client.getRole() == ClientRole.PLAYER) { - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_CLIENT_EXIT, result); - client.init(); + synchronized (locked){ + Room room = ServerContains.getRoom(clientSide.getRoomId()); + if (room == null) { + return; + } + String result = MapHelper.newInstance() + .put("roomId", room.getId()) + .put("exitClientId", clientSide.getId()) + .put("exitClientNickname", clientSide.getNickname()) + .json(); + for (ClientSide client : room.getClientSideList()) { + if (client.getRole() == ClientRole.PLAYER) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_CLIENT_EXIT, result); + client.init(); + } } - } - - notifyWatcherClientExit(room, clientSide); - ServerContains.removeRoom(room.getId()); + notifyWatcherClientExit(room, clientSide); + ServerContains.removeRoom(room.getId()); + } } /** diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_INFO_SET.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_INFO_SET.java new file mode 100644 index 0000000..4735a36 --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_INFO_SET.java @@ -0,0 +1,22 @@ +package org.nico.ratel.landlords.server.event; + +import org.nico.ratel.landlords.entity.ClientSide; +import org.nico.ratel.landlords.utils.JsonUtils; + +import java.util.Map; + +public class ServerEventListener_CODE_CLIENT_INFO_SET implements ServerEventListener { + + private static final String DEFAULT_VERSION = "v1.2.8"; + + @Override + public void call(ClientSide client, String info) { + Map infos = JsonUtils.fromJson(info, Map.class); + // Get client version + client.setVersion(DEFAULT_VERSION); + if (infos.containsKey("version")){ + client.setVersion(String.valueOf(infos.get("version"))); + } + } + +} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java index 1bc0475..844acda 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CLIENT_NICKNAME_SET.java @@ -12,7 +12,6 @@ public class ServerEventListener_CODE_CLIENT_NICKNAME_SET implements ServerEvent @Override public void call(ClientSide client, String nickname) { - if (nickname.trim().length() > NICKNAME_MAX_LENGTH || nickname.trim().isEmpty()) { String result = MapHelper.newInstance().put("invalidLength", nickname.trim().length()).json(); ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_CLIENT_NICKNAME_SET, 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 82d0cea..2daf3a7 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.*; +import org.nico.ratel.landlords.features.Features; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.print.SimplePrinter; @@ -156,16 +157,22 @@ private void gameOver(ClientSide winner, Room room) { .put("scores", clientScores) .json(); + boolean supportReady = true; for (ClientSide client : room.getClientSideList()) { - if (client.getRole() == ClientRole.ROBOT) { - continue; + if (client.getRole() == ClientRole.ROBOT || ! Features.supported(client.getVersion(), Features.READY)) { + supportReady = false; + break; } - - client.setStatus(ClientStatus.NOT_READY); - ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, result); } - room.setStatus(RoomStatus.WAIT); - room.initScoreRate(); + if (supportReady){ + room.setStatus(RoomStatus.WAIT); + room.initScoreRate(); + for (ClientSide client : room.getClientSideList()) { + ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, result); + } + }else{ + ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(winner, ""); + } notifyWatcherGameOver(room, result); } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java index f41764f..0752045 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_READY.java @@ -25,7 +25,7 @@ public void call(ClientSide clientSide, String data) { if (clientSide.getStatus() == ClientStatus.PLAYING || clientSide.getStatus() == ClientStatus.TO_CHOOSE || clientSide.getStatus() == ClientStatus.CALL_LANDLORD) { return; } - clientSide.setStatus(clientSide.getStatus() == ClientStatus.READY ? ClientStatus.NOT_READY : ClientStatus.READY); + clientSide.setStatus(clientSide.getStatus() == ClientStatus.READY ? ClientStatus.NO_READY : ClientStatus.READY); String result = MapHelper.newInstance() .put("clientNickName", clientSide.getNickname()) .put("status", clientSide.getStatus()) diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_WATCH.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_WATCH.java index 03b4598..532498f 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_WATCH.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_WATCH.java @@ -15,7 +15,7 @@ public class ServerEventListener_CODE_GAME_WATCH implements ServerEventListener @Override public void call(ClientSide clientSide, String data) { - Room room = ServerContains.getRoom(Integer.valueOf(data)); + Room room = ServerContains.getRoom(Integer.parseInt(data)); if (room == null) { String result = MapHelper.newInstance() diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java index 77e8b1c..3b3ff0a 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java @@ -28,7 +28,7 @@ public void call(ClientSide clientSide, String data) { clientSide.setRoomId(room.getId()); ServerContains.addRoom(room); - clientSide.setStatus(ClientStatus.NOT_READY); + clientSide.setStatus(ClientStatus.NO_READY); ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_CREATE_SUCCESS, Noson.reversal(room)); } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_JOIN.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_JOIN.java index ce7655d..ca24ab4 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_JOIN.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_JOIN.java @@ -9,6 +9,7 @@ import org.nico.ratel.landlords.enums.ClientEventCode; import org.nico.ratel.landlords.enums.ClientStatus; import org.nico.ratel.landlords.enums.RoomStatus; +import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.helper.MapHelper; import org.nico.ratel.landlords.server.ServerContains; @@ -34,6 +35,8 @@ public void call(ClientSide clientSide, String data) { ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_JOIN_FAIL_BY_FULL, result); return; } + // join default ready + clientSide.setStatus(ClientStatus.READY); clientSide.setRoomId(room.getId()); ConcurrentSkipListMap roomClientMap = (ConcurrentSkipListMap) room.getClientSideMap(); @@ -51,10 +54,11 @@ public void call(ClientSide clientSide, String data) { if (roomClientMap.size() == 3) { clientSide.setNext(roomClientList.getFirst()); roomClientList.getFirst().setPre(clientSide); - } + ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(room.getId())); + return; + } room.setStatus(RoomStatus.WAIT); - clientSide.setStatus(ClientStatus.NOT_READY); String result = MapHelper.newInstance() .put("clientId", clientSide.getId()) .put("clientNickname", clientSide.getNickname()) diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/proxy/WebsocketProxy.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/proxy/WebsocketProxy.java index b8ce421..ce4fa75 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/proxy/WebsocketProxy.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/proxy/WebsocketProxy.java @@ -54,11 +54,6 @@ protected void initChannel(SocketChannel ch) throws Exception { SimplePrinter.serverLog("The websocket server was successfully started on port " + port); //Init robot. RobotDecisionMakers.init(); - - ServerContains.THREAD_EXCUTER.execute(() -> { - Timer timer=new Timer(); - timer.schedule(new RoomClearTask(), 0L, 3000L); - }); f.channel().closeFuture().sync(); } finally { parentGroup.shutdownGracefully(); diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/timer/RoomClearTask.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/timer/RoomClearTask.java index cfd44ed..4fa4edb 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/timer/RoomClearTask.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/timer/RoomClearTask.java @@ -56,21 +56,21 @@ public void doing() { if (alreadyLiveTime > liveTime) { SimplePrinter.serverLog("room " + room.getId() + " live time overflow " + liveTime + ", closed!"); ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(room.getClientSideList().get(0), null); - return; + continue; } long diff = now - room.getLastFlushTime(); if (room.getStatus() != RoomStatus.STARTING && diff > waitingStatusInterval) { SimplePrinter.serverLog("room " + room.getId() + " starting waiting time overflow " + waitingStatusInterval + ", closed!"); ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(room.getClientSideList().get(0), null); - return; + continue; } if (room.getType() != RoomType.PVP) { - return; + continue; } if (diff <= startingStatusInterval) { - return; + continue; } boolean allRobots = true; @@ -86,7 +86,7 @@ public void doing() { if (allRobots) { SimplePrinter.serverLog("room " + room.getId() + " all is robots, closed!"); ServerEventListener.get(ServerEventCode.CODE_CLIENT_EXIT).call(currentPlayer, null); - return; + continue; } //kick this client ChannelUtils.pushToClient(currentPlayer.getChannel(), ClientEventCode.CODE_CLIENT_KICK, null); diff --git a/pom.xml b/pom.xml index 07a52cd..7b12ca4 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.smallnico.ratel landlords - 1.2.7 + 1.3.0 ratel https://github.com/ainilili/ratel pom From bfd3e7ecd2f670c9844bb1de4eb29f066e7a9488 Mon Sep 17 00:00:00 2001 From: doveeeee Date: Fri, 16 Jul 2021 20:52:51 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=87=86=E5=A4=87?= =?UTF-8?q?=E5=90=8E=E4=B8=8D=E5=BC=80=E6=88=BF=E9=97=B4=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/event/ServerEventListener_CODE_GAME_POKER_PLAY.java | 1 + 1 file changed, 1 insertion(+) 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 2daf3a7..7e20410 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 @@ -168,6 +168,7 @@ private void gameOver(ClientSide winner, Room room) { room.setStatus(RoomStatus.WAIT); room.initScoreRate(); for (ClientSide client : room.getClientSideList()) { + client.setStatus(ClientStatus.NO_READY); ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, result); } }else{ From c35acba0ad643dbedc3d69e22b4ea4965f6b9807 Mon Sep 17 00:00:00 2001 From: Nico Date: Mon, 19 Jul 2021 10:07:14 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E6=AF=8F=E4=B8=80=E5=B1=80=E7=9A=84?= =?UTF-8?q?=E5=BC=80=E5=A7=8B=E6=9B=B4=E6=96=B0=E6=88=BF=E9=97=B4=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/event/ServerEventListener_CODE_GAME_STARTING.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java index 5f0f493..1c46386 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 @@ -37,6 +37,8 @@ public void call(ClientSide clientSide, String data) { // Push start game messages room.setStatus(RoomStatus.STARTING); + room.setCreateTime(System.currentTimeMillis()); + room.setLastFlushTime(System.currentTimeMillis()); // Record the first speaker room.setFirstSellClient(startGrabClient.getId());