mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-26 18:26:23 +00:00
提交完美的无头模式
This commit is contained in:
@@ -21,5 +21,6 @@ public interface GActionEnum {
|
||||
|
||||
/*************** 裁决 *********************/
|
||||
int CR_REFEREE_PVP_MODE = 4001; //裁决PVP模式
|
||||
int CR_REFEREE_PVP_END = 4002; //裁决PVP结束
|
||||
|
||||
}
|
||||
|
@@ -1,15 +1,12 @@
|
||||
package cn.jisol.game.actions;
|
||||
|
||||
import cn.jisol.game.network.client.GClient;
|
||||
import cn.jisol.game.network.client.GRefereeClient;
|
||||
import cn.jisol.game.proto.GPVPMessage;
|
||||
import cn.jisol.ngame.actions.core.NAction;
|
||||
import cn.jisol.ngame.actions.core.NActionMethod;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 裁决员行为 处理裁决
|
||||
@@ -20,27 +17,12 @@ public class GRefereeAction {
|
||||
//裁决员列表
|
||||
public static Map<String, GRefereeClient> REFEREES;
|
||||
|
||||
//裁决PVP
|
||||
public static void onPVPReferee(GPVPMessage.GPVPStart info){
|
||||
|
||||
GRefereeClient referee;
|
||||
|
||||
//判断是否有裁决员 如果 没有裁决员则返回
|
||||
if(Objects.isNull(referee = getFreeReferee())) {
|
||||
System.out.println("没有裁决员");
|
||||
return;
|
||||
}
|
||||
|
||||
//如果有裁决员则向裁决员发送要裁决的信息
|
||||
referee.invoke(GActionEnum.CR_REFEREE_PVP_MODE,info);
|
||||
|
||||
}
|
||||
|
||||
//获取一个空闲的裁决员
|
||||
public static GRefereeClient getFreeReferee(){
|
||||
|
||||
for (String key : REFEREES.keySet()) {
|
||||
if (!(REFEREES.get(key).isReferee)) {
|
||||
REFEREES.get(key).isReferee = true;
|
||||
return REFEREES.get(key);
|
||||
}
|
||||
}
|
||||
@@ -48,4 +30,28 @@ public class GRefereeAction {
|
||||
|
||||
}
|
||||
|
||||
//放回一个空闲裁决员
|
||||
public static void addFreeReferee(GRefereeClient referee){
|
||||
referee.isReferee = false;
|
||||
}
|
||||
|
||||
//自动回收裁决
|
||||
public static boolean autoReferee(GRefereeActionAutoInter inter){
|
||||
GRefereeClient referee = getFreeReferee();
|
||||
if (Objects.nonNull(referee)) {
|
||||
referee = inter.run(referee);
|
||||
//未使用则收回
|
||||
if(Objects.nonNull(referee)){
|
||||
addFreeReferee(referee);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public interface GRefereeActionAutoInter{
|
||||
GRefereeClient run(GRefereeClient referee);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -8,6 +8,7 @@ import cn.jisol.game.controller.game.GPlayerPetController;
|
||||
import cn.jisol.game.controller.game.GPlayerTacticalController;
|
||||
import cn.jisol.game.entity.game.PlayerTactical;
|
||||
import cn.jisol.game.network.client.GClient;
|
||||
import cn.jisol.game.network.client.GRefereeClient;
|
||||
import cn.jisol.game.proto.GPVPMessage;
|
||||
import cn.jisol.game.service.PlayerTacticalService;
|
||||
import cn.jisol.ngame.actions.core.NAction;
|
||||
@@ -80,76 +81,104 @@ public class GPVPAction {
|
||||
|
||||
//判断是否有两人在等待 有 则两人匹配成功
|
||||
while (pool.size() >= 2){
|
||||
//匹配成功
|
||||
GClient client1 = pool.poll();
|
||||
GClient client2 = pool.poll();
|
||||
|
||||
if(Objects.isNull(client1) || Objects.isNull(client2)) continue;
|
||||
//确保玩家都在线
|
||||
if(!(client1.isOpen() && client2.isOpen())){
|
||||
if(client2.isOpen()) pool.addFirst(client2);
|
||||
if(client1.isOpen()) pool.addFirst(client1);
|
||||
continue;
|
||||
}
|
||||
if (!(GRefereeAction.autoReferee(referee -> {
|
||||
|
||||
//获取双方的阵型
|
||||
GPlayerTacticalController tacticalController = SpringBeanUtils.getBean(GPlayerTacticalController.class);
|
||||
GPlayerPetController petController = SpringBeanUtils.getBean(GPlayerPetController.class);
|
||||
//匹配成功
|
||||
GClient client1 = pool.poll();
|
||||
GClient client2 = pool.poll();
|
||||
|
||||
String leftTactical = tacticalController.getInfo(client1.player).data.getTacticalData();
|
||||
String rightTactical = tacticalController.getInfo(client2.player).data.getTacticalData();
|
||||
|
||||
|
||||
//如果有人阵法是默认 则 不开始
|
||||
if (leftTactical.equals(tacticalController.InitTactical) || rightTactical.equals(tacticalController.InitTactical)){
|
||||
if(leftTactical.equals(tacticalController.InitTactical)){
|
||||
client1.invoke(GActionEnum.C_MODE_PVP_END_WAIT);
|
||||
client1.invoke(GActionEnum.C_MODE_PVP_MESSAGE, GPVPMessage.GPVPText.newBuilder().setText("你的阵法没有宠物~").build());
|
||||
}else{
|
||||
//加入回列表
|
||||
pool.addFirst(client1);
|
||||
if(Objects.isNull(client1) || Objects.isNull(client2)) {
|
||||
return referee;
|
||||
}
|
||||
if(rightTactical.equals(tacticalController.InitTactical)){
|
||||
client2.invoke(GActionEnum.C_MODE_PVP_END_WAIT);
|
||||
client2.invoke(GActionEnum.C_MODE_PVP_MESSAGE, GPVPMessage.GPVPText.newBuilder().setText("你的阵法没有宠物~").build());
|
||||
}else{
|
||||
//加入回列表
|
||||
pool.addFirst(client2);
|
||||
//确保玩家都在线
|
||||
if(!(client1.isOpen() && client2.isOpen())){
|
||||
if(client2.isOpen()) pool.addFirst(client2);
|
||||
if(client1.isOpen()) pool.addFirst(client1);
|
||||
return referee;
|
||||
}
|
||||
|
||||
//获取双方的阵型
|
||||
GPlayerTacticalController tacticalController = SpringBeanUtils.getBean(GPlayerTacticalController.class);
|
||||
GPlayerPetController petController = SpringBeanUtils.getBean(GPlayerPetController.class);
|
||||
|
||||
String leftTactical = tacticalController.getInfo(client1.player).data.getTacticalData();
|
||||
String rightTactical = tacticalController.getInfo(client2.player).data.getTacticalData();
|
||||
|
||||
|
||||
//如果有人阵法是默认 则 不开始
|
||||
if (leftTactical.equals(tacticalController.InitTactical) || rightTactical.equals(tacticalController.InitTactical)){
|
||||
if(leftTactical.equals(tacticalController.InitTactical)){
|
||||
client1.invoke(GActionEnum.C_MODE_PVP_END_WAIT);
|
||||
client1.invoke(GActionEnum.C_MODE_PVP_MESSAGE, GPVPMessage.GPVPText.newBuilder().setText("你的阵法没有宠物~").build());
|
||||
}else{
|
||||
//加入回列表
|
||||
pool.addFirst(client1);
|
||||
}
|
||||
if(rightTactical.equals(tacticalController.InitTactical)){
|
||||
client2.invoke(GActionEnum.C_MODE_PVP_END_WAIT);
|
||||
client2.invoke(GActionEnum.C_MODE_PVP_MESSAGE, GPVPMessage.GPVPText.newBuilder().setText("你的阵法没有宠物~").build());
|
||||
}else{
|
||||
//加入回列表
|
||||
pool.addFirst(client2);
|
||||
}
|
||||
return referee;
|
||||
}
|
||||
|
||||
//构建匹配信息
|
||||
GPVPMessage.GPVPStart.Builder builder = GPVPMessage.GPVPStart.newBuilder()
|
||||
.setLeftPlayerId(client1.player.getPlayerId())
|
||||
.setRightPlayerId(client2.player.getPlayerId())
|
||||
.setLeftTactical(leftTactical)
|
||||
.setRightTactical(rightTactical);
|
||||
//构建玩家宠物列表
|
||||
petController.getPetList(client1.player).data.forEach(pet -> {
|
||||
builder.putLeftPets(pet.getPetId(), JSONUtil.toJsonStr(pet));
|
||||
});
|
||||
petController.getPetList(client2.player).data.forEach(pet -> {
|
||||
builder.putLeftPets(pet.getPetId(), JSONUtil.toJsonStr(pet));
|
||||
});
|
||||
|
||||
//取消等待
|
||||
client1.invoke(GActionEnum.C_MODE_PVP_END_WAIT);
|
||||
client2.invoke(GActionEnum.C_MODE_PVP_END_WAIT);
|
||||
|
||||
GPVPMessage.GPVPStart info = builder.build();
|
||||
|
||||
//PVP 开始
|
||||
client1.invoke(GActionEnum.C_MODE_PVP_START, info);
|
||||
client2.invoke(GActionEnum.C_MODE_PVP_START, info);
|
||||
|
||||
//PVP 裁决
|
||||
onPVPReferee(referee,info);
|
||||
|
||||
return null; //返回空则占用 需要 手动释放
|
||||
}))){
|
||||
System.out.println("匹配失败 暂无裁决者空闲");
|
||||
//如果获取不到裁决 则 返回
|
||||
return;
|
||||
}
|
||||
|
||||
//构建匹配信息
|
||||
GPVPMessage.GPVPStart.Builder builder = GPVPMessage.GPVPStart.newBuilder()
|
||||
.setLeftTactical(leftTactical)
|
||||
.setRightTactical(rightTactical);
|
||||
//构建玩家宠物列表
|
||||
petController.getPetList(client1.player).data.forEach(pet -> {
|
||||
builder.putLeftPets(pet.getPetId(), JSONUtil.toJsonStr(pet));
|
||||
});
|
||||
petController.getPetList(client2.player).data.forEach(pet -> {
|
||||
builder.putLeftPets(pet.getPetId(), JSONUtil.toJsonStr(pet));
|
||||
});
|
||||
|
||||
|
||||
|
||||
//取消等待
|
||||
client1.invoke(GActionEnum.C_MODE_PVP_END_WAIT);
|
||||
client2.invoke(GActionEnum.C_MODE_PVP_END_WAIT);
|
||||
|
||||
GPVPMessage.GPVPStart info = builder.build();
|
||||
|
||||
//PVP 开始
|
||||
client1.invoke(GActionEnum.C_MODE_PVP_START, info);
|
||||
client2.invoke(GActionEnum.C_MODE_PVP_START, info);
|
||||
|
||||
//发送裁决
|
||||
GRefereeAction.onPVPReferee(info);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//裁决PVP
|
||||
public static void onPVPReferee(GRefereeClient referee,GPVPMessage.GPVPStart info){
|
||||
//如果有裁决员则向裁决员发送要裁决的信息
|
||||
referee.invoke(GActionEnum.CR_REFEREE_PVP_MODE,info);
|
||||
}
|
||||
|
||||
//裁决结束返回
|
||||
@NActionMethod(GActionEnum.CR_REFEREE_PVP_END)
|
||||
public static void onPVPRefereeEnd(GPVPMessage.GPVPRefereeInfo info,GRefereeClient referee){
|
||||
|
||||
System.out.println("获胜的玩家是:"+info.getWinnerId());
|
||||
//裁决结束 等待下次裁决
|
||||
referee.isReferee = false;
|
||||
|
||||
}
|
||||
|
||||
//通知玩家等待定时器
|
||||
public static void onWaitTimer(){
|
||||
//向所有玩家发送等待
|
||||
|
@@ -74,9 +74,10 @@ public class WebSocket {
|
||||
|
||||
@OnMessage
|
||||
public void onMessage(Session session, InputStream inputStream){
|
||||
GClient client = CLIENTS.get(session.getId());
|
||||
GClient referee = REFEREES.get(session.getId());
|
||||
JNetwork.onMessage(inputStream,client,client,referee,CLIENTS,client.user,client.player);
|
||||
GClient client;
|
||||
if(Objects.isNull(client = CLIENTS.get(session.getId())))
|
||||
client = REFEREES.get(session.getId());
|
||||
JNetwork.onMessage(inputStream,client,client,CLIENTS,client.user,client.player);
|
||||
}
|
||||
|
||||
@OnClose
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -9,6 +9,8 @@ message GPVPStart {
|
||||
map<int64, string> leftPets = 2; //左边的宠物 key:宠物Id value 宠物配置Id
|
||||
string rightTactical = 3; //右边的布阵
|
||||
map<int64, string> rightPets = 4; //右边的宠物 key:宠物Id value 宠物配置Id
|
||||
int64 leftPlayerId = 5; //左边的玩家Id
|
||||
int64 rightPlayerId = 6; //右边的玩家Id
|
||||
}
|
||||
|
||||
//PVP 消息提示
|
||||
@@ -16,4 +18,10 @@ message GPVPText {
|
||||
string text = 1; //消息提示
|
||||
}
|
||||
|
||||
//PVP 裁决结束返回
|
||||
message GPVPRefereeInfo{
|
||||
//获胜玩家Id
|
||||
int64 winnerId = 1;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user