import { CardSeatInfo, ECardSeatState } from "../Table/CardSeat"; import * as _ from "underscore"; import { GExtUserSer } from "../../../Common/Service/ExtUserService"; import { CardTableInfo } from "../Table/CardTable"; import { ns_card } from "../../Config/_ini_"; import { CardRobotManager } from "./CardRobotManager"; import { CardRoom } from "../Room/CardRoom"; import { GRoomRobotCfgSer } from "../../../Common/Service/RoomRobotCfgService"; import { GLog } from "cgserver"; import { Timer } from "cgserver"; export class CardRobot { protected _robotMgr:CardRobotManager = null protected _user_id:number = -1 get userId() { return this._user_id } protected _table_info:CardTableInfo = null constructor(robotMgr:CardRobotManager,user_id:number) { this._robotMgr = robotMgr this._user_id = user_id } get seatInfo():CardSeatInfo { return this.getSeatByUserId(this._user_id) } protected _prepare_timer=new Timer() protected _timeout_handlers=new Map() protected _timout_code=0 setTimeout(fun,time) { ++this._timout_code let hd = setTimeout(()=> { this._timeout_handlers[this._timout_code]=null delete this._timeout_handlers[this._timout_code] fun() },time) this._timeout_handlers[this._timout_code]=hd } clearTimeout() { this._prepare_timer.clear() for(let code in this._timeout_handlers) { let hd = this._timeout_handlers[code] clearTimeout(hd) } this._timeout_handlers=new Map() } getSeatByUserId(user_id):CardSeatInfo { if(!this._table_info) { return null } if(!this._table_info.seats) { return null } return this._table_info.seats[user_id] } doEnterTable(table_id:number) { if(!table_id) { return false } let table = this._robotMgr.server.tableMgr.getTableByTableId(table_id) if(!table) { return false } if(table.roomInfo) { let min = table.roomInfo.min*3 let max = table.roomInfo.max if(max<0) { max = min*100 } let coin = _.random(min,max) GExtUserSer.updateOne({ coin:coin },{ id:this._user_id }) } this.send_enter_table(table_id) return true } doEnterRoom(room_id:number) { let room:CardRoom = this._robotMgr.server.roomMgr.getRoomById(room_id) let min = room.roomCfg.min let max = room.roomCfg.max if(max<0) { max = min*100 } let coin = _.random(min,max) GExtUserSer.updateOne({ coin:coin },{id:this._user_id,}) this.send_enter_room(room_id) } onMessage(msg) { setTimeout(()=> { this._onMessage(msg) },30) } protected _onMessage(msg) { // GLog.info(this._robotMgr.server.name+" robot_receive:-----------------------------------") // GLog.info(msg) let cmd = "receive_"+msg.cmd if(msg.errcode) { cmd+="_failed" } if(!this[cmd]) { cmd = cmd.replace("_"+this._robotMgr.server.name+"_","_") } if(this[cmd]) { try { this[cmd](msg) } catch(e) { if(e&&e.stack) { GLog.error(e.stack) } else { GLog.error(e+" msg:data="+JSON.stringify(msg)) } } } } send(msg) { setTimeout(()=> { this._send(msg) },30) } protected _send(msg) { //构造来源_user_id msg._user_id = this._user_id // GLog.info(this._robotMgr.server.name+" robot_send:-----------------------------------") // GLog.info(msg) let ws = this._robotMgr.server.getAnyWebSocket() if(!ws) { return } let cmd = "receive_"+msg.cmd if(!ws[cmd]) { cmd = cmd.replace("_"+this._robotMgr.server.name+"_","_") } if(ws[cmd]) { try { ws[cmd](msg) } catch(e) { if(e&&e.stack) { GLog.error(e.stack) } else { GLog.error(e+" send_msg:data="+JSON.stringify(msg)) } } } } send_enter_room(room_id) { let msg= { cmd:this._robotMgr.server.name+"_enter_room", room_id:room_id } this.send(msg) } send_enter_table(table_id) { let msg= { cmd:this._robotMgr.server.name+"_enter_table", table_id:table_id } this.send(msg) } receive_enter_table(jsonData) { this._table_info = jsonData.table_info if(!this._table_info.ctdm.auto_sitdown) { this.send_sitdown() } } receive_enter_table_failed(jsonData) { this.clearTimeout() if(jsonData.errcode.id!=12015//在其他牌桌 &&jsonData.errcode.id!=12034)//已经在座位上了 { this._robotMgr.onRobotLeave(this._user_id) } } receive_enter_table_bc(jsonData) { } send_sitdown() { let msg= { cmd:this._robotMgr.server.name+"_sitdown", table_id:this._table_info.id } this.send(msg) } receive_sitdown_bc(jsonData) { let seat:CardSeatInfo = jsonData.seat this._table_info.seats[seat.playerinfo.id]=seat if(seat.playerinfo.id==this._user_id &&seat.state!=ECardSeatState.Preparing &&!this._prepare_timer.isRunning &&!this._table_info.ctdm.auto_prepare &&(this._table_info.state==ns_card.ETableState.Prepare ||this._table_info.state==ns_card.ETableState.Waiting)) { this._prepare_timer.setTimeout(()=> { if(seat.state!=ECardSeatState.Preparing &&(this._table_info.state==ns_card.ETableState.Prepare ||this._table_info.state==ns_card.ETableState.Waiting)) { this.send_prepare() } },_.random(0,3000)) } } receive_apply_dismiss_table_bc(jsonData) { let apply_user_id = jsonData.user_id let si = this.getSeatByUserId(apply_user_id) if(si) { si.agree_dismiss = true } this._table_info.is_dismissing = true this._table_info.apply_dismiss_user_id = apply_user_id } send_agree_dismiss_table(isAgree:boolean) { let msg = { cmd:this._robotMgr.server.name+"_agree_dismiss_table", is_agree:isAgree } this.send(msg) } receive_agree_dismiss_table_bc(jsonData) { let user_id = jsonData.user_id let agree_dismiss = jsonData.is_agree let si = this.getSeatByUserId(user_id) si.agree_dismiss = agree_dismiss } /** * 牌桌被解散 * @param jsonData */ receive_dismiss_table_bc(jsonData) { this.clearTimeout() this._robotMgr.onRobotLeave(this._user_id) } send_leave_table() { let msg = { cmd:this._robotMgr.server.name+"_leave_table" } this.send(msg) } receive_leave_table_failed(jsonData) { let errcode = jsonData.errcode.id if(errcode==70005//原本就不再桌内了 ||errcode==12005//原本就不再桌内了 ||errcode==12010)//金币不足 { this.clearTimeout() this._robotMgr.onRobotLeave(this._user_id) } } receive_leave_table(jsonData) { //GLog.info("leave "+this._user_id+" time="+Date.now().toLocaleString(),true) this.clearTimeout() this._robotMgr.onRobotLeave(this._user_id) } receive_leave_table_bc(jsonData) { delete this._table_info.seats[jsonData.user_id] this._table_info.seats[jsonData.user_id]=null } /** * 开始准备 * @param jsonData */ receive_begin_prepare_bc(jsonData) { this._table_info.timeout = jsonData.deadline let pre_state = this._table_info.state this._table_info.state = ns_card.ETableState.Prepare if(pre_state==ns_card.ETableState.End) { this.seatInfo.state=ECardSeatState.Waiting } if(this.seatInfo.state!=ECardSeatState.Preparing &&!this._prepare_timer.isRunning) { let time = _.random(1000,5000) this._prepare_timer.setTimeout(()=> { if(this.seatInfo.state!=ECardSeatState.Preparing &&this._table_info.state==ns_card.ETableState.Prepare) { this.send_prepare() } },time) } } send_prepare() { let msg = { cmd:this._robotMgr.server.name+"_prepare" } this.send(msg) } receive_prepare_failed(jsonData) { //不在桌内 if(jsonData.errcode.id==12005) { //GLog.info("not in table "+this._user_id+" time="+Date.now().toLocaleString(),true) this.clearTimeout() this._robotMgr.onRobotLeave(this._user_id) return } //12035是因为已经准备了重复准备,或者时机不对,就不要轻易离开座子了没必要 if(jsonData.errcode.id!=12035) { this.send_leave_table() } } receive_prepare_bc(jsonData) { let user_id = jsonData.user_id let seat = this.getSeatByUserId(user_id) seat.state=ECardSeatState.Preparing if(user_id==this._user_id) { this._prepare_timer.clear() } } send_start_game() { let msg = { cmd:this._robotMgr.server.name+"_start_game" } this.send(msg) } receive_start_game(jsonData) { this._table_info = jsonData.table_info } receive_start_game_bc(jsonData) { this._table_info = jsonData.table_info } receive_game_end_bc(jsonData) { this.clearTimeout() this._robotMgr.onRobotLeave(this._user_id) } async receive_game_result_bc(jsonData) { this.clearTimeout() this._table_info.timeout = jsonData.timeout this._table_info.state = ns_card.ETableState.End let r = _.random(0,100) GRoomRobotCfgSer.getByRoomId(this._table_info.room_id).then((cfg)=> { if(r>=cfg.leave_rate) { return } r = _.random(1000,3500) this.setTimeout(()=> { this.send_leave_table() },r) }) } send_robot(robot) { let msg = { cmd:this._robotMgr.server.name+"_robot", robot:robot } this.send(msg) } receive_auto_bc(jsonData) { let seat = this.getSeatByUserId(jsonData.user_id) seat.auto = jsonData.auto } }