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 d4fd01e..213feb8 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 @@ -13,6 +13,7 @@ import org.nico.ratel.landlords.client.handler.DefaultChannelInitializer; import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.print.SimplePrinter; import org.nico.ratel.landlords.print.SimpleWriter; +import org.nico.ratel.landlords.utils.StreamUtils; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; @@ -24,7 +25,7 @@ public class SimpleClient { public static int id = -1; - public static String serverAddress = ""; + public static String serverAddress = null; public static int port = 0; @@ -38,35 +39,13 @@ public class SimpleClient { if(args[index].equalsIgnoreCase("-h") || args[index].equalsIgnoreCase("-host")) { serverAddress = args[index + 1]; } - if(args[index].equalsIgnoreCase("-pt") || args[index].equalsIgnoreCase("-pt")) { - int selection = Integer.parseInt(args[index + 1]); - - if( selection >= 1 && selection <= PokerHelper.totalPrinters ){ - PokerHelper.pokerPrinterType = selection - 1; - } - } - } } } - if(serverAddress.equals("") || port == 0){ - StringBuffer buffer = new StringBuffer(); - try { - URL url = new URL("https://raw.githubusercontent.com/abbychau/ratel/master/serverlist.json"); - URLConnection con = url.openConnection(); - con.setUseCaches(false); - InputStreamReader isr = new InputStreamReader(con.getInputStream()); - BufferedReader in = new BufferedReader(isr); - String s = null; - while ( (s = in.readLine()) != null) { - buffer.append(s).append("\n"); - } - } catch ( Exception e ) { - System.out.println(e.toString()); - buffer = null; - } - List serverAddressList = Noson.convert(buffer.toString(), new NoType>() {}); + if(serverAddress == null || port == 0){ + String serverInfo = StreamUtils.convertToString(new URL("https://raw.githubusercontent.com/abbychau/ratel/master/serverlist.json")); + List serverAddressList = Noson.convert(serverInfo, new NoType>() {}); SimplePrinter.printNotice("Please select a server:"); for(int i = 0; i < serverAddressList.size(); i++) { SimplePrinter.printNotice((i+1) + ". " + serverAddressList.get(i)); 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 9a54995..c4901ef 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 @@ -14,9 +14,16 @@ import org.nico.ratel.landlords.enums.PokerType; import org.nico.ratel.landlords.enums.SellType; public class PokerHelper { - + + /** + * 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 Comparator pokerComparator = new Comparator() { @@ -404,4 +411,72 @@ public class PokerHelper { } return builder.toString(); } + + public static List parsePokerSells(List pokers){ + List allPokerSell = new ArrayList<>(); + //single or double or three or four + int count = 0; + int lastLevel = -1; + List ps = new ArrayList<>(); + for(int index = 0; index < pokers.size(); index ++){ + int level = pokers.get(index).getLevel().getLevel(); + if(lastLevel == -1 || level == lastLevel){ + ++ count; + ps.add(pokers.get(index)); + }else{ + count = 0; + ps.clear(); + } + if(count == 1){ + allPokerSell.add(new PokerSell(level, SellType.SINGLE, ps)); + }else if(count == 2){ + allPokerSell.add(new PokerSell(level, SellType.DOUBLE, ps)); + }else if(count == 3){ + allPokerSell.add(new PokerSell(level, SellType.THREE, ps)); + }else if(count == 4){ + allPokerSell.add(new PokerSell(level + 999, SellType.BOMB, ps)); + } + } + + + return null; + } + + 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 ++){ + int level = pokers.get(index).getLevel().getLevel(); + if(lastLevel == -1){ + increase ++; + count ++; + score += lastLevel; + }else{ + if(level == lastLevel){ + ++ count; + }else{ + count = 0; + } + if(level < PokerLevel.LEVEL_2.getLevel() && level - 1 == lastLevel){ + ++ increase; + }else{ + increase = 0; + } + + score += (count + (increase > 4 ? increase : 0)) * level; + } + + if(level == PokerLevel.LEVEL_2.getLevel()){ + score += level * 2; + }else if(level > PokerLevel.LEVEL_2.getLevel()){ + score += level * 3; + } + lastLevel = level; + } + } + return score; + } } 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 new file mode 100644 index 0000000..5b695ed --- /dev/null +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/AbstractRobotDecisionMakers.java @@ -0,0 +1,19 @@ +package org.nico.ratel.landlords.robot; + +import java.util.List; + +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.entity.PokerSell; + +/** + * + * @author nico + * @version createTime:2018年11月15日 上午12:12:15 + */ + +public abstract class AbstractRobotDecisionMakers { + + public abstract List howToPlayPokers(PokerSell lastPokerSell, List myPokers); + + public abstract boolean howToChooseLandlord(List leftPokers, List rightPokers, List myPokers); +} 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 e9dce34..5f93a5b 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 @@ -12,14 +12,15 @@ import org.nico.ratel.landlords.entity.PokerSell; */ public class RobotDecisionMakers { + private static AbstractRobotDecisionMakers decisionMakers = new SimpleRobotDecisionMakers(); + public static List howToPlayPokers(PokerSell lastPokerSell, List myPokers){ return null; } - public static boolean howToChooseLandlord(List leftPokers, List rightPokers, List landlordPokers, List myPokers) { - - return true; + public static boolean howToChooseLandlord(List leftPokers, List rightPokers, List myPokers) { + return decisionMakers.howToChooseLandlord(leftPokers, rightPokers, myPokers); } } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/robot/SimpleRobotDecisionMakers.java b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/SimpleRobotDecisionMakers.java new file mode 100644 index 0000000..6b9001b --- /dev/null +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/robot/SimpleRobotDecisionMakers.java @@ -0,0 +1,30 @@ +package org.nico.ratel.landlords.robot; + +import java.util.List; + +import org.nico.ratel.landlords.entity.Poker; +import org.nico.ratel.landlords.entity.PokerSell; +import org.nico.ratel.landlords.helper.PokerHelper; + +/** + * + * @author nico + * @version createTime:2018年11月15日 上午12:13:49 + */ + +public class SimpleRobotDecisionMakers extends AbstractRobotDecisionMakers{ + + @Override + public List howToPlayPokers(PokerSell lastPokerSell, List myPokers) { + return null; + } + + @Override + public boolean howToChooseLandlord(List leftPokers, List rightPokers, List myPokers) { + int leftScore = PokerHelper.parsePokerColligationScore(leftPokers); + int rightScore = PokerHelper.parsePokerColligationScore(rightPokers); + int myScore = PokerHelper.parsePokerColligationScore(myPokers); + return myScore >= (leftScore + rightScore)/2; + } + +} 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 new file mode 100644 index 0000000..eda30cf --- /dev/null +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/utils/StreamUtils.java @@ -0,0 +1,48 @@ +package org.nico.ratel.landlords.utils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +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; + try { + while ((len = br.read(buf)) != -1) { + reqStr.append(new String(buf, 0, len)); + } + br.close(); + }catch(IOException e) { + return null; + }finally { + try { + br.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return reqStr.toString(); + } + + public static String convertToString(URL url) throws IOException { + URLConnection con = url.openConnection(); + con.setUseCaches(false); + return convertToString(con.getInputStream()); + } + + +} 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 e337d6b..cb528e1 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 @@ -1,11 +1,16 @@ package org.nico.ratel.landlords.server.robot; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Map; import org.nico.ratel.landlords.entity.ClientSide; +import org.nico.ratel.landlords.entity.Poker; import org.nico.ratel.landlords.entity.Room; import org.nico.ratel.landlords.enums.ServerEventCode; import org.nico.ratel.landlords.helper.MapHelper; +import org.nico.ratel.landlords.helper.PokerHelper; import org.nico.ratel.landlords.robot.RobotDecisionMakers; import org.nico.ratel.landlords.server.ServerContains; import org.nico.ratel.landlords.server.event.ServerEventListener; @@ -21,7 +26,21 @@ public class RobotEventListener_CODE_GAME_LANDLORD_ELECT implements RobotEventLi int turnClientId = (int) map.get("nextClientId"); if(turnClientId == robot.getId()) { - ServerEventListener.get(ServerEventCode.CODE_GAME_LANDLORD_ELECT).call(robot, String.valueOf(RobotDecisionMakers.howToChooseLandlord(robot.getPre().getPokers(), robot.getNext().getPokers(), room.getLandlordPokers(), robot.getPokers()))); + List landlordPokers = new ArrayList<>(20); + landlordPokers.addAll(robot.getPokers()); + landlordPokers.addAll(room.getLandlordPokers()); + + List leftPokers = new ArrayList<>(17); + leftPokers.addAll(robot.getPre().getPokers()); + + List rightPokers = new ArrayList<>(17); + leftPokers.addAll(robot.getNext().getPokers()); + + PokerHelper.sortPoker(landlordPokers); + PokerHelper.sortPoker(leftPokers); + PokerHelper.sortPoker(rightPokers); + + ServerEventListener.get(ServerEventCode.CODE_GAME_LANDLORD_ELECT).call(robot, String.valueOf(RobotDecisionMakers.howToChooseLandlord(leftPokers, rightPokers, landlordPokers))); } }