1.app.js
//引入socket
const Socket=require('socket.io');
const gameController=require('./game/gameController');
const app=Socket(8888);
console.log("hello world");
app.on('connection',function (socket) {
console.log(' a user connection');
socket.on('notify',function (notifyData) {//notify
console.log('服务器监听到');
console.log('notifyData='+JSON.stringify(notifyData));
switch (notifyData.type){
case 'random':
var accountID=notifyData.data.accountID;
var nickName=notifyData.data.nickName;
var cIndex=notifyData.callBackIndex;
//创建玩家实例
var player=gameController.createPlayer({accountID,nickName},socket);
//判断是否有空位房子
if(gameController.judgeLackRoom()){//有空位房子
gameController.joinRoom(player,cIndex); //直接加入房间
}else{
gameController.createRoom(player,cIndex); //创建房间并加入
}
break;
}
});
});
2.gameController.js
const Player=require('./player');
const Room=require('./room');
let _playerList=[];
let _roomFull=[];
let _newRoomList=[];
//创建玩家
exports.createPlayer=function (data,socket,callBackIndex) {
var player=Player(data,socket,callBackIndex);
_playerList.push(player);
return player;
};
//创建房间
exports.createRoom=function (player,cIndex) {
//回调客户端
player.sendPlayerChessColor({chessColor:'b'},cIndex);//返回玩家的棋子颜色黑色
var room=Room(); //创建房间
room.joinRoom(player); //玩家加入房间
player.room=room; //玩家保存房间引用
_newRoomList.push(room); //将房间加入到有空位的房间
};
//判断有空位的房子
exports.judgeLackRoom=function(){
if(_newRoomList.length===0){
return false;
}
return true;
};
//进入房间
exports.joinRoom=function (player,cIndex) {
player.sendPlayerChessColor({chessColor:'w'},cIndex)
var room=_newRoomList.pop(); //弹出有空位的房间
room.joinRoom(player); //玩家加入房间
player.room=room; //玩家保存房间的引用
room.sendPlayerStart(); //通知所有玩家可以开始游戏
_roomFull.push(room); //将此房间加入到满房间列表
}
3.player.js
module.exports=function (info,socket,cIndex) {
let that={};
let _socket=socket;
that.accountID=info.accountID;
that.nickName=info.nickName;
let _room=undefined;
const notify=function (type,data,callBackIndex) {
_socket.emit('notify',{
type:type,
data:data,
callBackIndex:callBackIndex
});
};
//玩家掉线监听
_socket.on('disconnect',()=>{
console.log('dis.....');
//通知对方掉线
//_room.playerOffLine(that);
});
//监听客户端消息
_socket.on('notify',function (notifyData) {
let type=notifyData.type;//获取客户端请求类型
let data=notifyData.data;//玩家发过来的数据
switch(type){
case 'upChess': //下棋
console.log('玩家棋上传');
console.log(JSON.stringify(data));
if(_room){
_room.sendPlayerChess(data); //通过房间对象广播玩家棋
}
break;
default:break;
}
});
that.sendPlayerStart=function () {
notify('start_game',{},null);
};
that.sendPlayerChess=function (data) {
notify('playChess',data,null);
};
that.sendPlayerChessColor=function (data,bIndex) {
notify('random',data,bIndex);
};
Object.defineProperty(that,'room',{
set(val){
_room=val;
}
});
return that;
}
4.room.js
module.exports=function () {
let that={};
let _playerList=[];
//----------广播玩家开始游戏----------
that.sendPlayerStart=function () {
for(var i=0;i<_playerList.length;i++){
_playerList[i].sendPlayerStart();
}
};
//-----------玩家加入房间--------------
that.joinRoom=function (player) {
_playerList.push(player);
//通知本房间所有玩家可以开始游戏
console.log("成功加入房间");
if(_playerList.length===2){
that.sendPlayerStart();
console.log('通知开始游戏');
}
};
//----------广播玩家出的棋子---------------
that.sendPlayerChess=function (data) {
for(var i=0;i<_playerList.length;i++){
_playerList[i].sendPlayerChess(data);
}
}
return that;
}