Skip to content

Commit

Permalink
Merge pull request #59 from ainilili/feature/medium-robot
Browse files Browse the repository at this point in the history
Add medium robots and improve client user experience
  • Loading branch information
ainilili authored Dec 20, 2020
2 parents 5e43097 + c065620 commit 124d83f
Show file tree
Hide file tree
Showing 23 changed files with 409 additions and 46 deletions.
4 changes: 2 additions & 2 deletions landlords-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
<parent>
<groupId>com.smallnico.ratel</groupId>
<artifactId>landlords</artifactId>
<version>1.2.3</version>
<version>1.2.4</version>
</parent>

<dependencies>
<dependency>
<groupId>com.smallnico.ratel</groupId>
<artifactId>landlords-common</artifactId>
<version>1.2.3</version>
<version>1.2.4</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SimpleClient {

public static String serverAddress;

public static int port = 80;
public static int port = 1024;

private static String[] serverAddressSource = new String[] {
"https://raw.githubusercontent.com/ainilili/ratel/master/serverlist.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public void call(Channel channel, String data) {
}

if(turnClientId == SimpleClient.id) {
SimplePrinter.printNotice("It's your turn. Do you want to rob the landlord? [Y/N] (enter [EXIT] to exit current room)");
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")) {
if(line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("e")) {
pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT);
}else if(line.equalsIgnoreCase("Y")){
pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "TRUE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

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.entity.PokerSell;
import org.nico.ratel.landlords.enums.PokerLevel;
import org.nico.ratel.landlords.enums.ServerEventCode;
import org.nico.ratel.landlords.helper.MapHelper;
import org.nico.ratel.landlords.helper.PokerHelper;
import org.nico.ratel.landlords.print.SimplePrinter;
import org.nico.ratel.landlords.print.SimpleWriter;

Expand All @@ -26,17 +29,67 @@ public void call(Channel channel, String data) {
SimplePrinter.printPokers(pokers);


SimplePrinter.printNotice("Please enter the combination you came up with (enter [EXIT] to exit current room, enter [PASS] to jump current round)");
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 combination.)");
String line = SimpleWriter.write("combination");

if(line == null){
SimplePrinter.printNotice("Invalid enter");
call(channel, data);
}else{
if(line.equalsIgnoreCase("PASS")) {
if(line.equalsIgnoreCase("pass") || line.equalsIgnoreCase("p")) {
pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_PASS);
}else if(line.equalsIgnoreCase("EXIT")){
}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")) {
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"))) {
SimplePrinter.printNotice("Up to you !");
call(channel, data);
return;
}else {
List<Poker> lastSellPokers = Noson.convert(lastSellPokersObj, new NoType<List<Poker>>() {});
List<PokerSell> sells = PokerHelper.validSells(PokerHelper.checkPokerType(lastSellPokers), pokers);
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 ++) {
SimplePrinter.printNotice(i + 1 + ". " + PokerHelper.textOnlyNoType(sells.get(i).getSellPokers()));
}
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")) {
call(channel, data);
return;
}else {
try {
int choose = Integer.valueOf(line);
if(choose < 1 || choose > sells.size()) {
SimplePrinter.printNotice("The input number must be in the range of 1 to " + sells.size() + ".");
}else {
List<Poker> choosePokers = sells.get(choose - 1).getSellPokers();
List<Character> options = new ArrayList<>();
for(Poker poker: choosePokers) {
options.add(poker.getLevel().getAlias()[0]);
}
pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY, Noson.reversal(options.toArray(new Character[] {})));
break;
}
}catch(NumberFormatException e) {
SimplePrinter.printNotice("Please input a number.");
}
}
}
}

// PokerHelper.validSells(lastPokerSell, pokers);
}else {
String[] strs = line.split(" ");
List<Character> options = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public void call(Channel channel, String data) {
SimplePrinter.printNotice("1. PvP");
SimplePrinter.printNotice("2. PvE");
SimplePrinter.printNotice("3. Settings");
SimplePrinter.printNotice("Please select an option above (enter [EXIT] to log out)");
SimplePrinter.printNotice("Please select an option above (enter [exit|e] to log out)");
String line = SimpleWriter.write("selection");

if(line.equalsIgnoreCase("EXIT")) {
if(line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("e")) {
System.exit(0);
}else {
int choose = OptionsUtils.getOptions(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public void call(Channel channel, String data) {
SimplePrinter.printNotice("1. Easy Mode");
SimplePrinter.printNotice("2. Medium Mode");
SimplePrinter.printNotice("3. Hard Mode");
SimplePrinter.printNotice("Please select an option above (enter [BACK] to return to options list)");
SimplePrinter.printNotice("Please select an option above (enter [back|b] to return to options list)");
String line = SimpleWriter.write("pve");

if(line.equalsIgnoreCase("BACK")) {
if(line.equalsIgnoreCase("back") || line.equalsIgnoreCase("b")) {
get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data);
}else {
int choose = OptionsUtils.getOptions(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public void call(Channel channel, String data) {
SimplePrinter.printNotice("2. Room List");
SimplePrinter.printNotice("3. Join Room");
SimplePrinter.printNotice("4. Spectate Game");
SimplePrinter.printNotice("Please select an option above (enter [BACK] to return to options list)");
SimplePrinter.printNotice("Please select an option above (enter [back|b] to return to options list)");
String line = SimpleWriter.write("pvp");

if(line.equalsIgnoreCase("BACK")) {
if(line.equalsIgnoreCase("back") || line.equalsIgnoreCase("b")) {
get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data);
}else {
int choose = OptionsUtils.getOptions(line);
Expand All @@ -30,10 +30,10 @@ public void call(Channel channel, String data) {
}else if(choose == 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] to return to options list)");
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")) {
if(line.equalsIgnoreCase("back") || line.equalsIgnoreCase("b")) {
call(channel, data);
}else {
int option = OptionsUtils.getOptions(line);
Expand All @@ -45,10 +45,10 @@ public void call(Channel channel, String data) {
}
}
} else if (choose == 4) {
SimplePrinter.printNotice("Please enter the room id you want to spectate (enter [BACK] to return to options list)");
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")) {
if(line.equalsIgnoreCase("back") || line.equalsIgnoreCase("b")) {
call(channel, data);
}else {
int option = OptionsUtils.getOptions(line);
Expand Down
2 changes: 1 addition & 1 deletion landlords-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>com.smallnico.ratel</groupId>
<artifactId>landlords</artifactId>
<version>1.2.3</version>
<version>1.2.4</version>
</parent>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,25 @@ public final int getLevel() {
public final void setLevel(int level) {
this.level = level;
}

public static final PokerLevel parseByName(String name) {
if(name == null) {
return null;
}
for(PokerLevel level: PokerLevel.values()) {
if(level.name.equals(name.toUpperCase())) {
return level;
}
}
return null;
}

public static final PokerLevel parseByLevel(int l) {
for(PokerLevel level: PokerLevel.values()) {
if(level.level == l) {
return level;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,41 @@ public int compare(Poker o1, Poker o2) {
public static void sortPoker(List<Poker> pokers){
Collections.sort(pokers, pokerComparator);
}

public static List<Poker> clonePokers(List<Poker> pokers){
List<Poker> newPokers = new ArrayList<Poker>(pokers.size());
for(Poker poker: pokers) {
newPokers.add(new Poker(poker.getLevel(), poker.getType()));
}
return newPokers;
}

public static List<PokerSell> validSells(PokerSell lastPokerSell, List<Poker> pokers) {
List<PokerSell> sells = PokerHelper.parsePokerSells(pokers);
if(lastPokerSell == null) {
return sells;
}

List<PokerSell> validSells = new ArrayList<PokerSell>();
for(PokerSell sell: sells) {
if(sell.getSellType() == lastPokerSell.getSellType()) {
if(sell.getScore() > lastPokerSell.getScore() && sell.getSellPokers().size() == lastPokerSell.getSellPokers().size()) {
validSells.add(sell);
}
}
if(sell.getSellType() == SellType.KING_BOMB) {
validSells.add(sell);
}
}
if(lastPokerSell.getSellType() != SellType.BOMB) {
for(PokerSell sell: sells) {
if(sell.getSellType() == SellType.BOMB) {
validSells.add(sell);
}
}
}
return validSells;
}

public static int[] getIndexes(Character[] options, List<Poker> pokers) {
List<Poker> copyList = new ArrayList<>(pokers.size());
Expand Down Expand Up @@ -538,7 +573,7 @@ private static void parseArgs(Set<Integer> existLevelSet, List<PokerSell> pokerS
}
}
}

private static void parsePokerSellStraight(List<PokerSell> pokerSells, SellType sellType) {
int minLenght = -1;
int width = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.entity.Poker;
import org.nico.ratel.landlords.entity.PokerSell;

Expand All @@ -13,7 +14,7 @@

public abstract class AbstractRobotDecisionMakers {

public abstract PokerSell howToPlayPokers(PokerSell lastPokerSell, List<Poker> myPokers);
public abstract PokerSell howToPlayPokers(PokerSell lastPokerSell, ClientSide robot);

public abstract boolean howToChooseLandlord(List<Poker> leftPokers, List<Poker> rightPokers, List<Poker> myPokers);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import java.util.Random;

import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.entity.Poker;
import org.nico.ratel.landlords.entity.PokerSell;
import org.nico.ratel.landlords.enums.SellType;
Expand All @@ -11,21 +12,19 @@
/**
*
* @author nico
* @version createTime:2018年11月15日 上午12:13:49
* @date 2018-11-15 12:13:49
*/

public class SimpleRobotDecisionMakers extends AbstractRobotDecisionMakers{
public class EasyRobotDecisionMakers extends AbstractRobotDecisionMakers{

private static Random random = new Random();

@Override
public PokerSell howToPlayPokers(PokerSell lastPokerSell, List<Poker> myPokers) {

public PokerSell howToPlayPokers(PokerSell lastPokerSell, ClientSide robot) {
if(lastPokerSell != null && lastPokerSell.getSellType() == SellType.KING_BOMB) {
return null;
}

List<PokerSell> sells = PokerHelper.parsePokerSells(myPokers);
List<PokerSell> sells = PokerHelper.parsePokerSells(robot.getPokers());
if(lastPokerSell == null) {
return sells.get(random.nextInt(sells.size()));
}
Expand Down
Loading

0 comments on commit 124d83f

Please sign in to comment.