diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_EXIT.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_EXIT.java index 4e7e324..0a9bcdc 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_EXIT.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CLIENT_EXIT.java @@ -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); diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java index 8139e6a..08934c3 100644 --- a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java @@ -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); } } diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVE.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVE.java new file mode 100644 index 0000000..c1df052 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVE.java @@ -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); + } + } + + } + + + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVP.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVP.java new file mode 100644 index 0000000..1d5120a --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_PVP.java @@ -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); + } + } + + } + + + +} diff --git a/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_SETTING.java b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_SETTING.java new file mode 100644 index 0000000..5e0e899 --- /dev/null +++ b/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS_SETTING.java @@ -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); + } + } + } + + + +} diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/Default.java b/landlords-common/src/main/java/org/nico/ratel/landlords/Default.java deleted file mode 100644 index 392def7..0000000 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/Default.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.nico.ratel.landlords; - -public class Default { - -} diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientSide.java b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientSide.java index 4d72f74..23cc02f 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientSide.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientSide.java @@ -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; + } + } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java index b8b4bf2..78ab41b 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java @@ -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 clientSideMap; private LinkedList 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; } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java index 3ec3ba6..3c06019 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java @@ -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("展示房间列表"), diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientRole.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientRole.java new file mode 100644 index 0000000..d25ce81 --- /dev/null +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientRole.java @@ -0,0 +1,9 @@ +package org.nico.ratel.landlords.enums; + +public enum ClientRole{ + + PLAYER, + + ROBOT + +} diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java index 7066b9d..9b70da8 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java @@ -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 + } diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientType.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientType.java index e75a66f..241f11b 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientType.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientType.java @@ -1,7 +1,5 @@ package org.nico.ratel.landlords.enums; -import java.io.Serializable; - public enum ClientType{ LANDLORD, diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerLevel.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerLevel.java index 1d96a51..5fb62ae 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerLevel.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerLevel.java @@ -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; diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomStatus.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomStatus.java index 6073f2f..486c05c 100644 --- a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomStatus.java +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomStatus.java @@ -1,7 +1,5 @@ package org.nico.ratel.landlords.enums; -import java.io.Serializable; - public enum RoomStatus{ BLANK("空闲"), diff --git a/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomType.java b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomType.java new file mode 100644 index 0000000..badd5f7 --- /dev/null +++ b/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomType.java @@ -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; + } + +} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java index d1c049c..7e474c9 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_GAME_STARTING.java @@ -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); + } + } + } + } } diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java index f290265..e884fcb 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE.java @@ -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); diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE_PVE.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE_PVE.java new file mode 100644 index 0000000..aa63f29 --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_ROOM_CREATE_PVE.java @@ -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())); + } + + + + + +} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java index 8e5049c..ec04da9 100644 --- a/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java @@ -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()); diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener.java new file mode 100644 index 0000000..a1dea24 --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener.java @@ -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 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 listenerClass = (Class) Class.forName(eventListener); + listener = listenerClass.newInstance(); + RobotEventListener.LISTENER_MAP.put(code, listener); + } + return listener; + }catch(ClassNotFoundException | InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } + return listener; + } + +} diff --git a/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_LANDLORD_ELECT.java b/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_LANDLORD_ELECT.java new file mode 100644 index 0000000..b19bcef --- /dev/null +++ b/landlords-server/src/main/java/org/nico/ratel/landlords/server/robot/RobotEventListener_CODE_GAME_LANDLORD_ELECT.java @@ -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 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 !"); + } + + } + +}