Commit bbc33eb8 authored by 范雪寒's avatar 范雪寒

feat: 替换华为云联机服务

parent a9f795ff
{"ver":"1.1.2","uuid":"c35bb2f6-f24a-4850-ae44-643f2fdc7541","isBundle":false,"bundleName":"","priority":1,"compressionType":{},"optimizeHotUpdate":{},"inlineSpriteFrames":{},"isRemoteBundle":{"ios":false,"android":false},"subMetas":{}}
\ No newline at end of file
{
"ver": "1.1.2",
"uuid": "c35bb2f6-f24a-4850-ae44-643f2fdc7541",
"isBundle": false,
"bundleName": "",
"priority": 1,
"compressionType": {},
"optimizeHotUpdate": {},
"inlineSpriteFrames": {},
"isRemoteBundle": {
"ios": false,
"android": false
},
"subMetas": {}
}
\ No newline at end of file
......@@ -30,7 +30,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
this._audioResList.push({ url: this.data.waitingTurn });
}
addPreloadAnima() {}
addPreloadAnima() { }
onLoadEnd() {
// TODO 加载完成后的逻辑写在这里, 下面的代码仅供参考
......@@ -54,17 +54,11 @@ export default class SceneComponent extends MyCocosSceneComponent {
async initData() {
this.networkHelper = new NetworkHelper();
this.initNetworkListener();
do {
this.playerId = await this.networkHelper.init("op_16", 2);
if (this.playerId === null) {
console.log("onDestroy");
this.networkHelper.onDestroy();
}
console.log("this.playerId = " + this.playerId);
} while (this.playerId === null);
this.playerId = await this.networkHelper.init("op_16", 2);
const room = this.networkHelper.room;
if (this.playerId == room.roomInfo.owner) {
if (this.playerId == room.ownerId) {
console.log("房主");
this.isRoomOwner = true;
this.server = new GameServer(2, this.networkHelper);
......@@ -642,9 +636,16 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.networkHelper.leaveRoom();
this.log("playerOffLine" + JSON.stringify(event));
});
this.networkHelper.on("gameStart", (event) => {});
this.networkHelper.on("frameEvent", (event) => {
event.data.frame.items.forEach(async (item) => {
this.networkHelper.on("gameStart", (event) => { });
this.networkHelper.on("frameEvent", (frameData) => {
const dataList = [];
if (frameData instanceof Array) {
dataList.push(...frameData);
} else {
dataList.push(frameData);
}
dataList.forEach(async (itemStr) => {
const item = JSON.parse(itemStr);
if (this.server) {
this.server.onFrameEvent(item.data);
}
......
import { Base64 } from "js-base64";
export class NetworkHelper {
_eventListeners: any = {};
ctor() { }
......@@ -10,10 +8,11 @@ export class NetworkHelper {
async init(roomType: string, maxPlayers: number) {
try {
this.initRoom();
await this.initListener();
await this.initRoom();
// await this.initListener();
return await this.joinRoom(roomType, maxPlayers);
} catch (e) {
console.log(e);
// return null;
}
}
......@@ -29,10 +28,6 @@ export class NetworkHelper {
await this.leaveRoom();
}
getRoomInfo() {
return this.room.roomInfo;
}
listenerInited = false;
initListener() {
......@@ -111,162 +106,94 @@ export class NetworkHelper {
});
}
room: MGOBE.Room;
initRoom() {
this.room = new MGOBE.Room();
MGOBE.Listener.add(this.room);
this.room.onJoinRoom = this.onJoinRoom.bind(this);
this.room.onLeaveRoom = this.onLeaveRoom.bind(this);
this.room.onRecvFromClient = this.onRecvFromClient.bind(this);
this.room.onChangePlayerNetworkState =
this.onChangePlayerNetworkState.bind(this);
this.room.onRecvFrame = this.onRecvFrame.bind(this);
this.room.onStartFrameSync = this.onStartFrameSync.bind(this);
this.room.onStopFrameSync = this.onStopFrameSync.bind(this);
this.room.onRecvFromGameSvr = this.onRecvFromGameSvr.bind(this);
console.log("this.room = " + JSON.stringify(this.room.roomInfo));
room: GOBE.Room;
client: GOBE.Client;
async initRoom() {
this.client = new window.GOBE.Client({
clientId: '857970243220294656',// 客户端ID
clientSecret: '489BFFB5D22BC39FDA0B1878F01F38C8049EBF68C856E25E63E7403A723A140D',// 客户端密钥
appId: '243650030995634151',// 应用的ID
openId: 'op_16' + Math.random(),// 玩家ID
});
console.log(this.client);
console.log('喵喵喵1');
let playerId;
this.client.init().then(() => {
// 初始化成功
playerId = this.client.playerId;
}).catch((e) => {
// 初始化失败
console.log('初始化失败')
console.log(e)
});
console.log('喵喵喵2');
}
player: any;
joinRoom(roomType: string, maxPlayers: number, customData: string = "") {
return new Promise((resolve, reject) => {
const playerName = "Tom" + Math.random();
const playerInfo = {
name: playerName,
customPlayerStatus: 0,
customProfile: customData,
};
const matchRoomPara = {
playerInfo,
maxPlayers: maxPlayers,
roomType: roomType,
};
this.room.matchRoom(matchRoomPara, async (event) => {
// console.log(JSON.stringify(event))
if (event.code !== 0) {
this.log("匹配失败");
reject(event);
return;
}
this.log("匹配成功: " + event.data.roomInfo.id);
const player = event.data.roomInfo.playerList.find(
(player) => player.name == playerName
);
this.player = player;
if (this.checkRoomOwnerOffLine()) {
if (this._eventListeners["playerOffLine"]) {
this._eventListeners["playerOffLine"](event);
}
this.log("房主掉线了");
resolve(null);
return;
}
resolve(player.id);
});
});
async joinRoom(roomType: string, maxPlayers: number) {
const matchRoomConfig = {
matchParams: {
matchRule: '',
matchRule2: '',
},
roomType: roomType,
customRoomProperties: '',
maxPlayers: maxPlayers,
};
const playerConfig = {
customPlayerStatus: 0,
customPlayerProperties: ''
};
console.log('汪汪汪1')
this.room = await this.client.matchRoom(matchRoomConfig, playerConfig);
this.player = this.room.player;
this.room.onJoin(this.onJoinRoom.bind(this));
this.room.onLeave(this.onLeaveRoom.bind(this));
this.room.onStartFrameSync(this.onStartFrameSync.bind(this));
this.room.onStopFrameSync(this.onStopFrameSync.bind(this));
this.room.onRecvFrame(this.onRecvFrame.bind(this));
this.room.onDisconnect(this.onDisconnect.bind(this));
this.room.onDismiss(this.onDisconnect.bind(this));
console.log('汪汪汪2')
}
leaveRoom() {
return new Promise((resolve, reject) => {
this.room.leaveRoom({}, (event) => {
console.log(JSON.stringify(event));
if (event.code === 0) {
console.log("退房成功" + this.room.roomInfo.id);
// this.room.initRoom();
resolve(null);
} else {
console.log("退房失败");
}
});
});
async leaveRoom() {
await this.room.leave();
console.log("退房成功");
}
dismissRoom() {
const roomInfo = this.room.roomInfo;
const player = roomInfo.playerList.find(
(player) => player.name == this.player.name
);
if (roomInfo.owner == player.id) {
return new Promise((resolve, reject) => {
this.room.dismissRoom({}, (res) => {
console.log("房间已解散");
});
});
async dismissRoom() {
if (this.room.ownerId == this.player.id) {
await this.client.dismissRoom();
console.log("房间已解散");
}
}
closeRoom() {
return new Promise((resolve, reject) => {
this.room.changeRoom({ isForbidJoin: true }, (event) => {
console.log("关门");
// console.log(JSON.stringify(event))
resolve(null);
});
});
}
sendMessage() {
const sendToClientPara = {
recvType: MGOBE.ENUM.RecvType.ROOM_ALL,
recvPlayerList: [],
msg: "hello" + Math.random(),
};
this.room.sendToClient(sendToClientPara, (event) => console.log(event));
}
startFrameSync() {
return new Promise((resolve, reject) => {
this.room.startFrameSync({}, (event) => {
console.log(JSON.stringify(event));
if (event.code === 0) {
cc.log("开始帧同步成功,请到控制台查看具体帧同步信息");
resolve(null);
} else {
reject();
}
});
});
return;
// return new Promise((resolve, reject) => {
// this.room.changeRoom({ isForbidJoin: true }, (event) => {
// console.log("关门");
// // console.log(JSON.stringify(event))
// resolve(null);
// });
// });
}
stopFrameSync() {
return new Promise((resolve, reject) => {
if (!this.room) {
resolve(null);
return;
}
this.room.stopFrameSync({}, (event) => {
console.log(JSON.stringify(event));
if (event.code === 0) {
console.log("停止帧同步成功");
resolve(null);
} else {
console.log("停止帧同步失败");
reject(event.code);
}
});
});
async startFrameSync() {
await this.room.startFrameSync();
cc.log("开始帧同步成功,请到控制台查看具体帧同步信息");
}
sendToServer() {
const sendToGameServerPara = {
data: {
cmd: 1,
},
};
this.room.sendToGameSvr(sendToGameServerPara, (event) =>
console.log(event)
);
async stopFrameSync() {
await this.room.stopFrameSync();
console.log("停止帧同步成功");
}
sendFrame(data: any) {
console.log('sendFrame: ' + JSON.stringify(data));
this.room.sendFrame({ data }, (err) => {
if (err.code != 0) {
console.log("err", err);
}
});
this.room.sendFrame(JSON.stringify(data));
}
onJoinRoom(event) {
......@@ -284,22 +211,10 @@ export class NetworkHelper {
}
onRecvFromClient() { }
checkRoomOwnerOffLine() {
return this.room.roomInfo.playerList.some(player => {
return (
player.commonNetworkState == MGOBE.types.NetworkState.COMMON_OFFLINE ||
player.relayNetworkState == MGOBE.types.NetworkState.RELAY_OFFLINE
);
})
}
onChangePlayerNetworkState(event) {
this.log("玩家网络变化: " + JSON.stringify(event));
if (this.checkRoomOwnerOffLine()) {
if (this._eventListeners["playerOffLine"]) {
this._eventListeners["playerOffLine"](event);
}
this.log("房主掉线了");
onDisconnect(event) {
this.log("玩家掉线了: " + JSON.stringify(event));
if (this._eventListeners["playerOffLine"]) {
this._eventListeners["playerOffLine"](event);
}
}
......
{
"ver": "1.1.2",
"uuid": "4e259305-46b1-43d9-8d93-25fea822e0d9",
"isBundle": false,
"bundleName": "",
"priority": 1,
"compressionType": {},
"optimizeHotUpdate": {},
"inlineSpriteFrames": {},
"isRemoteBundle": {},
"subMetas": {}
}
\ No newline at end of file
This diff is collapsed.
{
"ver": "2.0.0",
"uuid": "de16aac1-0c28-4863-91c6-20105020daae",
"subMetas": {}
}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"ver": "1.0.8",
"uuid": "291a4bb1-3d0d-41ce-9506-c5808b1d2081",
"isPlugin": true,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
{
"type": "software",
"scope": "game",
"name": "GameOBESDK",
"version": "1.1.4.300",
"playbook": ""
}
\ No newline at end of file
{
"ver": "1.0.0",
"uuid": "f7dd520f-d3cf-4b95-b0c3-9f3a7ee3d7c6",
"subMetas": {}
}
\ No newline at end of file
{
"ver": "1.0.8",
"uuid": "497a857a-a098-429a-97b0-c1a6ae0b2a16",
"isPlugin": true,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
......@@ -345,12 +345,12 @@ export function showTrebleFirework(baseNode, rabbonList) {
showFireworks(right);
}
export function onHomeworkFinish() {
export function onHomeworkFinish(data: any = null) {
const middleLayer = cc.find('middleLayer');
if (middleLayer) {
const middleLayerComponent = middleLayer.getComponent('middleLayer');
if (middleLayerComponent.role == 'student') {
middleLayerComponent.onHomeworkFinish(() => { });
middleLayerComponent.onHomeworkFinish(() => { }, data);
}
} else {
console.log('onHomeworkFinish');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment