Files
youlegames/codes/games/sales_service/youle/server_room/class.room.js
2026-02-04 23:47:45 +08:00

434 lines
14 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
///////////////////////////////////////////////////
////////// cls_youle_room_room: 房间 //////////
///////////////////////////////////////////////////
var cls_youle_room_room = cls_youle_room_room || {
//构造函数
new: function(o_game, roomcode, roomtype, needroomcard, asetcount){
var room = {};
//基本信息
room.o_game = o_game; //游戏
room.roomcode = roomcode; //房号
room.roomtype = roomtype; //房间类型
room.needroomcard = needroomcard; //开房需要的房卡数量
room.asetcount = asetcount; //总局数
room.createtime = min_now(); //开房时间
room.seatlist = []; //座位列表
var makewarplayercount = youle_room.import.makewar_playercount(o_game, roomtype);
if (makewarplayercount){
room.seatlist.length = makewarplayercount;
} else {
room.seatlist.length = o_game.seatcount;
}
room.battlestate = 0; //游戏状态 0:未开战 1:已开战
room.makewartime = null; //开战时间
room.owner = null; //房主
room.applyfreetime = null; //申请解散的时间
room.freetimer = null; //默认同意解散房间的定时器
room.wasdeduct = false; //是否扣除了房主的房卡
//24小时后如果没有玩家在房间内则解散房间的定时器
var do_freeroom24 = function(){
for (var i = 0; i < room.seatlist.length; i++) {
if (room.seatlist[i]){
return;
}
}
min_closetime(room.freetimer24);
room.freetimer24 = null;
youle_room.export.save_grade(room, null, null, 1);
}
room.freetimer24 = min_ontime(do_freeroom24, 24 * 60 * 60 * 1000);
//方法
room.method = {};
//查找玩家
room.method.find_player = function(playerid){
return cls_youle_room_room.find_player(room, playerid);
}
//获取玩家人数
room.method.get_playercount = function(){
return cls_youle_room_room.get_playercount(room);
}
//获取已准备的玩家人数
room.method.get_playercount_prepare = function(){
return cls_youle_room_room.get_playercount_prepare(room);
}
//获取房间内所有玩家的基本信息
room.method.get_playerlist = function(){
return cls_youle_room_room.get_playerlist(room);
}
//房间是否满桌
room.method.get_isfull = function(){
return cls_youle_room_room.get_isfull(room);
}
//获取房间内所有玩家的申请解散房间状态
room.method.get_agreefree = function(){
return cls_youle_room_room.get_agreefree(room);
}
//给房间内某个位置的玩家发包
room.method.sendpack_toseat = function(msg, seat){
cls_youle_room_room.sendpack_toseat(room, msg, seat);
}
//给房间内的其他玩家发包
room.method.sendpack_toother = function(msg, selfseat){
cls_youle_room_room.sendpack_toother(room, msg, selfseat);
}
//默认同意解散倒计时
room.method.get_freeroom_countdown = function(){
return cls_youle_room_room.get_freeroom_countdown(room);
}
//打开默认同意解散的定时器
room.method.open_free_timer = function(){
cls_youle_room_room.open_free_timer(room);
}
//关闭默认同意解散的定时器
room.method.close_free_timer = function(){
cls_youle_room_room.close_free_timer(room);
}
//获取房间信息
room.method.get_roominfo = function(){
return cls_youle_room_room.get_roominfo(room);
}
return room;
},
//查找玩家
find_player: function(o_room, playerid){
var o_player = null;
for (var i = 0; i < o_room.seatlist.length; i++){
if (o_room.seatlist[i]){
if (o_room.seatlist[i].playerid == playerid){
o_player = o_room.seatlist[i];
break;
}
}
}
return o_player;
},
//获取玩家人数
get_playercount: function(o_room){
var count = 0;
for (var i = 0; i < o_room.seatlist.length; i++){
if (o_room.seatlist[i]){
count++;
}
}
return count;
},
//获取已准备的玩家人数
get_playercount_prepare: function(o_room){
var count = 0;
for (var i = 0; i < o_room.seatlist.length; i++){
if (o_room.seatlist[i] && o_room.seatlist[i].gameinfo.isprepare){
count++;
}
}
return count;
},
//获取房间内所有玩家的基本信息
get_playerlist: function(o_room){
var playerlist = [];
for (var i = 0; i < o_room.seatlist.length; i++) {
if (o_room.seatlist[i]){
var player = o_room.seatlist[i].method.get_info();
playerlist.push(player);
} else {
playerlist.push(null);
}
}
return playerlist;
},
//房间是否满桌
get_isfull: function(o_room){
for (var i = 0; i < o_room.seatlist.length; i++) {
if (!o_room.seatlist[i] || !o_room.seatlist[i].gameinfo.isprepare){
return false;
}
}
return true;
},
//获取房间内所有玩家的申请解散房间状态
get_agreefree: function(o_room){
if (!o_room.applyfreetime){
return null;
}
var info = {};
//默认同意解散倒计时
info.countdown = o_room.method.get_freeroom_countdown();
var list = [];
for (var i = 0; i < o_room.seatlist.length; i++) {
if (o_room.seatlist[i]){
list.push(o_room.seatlist[i].gameinfo.agree);
} else {
list.push(null);
}
};
info.state = list;
return info;
},
//给房间内某个位置的玩家发包
sendpack_toseat: function(o_room, msg, seat){
if (seat < o_room.seatlist.length){
if (o_room.seatlist[seat]) {
msg.conmode = o_room.seatlist[seat].conmode;
msg.fromid = o_room.seatlist[seat].fromid;
youle_room.app.SendPack(msg);
}
}
},
//给房间内的其他玩家发包
sendpack_toother: function(o_room, msg, selfseat){
// for (var i = 0; i < o_room.seatlist.length; i++) {
// if (o_room.seatlist[i] && (i != selfseat)) {
// msg.conmode = o_room.seatlist[i].conmode;
// msg.fromid = o_room.seatlist[i].fromid;
// youle_room.app.SendPack(msg);
// }
// }
var _toids_tcp = [];
for (var i = 0; i < o_room.seatlist.length; i++) {
if (o_room.seatlist[i] && (i != selfseat)) {
if (o_room.seatlist[i].conmode && o_room.seatlist[i].fromid){
if (o_room.seatlist[i].conmode == "tcp"){
_toids_tcp.push(o_room.seatlist[i].fromid);
} else {
msg.conmode = o_room.seatlist[i].conmode;
msg.fromid = o_room.seatlist[i].fromid;
youle_room.app.SendPack(msg);
}
}
}
}
if (_toids_tcp.length > 0){
msg.conmode = "tcp";
youle_room.app.SendPack(msg, _toids_tcp);
}
},
//默认同意解散房间的倒计时时间
get_freeroom_countdown: function(o_room){
var apply = new Date(o_room.applyfreetime);
var now = new Date();
return o_room.o_game.o_agent.freeroom - parseInt((now.getTime() - apply.getTime()) / 1000);
},
//打开默认同意解散的定时器
open_free_timer: function(o_room){
var do_freeroom = function(){
o_room.freetimer = null;
for (var i = 0; i < o_room.seatlist.length; i++){
if (o_room.seatlist[i]){
if (o_room.seatlist[i].gameinfo.agree == -1){
o_room.seatlist[i].gameinfo.agree = 1;
}
}
}
//通知代理商服务器解散房间
var o_game = o_room.o_game;
var o_agent = o_game.o_agent;
var msg1 = {};
msg1.app = youle_room.app.appname;
msg1.route = youle_room.app.youle_agent.routename;
msg1.rpc = "free_room";
msg1.data = {};
msg1.data.agentid = o_agent.agentid;
msg1.data.gameid = o_game.gameid;
msg1.data.roomcode = o_room.roomcode;
//玩家列表
msg1.data.playeridlist = [];
for (var i = 0; i < o_room.seatlist.length; i++) {
if (o_room.seatlist[i]){
msg1.data.playeridlist.push(o_room.seatlist[i].playerid);
}
}
if (o_agent.server == o_game.roomserver){
//房间服务器与代理商服务器是同一地址
youle_room.app.youle_agent[msg1.rpc](msg1);
//通知前端房间内的所有人解散房间
var msg2 = {};
msg2.app = youle_room.app.appname;
msg2.route = youle_room.routename;
msg2.rpc = "free_room";
msg2.data = {};
//获取房间内各玩家的解散房间状态
msg2.data.seats = o_room.method.get_agreefree().state;
//获取游戏的解散包
// if (o_room.battlestate == 1){
// msg2.data.deskfree = youle_room.import.get_disbandRoom(o_room);
// }
// o_room.method.sendpack_toother(msg2, -1);
if (o_room.battlestate == 1){
var _deskfree = youle_room.import.get_disbandRoom(o_room);
if (_deskfree){
if (_deskfree.sendtype && _deskfree.sendtype == 1 && _deskfree.seatlist){
//子游戏返回的解散json数据如果有sendtype属性且值等于1则表示需要向不同的玩家各自发包且根据seatlist数组对应给各位置的玩家发游戏解散包
for (var n = 0; n < _deskfree.seatlist.length; n++) {
if (_deskfree.seatlist){
msg2.data.deskfree = _deskfree.seatlist[n];
o_room.method.sendpack_toseat(msg2, n);
}
}
} else {
msg2.data.deskfree = _deskfree;
o_room.method.sendpack_toother(msg2, -1);
}
} else {
o_room.method.sendpack_toother(msg2, -1);
}
} else {
o_room.method.sendpack_toother(msg2, -1);
}
//释放房间
o_game.method.free_room(o_room);
} else {
//房间服务器与代理商服务器不是同一地址
var cfg = {};
cfg.url = "http://" + o_agent.server + ":" + o_agent.httpport + "/index.html";
cfg.type = "POST";
cfg.data = JSON.stringify(msg1);
cfg.success = function(str){
var _info = {};
//获取房间内各玩家的解散房间状态
_info.seats = o_room.method.get_agreefree().state;
//获取游戏的解散包
// if (o_room.battlestate == 1){
// _info.deskfree = youle_room.import.get_disbandRoom(o_room);
// }
var _deskfree = null;
if (o_room.battlestate == 1){
_deskfree = youle_room.import.get_disbandRoom(o_room);
}
//通知前端房间内的所有人需要断开房间服务器,连接代理服务器
for (var i = 0; i < o_room.seatlist.length; i++) {
if (o_room.seatlist[i]) {
var msg2 = {};
msg2.app = youle_room.app.appname;
msg2.route = youle_room.routename;
msg2.rpc = "connect_agentserver";
msg2.data = {};
msg2.data.agentserver = o_agent.server + ":" + o_agent.tcpport;
msg2.data.data = {};
msg2.data.data.opt = "free_room";
msg2.data.data.playerid = o_room.seatlist[i].playerid;
msg2.data.data.agentid = o_agent.agentid;
msg2.data.data.gameid = o_game.gameid;
if (_deskfree){
if (_deskfree.sendtype && _deskfree.sendtype == 1 && _deskfree.seatlist){
//子游戏返回的解散json数据如果有sendtype属性且值等于1则表示需要向不同的玩家各自发包且根据seatlist数组对应给各位置的玩家发游戏解散包
_info.deskfree = _deskfree.seatlist[i];
} else {
_info.deskfree = _deskfree;
}
}
msg2.data.data.info = _info;
msg2.conmode = o_room.seatlist[i].conmode;
msg2.fromid = o_room.seatlist[i].fromid;
//此处conmode = null是为了避免触发玩家离线通知其他玩家事件
o_room.seatlist[i].conmode = null;
o_room.seatlist[i].fromid = null;
youle_room.app.SendPack(msg2);
}
}
//释放房间
o_game.method.free_room(o_room);
};
cfg.error = function(str){};
min_http(cfg);
}
}
o_room.freetimer = min_ontimeout(do_freeroom, o_room.o_game.o_agent.freeroom * 1000);
},
//关闭默认同意解散的定时器
close_free_timer: function(o_room){
min_closetime(o_room.freetimer);
o_room.freetimer = null;
},
//获取房间信息
get_roominfo: function(o_room){
var info = {};
info.roomtype = o_room.roomtype; //房间类型
info.needroomcard = o_room.needroomcard; //开房需要的房卡数量
info.asetcount = o_room.asetcount; //总局数
info.createtime = o_room.createtime; //开房时间
info.makewartime = o_room.makewartime; //开战时间
info.battlestate = o_room.battlestate; //游戏状态 0:未开战 1:已开战
info.wasdeduct = o_room.wasdeduct; //是否扣除了房主的房卡
info.roommode = o_room.roommode; //是否是星星场
info.beanfloor = o_room.beanfloor; //星星下限
info.beanmult = o_room.beanmult; //星星场倍数
info.needprepare = o_room.needprepare; //是否需要准备
info.advanced_type = o_room.advanced_type; //是否高级房间
info.advanced_teabean = o_room.advanced_teabean; //茶水费
info.infinite = o_room.infinite; //是否无限场
//房主id
if (o_room.owner){
info.ownerid = o_room.owner.playerid;
} else {
info.ownerid = null;
}
//玩家列表
info.seatlist = [];
for (var i = 0; i < o_room.seatlist.length; i++) {
if (!o_room.seatlist[i]){
info.seatlist.push(null);
} else {
var p = {};
p.playerid = o_room.seatlist[i].playerid;
p.nickname = o_room.seatlist[i].nickname;
p.onstate = o_room.seatlist[i].onstate; //在线状态 null未连接 0在线 1离线 2电话中
p.needroomcard = o_room.seatlist[i].needroomcard;
p.wasdeduct = o_room.seatlist[i].wasdeduct;
p.bean = o_room.seatlist[i].bean;
p.isowner = o_room.seatlist[i].gameinfo.isowner;
p.isbet = o_room.seatlist[i].gameinfo.isbet;
p.grade = o_room.seatlist[i].gameinfo.grade;
p.isprepare = o_room.seatlist[i].gameinfo.isprepare;
var _agentsvr_agent = youle_agent.agents.method.find_agent(o_room.o_game.o_agent.agentid);
var _agentsvr_player = _agentsvr_agent.method.find_player(o_room.seatlist[i].playerid);
p.unionid = _agentsvr_player.unionid;
info.seatlist.push(p);
}
}
return info;
}
}