merge 1.1.0 changes

This commit is contained in:
Abby
2018-11-14 22:07:20 +09:00
21 changed files with 391 additions and 38 deletions
@@ -21,7 +21,7 @@ public class ClientEventListener_CODE_CLIENT_EXIT extends ClientEventListener{
if(exitClientId == SimpleClient.id) {
role = "You";
}else {
role = (String) map.get("exitClientNickname");
role = String.valueOf(map.get("exitClientNickname"));
}
SimplePrinter.printNotice(role + " withdrew from the room. Room disbanded!!");
get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data);
@@ -1,6 +1,6 @@
package org.nico.ratel.landlords.client.event;
import org.nico.ratel.landlords.enums.ServerEventCode;
import org.nico.ratel.landlords.enums.ClientEventCode;
import org.nico.ratel.landlords.print.SimplePrinter;
import org.nico.ratel.landlords.print.SimpleWriter;
import org.nico.ratel.landlords.utils.OptionsUtils;
@@ -12,37 +12,28 @@ public class ClientEventListener_CODE_SHOW_OPTIONS extends ClientEventListener{
@Override
public void call(Channel channel, String data) {
SimplePrinter.printNotice("Options: ");
SimplePrinter.printNotice("1. Create Room");
SimplePrinter.printNotice("2. Room List");
SimplePrinter.printNotice("3. Join Room");
SimplePrinter.printNotice("1. PvP");
SimplePrinter.printNotice("2. PvE");
SimplePrinter.printNotice("3. Setting");
SimplePrinter.printNotice("Please enter the number of options");
String line = SimpleWriter.write("options");
while(line == null || (! line.equals("1") && ! line.equals("2") && ! line.equals("3"))) {
SimplePrinter.printNotice("Invalid options, please choose again: ");
line = SimpleWriter.write("options");
while(line == null || OptionsUtils.getOptions(line) == -1) {
SimplePrinter.printNotice("Invalid option, please choose again");
line = SimpleWriter.write("option");
}
int choose = Integer.valueOf(line);
if(choose == 1) {
pushToServer(channel, ServerEventCode.CODE_ROOM_CREATE, null);
get(ClientEventCode.CODE_SHOW_OPTIONS_PVP).call(channel, data);
}else if(choose == 2){
pushToServer(channel, ServerEventCode.CODE_GET_ROOMS, null);
get(ClientEventCode.CODE_SHOW_OPTIONS_PVE).call(channel, data);
}else if(choose == 3){
get(ClientEventCode.CODE_SHOW_OPTIONS_SETTING).call(channel, data);
}else {
SimplePrinter.printNotice("Please enter the room id you want to join (enter [BACK] return options list)");
line = SimpleWriter.write("roomid");
if(line.equalsIgnoreCase("BACK")) {
call(channel, data);
}else {
int option = OptionsUtils.getOptions(line);
if(line == null || option < 1) {
SimplePrinter.printNotice("Invalid options, please choose again: ");
call(channel, data);
}else{
pushToServer(channel, ServerEventCode.CODE_ROOM_JOIN, String.valueOf(option));
}
}
SimplePrinter.printNotice("Invalid option, please choose again");
call(channel, data);
}
}
@@ -0,0 +1,45 @@
package org.nico.ratel.landlords.client.event;
import org.nico.ratel.landlords.enums.ClientEventCode;
import org.nico.ratel.landlords.enums.ServerEventCode;
import org.nico.ratel.landlords.print.SimplePrinter;
import org.nico.ratel.landlords.print.SimpleWriter;
import org.nico.ratel.landlords.utils.OptionsUtils;
import io.netty.channel.Channel;
public class ClientEventListener_CODE_SHOW_OPTIONS_PVE extends ClientEventListener{
@Override
public void call(Channel channel, String data) {
SimplePrinter.printNotice("PVE: ");
SimplePrinter.printNotice("1. Simple Model");
SimplePrinter.printNotice("2. Medium Model");
SimplePrinter.printNotice("3. Difficulty Model");
SimplePrinter.printNotice("Please enter the number of options (enter [BACK] return options list)");
String line = SimpleWriter.write("pve");
if(line.equalsIgnoreCase("BACK")) {
get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data);
}else {
while(line == null || OptionsUtils.getOptions(line) == -1) {
SimplePrinter.printNotice("Invalid option, please choose again");
line = SimpleWriter.write("pve");
}
int choose = Integer.valueOf(line);
if(0 < choose && choose < 4) {
pushToServer(channel, ServerEventCode.CODE_ROOM_CREATE, null);
}else {
SimplePrinter.printNotice("Invalid option, please choose again");
call(channel, data);
}
}
}
}
@@ -0,0 +1,61 @@
package org.nico.ratel.landlords.client.event;
import org.nico.ratel.landlords.enums.ClientEventCode;
import org.nico.ratel.landlords.enums.ServerEventCode;
import org.nico.ratel.landlords.print.SimplePrinter;
import org.nico.ratel.landlords.print.SimpleWriter;
import org.nico.ratel.landlords.utils.OptionsUtils;
import io.netty.channel.Channel;
public class ClientEventListener_CODE_SHOW_OPTIONS_PVP extends ClientEventListener{
@Override
public void call(Channel channel, String data) {
SimplePrinter.printNotice("PVP: ");
SimplePrinter.printNotice("1. Create Room");
SimplePrinter.printNotice("2. Room List");
SimplePrinter.printNotice("3. Join Room");
SimplePrinter.printNotice("Please enter the number of options (enter [BACK] return options list)");
String line = SimpleWriter.write("pvp");
if(line.equalsIgnoreCase("BACK")) {
get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data);
}else {
while(line == null || OptionsUtils.getOptions(line) == -1) {
SimplePrinter.printNotice("Invalid option, please choose again");
line = SimpleWriter.write("pvp");
}
int choose = Integer.valueOf(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 want to join (enter [BACK] return options list)");
line = SimpleWriter.write("roomid");
if(line.equalsIgnoreCase("BACK")) {
call(channel, data);
}else {
int option = OptionsUtils.getOptions(line);
if(line == null || option < 1) {
SimplePrinter.printNotice("Invalid options, please choose again");
call(channel, data);
}else{
pushToServer(channel, ServerEventCode.CODE_ROOM_JOIN, String.valueOf(option));
}
}
}else {
SimplePrinter.printNotice("Invalid option, please choose again");
call(channel, data);
}
}
}
}
@@ -0,0 +1,48 @@
package org.nico.ratel.landlords.client.event;
import org.nico.ratel.landlords.enums.ClientEventCode;
import org.nico.ratel.landlords.helper.PokerHelper;
import org.nico.ratel.landlords.print.SimplePrinter;
import org.nico.ratel.landlords.print.SimpleWriter;
import org.nico.ratel.landlords.utils.OptionsUtils;
import io.netty.channel.Channel;
public class ClientEventListener_CODE_SHOW_OPTIONS_SETTING extends ClientEventListener{
@Override
public void call(Channel channel, String data) {
SimplePrinter.printNotice("Setting: ");
SimplePrinter.printNotice("1. Sniper Mode (Poker style camouflage)");
SimplePrinter.printNotice("2. Normal Mode");
SimplePrinter.printNotice("Please enter the number of setting (enter [BACK] return options list)");
String line = SimpleWriter.write("setting");
if(line.equalsIgnoreCase("BACK")) {
get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data);
}else {
while(line == null || OptionsUtils.getOptions(line) == -1) {
SimplePrinter.printNotice("Invalid setting, please choose again");
line = SimpleWriter.write("setting");
}
int choose = Integer.valueOf(line);
if(choose == 1) {
PokerHelper.disguise = true;
SimplePrinter.printNotice("Game mode switch to [Sniper Model]");
get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data);
}else if(choose == 2){
PokerHelper.disguise = false;
SimplePrinter.printNotice("Game mode switch to [Normal Model]");
get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data);
}else {
SimplePrinter.printNotice("Invalid setting, please choose again");
call(channel, data);
}
}
}
}
@@ -1,5 +0,0 @@
package org.nico.ratel.landlords;
public class Default {
}
@@ -2,6 +2,7 @@ package org.nico.ratel.landlords.entity;
import java.util.List;
import org.nico.ratel.landlords.enums.ClientRole;
import org.nico.ratel.landlords.enums.ClientStatus;
import org.nico.ratel.landlords.enums.ClientType;
@@ -19,6 +20,8 @@ public class ClientSide{
private ClientStatus status;
private ClientRole role;
private ClientType type;
private ClientSide next;
@@ -44,6 +47,14 @@ public class ClientSide{
pre = null;
}
public final ClientRole getRole() {
return role;
}
public final void setRole(ClientRole role) {
this.role = role;
}
public final String getNickname() {
return nickname;
}
@@ -116,4 +127,26 @@ public class ClientSide{
this.pre = pre;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ClientSide other = (ClientSide) obj;
if (id != other.id)
return false;
return true;
}
}
@@ -6,6 +6,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;
import org.nico.ratel.landlords.enums.RoomStatus;
import org.nico.ratel.landlords.enums.RoomType;
public class Room{
@@ -15,6 +16,8 @@ public class Room{
private RoomStatus status;
private RoomType type;
private Map<Integer, ClientSide> clientSideMap;
private LinkedList<ClientSide> clientSideList;
@@ -39,6 +42,14 @@ public class Room{
this.status = RoomStatus.BLANK;
}
public final RoomType getType() {
return type;
}
public final void setType(RoomType type) {
this.type = type;
}
public final PokerSell getLastPokerShell() {
return lastPokerShell;
}
@@ -10,7 +10,13 @@ public enum ClientEventCode implements Serializable{
CODE_CLIENT_CONNECT("客户端加入成功"),
CODE_SHOW_OPTIONS("展示选项"),
CODE_SHOW_OPTIONS("全局选项列表"),
CODE_SHOW_OPTIONS_SETTING("设置选项"),
CODE_SHOW_OPTIONS_PVP("玩家对战选项"),
CODE_SHOW_OPTIONS_PVE("人机对战选项"),
CODE_SHOW_ROOMS("展示房间列表"),
@@ -0,0 +1,9 @@
package org.nico.ratel.landlords.enums;
public enum ClientRole{
PLAYER,
ROBOT
}
@@ -1,7 +1,5 @@
package org.nico.ratel.landlords.enums;
import java.io.Serializable;
public enum ClientStatus{
TO_CHOOSE,
@@ -14,5 +12,7 @@ public enum ClientStatus{
CALL_LANDLORD,
PLAYING
}
@@ -1,7 +1,5 @@
package org.nico.ratel.landlords.enums;
import java.io.Serializable;
public enum ClientType{
LANDLORD,
@@ -1,6 +1,5 @@
package org.nico.ratel.landlords.enums;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@@ -1,7 +1,5 @@
package org.nico.ratel.landlords.enums;
import java.io.Serializable;
public enum RoomStatus{
BLANK("空闲"),
@@ -0,0 +1,24 @@
package org.nico.ratel.landlords.enums;
public enum RoomType{
PVP("玩家对战"),
PVE("人机对战"),
;
private String msg;
private RoomType(String msg) {
this.msg = msg;
}
public final String getMsg() {
return msg;
}
public final void setMsg(String msg) {
this.msg = msg;
}
}
@@ -11,10 +11,12 @@ import org.nico.ratel.landlords.entity.Room;
import org.nico.ratel.landlords.enums.ClientEventCode;
import org.nico.ratel.landlords.enums.ClientType;
import org.nico.ratel.landlords.enums.RoomStatus;
import org.nico.ratel.landlords.enums.RoomType;
import org.nico.ratel.landlords.helper.MapHelper;
import org.nico.ratel.landlords.helper.PokerHelper;
import org.nico.ratel.landlords.helper.TimeHelper;
import org.nico.ratel.landlords.server.ServerContains;
import org.nico.ratel.landlords.server.robot.RobotEventListener;
public class ServerEventListener_CODE_GAME_STARTING implements ServerEventListener{
@@ -41,6 +43,7 @@ public class ServerEventListener_CODE_GAME_STARTING implements ServerEventListen
// Push start game messages
room.setStatus(RoomStatus.STARTING);
for(ClientSide client: roomClientList) {
client.setType(ClientType.PEASANT);
@@ -53,9 +56,19 @@ public class ServerEventListener_CODE_GAME_STARTING implements ServerEventListen
.put("pokers", client.getPokers())
.json();
ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_STARTING, result);
if(room.getType() == RoomType.PVP) {
ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_STARTING, result);
}else {
if(client.equals(clientSide)) {
ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_STARTING, result);
}else {
RobotEventListener.get(ClientEventCode.CODE_GAME_LANDLORD_ELECT).call(clientSide, client, result);
}
}
}
}
}
@@ -6,6 +6,7 @@ import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.entity.Room;
import org.nico.ratel.landlords.enums.ClientEventCode;
import org.nico.ratel.landlords.enums.RoomStatus;
import org.nico.ratel.landlords.enums.RoomType;
import org.nico.ratel.landlords.server.ServerContains;
public class ServerEventListener_CODE_ROOM_CREATE implements ServerEventListener{
@@ -15,6 +16,7 @@ public class ServerEventListener_CODE_ROOM_CREATE implements ServerEventListener
Room room = new Room(ServerContains.getServerId());
room.setStatus(RoomStatus.BLANK);
room.setType(RoomType.PVP);
room.setRoomOwner(clientSide.getNickname());
room.getClientSideMap().put(clientSide.getId(), clientSide);
room.getClientSideList().add(clientSide);
@@ -0,0 +1,43 @@
package org.nico.ratel.landlords.server.event;
import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.entity.Room;
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.server.ServerContains;
public class ServerEventListener_CODE_ROOM_CREATE_PVE implements ServerEventListener{
@Override
public void call(ClientSide clientSide, String data) {
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);
clientSide.setRoomId(room.getId());
ServerContains.ROOM_MAP.put(room.getId(), room);
//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);
room.getClientSideMap().put(robot.getId(), robot);
room.getClientSideList().add(robot);
}
ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, String.valueOf(room.getId()));
}
}
@@ -6,6 +6,7 @@ import org.nico.ratel.landlords.channel.ChannelUtils;
import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.entity.ServerTransferData.ServerTransferDataProtoc;
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.ServerEventCode;
import org.nico.ratel.landlords.print.SimplePrinter;
@@ -24,6 +25,8 @@ public class TransferHandler extends ChannelInboundHandlerAdapter{
ClientSide clientSide = new ClientSide(((InetSocketAddress)ch.remoteAddress()).getPort(), ClientStatus.TO_CHOOSE, ch);
clientSide.setNickname(String.valueOf(clientSide.getId()));
clientSide.setRole(ClientRole.PLAYER);
ServerContains.CLIENT_SIDE_MAP.put(clientSide.getId(), clientSide);
SimplePrinter.serverLog("Has client connect to the server" + clientSide.getId());
@@ -0,0 +1,35 @@
package org.nico.ratel.landlords.server.robot;
import java.util.HashMap;
import java.util.Map;
import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.enums.ClientEventCode;
public interface RobotEventListener {
final static String LISTENER_PREFIX = "org.nico.ratel.landlords.server.robot.RobotEventListener_";
public final static Map<ClientEventCode, RobotEventListener> LISTENER_MAP = new HashMap<>();
public void call(ClientSide player, ClientSide robot, String data);
public static RobotEventListener get(ClientEventCode code) {
RobotEventListener listener = null;
try {
if(RobotEventListener.LISTENER_MAP.containsKey(code)){
listener = RobotEventListener.LISTENER_MAP.get(code);
}else{
String eventListener = LISTENER_PREFIX + code.name();
Class<RobotEventListener> listenerClass = (Class<RobotEventListener>) Class.forName(eventListener);
listener = listenerClass.newInstance();
RobotEventListener.LISTENER_MAP.put(code, listener);
}
return listener;
}catch(ClassNotFoundException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return listener;
}
}
@@ -0,0 +1,39 @@
package org.nico.ratel.landlords.server.robot;
import java.util.Map;
import org.nico.ratel.landlords.client.SimpleClient;
import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.enums.ServerEventCode;
import org.nico.ratel.landlords.helper.MapHelper;
import org.nico.ratel.landlords.print.SimplePrinter;
import org.nico.ratel.landlords.print.SimpleWriter;
public class RobotEventListener_CODE_GAME_LANDLORD_ELECT implements RobotEventListener{
@Override
public void call(ClientSide player, ClientSide robot, String data) {
Map<String, Object> map = MapHelper.parser(data);
int turnClientId = (int) map.get("nextClientId");
if(turnClientId == robot.getId()) {
SimplePrinter.printNotice("It's your turn. Do you want to rob the landlord? [Y/N] (enter [EXIT] to exit current room)");
String line = SimpleWriter.write("Y/N");
if(line.equalsIgnoreCase("EXIT")) {
pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT);
}else if(line.equalsIgnoreCase("Y")){
pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "TRUE");
}else if(line.equalsIgnoreCase("N")){
pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "FALSE");
}else{
SimplePrinter.printNotice("Invalid options");
call(channel, data);
}
}else {
SimplePrinter.printNotice("It's " + map.get("nextClientNickname") + "'s turn. Please wait patiently for his/her confirmation !");
}
}
}