代码整体重构

This commit is contained in:
doveeeee
2021-07-13 19:41:50 +08:00
parent f53553f844
commit e11b976bbd
16 changed files with 964 additions and 989 deletions
@@ -8,7 +8,7 @@ import org.nico.ratel.landlords.utils.OptionsUtils;
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 class ClientEventListener_CODE_SHOW_OPTIONS_PVE extends ClientEventListen
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));
}
}
@@ -8,7 +8,7 @@ import org.nico.ratel.landlords.utils.OptionsUtils;
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 class ClientEventListener_CODE_SHOW_OPTIONS_PVP extends ClientEventListen
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")) {
get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data);
}else {
int choose = OptionsUtils.getOptions(line);
if(choose == 1) {
pushToServer(channel, ServerEventCode.CODE_ROOM_CREATE, null);
}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|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 == null) {
SimplePrinter.printNotice("Invalid options, please choose again");
call(channel, data);
return;
}
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 {
if (line.equalsIgnoreCase("BACK") || line.equalsIgnoreCase("b")) {
get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data);
return;
}
int choose = OptionsUtils.getOptions(line);
switch (choose) {
case 1:
pushToServer(channel, ServerEventCode.CODE_ROOM_CREATE, null);
break;
case 2:
pushToServer(channel, ServerEventCode.CODE_GET_ROOMS, null);
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));
}
}
@@ -17,44 +17,42 @@ import io.netty.handler.timeout.IdleStateEvent;
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<String, Object> wrapMap = new HashMap<>(3);
wrapMap.put("code", code);
wrapMap.put("data", clientTransferData.getData());
if (User.INSTANCE.isWatching()) {
Map<String, Object> 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());
}
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) {
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 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) {
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);
}
@@ -12,12 +12,12 @@ import io.netty.handler.codec.MessageToMessageCodec;
public class SecondProtobufCodec extends MessageToMessageCodec<ClientTransferDataProtoc, MessageLite> {
@Override
protected void encode(ChannelHandlerContext ctx, MessageLite msg, List<Object> out) throws Exception {
protected void encode(ChannelHandlerContext ctx, MessageLite msg, List<Object> out) {
out.add(msg);
}
@Override
protected void decode(ChannelHandlerContext ctx, ClientTransferDataProtoc msg, List<Object> out) throws Exception {
protected void decode(ChannelHandlerContext ctx, ClientTransferDataProtoc msg, List<Object> out) {
out.add(msg);
}
@@ -53,10 +53,10 @@ public class PokerHelper {
}
}
public static void sortPoker(List<Poker> pokers){
Collections.sort(pokers, pokerComparator);
public static void sortPoker(List<Poker> pokers) {
pokers.sort(pokerComparator);
}
public static List<Poker> clonePokers(List<Poker> pokers){
List<Poker> newPokers = new ArrayList<Poker>(pokers.size());
for(Poker poker: pokers) {
@@ -64,7 +64,7 @@ public class PokerHelper {
}
return newPokers;
}
public static List<PokerSell> validSells(PokerSell lastPokerSell, List<Poker> pokers) {
List<PokerSell> sells = PokerHelper.parsePokerSells(pokers);
if(lastPokerSell == null) {
@@ -96,22 +96,23 @@ public class PokerHelper {
List<Poker> 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 class PokerHelper {
return indexes;
}
public static boolean checkPokerIndex(int[] indexes, List<Poker> 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<Poker> 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<Poker> 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 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++;
}
}
int startIndex = -1;
int endIndex = -1;
int count = 0;
if (singleCount == doubleCount && singleCount == threeCount && singleCount == 0 && fourCount == 1) {
return new PokerSell(SellType.BOMB, pokers, startIndex);
}
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 ++;
}
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 (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);
}
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(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(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 (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(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 (fourEndIndex - fourStartIndex + 1 == fourCount && fourEndIndex < PokerLevel.LEVEL_2.getLevel()) {
return new PokerSell(SellType.FOUR_STRAIGHT_WITH_SINGLE, pokers, fourEndIndex);
}
}
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);
}
}
} else if (doubleCount != 0 && doubleCount == fourCount * 2 && singleCount == 0 && threeCount == 0) {
if (fourCount == 1) {
return new PokerSell(SellType.FOUR_ZONES_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);
}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 (fourEndIndex - fourStartIndex + 1 == fourCount && fourEndIndex < PokerLevel.LEVEL_2.getLevel()) {
return new PokerSell(SellType.FOUR_STRAIGHT_WITH_DOUBLE, pokers, fourEndIndex);
}
}
}
@@ -256,37 +252,37 @@ public class PokerHelper {
}
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<Poker> getPoker(int[] indexes, List<Poker> pokers){
public static List<Poker> getPoker(int[] indexes, List<Poker> pokers) {
List<Poker> 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<Poker> pres, List<Poker> currents){
public static boolean comparePoker(List<Poker> pres, List<Poker> currents) {
return true;
}
public static List<List<Poker>> distributePoker(){
public static List<List<Poker>> distributePoker() {
Collections.shuffle(basePokers);
List<List<Poker>> pokersList = new ArrayList<List<Poker>>();
List<Poker> pokers1 = new ArrayList<>(17);
@@ -301,7 +297,7 @@ public class PokerHelper {
pokersList.add(pokers2);
pokersList.add(pokers3);
pokersList.add(pokers4);
for(List<Poker> pokers: pokersList) {
for (List<Poker> pokers : pokersList) {
sortPoker(pokers);
}
return pokersList;
@@ -309,50 +305,51 @@ public class PokerHelper {
public static String printPoker(List<Poker> 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<Poker> pokers){
StringBuilder builder = new StringBuilder();
if(pokers != null && pokers.size() > 0) {
for(int index = 0; index < pokers.size(); index ++) {
if(index == 0) {
private static String buildHandStringSharp(List<Poker> pokers) {
StringBuilder builder = new StringBuilder();
if (pokers != null && pokers.size() > 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 @@ public class PokerHelper {
}
return builder.toString();
}
private static String buildHandStringRounded(List<Poker> pokers){
StringBuilder builder = new StringBuilder();
if(pokers != null && pokers.size() > 0) {
for(int index = 0; index < pokers.size(); index ++) {
if(index == 0) {
private static String buildHandStringRounded(List<Poker> pokers) {
StringBuilder builder = new StringBuilder();
if (pokers != null && pokers.size() > 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 @@ public class PokerHelper {
return builder.toString();
}
private static String textOnly(List<Poker> pokers){
private static String textOnly(List<Poker> 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);
}
}
return builder.toString();
}
public static String textOnlyNoType(List<Poker> 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 + " ");
builder.append(name).append(type);
}
}
return builder.toString();
}
public static int parsePokerColligationScore(List<Poker> pokers){
public static String textOnlyNoType(List<Poker> pokers) {
StringBuilder builder = new StringBuilder();
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<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 ++){
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 class PokerHelper {
return score;
}
public static List<PokerSell> parsePokerSells(List<Poker> pokers){
public static List<PokerSell> parsePokerSells(List<Poker> pokers) {
List<PokerSell> pokerSells = new ArrayList<>();
int size = pokers.size();
@@ -469,27 +468,26 @@ public class PokerHelper {
int count = 0;
int lastLevel = -1;
List<Poker> 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 class PokerHelper {
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<PokerSell> pokerSells, PokerSell pokerSell, int deep, SellType sellType, SellType targetSellType) {
Set<Integer> 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<Integer> existLevelSet, List<PokerSell> pokerSells, Set<List<Poker>> pokersList, PokerSell pokerSell, int deep, SellType sellType, SellType targetSellType) {
if(deep == 0) {
List<Poker> allPokers = new ArrayList<>();
allPokers.addAll(pokerSell.getSellPokers());
for(List<Poker> ps: pokersList) {
if (deep == 0) {
List<Poker> allPokers = new ArrayList<>(pokerSell.getSellPokers());
for (List<Poker> 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 @@ public class PokerHelper {
}
}
}
private static void parsePokerSellStraight(List<PokerSell> 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 @@ public class PokerHelper {
int increase_1 = 0;
int lastLevel_1 = -1;
List<Poker> 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<Poker> 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());
}
}
lastLevel_1 = level;
if (sell.getSellType() != sellType) {
continue;
}
}
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<Poker> pokers = ListUtils.getList(sellPokers_1.subList(subIndex * width, (subIndex + len) * width));
pokerSells.add(new PokerSell(targetSellType, pokers, pokers.get(pokers.size() - 1).getLevel().getLevel()));
}
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;
}
}
increase_1 = 0;
sellPokers_1.clear();
sellPokers_1.addAll(sell.getSellPokers());
lastLevel_1 = level;
}
addPokers(pokerSells, minLength, width, targetSellType, increase_1, sellPokers_1);
}
private static void addPokers(List<PokerSell> pokerSells, int minLenght, int width, SellType targetSellType, int increase_1, List<Poker> 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<Poker> pokers = ListUtils.getList(sellPokers_1.subList(subIndex * width, (subIndex + len) * width));
pokerSells.add(new PokerSell(targetSellType, pokers, pokers.get(pokers.size() - 1).getLevel().getLevel()));
}
}
}
sellPokers_1.clear();
}
}
@@ -4,6 +4,7 @@ import org.junit.Assert;
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 @@ import java.util.ArrayList;
public class PokerHelperTest {
private ArrayList<Poker> pokers = new ArrayList<>();
private ArrayList<Poker> 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));
}
@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 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 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 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 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 testPrintPoker() {
Assert.assertNotNull(PokerHelper.printPoker(pokers));
}
@Test
public void testDistributePoker() {
Assert.assertNotNull(PokerHelper.distributePoker());
}
@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());
@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());
}
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());
@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.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(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.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.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));
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_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());
}
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());
@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.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.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());
}
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());
@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());
}
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());
@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());
}
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());
}
}
@@ -15,23 +15,24 @@ public class ServerEventListener_CODE_CLIENT_EXIT implements ServerEventListener
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();
}
}
notifyWatcherClientExit(room, clientSide);
ServerContains.removeRoom(room.getId());
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());
}
/**
@@ -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);
}
}
@@ -15,24 +15,25 @@ public class ServerEventListener_CODE_CLIENT_OFFLINE implements ServerEventListe
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();
}
}
}
ServerContains.removeRoom(room.getId());
if (room == null) {
ServerContains.CLIENT_SIDE_MAP.remove(clientSide.getId());
return;
}
ServerContains.CLIENT_SIDE_MAP.remove(clientSide.getId());
}
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());
}
}
@@ -15,82 +15,83 @@ import org.nico.ratel.landlords.server.robot.RobotEventListener;
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);
}
/**
@@ -8,117 +8,111 @@ import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.entity.Poker;
import org.nico.ratel.landlords.entity.PokerSell;
import org.nico.ratel.landlords.entity.Room;
import org.nico.ratel.landlords.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<Poker> 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);
}
}
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;
}
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);
}
}
boolean sellFlag = true;
List<Poker> 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();
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 ((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);
}
}
@@ -26,12 +26,7 @@ public class ServerEventListener_CODE_ROOM_CREATE implements ServerEventListener
clientSide.setRoomId(room.getId());
ServerContains.addRoom(room);
ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_CREATE_SUCCESS, Noson.reversal(room));
}
}
@@ -3,63 +3,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.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()));
}
}
@@ -7,68 +7,69 @@ 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.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<Integer, ClientSide> roomClientMap = (ConcurrentSkipListMap<Integer, ClientSide>) room.getClientSideMap();
LinkedList<ClientSide> roomClientList = room.getClientSideList();
if(roomClientList.size() > 0){
ClientSide pre = roomClientList.getLast();
pre.setNext(clientSide);
clientSide.setPre(pre);
}
roomClientList.add(clientSide);
roomClientMap.put(clientSide.getId(), clientSide);
if(roomClientMap.size() == 3) {
clientSide.setNext(roomClientList.getFirst());
roomClientList.getFirst().setPre(clientSide);
ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(room.getId()));
}else {
room.setStatus(RoomStatus.WAIT);
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);
}
}
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());
ConcurrentSkipListMap<Integer, ClientSide> roomClientMap = (ConcurrentSkipListMap<Integer, ClientSide>) room.getClientSideMap();
LinkedList<ClientSide> roomClientList = room.getClientSideList();
if (roomClientList.size() > 0) {
ClientSide pre = roomClientList.getLast();
pre.setNext(clientSide);
clientSide.setPre(pre);
}
roomClientList.add(clientSide);
roomClientMap.put(clientSide.getId(), clientSide);
if (roomClientMap.size() == 3) {
clientSide.setNext(roomClientList.getFirst());
roomClientList.getFirst().setPre(clientSide);
ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(room.getId()));
return;
}
room.setStatus(RoomStatus.WAIT);
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);
}
/**
@@ -58,39 +58,39 @@ public class ProtobufTransferHandler extends ChannelInboundHandlerAdapter {
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);
}
}
}
}
@@ -17,124 +17,117 @@ import org.nico.ratel.landlords.server.ServerContains;
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<Integer, Room> 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);
if (rooms == null || rooms.isEmpty()) {
return;
}
notifyWatcherClientKick(room, currentPlayer);
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;
}
//client current player
room.getClientSideMap().remove(currentPlayer.getId());
room.getClientSideList().remove(currentPlayer);
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;
}
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());
if (diff <= startingStatusInterval) {
return;
}
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);
}
}
}
}
}
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);
}
}