merge upstream

This commit is contained in:
Abby
2018-11-15 14:18:02 +09:00
7 changed files with 202 additions and 31 deletions
@@ -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<String> serverAddressList = Noson.convert(buffer.toString(), new NoType<List<String>>() {});
if(serverAddress == null || port == 0){
String serverInfo = StreamUtils.convertToString(new URL("https://raw.githubusercontent.com/abbychau/ratel/master/serverlist.json"));
List<String> serverAddressList = Noson.convert(serverInfo, new NoType<List<String>>() {});
SimplePrinter.printNotice("Please select a server:");
for(int i = 0; i < serverAddressList.size(); i++) {
SimplePrinter.printNotice((i+1) + ". " + serverAddressList.get(i));
@@ -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<Poker> basePokers = new ArrayList<Poker>(54);
private static Comparator<Poker> pokerComparator = new Comparator<Poker>() {
@@ -404,4 +411,72 @@ public class PokerHelper {
}
return builder.toString();
}
public static List<PokerSell> parsePokerSells(List<Poker> pokers){
List<PokerSell> allPokerSell = new ArrayList<>();
//single or double or three or four
int count = 0;
int lastLevel = -1;
List<Poker> 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<Poker> 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;
}
}
@@ -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 createTime2018年11月15日 上午12:12:15
*/
public abstract class AbstractRobotDecisionMakers {
public abstract List<Poker> howToPlayPokers(PokerSell lastPokerSell, List<Poker> myPokers);
public abstract boolean howToChooseLandlord(List<Poker> leftPokers, List<Poker> rightPokers, List<Poker> myPokers);
}
@@ -12,14 +12,15 @@ import org.nico.ratel.landlords.entity.PokerSell;
*/
public class RobotDecisionMakers {
private static AbstractRobotDecisionMakers decisionMakers = new SimpleRobotDecisionMakers();
public static List<Poker> howToPlayPokers(PokerSell lastPokerSell, List<Poker> myPokers){
return null;
}
public static boolean howToChooseLandlord(List<Poker> leftPokers, List<Poker> rightPokers, List<Poker> landlordPokers, List<Poker> myPokers) {
return true;
public static boolean howToChooseLandlord(List<Poker> leftPokers, List<Poker> rightPokers, List<Poker> myPokers) {
return decisionMakers.howToChooseLandlord(leftPokers, rightPokers, myPokers);
}
}
@@ -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 createTime2018年11月15日 上午12:13:49
*/
public class SimpleRobotDecisionMakers extends AbstractRobotDecisionMakers{
@Override
public List<Poker> howToPlayPokers(PokerSell lastPokerSell, List<Poker> myPokers) {
return null;
}
@Override
public boolean howToChooseLandlord(List<Poker> leftPokers, List<Poker> rightPokers, List<Poker> myPokers) {
int leftScore = PokerHelper.parsePokerColligationScore(leftPokers);
int rightScore = PokerHelper.parsePokerColligationScore(rightPokers);
int myScore = PokerHelper.parsePokerColligationScore(myPokers);
return myScore >= (leftScore + rightScore)/2;
}
}
@@ -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());
}
}
@@ -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<Poker> landlordPokers = new ArrayList<>(20);
landlordPokers.addAll(robot.getPokers());
landlordPokers.addAll(room.getLandlordPokers());
List<Poker> leftPokers = new ArrayList<>(17);
leftPokers.addAll(robot.getPre().getPokers());
List<Poker> 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)));
}
}