Commit 82c66511 authored by limingzhe's avatar limingzhe

feat: 人机版

parent 507f6744
No preview for this file type
...@@ -470,7 +470,9 @@ cc.Class({ ...@@ -470,7 +470,9 @@ cc.Class({
console.log('~~~~~~~ 4'); console.log('~~~~~~~ 4');
this.networkHelper.startFrameSync(); this.networkHelper.startFrameSync(() => {
});
this.checkGameStart(); this.checkGameStart();
...@@ -485,10 +487,12 @@ cc.Class({ ...@@ -485,10 +487,12 @@ cc.Class({
return; return;
} }
this._timeoutIds.push(setTimeout(() => { this._timeoutIds.push(setTimeout(() => {
this.checkIsTeacher(); this.checkIsTeacher();
if (this.isTeacher) { console.log('this.isTeacher: ' , this.isTeacher);
this.networkHelper.closeRoom(); if (this.isTeacher) {
// this.networkHelper.closeRoom();
this.addAiUser(); this.addAiUser();
} else { } else {
...@@ -518,6 +522,10 @@ cc.Class({ ...@@ -518,6 +522,10 @@ cc.Class({
}, },
checkIsTeacher(room) { checkIsTeacher(room) {
this.isTeacher = this.networkHelper.checkIsOwner();
return;
const playerList = this.networkHelper.room.roomInfo.playerList; const playerList = this.networkHelper.room.roomInfo.playerList;
console.log(' playerList: ', JSON.stringify(playerList)); console.log(' playerList: ', JSON.stringify(playerList));
...@@ -559,8 +567,9 @@ cc.Class({ ...@@ -559,8 +567,9 @@ cc.Class({
}, },
initServerAllUser(room) { initServerAllUser(room) {
const roomInfo = room.roomInfo // const roomInfo = room.roomInfo
this.serverAllUser = roomInfo.playerList; // this.serverAllUser = roomInfo.playerList;
this.serverAllUser = this.networkHelper.getOnlinePlayers();
}, },
addNetworkListener(nh) { addNetworkListener(nh) {
...@@ -583,13 +592,17 @@ cc.Class({ ...@@ -583,13 +592,17 @@ cc.Class({
}); });
nh.on('frameEvent', (event) => { nh.on('frameEvent', (event) => {
if (event.data.frame.items.length > 0) { const frameInfo = event.frameInfo;
console.log('frameEvent: ', event) if (!frameInfo || frameInfo.length == 0) {
return;
} }
console.log('frameEvent~~~~: ', event)
frameInfo.forEach((frameData) => {
event.data.frame.items.forEach(item => { frameData.data = JSON.parse(frameData.data);
const res = frameData.data;
const res = item.data;
switch (res.type) { switch (res.type) {
case 'game_end': case 'game_end':
console.log(' in game_end ~') console.log(' in game_end ~')
...@@ -645,6 +658,9 @@ cc.Class({ ...@@ -645,6 +658,9 @@ cc.Class({
}, },
onAddAi(data) { onAddAi(data) {
console.log(' in onAddAi ');
this.addUser({ id: data.id, name: data.name, isAi: true }) this.addUser({ id: data.id, name: data.name, isAi: true })
}, },
...@@ -778,6 +794,9 @@ cc.Class({ ...@@ -778,6 +794,9 @@ cc.Class({
getUserName(uuid) { getUserName(uuid) {
const arr = this.serverAllUser; const arr = this.serverAllUser;
for (let i = 0; i < arr.length; i++) { for (let i = 0; i < arr.length; i++) {
console.log('this.serverAllUser[i].id: ', this.serverAllUser[i]);
console.log('uuid: ', uuid);
if (this.serverAllUser[i].id == uuid) { if (this.serverAllUser[i].id == uuid) {
return this.serverAllUser[i].name; return this.serverAllUser[i].name;
} }
...@@ -1264,7 +1283,7 @@ cc.Class({ ...@@ -1264,7 +1283,7 @@ cc.Class({
const playerData = [{ const playerData = [{
isSelf: true, isSelf: false,
name: 'Chris', name: 'Chris',
headUrl: 'http://staging-teach.cdn.ireadabc.com/0751c28419a0e8ffb1f0e84435b081ce.png' headUrl: 'http://staging-teach.cdn.ireadabc.com/0751c28419a0e8ffb1f0e84435b081ce.png'
}, { }, {
...@@ -1317,11 +1336,15 @@ cc.Class({ ...@@ -1317,11 +1336,15 @@ cc.Class({
const user = this.serverAllUser[i]; const user = this.serverAllUser[i];
const isSelf = user.id == this.playerId; const isSelf = user.id == this.playerId;
// if (isSelf) { let headUrl = playerData[i].headUrl;
// user.name = nameList[RandomInt(0, 4)]; if (isSelf) {
// } else { user.name = user.nick_name
headUrl = user.playerInfo.avatar;
console.log('avatar: ', user.playerInfo.avatar)
} else {
// user.name = nameList[RandomInt(0, 6)];
user.name = playerData[i].name user.name = playerData[i].name
// } }
script.addPlayer(user.name, isSelf, playerData[i].headUrl); script.addPlayer(user.name, isSelf, playerData[i].headUrl);
if (this.isTeacher) { if (this.isTeacher) {
......
This diff is collapsed.
{ {
"ver": "1.0.8", "ver": "1.0.8",
"uuid": "3b5661aa-abb8-448c-b4e5-22ce7bbb78b7", "uuid": "34bd0c05-ee9d-4934-a300-5acdbc4d18cf",
"isPlugin": false, "isPlugin": false,
"loadPluginInWeb": true, "loadPluginInWeb": true,
"loadPluginInNative": true, "loadPluginInNative": true,
......
import { RandomInt } from "../script/util";
export class NetworkHelper {
_eventListeners: any = {};
ctor() { }
on(eventName, func) {
this._eventListeners[eventName] = func;
}
async init(roomType: string, maxPlayers: number) {
this.initRoom();
try {
return await this.joinRoom(roomType, maxPlayers);
} catch (e) {
await this.initListener();
this.initRoom();
return await this.joinRoom(roomType, maxPlayers);
}
}
async startGame() {
await this.startFrameSync();
await this.closeRoom();
}
async stopGame() {
await this.stopFrameSync();
await this.leaveRoom();
}
getRoomInfo() {
return this.room.roomInfo;
}
initListener() {
return new Promise((resolve, reject) => {
const gameId = "obg-i4ql53h1"; //替换为控制台上的 游戏 ID
const secretKey = "060d81c7abaf24c8ce2afc5a725c152062676d35"; //替换为控制台上的 游戏 Key
const serverUrl = "i4ql53h1.wxlagame.com"; //替换为控制台上的 域名
const gameInfo = {
gameId: gameId,
openId: "openid_test" + Math.random(), //自定义的用户唯一ID
secretKey: secretKey
};
const config = {
url: serverUrl,
reconnectMaxTimes: 5,
reconnectInterval: 1000,
resendInterval: 1000,
resendTimeout: 10000,
cacertNativeUrl: ""
};
MGOBE.DebuggerLog.enable = false;
// 如果是原生平台,则加载 Cert 证书,否则会提示 WSS 错误
if (cc.sys.isNative) {
let cacertFile;
const cacertNode = cc.find('cacertNode');
if (cacertNode) {
cacertFile = cacertNode.getComponent('cacert').cacertFile;
}
if (!cacertFile) {
this.log('没有cacertFile!!!');
}
config.cacertNativeUrl = cc.loader.md5Pipe && cc.ENGINE_VERSION < "2.4.0" ?
cc.loader.md5Pipe.transformURL(cacertFile.nativeUrl) :
cacertFile.nativeUrl;
}
MGOBE.Listener.init(gameInfo, config, event => {
this.log(JSON.stringify(event));
if (event.code !== 0) {
this.log("初始化失败: " + event.code);
reject();
return;
}
this.log("初始化成功");
resolve(null);
});
});
}
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.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);
this.log('this.room = ' + JSON.stringify(this.room.roomInfo));
}
joinRoom(roomType: string, maxPlayers: number, customData: string = '') {
return new Promise((resolve, reject) => {
const playerName = "Player-" + Math.round( Math.random() * 1000);
const playerInfo = {
name: playerName,
customPlayerStatus: 0,
customProfile: customData,
};
const matchRoomPara = {
playerInfo,
maxPlayers: maxPlayers,
roomType: roomType
};
this.room.matchRoom(matchRoomPara, event => {
console.log(event);
if (event.code === 0) {
cc.log("匹配成功");
const player = event.data.roomInfo.playerList.find(player => player.name == playerName);
resolve(player.id);
} else {
cc.log("匹配失败");
reject();
}
});
});
}
leaveRoom() {
return new Promise((resolve, reject) => {
this.room.leaveRoom({}, event => {
console.log(event);
if (event.code === 0) {
cc.log("退房成功", this.room.roomInfo.id);
this.room.initRoom();
resolve(null);
}
});
});
}
closeRoom() {
return new Promise((resolve, reject) => {
this.room.changeRoom({ isForbidJoin: true }, (event) => {
console.log('关门')
console.log(event);
resolve(null);
});
});
}
sendMessage(msg) {
if (!msg) {
return;
}
if (typeof (msg) == 'object') {
msg = JSON.stringify(msg);
}
const sendToClientPara = {
recvType: MGOBE.ENUM.RecvType.ROOM_ALL,
recvPlayerList: [],
msg: msg
};
this.room.sendToClient(sendToClientPara, event => console.log(event));
}
startFrameSync() {
return new Promise((resolve, reject) => {
this.room.startFrameSync({}, event => {
console.log(event);
if (event.code === 0) {
cc.log("开始帧同步成功,请到控制台查看具体帧同步信息");
resolve(null);
} else {
reject();
}
});
});
}
stopFrameSync() {
return new Promise((resolve, reject) => {
this.room.stopFrameSync({}, event => {
console.log(event);
if (event.code === 0) {
cc.log("停止帧同步成功");
resolve(null);
} else {
reject(event.code);
}
});
});
}
sendToServer() {
const sendToGameServerPara = {
data: {
cmd: 1
}
};
this.room.sendToGameSvr(sendToGameServerPara, event => console.log(event));
}
sendFrame(data: any) {
this.room.sendFrame({ data }, err => {
if (err.code != 0) {
console.log("err", err)
}
});
}
sendEvent(key: String, data: Object) {
this.sendFrame({key, data});
}
onJoinRoom(event) {
if (this._eventListeners['playerJoin']) {
this._eventListeners['playerJoin'](event);
}
this.room.roomInfo = event.data.roomInfo;
console.log("新玩家加入", event.data.joinPlayerId);
}
onLeaveRoom(event) {
if (this._eventListeners['playerLeave']) {
this._eventListeners['playerLeave'](event);
}
this.room.roomInfo = event.data.roomInfo;
console.log("玩家退出", event.data.leavePlayerId);
}
onRecvFromClient() { }
onRecvFrame(event) {
if (this._eventListeners['frameEvent']) {
this._eventListeners['frameEvent'](event);
}
}
onStartFrameSync(event) {
if (this._eventListeners['gameStart']) {
this._eventListeners['gameStart'](event);
}
}
onStopFrameSync(event) { }
onRecvFromGameSvr() { }
log(str) {
const node = cc.find('middleLayer');
if (node) {
node.getComponent('middleLayer').log(str);
} else {
cc.log(str);
}
}
async onDestroy() {
try {
this.log('onDestroy1')
MGOBE.Listener.clear();
await this.stopGame();
this.log('onDestroy2')
} catch (e) {
this.log(JSON.stringify(e));
}
}
}
\ No newline at end of file
{
"ver": "1.0.8",
"uuid": "3b5661aa-abb8-448c-b4e5-22ce7bbb78b7",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
{
"ver": "1.1.2",
"uuid": "821c2d0d-9578-4d1c-9d43-1432e2e9a23c",
"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": "758b0cbc-9597-4f72-b7d7-eafd5bfa5684",
"subMetas": {}
}
\ No newline at end of file
This diff is collapsed.
{
"ver": "1.0.8",
"uuid": "f8a6fa03-d3af-4c33-8c3b-2f21e7c5e175",
"isPlugin": true,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
{
"type": "software",
"scope": "game",
"name": "GameOBESDK",
"version": "1.1.5.300",
"playbook": ""
}
\ No newline at end of file
{
"ver": "1.0.0",
"uuid": "0614d8cf-dd45-41f8-87d4-bee49ddea0f3",
"subMetas": {}
}
\ No newline at end of file
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