import {game,AudioClip,Prefab,Component, _decorator, Game ,director,Node} from "cc"; import { ClientEvent } from "./clientEvent"; import ItemDataConfig, { itemData } from "./config/ItemDataConfig"; import { Constant, item_type } from "./constant"; import GBoardChess from "./Game/ChessGame/GBoardChess"; import { http } from "./Game/http"; import UIDialog from "./Game/UIDialog"; import ADSDK from "./gcommon/AdSdk/ADSDK"; import ScenceMng from "./gcommon/ScenceMng"; import UILoading from "./gcommon/UILoading"; import { UIManager } from "./gcommon/UIManager"; import UITips from "./gcommon/UITips"; import { native_manager } from "./native_manager"; import gameSocket from "./socket/gameSocket"; import { msgManager } from "./socket/msgManager"; import { Tools } from "./Tools"; import { activityData } from "./UserData/activityData"; import { bagData } from "./UserData/bagData"; import { emailData } from "./UserData/emailData"; import { privacyData } from "./UserData/privacyData"; import { roomData } from "./UserData/roomData"; import { userData } from "./UserData/userData"; import { wearData } from "./UserData/wearData"; import { welfareData } from "./UserData/welfareData"; export const enum quit_chess_show_view{ renji, //跳转人机选择界面 haoyou,//跳转创建好友对战 jifen,//跳转积分界面 tongqian,//跳转铜钱场界面 login,//登陆 beitichu, } var check_status = 'wait'; const { ccclass, property } = _decorator; @ccclass export class GameMng extends Component { // @property({ type: Prefab }) // public chessselCell: Prefab | null = null; @property({ type: Prefab }) public uijifen_rule: Prefab | null = null; @property({ type: Prefab }) public uiloading: Prefab | null = null; // @property({ type: Prefab }) // public uisetting: Prefab | null = null; @property({ type: Prefab }) public uitips: Prefab | null = null; @property({ type: Prefab }) public uiduizhan: Prefab | null = null; @property({ type: Prefab }) public uiDialog: Prefab | null = null; @property({ type: Prefab }) public uiWaitView: Prefab | null = null; @property({ type: Prefab }) public uiuserinfo: Prefab | null = null; @property({ type: Prefab }) public uiSettingView: Prefab | null = null; @property({ type: Prefab }) public uiChessSettingView: Prefab | null = null; @property(AudioClip) mainbgm: AudioClip = null; @property(AudioClip) buttonclick: AudioClip = null; @property(AudioClip) startclip: AudioClip = null; @property(AudioClip) winclip: AudioClip = null; @property(AudioClip) failclip: AudioClip = null; @property(AudioClip) eatclip: AudioClip = null; @property(AudioClip) pickclip: AudioClip = null; @property(AudioClip) putclip: AudioClip = null; @property(AudioClip) kingclip: AudioClip = null; @property(AudioClip) kill: AudioClip = null; @property(AudioClip) gamebgm: AudioClip = null; // @property({ type: Prefab }) // public canjuCell: Prefab | null = null; @property({ type: Prefab }) public createFirend: Prefab | null = null; @property({ type: Prefab }) public joinRoom: Prefab | null = null; @property({ type: Prefab }) public shop_view: Prefab | null = null; @property({ type: Prefab }) public bag_view: Prefab | null = null; @property({ type: Prefab }) public email_view: Prefab | null = null; @property({ type: Prefab }) public activity_view: Prefab | null = null; @property({ type: Prefab }) public welfare_view: Prefab | null = null; @property({ type: Prefab }) public music_setting_view: Prefab | null = null; @property({ type: Prefab }) public privacy_setting_view: Prefab | null = null; @property({ type: Prefab }) public about_view: Prefab | null = null; @property({ type: Prefab }) public self_info_view: Prefab | null = null; @property({ type: Prefab }) public invite_firend_view: Prefab | null = null; @property({ type: Prefab }) public send_firend_duizhan: Prefab | null = null; @property({ type: Prefab }) public recv_firend_duizhan: Prefab | null = null; static Instance:GameMng=null; static _userData:userData = null; public loadingLayer: Node | null = null; public quit_view: quit_chess_show_view | null = null; static isFirstRunApp:boolean = false; static reward_tong_bi:boolean = false; static UserDataInstance(){ if(GameMng._userData==null){ GameMng._userData = new userData(); } return GameMng._userData; } static getBagItemList():itemData[]{ var list:itemData[] =[] for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) { const element = GameMng._userData.bag.item_list[index]; if(element.item_type===item_type.dao_ju){ list.push(element) } } return list } static getFirendList():userData[]{ return GameMng._userData.firend_list } static getEmailList():emailData[]{ return GameMng._userData.email_list } static getWelfareList():welfareData[]{ let list = []; for (let index = 0; index < 10; index++) { list.push(new welfareData) } return list } static getActivityList():activityData[]{ let list = []; for (let index = 0; index < 10; index++) { list.push(new activityData) } return list } static getBagQiPanList():itemData[]{ var list:itemData[] =[] for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) { const element = GameMng._userData.bag.item_list[index]; if(element.item_type===item_type.qi_pan){ list.push(element) } } return list } static getBagQiZiList():itemData[]{ var list:itemData[] =[] for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) { const element = GameMng._userData.bag.item_list[index]; if(element.item_type===item_type.qi_zi){ list.push(element) } } return list } static getBagTouXiangKuangList():itemData[]{ var list:itemData[] =[] for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) { const element = GameMng._userData.bag.item_list[index]; if(element.item_type===item_type.tou_xiang_kuang){ list.push(element) } } return list } static getBagJiShiQiList():itemData[]{ var list:itemData[] =[] for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) { const element = GameMng._userData.bag.item_list[index]; if(element.item_type===item_type.ji_shi_qi){ list.push(element) } } return list } static getBagQiPaoList():itemData[]{ var list:itemData[] =[] for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) { const element = GameMng._userData.bag.item_list[index]; if(element.item_type===item_type.qi_pao){ list.push(element) } } return list } static setUserData(_json:userData){ // let str = JSON.stringify(_json) // let obj = JSON.parse(str) // GameMng._userData = obj as userData GameMng._userData = _json GameMng.temp(); } /** @deprecated 💥临时调试使用。记得后面删掉 */ static temp(){ if(GameMng._userData.privacy==null||GameMng._userData.privacy==undefined){ // GameMng._userData.privacy = new privacyData() } // if(GameMng._userData.bag==null||GameMng._userData.bag==undefined){ // // GameMng._userData.bag = new bagData() // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("1001")) // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("1002")) // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("1003")) // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("4000")) // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("5000")) // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("6000")) // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("2000")) // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("3000")) // } if(GameMng._userData.wear==null||GameMng._userData.wear==undefined){ // GameMng._userData.wear = new wearData() } if(GameMng._userData.firend_list==null||GameMng._userData.firend_list==undefined){ // GameMng._userData.firend_list = [] for (let index = 1; index <=10; index++) { let u = new userData() u.user_name = index+"号测试人员" GameMng._userData.firend_list.push(u) } } if(GameMng._userData.email_list==null||GameMng._userData.email_list==undefined){ // GameMng._userData.email_list = [] for (let index = 1; index <=10; index++) { let u = new emailData() u.title = index+"号测试人员" GameMng._userData.email_list.push(u) } } } static add_chat_history(id){ if(GameMng._userData.room!=null){ if(GameMng._userData.room.chat_history_list===null||GameMng._userData.room.chat_history_list===undefined){ GameMng._userData.room.chat_history_list = [] } GameMng._userData.room.chat_history_list.push(id) } } static updateRoomData(room:roomData){ if(GameMng._userData!=null){ if(GameMng._userData.room==null){ GameMng._userData.room = new roomData() } if(room.rival==null){ GameMng._userData.room.rival =null; } // if(room.chess==null){ // GameMng._userData.room.chess =null; // } for (const key in room) { if (Object.prototype.hasOwnProperty.call(room, key)) { const element = room[key]; GameMng._userData.room[key] = element; } } } } static updateUserData(User_Data:userData){ if(GameMng._userData!=null){ for (const key in User_Data) { if (Object.prototype.hasOwnProperty.call(User_Data, key)) { const element = User_Data[key]; GameMng._userData[key] = element; } } } } onLoad(){ director.addPersistRootNode(this.node); var self = this; GameMng.Instance=this; self.unschedule(self.tick) self.schedule(self.tick,2.5) game.on(Game.EVENT_HIDE, function () { console.log("游戏进入后台"); }, this); game.on(Game.EVENT_SHOW, function(){ console.log("游戏进入前台"); native_manager.check_jump_info() ClientEvent.dispatchEvent(Constant.EVENT_TYPE.MSG_HEART_BEAT) //ADSDK.ShareCallBack(); },this); if(!GameMng.isFirstRunApp){ GameMng.isFirstRunApp = true; native_manager.check_jump_info() } ClientEvent.on(Constant.EVENT_TYPE.MSG_EXTRUSION_LINE,this.extrusion_line,this) ClientEvent.on(Constant.EVENT_TYPE.MSG_HEART_BEAT,this.startTick,this) ClientEvent.on(Constant.EVENT_TYPE.MSG_UPDATE_USER_INFO,this.update_user_info,this) } update_user_info(User_Data:userData){ UIManager.removeLoadingLayer() let isUpdateTongQian = false; isUpdateTongQian = User_Data.bag.tong_bi!=GameMng._userData.bag.tong_bi if(User_Data.bag.tong_bi>GameMng._userData.bag.tong_bi){ UITips.show("获取铜钱"+(User_Data.bag.tong_bi -GameMng._userData.bag.tong_bi)) } GameMng.updateUserData(User_Data); if(isUpdateTongQian){ ClientEvent.dispatchEvent(Constant.UI_EVENT.UI_MSG_UPDATE_TONG_QIAN) } } extrusion_line(obj:any){ let name = obj.type===0?"你的账号正在被登陆,是否重新登陆?":"请重新登陆" let isSHow = obj.type===0? true:false gameSocket.Instance.stopConnected = true; gameSocket.Instance.close() UIDialog.Show(()=>{ GameMng.Instance.quit_view = quit_chess_show_view.login ScenceMng.Instance.load('GameLaunch'); },()=>{ },name,null,isSHow) console.log("被挤掉线",obj.type) } startTick(){ this.unschedule(this.tick) if(GameMng._userData!=null){ check_status = 'wait'; gameSocket.Instance.is_online_status = false; this.heartbeat() this.loadingLayer = UILoading.deley_show(2.5) this.scheduleOnce(()=>{ this.heartbeat() if(!gameSocket.Instance.is_online_status){ }else{ console.log("what the fuck?") UIManager.removeLoadingLayer() this.loadingLayer = null; } },1) } this.schedule(this.tick,2.5) } heartbeat(){ if(check_status==='wait'){ gameSocket.Instance.heartbeat(); check_status = 'send' }else{ if(check_status ==='send'){ check_status = 'wait' if(this.check_heartbeat_succeed()){ console.log("心跳成功") if(GameMng.reward_tong_bi){ GameMng.reward_tong_bi = false; msgManager.ad_tongbi(1) } gameSocket.Instance.is_online_status = true; }else{ gameSocket.Instance.is_online_status = false; console.log("心跳失败") // gameSocket.Instance.close() } } // this.scheduleOnce(()=>{ // },2.5) } } check_heartbeat_succeed():boolean{ return gameSocket.Instance.check_heartbeat_succeed; } tick(){ console.log("schedule reConnect",gameSocket.Instance.is_connected) if(!gameSocket.Instance.is_connected||!gameSocket.Instance.is_online_status){ if(this.loadingLayer ===null){ this.loadingLayer = UIManager.AddPrefab(GameMng.Instance.uiloading) }else{ if(gameSocket.Instance.stopConnected){ this.loadingLayer.active = false; } } }else{ if(this.loadingLayer&&gameSocket.Instance.is_online_status){ UIManager.removeLoadingLayer() this.loadingLayer = null; } } if(gameSocket.Instance.is_connected){ this.heartbeat() }else{ msgManager.reConnect() } } }