GameMng.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. import {game,AudioClip,Prefab,Component, _decorator, Game ,director,Node} from "cc";
  2. import { ClientEvent } from "./clientEvent";
  3. import ItemDataConfig, { itemData } from "./config/ItemDataConfig";
  4. import { Constant, item_type } from "./constant";
  5. import GBoardChess from "./Game/ChessGame/GBoardChess";
  6. import { http } from "./Game/http";
  7. import UIDialog from "./Game/UIDialog";
  8. import ADSDK from "./gcommon/AdSdk/ADSDK";
  9. import ScenceMng from "./gcommon/ScenceMng";
  10. import UILoading from "./gcommon/UILoading";
  11. import { UIManager } from "./gcommon/UIManager";
  12. import UITips from "./gcommon/UITips";
  13. import { native_manager } from "./native_manager";
  14. import gameSocket from "./socket/gameSocket";
  15. import { msgManager } from "./socket/msgManager";
  16. import { Tools } from "./Tools";
  17. import { activityData } from "./UserData/activityData";
  18. import { bagData } from "./UserData/bagData";
  19. import { emailData } from "./UserData/emailData";
  20. import { privacyData } from "./UserData/privacyData";
  21. import { roomData } from "./UserData/roomData";
  22. import { userData } from "./UserData/userData";
  23. import { wearData } from "./UserData/wearData";
  24. import { welfareData } from "./UserData/welfareData";
  25. export const enum quit_chess_show_view{
  26. renji, //跳转人机选择界面
  27. haoyou,//跳转创建好友对战
  28. jifen,//跳转积分界面
  29. tongqian,//跳转铜钱场界面
  30. login,//登陆
  31. beitichu,
  32. }
  33. var check_status = 'wait';
  34. const { ccclass, property } = _decorator;
  35. @ccclass
  36. export class GameMng extends Component {
  37. // @property({ type: Prefab })
  38. // public chessselCell: Prefab | null = null;
  39. @property({ type: Prefab })
  40. public uijifen_rule: Prefab | null = null;
  41. @property({ type: Prefab })
  42. public uiloading: Prefab | null = null;
  43. // @property({ type: Prefab })
  44. // public uisetting: Prefab | null = null;
  45. @property({ type: Prefab })
  46. public uitips: Prefab | null = null;
  47. @property({ type: Prefab })
  48. public uiduizhan: Prefab | null = null;
  49. @property({ type: Prefab })
  50. public uiDialog: Prefab | null = null;
  51. @property({ type: Prefab })
  52. public uiWaitView: Prefab | null = null;
  53. @property({ type: Prefab })
  54. public uiuserinfo: Prefab | null = null;
  55. @property({ type: Prefab })
  56. public uiSettingView: Prefab | null = null;
  57. @property({ type: Prefab })
  58. public uiChessSettingView: Prefab | null = null;
  59. @property({ type: Prefab })
  60. public friend_list_view: Prefab | null = null;
  61. @property({ type: Prefab })
  62. public rank_view: Prefab | null = null;
  63. @property(AudioClip)
  64. mainbgm: AudioClip = null;
  65. @property(AudioClip)
  66. buttonclick: AudioClip = null;
  67. @property(AudioClip)
  68. startclip: AudioClip = null;
  69. @property(AudioClip)
  70. winclip: AudioClip = null;
  71. @property(AudioClip)
  72. failclip: AudioClip = null;
  73. @property(AudioClip)
  74. eatclip: AudioClip = null;
  75. @property(AudioClip)
  76. pickclip: AudioClip = null;
  77. @property(AudioClip)
  78. putclip: AudioClip = null;
  79. @property(AudioClip)
  80. kingclip: AudioClip = null;
  81. @property(AudioClip)
  82. kill: AudioClip = null;
  83. @property(AudioClip)
  84. gamebgm: AudioClip = null;
  85. // @property({ type: Prefab })
  86. // public canjuCell: Prefab | null = null;
  87. @property({ type: Prefab })
  88. public createFirend: Prefab | null = null;
  89. @property({ type: Prefab })
  90. public joinRoom: Prefab | null = null;
  91. @property({ type: Prefab })
  92. public shop_view: Prefab | null = null;
  93. @property({ type: Prefab })
  94. public bag_view: Prefab | null = null;
  95. @property({ type: Prefab })
  96. public email_view: Prefab | null = null;
  97. @property({ type: Prefab })
  98. public activity_view: Prefab | null = null;
  99. @property({ type: Prefab })
  100. public welfare_view: Prefab | null = null;
  101. @property({ type: Prefab })
  102. public music_setting_view: Prefab | null = null;
  103. @property({ type: Prefab })
  104. public privacy_setting_view: Prefab | null = null;
  105. @property({ type: Prefab })
  106. public about_view: Prefab | null = null;
  107. @property({ type: Prefab })
  108. public self_info_view: Prefab | null = null;
  109. @property({ type: Prefab })
  110. public invite_firend_view: Prefab | null = null;
  111. @property({ type: Prefab })
  112. public send_firend_duizhan: Prefab | null = null;
  113. @property({ type: Prefab })
  114. public recv_firend_duizhan: Prefab | null = null;
  115. @property({ type: Prefab })
  116. public yue_zhan_view: Prefab | null = null;
  117. static Instance:GameMng=null;
  118. static _userData:userData = null;
  119. public loadingLayer: Node | null = null;
  120. public quit_view: quit_chess_show_view | null = null;
  121. static isFirstRunApp:boolean = false;
  122. static reward_tong_bi:boolean = false;
  123. static firendOpenId:string = "";
  124. static UserDataInstance(){
  125. if(GameMng._userData==null){
  126. GameMng._userData = new userData();
  127. }
  128. return GameMng._userData;
  129. }
  130. static isMyFirend(user_id:number){
  131. let isF = false;
  132. for (let index = 0; index < GameMng._userData.friend_list.length; index++) {
  133. if(user_id== GameMng._userData.friend_list[index].userid){
  134. isF = true;
  135. break;
  136. }
  137. }
  138. for (let index = 0; index < GameMng._userData.online_friend_list.length; index++) {
  139. if(user_id== GameMng._userData.online_friend_list[index].userid){
  140. isF = true;
  141. break;
  142. }
  143. }
  144. return isF;
  145. }
  146. static getBagItemList():itemData[]{
  147. var list:itemData[] =[]
  148. for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) {
  149. const element = GameMng._userData.bag.item_list[index];
  150. if(element.item_type===item_type.dao_ju){
  151. list.push(element)
  152. }
  153. }
  154. return list
  155. }
  156. static getFirendRankList():userData[]{
  157. let n_list = GameMng._userData.friend_list
  158. n_list.push(GameMng._userData)
  159. return n_list.sort((a,b)=>{ return b.integral-a.integral;})
  160. }
  161. static getFirendList():userData[]{
  162. return GameMng._userData.friend_list
  163. }
  164. static getFriedDataById(friend:number){
  165. for (let index = 0; index < GameMng._userData.online_friend_list.length; index++) {
  166. const element = GameMng._userData.online_friend_list[index];
  167. if(element.userid ===friend){
  168. return element
  169. }
  170. }
  171. return null;
  172. }
  173. static getEmailList():emailData[]{
  174. return GameMng._userData.email_list
  175. }
  176. static getWelfareList():welfareData[]{
  177. let list = [];
  178. for (let index = 0; index < 10; index++) {
  179. list.push(new welfareData)
  180. }
  181. return list
  182. }
  183. static getActivityList():activityData[]{
  184. let list = [];
  185. for (let index = 0; index < 10; index++) {
  186. list.push(new activityData)
  187. }
  188. return list
  189. }
  190. static getBagQiPanList():itemData[]{
  191. var list:itemData[] =[]
  192. for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) {
  193. const element = GameMng._userData.bag.item_list[index];
  194. if(element.item_type===item_type.qi_pan){
  195. list.push(element)
  196. }
  197. }
  198. return list
  199. }
  200. static getBagQiZiList():itemData[]{
  201. var list:itemData[] =[]
  202. for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) {
  203. const element = GameMng._userData.bag.item_list[index];
  204. if(element.item_type===item_type.qi_zi){
  205. list.push(element)
  206. }
  207. }
  208. return list
  209. }
  210. static getBagTouXiangKuangList():itemData[]{
  211. var list:itemData[] =[]
  212. for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) {
  213. const element = GameMng._userData.bag.item_list[index];
  214. if(element.item_type===item_type.tou_xiang_kuang){
  215. list.push(element)
  216. }
  217. }
  218. return list
  219. }
  220. static getBagJiShiQiList():itemData[]{
  221. var list:itemData[] =[]
  222. for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) {
  223. const element = GameMng._userData.bag.item_list[index];
  224. if(element.item_type===item_type.ji_shi_qi){
  225. list.push(element)
  226. }
  227. }
  228. return list
  229. }
  230. static getBagQiPaoList():itemData[]{
  231. var list:itemData[] =[]
  232. for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) {
  233. const element = GameMng._userData.bag.item_list[index];
  234. if(element.item_type===item_type.qi_pao){
  235. list.push(element)
  236. }
  237. }
  238. return list
  239. }
  240. static setUserData(_json:userData){
  241. // let str = JSON.stringify(_json)
  242. // let obj = JSON.parse(str)
  243. // GameMng._userData = obj as userData
  244. GameMng._userData = _json
  245. GameMng._userData.friend_list = []
  246. GameMng._userData.online_friend_list = []
  247. GameMng.temp();
  248. }
  249. /** @deprecated 💥临时调试使用。记得后面删掉 */
  250. static temp(){
  251. if(GameMng._userData.privacy==null||GameMng._userData.privacy==undefined){ //
  252. GameMng._userData.privacy = new privacyData()
  253. }
  254. // if(GameMng._userData.bag==null||GameMng._userData.bag==undefined){ //
  255. // GameMng._userData.bag = new bagData()
  256. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("1001"))
  257. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("1002"))
  258. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("1003"))
  259. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("4000"))
  260. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("5000"))
  261. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("6000"))
  262. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("2000"))
  263. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("3000"))
  264. // }
  265. if(GameMng._userData.wear==null||GameMng._userData.wear==undefined){ //
  266. GameMng._userData.wear = new wearData()
  267. }
  268. // if(GameMng._userData.firend_list==null||GameMng._userData.firend_list==undefined){ //
  269. // GameMng._userData.firend_list = []
  270. // for (let index = 1; index <=10; index++) {
  271. // let u = new userData()
  272. // u.user_name = index+"号测试人员"
  273. // GameMng._userData.firend_list.push(u)
  274. // }
  275. // }
  276. if(GameMng._userData.email_list==null||GameMng._userData.email_list==undefined){ //
  277. GameMng._userData.email_list = []
  278. for (let index = 1; index <=10; index++) {
  279. let u = new emailData()
  280. u.title = index+"号测试人员"
  281. GameMng._userData.email_list.push(u)
  282. }
  283. }
  284. }
  285. static add_chat_history(id){
  286. if(GameMng._userData.room!=null){
  287. if(GameMng._userData.room.chat_history_list===null||GameMng._userData.room.chat_history_list===undefined){
  288. GameMng._userData.room.chat_history_list = []
  289. }
  290. GameMng._userData.room.chat_history_list.push(id)
  291. }
  292. }
  293. static updateRoomData(room:roomData){
  294. if(GameMng._userData!=null){
  295. if(GameMng._userData.room==null){
  296. GameMng._userData.room = new roomData()
  297. }
  298. if(room.rival==null){
  299. GameMng._userData.room.rival =null;
  300. }
  301. // if(room.chess==null){
  302. // GameMng._userData.room.chess =null;
  303. // }
  304. for (const key in room) {
  305. if (Object.prototype.hasOwnProperty.call(room, key)) {
  306. const element = room[key];
  307. GameMng._userData.room[key] = element;
  308. }
  309. }
  310. }
  311. }
  312. static updateUserData(User_Data:userData){
  313. if(GameMng._userData!=null){
  314. for (const key in User_Data) {
  315. if (Object.prototype.hasOwnProperty.call(User_Data, key)) {
  316. const element = User_Data[key];
  317. GameMng._userData[key] = element;
  318. }
  319. }
  320. }
  321. }
  322. onLoad(){
  323. director.addPersistRootNode(this.node);
  324. var self = this;
  325. GameMng.Instance=this;
  326. self.unschedule(self.tick)
  327. self.schedule(self.tick,2.5)
  328. game.on(Game.EVENT_HIDE, function () {
  329. console.log("游戏进入后台");
  330. }, this);
  331. game.on(Game.EVENT_SHOW, function(){
  332. console.log("游戏进入前台");
  333. native_manager.check_jump_info()
  334. ClientEvent.dispatchEvent(Constant.EVENT_TYPE.MSG_HEART_BEAT)
  335. //ADSDK.ShareCallBack();
  336. },this);
  337. if(!GameMng.isFirstRunApp){
  338. GameMng.isFirstRunApp = true;
  339. native_manager.check_jump_info()
  340. }
  341. ClientEvent.on(Constant.EVENT_TYPE.MSG_EXTRUSION_LINE,this.extrusion_line,this)
  342. ClientEvent.on(Constant.EVENT_TYPE.MSG_HEART_BEAT,this.startTick,this)
  343. ClientEvent.on(Constant.EVENT_TYPE.MSG_UPDATE_USER_INFO,this.update_user_info,this)
  344. ClientEvent.on(Constant.EVENT_TYPE.MSG_GET_ONLINE_FIREND_LIST,this.get_online_firend_list,this)
  345. ClientEvent.on(Constant.EVENT_TYPE.MSG_GET_FIREND_LIST,this.get_firend_list,this)
  346. }
  347. get_firend_list(friend_list:userData[]){
  348. GameMng._userData.friend_list = friend_list;
  349. ClientEvent.dispatchEvent(Constant.UI_EVENT.UI_MSG_UPDATE_FIREND_LIST);
  350. }
  351. get_online_firend_list(online_friend_list:userData[]){
  352. GameMng._userData.online_friend_list = online_friend_list;
  353. ClientEvent.dispatchEvent(Constant.UI_EVENT.UI_MSG_UPDATE_ONLINE_FIREND_LIST);
  354. }
  355. update_user_info(User_Data:userData){
  356. UIManager.removeLoadingLayer()
  357. let isUpdateTongQian = false;
  358. isUpdateTongQian = User_Data.bag.tong_bi!=GameMng._userData.bag.tong_bi
  359. if(User_Data.bag.tong_bi>GameMng._userData.bag.tong_bi){
  360. UITips.show("获取铜钱"+(User_Data.bag.tong_bi -GameMng._userData.bag.tong_bi))
  361. }
  362. GameMng.updateUserData(User_Data);
  363. if(isUpdateTongQian){
  364. ClientEvent.dispatchEvent(Constant.UI_EVENT.UI_MSG_UPDATE_TONG_QIAN)
  365. }
  366. }
  367. extrusion_line(obj:any){
  368. let name = obj.type===0?"你的账号正在被登陆,是否重新登陆?":"请重新登陆"
  369. let isSHow = obj.type===0? true:false
  370. gameSocket.Instance.stopConnected = true;
  371. gameSocket.Instance.close()
  372. UIDialog.Show(()=>{
  373. GameMng.Instance.quit_view = quit_chess_show_view.login
  374. ScenceMng.Instance.load('GameLaunch');
  375. },()=>{
  376. },name,null,isSHow)
  377. console.log("被挤掉线",obj.type)
  378. }
  379. startTick(){
  380. this.unschedule(this.tick)
  381. if(GameMng._userData!=null){
  382. check_status = 'wait';
  383. gameSocket.Instance.is_online_status = false;
  384. this.heartbeat()
  385. this.loadingLayer = UILoading.deley_show(2.5)
  386. this.scheduleOnce(()=>{
  387. this.heartbeat()
  388. if(!gameSocket.Instance.is_online_status){
  389. }else{
  390. console.log("what the fuck?")
  391. UIManager.removeLoadingLayer()
  392. this.loadingLayer = null;
  393. }
  394. },1)
  395. }
  396. this.schedule(this.tick,2.5)
  397. }
  398. heartbeat(){
  399. if(check_status==='wait'){
  400. gameSocket.Instance.heartbeat();
  401. check_status = 'send'
  402. }else{
  403. if(check_status ==='send'){
  404. check_status = 'wait'
  405. if(this.check_heartbeat_succeed()){
  406. console.log("心跳成功")
  407. gameSocket.Instance.is_online_status = true;
  408. }else{
  409. gameSocket.Instance.is_online_status = false;
  410. console.log("心跳失败")
  411. // gameSocket.Instance.close()
  412. }
  413. }
  414. // this.scheduleOnce(()=>{
  415. // },2.5)
  416. }
  417. }
  418. check_heartbeat_succeed():boolean{
  419. return gameSocket.Instance.check_heartbeat_succeed;
  420. }
  421. tick(){
  422. console.log("schedule reConnect",gameSocket.Instance.is_connected)
  423. if(!gameSocket.Instance.is_connected||!gameSocket.Instance.is_online_status){
  424. if(this.loadingLayer ===null){
  425. this.loadingLayer = UIManager.AddPrefab(GameMng.Instance.uiloading)
  426. }else{
  427. if(gameSocket.Instance.stopConnected){
  428. this.loadingLayer.active = false;
  429. }
  430. }
  431. }else{
  432. if(this.loadingLayer&&gameSocket.Instance.is_online_status){
  433. UIManager.removeLoadingLayer()
  434. this.loadingLayer = null;
  435. }
  436. }
  437. if(gameSocket.Instance.is_connected){
  438. this.heartbeat()
  439. }else{
  440. msgManager.reConnect()
  441. }
  442. }
  443. }