提交完美的无头模式

This commit is contained in:
PC-20230316NUNE\Administrator 2023-11-22 17:46:08 +08:00
parent 7389f6d716
commit f890be0728
17 changed files with 975 additions and 175 deletions

View File

@ -28,10 +28,10 @@ import AppAction from "./AppAction";
// let APIPath = `http://localhost:8080` // let APIPath = `http://localhost:8080`
// let WsPath = `ws://localhost:8080/websocket` // let WsPath = `ws://localhost:8080/websocket`
let APIPath = `http://192.168.1.23:8080` // let APIPath = `http://192.168.1.23:8080`
let WsPath = `ws://192.168.1.23:8080/websocket` // let WsPath = `ws://192.168.1.23:8080/websocket`
// let APIPath = `http://192.168.0.174:8080` let APIPath = `http://192.168.0.123:8080`
// let WsPath = `ws://192.168.0.174:8080/websocket` let WsPath = `ws://192.168.0.123:8080/websocket`
// let APIPath = `https://api.pet.jisol.cn` // let APIPath = `https://api.pet.jisol.cn`
// let WsPath = `wss://api.pet.jisol.cn/websocket` // let WsPath = `wss://api.pet.jisol.cn/websocket`
@ -231,8 +231,8 @@ export const app = {
} }
app.api.addRequestInterceptors((config:JAPIConfig) => { app.api.addRequestInterceptors((config:JAPIConfig) => {
// //设置Token //设置Token
// if(StorageData.get(StorageEnum.Token)) if(StorageData.get(StorageEnum.Token))
// config.headers["Token"] = StorageData.get(StorageEnum.Token); config.headers["Token"] = StorageData.get(StorageEnum.Token);
return true; return true;
}) })

View File

@ -1,3 +1,5 @@
//打包环境 //打包环境
export enum Env{ export enum Env{
Server, //服务器模式 Server, //服务器模式
@ -5,4 +7,5 @@ export enum Env{
} }
//当前环境 //当前环境
export const EnvCurrent:Env = Env.Server; export const EnvCurrent:Env = Env.H5;

View File

@ -1,7 +1,6 @@
import { JNSyncFrameEvent } from "../../../extensions/ngame/assets/ngame/sync/frame/JNSyncFrame";
import { app } from "../App"; import { app } from "../App";
import { Env, EnvCurrent } from "../Env"; import { Env, EnvCurrent } from "../Env";
import GBattleModeManager, { BattleMode } from "../battle/GBattleModeManager"; import GBattleModeManager, { BattleMode, GBattleModeEvent } from "../battle/GBattleModeManager";
import { GAction } from "../consts/GAction"; import { GAction } from "../consts/GAction";
import { GActionType } from "../consts/GActionType"; import { GActionType } from "../consts/GActionType";
import { GUI } from "../ui/UIConfig"; import { GUI } from "../ui/UIConfig";
@ -12,6 +11,8 @@ export interface GPVPStart{
leftPets:{[key:number]:string}; //左边玩家的宠物 leftPets:{[key:number]:string}; //左边玩家的宠物
rightTactical:string; //右边玩家的阵法 rightTactical:string; //右边玩家的阵法
rightPets:{[key:number]:string}; //左边玩家的宠物 rightPets:{[key:number]:string}; //左边玩家的宠物
leftPlayerId:number; //左边的玩家Id
rightPlayerId:number; //右边的玩家Id
} }
@ -22,6 +23,7 @@ export interface GPVPText{
export default class PVPAction extends BaseAction { export default class PVPAction extends BaseAction {
onInit(){ onInit(){
app.socket.on(GAction.C_MODE_PVP_START_WAIT,this.onModePVPStartWait,this); app.socket.on(GAction.C_MODE_PVP_START_WAIT,this.onModePVPStartWait,this);
app.socket.on(GAction.C_MODE_PVP_END_WAIT,this.onModePVPEndWait,this); app.socket.on(GAction.C_MODE_PVP_END_WAIT,this.onModePVPEndWait,this);
app.socket.on(GAction.C_MODE_PVP_START,this.onModePVPStart,this,GActionType.GPVPStart); app.socket.on(GAction.C_MODE_PVP_START,this.onModePVPStart,this,GActionType.GPVPStart);
@ -29,7 +31,9 @@ export default class PVPAction extends BaseAction {
if(EnvCurrent == Env.Server){ if(EnvCurrent == Env.Server){
app.socket.on(GAction.CR_REFEREE_PVP_MODE,this.onCrReferee,this,GActionType.GPVPStart); //监听PVP裁决 app.socket.on(GAction.CR_REFEREE_PVP_MODE,this.onCrReferee,this,GActionType.GPVPStart); //监听PVP裁决
app.event.on(GBattleModeEvent.Close,this.onBattleRefereeClose,this); //模式结束运行
} }
} }
//PVP开始等待 //PVP开始等待
@ -63,6 +67,16 @@ export default class PVPAction extends BaseAction {
app.layer.Open(GUI.Tips,{text:info.text}) app.layer.Open(GUI.Tips,{text:info.text})
} }
//裁决结束返回
onBattleRefereeClose(type:BattleMode,data?:number){
if(type == BattleMode.PVP){
console.log("裁决结束胜利玩家",data)
app.socket.Send(GAction.CR_REFEREE_PVP_END,{
winnerId:data
},GActionType.GPVPRefereeInfo)
}
}
} }

View File

@ -37,8 +37,8 @@ export default class GBaseMode<T,DT> extends GObject<T> {
} }
//结束场景 //结束场景
Close(){ Close(data?:any){
GBattleModeManager.getIns().Close(); GBattleModeManager.getIns().Close(data);
} }
onSyncUpdate(dt: number,frame:JNFrameInfo, input?: T) { onSyncUpdate(dt: number,frame:JNFrameInfo, input?: T) {

View File

@ -22,6 +22,12 @@ export interface GBattleModeInfo{
root:Node, //世界场景Root root:Node, //世界场景Root
} }
//事件
export enum GBattleModeEvent{
//关闭模式
Close = "GBattleModeEvent_Close",
}
//全局战斗模式管理器 //全局战斗模式管理器
export default class GBattleModeManager extends Singleton { export default class GBattleModeManager extends Singleton {
@ -93,13 +99,16 @@ export default class GBattleModeManager extends Singleton {
} }
//关闭当前模式 //关闭当前模式
async Close(){ async Close(data?:any){
//主动调用场景销毁 //主动调用场景销毁
app.sync.onReset(); app.sync.onReset();
let current = this.current;
this.current = null; this.current = null;
this.frameNoSwitch = true; this.frameNoSwitch = true;
//结束通知
app.event.emit(GBattleModeEvent.Close,current,data);
} }
//设置自动推帧 ( 帧不由addFrame控制 管理器自动推帧) //设置自动推帧 ( 帧不由addFrame控制 管理器自动推帧)
@ -109,8 +118,6 @@ export default class GBattleModeManager extends Singleton {
this.autoTime = 0; this.autoTime = 0;
} }
//设置自动追帧
//清除当前模式 //清除当前模式
private clear(){ private clear(){
if(!this.isInit) return; if(!this.isInit) return;

View File

@ -214,10 +214,14 @@ export default class GPVPMode extends GBaseMode<{},GPVPStart>{
//判断是否有队伍都死亡 //判断是否有队伍都死亡
if(this.getOnesRoleAlive(GPVPModePlayerEnum.PLAYER).length == 0 || this.getOnesRoleAlive(GPVPModePlayerEnum.ENEMY).length == 0){ if(this.getOnesRoleAlive(GPVPModePlayerEnum.PLAYER).length == 0 || this.getOnesRoleAlive(GPVPModePlayerEnum.ENEMY).length == 0){
this.isEndGame = true; this.isEndGame = true;
let winner = this.data.leftPlayerId;
if(this.getOnesRoleAlive(GPVPModePlayerEnum.ENEMY).length){
winner = this.data.rightPlayerId;
}
//结束游戏 //结束游戏
JNFrameTime.getInstance().setTimeout(() => { JNFrameTime.getInstance().setTimeout(() => {
console.log(this.getOnesRoleAlive(GPVPModePlayerEnum.PLAYER).length,this.getOnesRoleAlive(GPVPModePlayerEnum.ENEMY).length) console.log(this.getOnesRoleAlive(GPVPModePlayerEnum.PLAYER).length,this.getOnesRoleAlive(GPVPModePlayerEnum.ENEMY).length)
this.Close(); this.Close(winner);
},3000) },3000)
} }

View File

@ -19,6 +19,7 @@ export enum GAction {
/*************** 裁决 *********************/ /*************** 裁决 *********************/
CR_REFEREE_PVP_MODE = 4001, //裁决PVP模式 CR_REFEREE_PVP_MODE = 4001, //裁决PVP模式
CR_REFEREE_PVP_END = 4002, //裁决PVP结束
} }

View File

@ -15,4 +15,7 @@ export enum GActionType {
GPVPStart = "GPVPStart", //PVP 开始 GPVPStart = "GPVPStart", //PVP 开始
GPVPText = "GPVPText", //PVP 提示 GPVPText = "GPVPText", //PVP 提示
/*************** 裁决 ********************/
GPVPRefereeInfo = "GPVPRefereeInfo" //PVP 裁决信息
} }

@ -1 +1 @@
Subproject commit 95f8a3c6c6310f66d8671958e940291020636a81 Subproject commit e527d34fb68064a47d20d21764044c7cda2ed719

View File

@ -9,6 +9,8 @@ message GPVPStart {
map<int64, string> leftPets = 2; // key:Id value Id map<int64, string> leftPets = 2; // key:Id value Id
string rightTactical = 3; // string rightTactical = 3; //
map<int64, string> rightPets = 4; // key:Id value Id map<int64, string> rightPets = 4; // key:Id value Id
int64 leftPlayerId = 5; //Id
int64 rightPlayerId = 6; //Id
} }
//PVP //PVP
@ -16,4 +18,10 @@ message GPVPText {
string text = 1; // string text = 1; //
} }
//PVP
message GPVPRefereeInfo{
//Id
int64 winnerId = 1;
}

View File

@ -21,5 +21,6 @@ public interface GActionEnum {
/*************** 裁决 *********************/ /*************** 裁决 *********************/
int CR_REFEREE_PVP_MODE = 4001; //裁决PVP模式 int CR_REFEREE_PVP_MODE = 4001; //裁决PVP模式
int CR_REFEREE_PVP_END = 4002; //裁决PVP结束
} }

View File

@ -1,15 +1,12 @@
package cn.jisol.game.actions; package cn.jisol.game.actions;
import cn.jisol.game.network.client.GClient;
import cn.jisol.game.network.client.GRefereeClient; import cn.jisol.game.network.client.GRefereeClient;
import cn.jisol.game.proto.GPVPMessage; import cn.jisol.game.proto.GPVPMessage;
import cn.jisol.ngame.actions.core.NAction; import cn.jisol.ngame.actions.core.NAction;
import cn.jisol.ngame.actions.core.NActionMethod; import cn.jisol.ngame.actions.core.NActionMethod;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
/** /**
* 裁决员行为 处理裁决 * 裁决员行为 处理裁决
@ -20,27 +17,12 @@ public class GRefereeAction {
//裁决员列表 //裁决员列表
public static Map<String, GRefereeClient> REFEREES; 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(){ public static GRefereeClient getFreeReferee(){
for (String key : REFEREES.keySet()) { for (String key : REFEREES.keySet()) {
if (!(REFEREES.get(key).isReferee)) { if (!(REFEREES.get(key).isReferee)) {
REFEREES.get(key).isReferee = true;
return REFEREES.get(key); 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);
}
} }

View File

@ -8,6 +8,7 @@ import cn.jisol.game.controller.game.GPlayerPetController;
import cn.jisol.game.controller.game.GPlayerTacticalController; import cn.jisol.game.controller.game.GPlayerTacticalController;
import cn.jisol.game.entity.game.PlayerTactical; import cn.jisol.game.entity.game.PlayerTactical;
import cn.jisol.game.network.client.GClient; import cn.jisol.game.network.client.GClient;
import cn.jisol.game.network.client.GRefereeClient;
import cn.jisol.game.proto.GPVPMessage; import cn.jisol.game.proto.GPVPMessage;
import cn.jisol.game.service.PlayerTacticalService; import cn.jisol.game.service.PlayerTacticalService;
import cn.jisol.ngame.actions.core.NAction; import cn.jisol.ngame.actions.core.NAction;
@ -80,76 +81,104 @@ public class GPVPAction {
//判断是否有两人在等待 则两人匹配成功 //判断是否有两人在等待 则两人匹配成功
while (pool.size() >= 2){ while (pool.size() >= 2){
//匹配成功
GClient client1 = pool.poll();
GClient client2 = pool.poll();
if(Objects.isNull(client1) || Objects.isNull(client2)) continue; if (!(GRefereeAction.autoReferee(referee -> {
//确保玩家都在线
if(!(client1.isOpen() && client2.isOpen())){
if(client2.isOpen()) pool.addFirst(client2);
if(client1.isOpen()) pool.addFirst(client1);
continue;
}
//获取双方的阵型 //匹配成功
GPlayerTacticalController tacticalController = SpringBeanUtils.getBean(GPlayerTacticalController.class); GClient client1 = pool.poll();
GPlayerPetController petController = SpringBeanUtils.getBean(GPlayerPetController.class); GClient client2 = pool.poll();
String leftTactical = tacticalController.getInfo(client1.player).data.getTacticalData(); if(Objects.isNull(client1) || Objects.isNull(client2)) {
String rightTactical = tacticalController.getInfo(client2.player).data.getTacticalData(); return referee;
//如果有人阵法是默认 不开始
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); if(!(client1.isOpen() && client2.isOpen())){
client2.invoke(GActionEnum.C_MODE_PVP_MESSAGE, GPVPMessage.GPVPText.newBuilder().setText("你的阵法没有宠物~").build()); if(client2.isOpen()) pool.addFirst(client2);
}else{ if(client1.isOpen()) pool.addFirst(client1);
//加入回列表 return referee;
pool.addFirst(client2);
} }
//获取双方的阵型
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; 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(){ public static void onWaitTimer(){
//向所有玩家发送等待 //向所有玩家发送等待

View File

@ -74,9 +74,10 @@ public class WebSocket {
@OnMessage @OnMessage
public void onMessage(Session session, InputStream inputStream){ public void onMessage(Session session, InputStream inputStream){
GClient client = CLIENTS.get(session.getId()); GClient client;
GClient referee = REFEREES.get(session.getId()); if(Objects.isNull(client = CLIENTS.get(session.getId())))
JNetwork.onMessage(inputStream,client,client,referee,CLIENTS,client.user,client.player); client = REFEREES.get(session.getId());
JNetwork.onMessage(inputStream,client,client,CLIENTS,client.user,client.player);
} }
@OnClose @OnClose

View File

@ -9,6 +9,8 @@ message GPVPStart {
map<int64, string> leftPets = 2; // key:Id value Id map<int64, string> leftPets = 2; // key:Id value Id
string rightTactical = 3; // string rightTactical = 3; //
map<int64, string> rightPets = 4; // key:Id value Id map<int64, string> rightPets = 4; // key:Id value Id
int64 leftPlayerId = 5; //Id
int64 rightPlayerId = 6; //Id
} }
//PVP //PVP
@ -16,4 +18,10 @@ message GPVPText {
string text = 1; // string text = 1; //
} }
//PVP
message GPVPRefereeInfo{
//Id
int64 winnerId = 1;
}

View File

@ -1,12 +1,12 @@
// const URL = "http://192.168.0.174:7457/web-desktop/web-desktop/index.html" // const URL = "http://192.168.0.174:7457/web-desktop/web-desktop/index.html"
// const express = require("express"); // const express = require("express");
// const app = express(); // const app = express();
const URL = "http://192.168.1.23:7456/web-desktop/web-desktop-001/index.html" const URL = "http://192.168.0.123:7457/web-desktop/web-desktop/index.html"
// const runCocos = () => { // const runCocos = () => {
const { JSDOM,ResourceLoader } = require('jsdom') const { JSDOM,ResourceLoader } = require('jsdom')
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args)); const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const axios = require('axios').default;
const WebGL = require('gl'); const WebGL = require('gl');
const { request } = require('express'); const { request } = require('express');
const WebGLRenderingContext = WebGL.WebGLRenderingContext; const WebGLRenderingContext = WebGL.WebGLRenderingContext;
@ -44,6 +44,7 @@ fetch(URL).then(res => res.text()).then(html => {
window.fetch = fetch; window.fetch = fetch;
window.GUser = "100000"; window.GUser = "100000";
window.GPassworld = "123456"; window.GPassworld = "123456";
window['axios'] = axios;
}) })
setTimeout(() => {}, 99999999); setTimeout(() => {}, 99999999);