class ClassicModel extends egret . DisplayObjectContainer { private layout ; private user : User ; private webSocket : egret . WebSocket ; private portrait : PortraitContainer ; private portraitLeft : PortraitOtherContainer ; private portraitRight : PortraitOtherContainer ; private pokerContainer : PukerContainer ; private publicPokerContainer : PukerBottomContainer ; private pokerPlay : PukerPlayContainer ; private pokerPlayRight : PukerOtherContainer ; private pokerPlayLeft : PukerOtherContainer ; private textRight : TextOtherContainer ; private textLeft : TextOtherContainer ; private buttons : PlayerButtonContainer ; private poc1Index : number = Math . floor ( Math . random ( ) * 10 ) + 1 ; private poc2Index : number = Math . floor ( Math . random ( ) * 10 ) + 1 ; private poc3Index : number = Math . floor ( Math . random ( ) * 10 ) + 1 ; private leftPlayer : User = PukerUtils . getRandomUser ( ) ; private rightPlayer : User = PukerUtils . getRandomUser ( ) ; private roomId : string ; private landlordSeat : number ; /**
     * 玩家的牌
     */ private pokers : Array < Poker > = new Array < Poker > ( ) ; /**
     * 当前房间出的牌
     */ private activityPokers : Array < Poker > = new Array < Poker > ( ) ; /**
     * 房间里当前出的牌的玩家的座位号
     */ private activityPlayerSeat : number = 1 ; /**
     * 底牌
     */ private publicPokers : Array < Poker > = new Array < Poker > ( ) ; private selectedPokers : Array < Poker > = new Array < Poker > ( ) ; public UUID : string ; /**
     * 联机游戏场景
     * user  玩家
     */ public constructor ( user ) { super ( ) ; this . user = user ; this . layout = RES . getRes ( "layout_json" ) . layout ; let sky = new Layout ( this . layout . stageWidth , this . layout . stageHeight ) ; this . addChild ( sky ) ; let tip = new TextTip ( "返回大厅" , 10 , 10 , 64 ) ; tip . touchEnabled = true ; tip . addEventListener ( egret . TouchEvent . TOUCH_TAP , this . button_gameOver , this ) ; this . addChild ( tip ) ; this . show ( ) ; } /**
     * 显示游戏场景
     */ private show ( ) { console . log ( "进入游戏房间" ) ; this . init ( "ws://132.232.2.225:8800/pokerWebSocket" , this . user . getName ( ) ) ; this . showPortrait ( false ) ; } private init ( url , userName ) { this . webSocket = new egret . WebSocket ( ) ; this . webSocket . addEventListener ( egret . ProgressEvent . SOCKET_DATA , this . onReceiveData , this ) ; this . webSocket . addEventListener ( egret . Event . CONNECT , this . onConnected , this ) ; this . webSocket . addEventListener ( egret . Event . CLOSE , this . onConnectClose , this ) ; this . webSocket . addEventListener ( egret . IOErrorEvent . IO_ERROR , this . onIOError , this ) ; this . webSocket . connectByUrl ( url + "/" + userName ) ; } public send ( data ) { this . webSocket . writeUTF ( JSON . stringify ( data ) ) ; console . log ( "消息发送成功" , JSON . stringify ( data ) ) ; } private onReceiveData ( e ) { let response = this . webSocket . readUTF ( ) ; console . log ( "onReceiveData:" , response ) ; if ( response . length == 0 ) { return ; } let res = JSON . parse ( response ) ; switch ( res . code ) { case RoomManager . Response_InitUserOK : { console . log ( "用户初始化完成" , "用户Id" , res . data . userId ) ; this . user . setId ( res . data . userId ) ; let request = new Object ; let body = new Object ; request . code = RoomManager . Request_onRoom ; body . userId = this . user . getId ( ) ; body . userName = this . user . getName ( ) ; request . data = body ; this . send ( request ) ; } break ; case RoomManager . Response_RoomInfo : { console . log ( "进入游戏房间成功" , "房间号" , res . data . roomId ) ; this . roomId = res . data . roomId ; let players = res . data . players ; for ( let i = 0 ; i < players . length ; i ++ ) { if ( players [ i ] . id == this . user . getId ( ) ) { this . user . setSeat ( players [ i ] . seat ) ; } } for ( let i = 0 ; i < players . length ; i ++ ) { if ( players [ i ] . id != this . user . getId ( ) ) { if ( this . isLeft ( players [ i ] . seat ) ) { this . leftPlayer . setSeat ( players [ i ] . seat ) ; this . leftPlayer . setName ( players [ i ] . name ) ; } else { this . rightPlayer . setSeat ( players [ i ] . seat ) ; this . rightPlayer . setName ( players [ i ] . name ) ; } this . showOtherPortrait ( players [ i ] . seat , false ) ; } } } break ; case RoomManager . Response_NewPlayerJoin : { console . log ( "有新玩家加入" , res . data . player ) ; let player = res . data . player ; if ( this . isLeft ( player . seat ) ) { this . leftPlayer . setSeat ( player . seat ) ; this . leftPlayer . setName ( player . name ) ; } else { this . rightPlayer . setSeat ( player . seat ) ; this . rightPlayer . setName ( player . name ) ; } this . showOtherPortrait ( player . seat , false ) ; } break ; case RoomManager . Response_Reday : { console . log ( "房间人满，准备开始游戏------------" , "房间号" , res . data . roomId ) ; let request = new Object ; let body = new Object ; request . code = RoomManager . Request_BeReady ; body . roomId = res . data . roomId ; body . userId = this . user . getId ( ) ; request . data = body ; this . send ( request ) ; } break ; case RoomManager . Response_DealPoker : { console . log ( "------------发牌---------------" ) ; console . log ( "我的牌" , res . data . pokers ) ; let list = res . data . pokers ; for ( let i = 0 ; i < list . length ; i ++ ) { this . pokers . push ( new Poker ( list [ i ] . id , list [ i ] . sort ) ) ; } this . showPokersDynamic ( ) ; } break ; case RoomManager . Response_ToCallTheLandlord : { } break ; case RoomManager . Response_LandlordAndLastCard : { console . log ( "------------产生地主---------------" ) ; console . log ( "底牌" , res . data . publicPokers ) ; console . log ( "地主座位号" , res . data . landlordSeat ) ; let landlordSeat = res . data . landlordSeat ; this . landlordSeat = landlordSeat ; this . activityPlayerSeat = landlordSeat ; let publicPokers = res . data . publicPokers ; for ( let i = 0 ; i < publicPokers . length ; i ++ ) { this . publicPokers . push ( new Poker ( publicPokers [ i ] . id , publicPokers [ i ] . sort ) ) ; } this . showPublicPokers ( ) ; if ( this . user . getSeat ( ) == landlordSeat ) { this . user . setLandlord ( true ) ; this . pokers = PukerUtils . sortDescPokers ( this . pokers . concat ( this . publicPokers ) ) ; this . selectedPokers = new Array < Poker > ( ) ; this . showPokers ( ) ; console . log ( "------------刷新自己的牌---------------" , this . pokers ) ; this . showPortrait ( true ) ; this . showButtons ( RoomManager . ButtonsDiscard ) ; } else { if ( this . leftPlayer . getSeat ( ) == landlordSeat ) this . leftPlayer . setLandlord ( true ) ; else this . rightPlayer . setLandlord ( true ) ; this . showOtherPortrait ( landlordSeat , true ) ; } } break ; case RoomManager . Response_Discard : { console . log ( "------------有玩家出牌---------------" ) ; console . log ( "玩家座位号" , res . data . seat ) ; console . log ( "牌" , res . data . pokers ) ; let seat = res . data . seat ; let array = res . data . pokers ; let nextSeat = res . data . nextSeat ; if ( seat == this . user . getSeat ( ) ) { if ( array == null || array . length < 1 ) { console . log ( "--------自己未出牌----------" ) ; } else { this . activityPlayerSeat = seat ; let activityPokers = new Array < Poker > ( ) ; for ( let i = 0 ; i < array . length ; i ++ ) { activityPokers . push ( new Poker ( array [ i ] . id , array [ i ] . sort ) ) ; } this . activityPokers = activityPokers ; this . pokers = PukerUtils . removePokers ( this . pokers , activityPokers ) ; this . showPokers ( ) ; this . showPokerPlay ( ) ; } this . selectedPokers = new Array < Poker > ( ) ; } else { if ( array == null || array . length < 1 ) { console . log ( "--------别人未出牌----------" ) ; if ( this . isLeft ( seat ) ) { this . clearChileByName ( "pokerPlayLeft" ) ; this . clearChileByName ( "textLeft" ) ; this . textLeft = new TextOtherContainer ( PukerUtils . getRandomTextTip ( ) , true ) ; this . textLeft . name = "textLeft" ; this . addChild ( this . textLeft ) ; } else { this . clearChileByName ( "pokerPlayRight" ) ; this . clearChileByName ( "textRight" ) ; this . textRight = new TextOtherContainer ( PukerUtils . getRandomTextTip ( ) , false ) ; this . textRight . name = "textRight" ; this . addChild ( this . textRight ) ; } } else { this . activityPlayerSeat = seat ; let activityPokers = new Array < Poker > ( ) ; for ( let i = 0 ; i < array . length ; i ++ ) { activityPokers . push ( new Poker ( array [ i ] . id , array [ i ] . sort ) ) ; } this . activityPokers = activityPokers ; if ( this . isLeft ( seat ) ) { this . clearChileByName ( "pokerPlayLeft" ) ; this . clearChileByName ( "textLeft" ) ; this . pokerPlayLeft = new PukerOtherContainer ( activityPokers , true ) ; this . pokerPlayLeft . name = "pokerPlayLeft" ; this . addChild ( this . pokerPlayLeft ) ; } else { this . clearChileByName ( "pokerPlayRight" ) ; this . clearChileByName ( "textRight" ) ; this . pokerPlayRight = new PukerOtherContainer ( activityPokers , false ) ; this . pokerPlayRight . name = "pokerPlayLeft" ; this . addChild ( this . pokerPlayRight ) ; } } } if ( nextSeat == this . user . getSeat ( ) ) { this . showButtons ( RoomManager . ButtonsDiscard ) ; } } break ; case RoomManager . Response_GameOver : { console . log ( "------------游戏结束---------------" ) ; console . log ( "胜利者" , res . data . victory ) ; let victory = res . data . victory ; if ( victory == RoomManager . Victory_Landlord ) { if ( this . user . getSeat ( ) == this . landlordSeat ) { console . log ( "---------胜利--------" ) ; this . showResult ( true ) ; } else { console . log ( "---------失败--------" ) ; this . showResult ( false ) ; } } else { if ( this . user . getSeat ( ) == this . landlordSeat ) { console . log ( "---------失败--------" ) ; this . showResult ( false ) ; } else { console . log ( "---------胜利--------" ) ; this . showResult ( true ) ; } } } break ; default : { console . log ( "onReceiveData:" , res . code , res . data ) ; } break ; } } private onConnected ( ) { console . log ( "成功连接到服务器-------------" ) ; } private onConnectClose ( ) { console . log ( 'webSocket' , 'connect closed' ) ; } private onIOError ( ) { console . log ( 'webSocket' , 'IO Error' ) ; } private showPublicPokers ( ) { /** 显示底牌 */ this . publicPokerContainer = new PukerBottomContainer ( this . publicPokers ) ; this . publicPokerContainer . name = "publicPokerContainer" ; this . addChild ( this . publicPokerContainer ) ; } /**
     * 显示自己的牌
     */ private showPokers ( ) { this . clearChileByName ( "pokerContainer" ) ; this . pokerContainer = new PukerContainer ( this , this . pokers ) ; this . pokerContainer . name = "pokerContainer" ; this . addChild ( this . pokerContainer ) ; } /**
     * 动态显示扑克牌
     */ private showPokersDynamic ( ) { this . pokers = PukerUtils . sortDescPokers ( this . pokers ) ; let index = this . pokers . length ; let i = 0 ; let pokers = new Array < Poker > ( ) ; this . createTimer ( 300 , index , ( ) => { console . log ( "发牌：" , i ) ; pokers . push ( this . pokers [ i ] ) ; this . clearChileByName ( "pokerContainer" ) ; this . pokerContainer = new PukerContainer ( this , pokers ) ; this . pokerContainer . name = "pokerContainer" ; this . addChild ( this . pokerContainer ) ; i ++ ; } , ( ) => { console . log ( "发牌结束" ) ; this . showPokers ( ) ; } ) ; } private showPokerPlay ( ) { this . clearChileByName ( "pokerPlay" ) ; this . pokerPlay = new PukerPlayContainer ( PukerUtils . sortDescPokers ( this . selectedPokers ) ) ; this . pokerPlay . name = "pokerPlay" ; this . addChild ( this . pokerPlay ) ; } private clearChileByName ( name ) { if ( this . getChildByName ( name ) != null ) { this . removeChild ( this . getChildByName ( name ) ) ; } } /**
     * 显示玩家头像
     */ private showPortrait ( isLandlord ) { this . clearChileByName ( "portrait" ) ; this . portrait = new PortraitContainer ( this . user , 7 , isLandlord ) ; this . portrait . name = "portrait" ; this . addChild ( this . portrait ) ; } private clearOtherPortrait ( seat ) { let name ; if ( this . isLeft ( seat ) ) { name = "portraitLeft" ; } else { name = "portraitRight" ; } if ( this . getChildByName ( name ) != null ) { this . removeChild ( this . getChildByName ( name ) ) ; } } private showOtherPortrait ( seat , isLandlord ) { this . clearOtherPortrait ( seat ) ; if ( this . isLeft ( seat ) ) { this . portraitLeft = new PortraitOtherContainer ( this . leftPlayer , this . poc3Index , this . isLeft ( seat ) , isLandlord ) ; this . portraitLeft . name = "portraitLeft" ; this . addChild ( this . portraitLeft ) ; } else { this . portraitRight = new PortraitOtherContainer ( this . rightPlayer , this . poc3Index , this . isLeft ( seat ) , isLandlord ) ; this . portraitRight . name = "portraitRight" ; this . addChild ( this . portraitRight ) ; } } /**
     * 是否是上家
     */ private isLeft ( seat ) { if ( seat == this . user . getSeat ( ) + 1 || seat == this . user . getSeat ( ) % 3 + 1 ) return false ; return true ; } private createTimer ( delay , times , timerRun = ( times ) => { } , timerEnd = ( ) => { } ) { var timer = new egret . Timer ( delay , times ) ; timer . addEventListener ( egret . TimerEvent . TIMER , timerRun , this ) ; timer . addEventListener ( egret . TimerEvent . TIMER_COMPLETE , timerEnd , this ) ; timer . start ( ) ; } /**
     * 游戏结束，返回大厅
     */ public button_gameOver ( ) { console . log ( "返回大厅" ) ; let gameHall = new GameHall ( this . user ) ; gameHall . name = "gameHall" ; this . parent . addChild ( gameHall ) ; this . parent . removeChild ( this ) ; } /**
     * 再来一局
     */ public button_restart ( ) { let playerP2c = new StandaloneModel ( this . user ) ; this . parent . addChild ( playerP2c ) ; this . parent . removeChild ( this ) ; } /**
     * 显示出牌按钮组
     */ private showButtons ( type ) { this . clearChileByName ( "pokerPlay" ) ; this . clearChileByName ( "buttons" ) ; this . buttons = new PlayerButtonContainer ( this , type ) ; this . buttons . name = "buttons" ; this . addChild ( this . buttons ) ; } public buttonChuPai ( evt ) { if ( PukerTypeUtils . getType ( this . selectedPokers ) == null ) { console . log ( "你这选的是啥呀" ) ; return ; } if ( this . activityPlayerSeat != this . user . getSeat ( ) && this . activityPokers != null && this . activityPokers . length > 0 ) { if ( ! PukerCompareUtils . comparePukers ( this . selectedPokers , this . activityPokers ) ) { console . log ( "没有上家的牌大" ) ; return ; } } let request = new Object ; let body = new Object ; request . code = RoomManager . Request_Discard ; body . roomId = this . roomId ; body . userId = this . user . getId ( ) ; let pokerIds = "" ; for ( let i = 0 ; i < this . selectedPokers . length ; i ++ ) { pokerIds += this . selectedPokers [ i ] . getId ( ) + "," ; } body . pokers = pokerIds ; request . data = body ; this . send ( request ) ; this . clearChileByName ( "buttons" ) ; } public buttonBuYao ( evt ) { if ( this . pokers . length == 20 || this . activityPlayerSeat == this . user . getSeat ( ) ) { console . log ( "-------不能不出-------" ) ; return ; } let request = new Object ; let body = new Object ; request . code = RoomManager . Request_Discard ; body . roomId = this . roomId ; body . userId = this . user . getId ( ) ; request . data = body ; this . send ( request ) ; this . clearChileByName ( "buttons" ) ; } public buttonTiShi ( evt ) { } public pukerClick ( evt ) { let p = RES . getRes ( "layout_json" ) . puker ; let y = p . pukerUpMove ; let draggedObject = evt . currentTarget ; let id = parseInt ( draggedObject . name . split ( "," ) [ 0 ] ) ; let orderValue = parseInt ( draggedObject . name . split ( "," ) [ 1 ] ) ; if ( draggedObject . y == y ) { draggedObject . y = 0 ; this . selectedPokers . push ( new Poker ( id , orderValue ) ) ; } else { draggedObject . y = y ; let poker = new Poker ( id , orderValue ) ; console . log ( "poker" , poker . toString ( ) , "array" , this . selectedPokers ) ; this . selectedPokers = PukerUtils . removePokers ( this . selectedPokers , [ poker ] ) ; console . log ( "removedArray" , this . selectedPokers ) ; } } private showResult ( isVictory ) { if ( this . getChildByName ( "result" ) != null ) { this . removeChild ( this . getChildByName ( "result" ) ) ; } let result = new GameResultContainer ( isVictory ) ; result . name = "result" ; this . addChild ( result ) ; console . log ( "showResult 游戏结束" ) ; this . showButtons ( RoomManager . ButtonsGameOver ) ; } }