Skip to content

Commit

Permalink
添加准备系统 游戏结束后不再自动退出房间
Browse files Browse the repository at this point in the history
  • Loading branch information
doveeeee committed Jul 14, 2021
1 parent bb9500f commit d8bbde8
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class ClientEventListener_CODE_GAME_OVER extends ClientEventListener {
public void call(Channel channel, String data) {
Map<String, Object> map = MapHelper.parser(data);
SimplePrinter.printNotice("\nPlayer " + map.get("winnerNickname") + "[" + map.get("winnerType") + "]" + " won the game");
SimplePrinter.printNotice("Game over, friendship first, competition second\n");
ClientEventListener_CODE_GAME_READY.gameReady(channel);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void call(Channel channel, String data) {
List<Poker> lastSellPokers = Noson.convert(lastSellPokersObj, new NoType<List<Poker>>() {
});
List<PokerSell> sells = PokerHelper.validSells(PokerHelper.checkPokerType(lastSellPokers), pokers);
if (sells == null || sells.size() == 0) {
if (sells.size() == 0) {
SimplePrinter.printNotice("It is a pity that, there is no winning combination...");
call(channel, data);
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.nico.ratel.landlords.client.event;

import io.netty.channel.Channel;
import org.nico.ratel.landlords.channel.ChannelUtils;
import org.nico.ratel.landlords.client.SimpleClient;
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;

import java.util.Map;

public class ClientEventListener_CODE_GAME_READY extends ClientEventListener {
@Override
public void call(Channel channel, String data) {
Map<String, Object> map = MapHelper.parser(data);
if (SimpleClient.id == (int) map.get("clientId")) {
SimplePrinter.printNotice("you are ready to play game.");
return;
}
SimplePrinter.printNotice(map.get("clientNickName").toString() + " is ready to play game.");
}

static void gameReady(Channel channel) {
SimplePrinter.printNotice("\nPress any key to get ready(Press [exit|e] to exit the room)");
String line = SimpleWriter.write("notReady");
if (line.equals("")) {
gameReady(channel);
return;
}
if (line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("e")) {
ChannelUtils.pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT, "");
return;
}
ChannelUtils.pushToServer(channel, ServerEventCode.CODE_GAME_READY, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ private void printGameResult(Object rawData, Channel channel) {
Map<String, Object> map = MapHelper.parser(rawData.toString());

printNoticeWithTime("Player [" + map.get("winnerNickname") + "](" + map.get("winnerType") + ") won the game.");
quitWatch(channel);
}

private void printKickInfo(Object rawData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public void call(Channel channel, String data) {

SimplePrinter.printNotice("You have created a room with id " + room.getId());
SimplePrinter.printNotice("Please wait for other players to join !");

ClientEventListener_CODE_GAME_READY.gameReady(channel);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void call(Channel channel, String data) {
if (SimpleClient.id == joinClientId) {
SimplePrinter.printNotice("You have joined room:" + map.get("roomId") + ". There are " + map.get("roomClientCount") + " players in the room now.");
SimplePrinter.printNotice("Please wait for other players to join. The game would start at three players!");
ClientEventListener_CODE_GAME_READY.gameReady(channel);
} else {
SimplePrinter.printNotice(map.get("clientNickname") + " joined room, there are currently " + map.get("roomClientCount") + " in the room.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Room(int id) {
this.id = id;
this.clientSideMap = new ConcurrentSkipListMap<>();
this.clientSideList = new LinkedList<>();
this.status = RoomStatus.BLANK;
this.status = RoomStatus.WAIT;
}

public final long getCreateTime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public enum ClientEventCode implements Serializable {

CODE_PVE_DIFFICULTY_NOT_SUPPORT("人机难度不支持"),

CODE_GAME_READY("准备开始游戏"),

CODE_GAME_WATCH("观战"),

CODE_GAME_WATCH_SUCCESSFUL("观战成功");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public enum ClientStatus {

TO_CHOOSE,

NO_READY,
NOT_READY,

READY,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public enum ServerEventCode implements Serializable {

CODE_GAME_STARTING("游戏开始"),

CODE_GAME_READY("玩家准备"),

CODE_GAME_LANDLORD_ELECT("抢地主"),

CODE_GAME_POKER_PLAY("出牌环节"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ public void call(ClientSide clientSide, String data) {
for(ClientSide client: room.getClientSideList()) {
if(client.getRole() == ClientRole.PLAYER) {
ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_OVER, result);
client.setStatus(ClientStatus.NOT_READY);
}
}
room.setStatus(RoomStatus.WAIT);

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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.*;
import org.nico.ratel.landlords.helper.MapHelper;
import org.nico.ratel.landlords.print.SimplePrinter;
import org.nico.ratel.landlords.server.ServerContains;

import java.util.concurrent.ConcurrentSkipListMap;

public class ServerEventListener_CODE_GAME_READY implements ServerEventListener {
@Override
public void call(ClientSide clientSide, String data) {
Room room = ServerContains.getRoom(clientSide.getRoomId());
SimplePrinter.serverLog("房间状态:" + room.getStatus());
SimplePrinter.serverLog("玩家状态:" + clientSide.getStatus());
if (room.getStatus() == RoomStatus.STARTING) {
return;
}
if (clientSide.getStatus() == ClientStatus.PLAYING || clientSide.getStatus() == ClientStatus.TO_CHOOSE || clientSide.getStatus() == ClientStatus.CALL_LANDLORD) {
return;
}
clientSide.setStatus(clientSide.getStatus() == ClientStatus.READY ? ClientStatus.NOT_READY : ClientStatus.READY);
String result = MapHelper.newInstance()
.put("clientNickName", clientSide.getNickname())
.put("status", clientSide.getStatus())
.put("clientId", clientSide.getId())
.json();
boolean allReady = true;
ConcurrentSkipListMap<Integer, ClientSide> roomClientMap = (ConcurrentSkipListMap<Integer, ClientSide>) room.getClientSideMap();
if (roomClientMap.size() < 3) {
allReady = false;
} else {
for (ClientSide client : room.getClientSideList()) {
if (client.getRole() == ClientRole.PLAYER && client.getStatus() != ClientStatus.READY) {
allReady = false;
break;
}
}
}

for (ClientSide client : room.getClientSideList()) {
if (client.getRole() == ClientRole.PLAYER) {
ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_GAME_READY, result);
}
}

if (allReady) {
ServerEventListener.get(ServerEventCode.CODE_GAME_STARTING).call(clientSide, data);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void call(ClientSide clientSide, String data) {

for (ClientSide client : roomClientList) {
client.setType(ClientType.PEASANT);
client.setStatus(ClientStatus.PLAYING);

String result = MapHelper.newInstance()
.put("roomId", room.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,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.ClientStatus;
import org.nico.ratel.landlords.enums.RoomStatus;
import org.nico.ratel.landlords.enums.RoomType;
import org.nico.ratel.landlords.server.ServerContains;
Expand All @@ -15,7 +16,7 @@ public class ServerEventListener_CODE_ROOM_CREATE implements ServerEventListener
public void call(ClientSide clientSide, String data) {

Room room = new Room(ServerContains.getServerId());
room.setStatus(RoomStatus.BLANK);
room.setStatus(RoomStatus.WAIT);
room.setType(RoomType.PVP);
room.setRoomOwner(clientSide.getNickname());
room.getClientSideMap().put(clientSide.getId(), clientSide);
Expand All @@ -27,6 +28,8 @@ public void call(ClientSide clientSide, String data) {
clientSide.setRoomId(room.getId());
ServerContains.addRoom(room);

clientSide.setStatus(ClientStatus.NOT_READY);

ChannelUtils.pushToClient(clientSide.getChannel(), ClientEventCode.CODE_ROOM_CREATE_SUCCESS, Noson.reversal(room));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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;

Expand Down Expand Up @@ -52,12 +51,10 @@ public void call(ClientSide clientSide, String data) {
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);

clientSide.setStatus(ClientStatus.NOT_READY);
String result = MapHelper.newInstance()
.put("clientId", clientSide.getId())
.put("clientNickname", clientSide.getNickname())
Expand Down

0 comments on commit d8bbde8

Please sign in to comment.