GameMng.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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(AudioClip)
  60. mainbgm: AudioClip = null;
  61. @property(AudioClip)
  62. buttonclick: AudioClip = null;
  63. @property(AudioClip)
  64. startclip: AudioClip = null;
  65. @property(AudioClip)
  66. winclip: AudioClip = null;
  67. @property(AudioClip)
  68. failclip: AudioClip = null;
  69. @property(AudioClip)
  70. eatclip: AudioClip = null;
  71. @property(AudioClip)
  72. pickclip: AudioClip = null;
  73. @property(AudioClip)
  74. putclip: AudioClip = null;
  75. @property(AudioClip)
  76. kingclip: AudioClip = null;
  77. @property(AudioClip)
  78. kill: AudioClip = null;
  79. @property(AudioClip)
  80. gamebgm: AudioClip = null;
  81. // @property({ type: Prefab })
  82. // public canjuCell: Prefab | null = null;
  83. @property({ type: Prefab })
  84. public createFirend: Prefab | null = null;
  85. @property({ type: Prefab })
  86. public joinRoom: Prefab | null = null;
  87. @property({ type: Prefab })
  88. public shop_view: Prefab | null = null;
  89. @property({ type: Prefab })
  90. public bag_view: Prefab | null = null;
  91. @property({ type: Prefab })
  92. public email_view: Prefab | null = null;
  93. @property({ type: Prefab })
  94. public activity_view: Prefab | null = null;
  95. @property({ type: Prefab })
  96. public welfare_view: Prefab | null = null;
  97. @property({ type: Prefab })
  98. public music_setting_view: Prefab | null = null;
  99. @property({ type: Prefab })
  100. public privacy_setting_view: Prefab | null = null;
  101. @property({ type: Prefab })
  102. public about_view: Prefab | null = null;
  103. @property({ type: Prefab })
  104. public self_info_view: Prefab | null = null;
  105. @property({ type: Prefab })
  106. public invite_firend_view: Prefab | null = null;
  107. @property({ type: Prefab })
  108. public send_firend_duizhan: Prefab | null = null;
  109. @property({ type: Prefab })
  110. public recv_firend_duizhan: Prefab | null = null;
  111. static Instance:GameMng=null;
  112. static _userData:userData = null;
  113. public loadingLayer: Node | null = null;
  114. public quit_view: quit_chess_show_view | null = null;
  115. static isFirstRunApp:boolean = false;
  116. static reward_tong_bi:boolean = false;
  117. static UserDataInstance(){
  118. if(GameMng._userData==null){
  119. GameMng._userData = new userData();
  120. }
  121. return GameMng._userData;
  122. }
  123. static getBagItemList():itemData[]{
  124. var list:itemData[] =[]
  125. for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) {
  126. const element = GameMng._userData.bag.item_list[index];
  127. if(element.item_type===item_type.dao_ju){
  128. list.push(element)
  129. }
  130. }
  131. return list
  132. }
  133. static getFirendList():userData[]{
  134. return GameMng._userData.firend_list
  135. }
  136. static getEmailList():emailData[]{
  137. return GameMng._userData.email_list
  138. }
  139. static getWelfareList():welfareData[]{
  140. let list = [];
  141. for (let index = 0; index < 10; index++) {
  142. list.push(new welfareData)
  143. }
  144. return list
  145. }
  146. static getActivityList():activityData[]{
  147. let list = [];
  148. for (let index = 0; index < 10; index++) {
  149. list.push(new activityData)
  150. }
  151. return list
  152. }
  153. static getBagQiPanList():itemData[]{
  154. var list:itemData[] =[]
  155. for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) {
  156. const element = GameMng._userData.bag.item_list[index];
  157. if(element.item_type===item_type.qi_pan){
  158. list.push(element)
  159. }
  160. }
  161. return list
  162. }
  163. static getBagQiZiList():itemData[]{
  164. var list:itemData[] =[]
  165. for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) {
  166. const element = GameMng._userData.bag.item_list[index];
  167. if(element.item_type===item_type.qi_zi){
  168. list.push(element)
  169. }
  170. }
  171. return list
  172. }
  173. static getBagTouXiangKuangList():itemData[]{
  174. var list:itemData[] =[]
  175. for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) {
  176. const element = GameMng._userData.bag.item_list[index];
  177. if(element.item_type===item_type.tou_xiang_kuang){
  178. list.push(element)
  179. }
  180. }
  181. return list
  182. }
  183. static getBagJiShiQiList():itemData[]{
  184. var list:itemData[] =[]
  185. for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) {
  186. const element = GameMng._userData.bag.item_list[index];
  187. if(element.item_type===item_type.ji_shi_qi){
  188. list.push(element)
  189. }
  190. }
  191. return list
  192. }
  193. static getBagQiPaoList():itemData[]{
  194. var list:itemData[] =[]
  195. for (let index = 0; index < GameMng._userData.bag.item_list.length; index++) {
  196. const element = GameMng._userData.bag.item_list[index];
  197. if(element.item_type===item_type.qi_pao){
  198. list.push(element)
  199. }
  200. }
  201. return list
  202. }
  203. static setUserData(_json:userData){
  204. // let str = JSON.stringify(_json)
  205. // let obj = JSON.parse(str)
  206. // GameMng._userData = obj as userData
  207. GameMng._userData = _json
  208. GameMng.temp();
  209. }
  210. /** @deprecated 💥临时调试使用。记得后面删掉 */
  211. static temp(){
  212. if(GameMng._userData.privacy==null||GameMng._userData.privacy==undefined){ //
  213. GameMng._userData.privacy = new privacyData()
  214. }
  215. // if(GameMng._userData.bag==null||GameMng._userData.bag==undefined){ //
  216. // GameMng._userData.bag = new bagData()
  217. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("1001"))
  218. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("1002"))
  219. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("1003"))
  220. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("4000"))
  221. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("5000"))
  222. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("6000"))
  223. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("2000"))
  224. // GameMng._userData.bag.item_list.push(ItemDataConfig.Instance.getItemById("3000"))
  225. // }
  226. if(GameMng._userData.wear==null||GameMng._userData.wear==undefined){ //
  227. GameMng._userData.wear = new wearData()
  228. }
  229. if(GameMng._userData.firend_list==null||GameMng._userData.firend_list==undefined){ //
  230. GameMng._userData.firend_list = []
  231. for (let index = 1; index <=10; index++) {
  232. let u = new userData()
  233. u.user_name = index+"号测试人员"
  234. GameMng._userData.firend_list.push(u)
  235. }
  236. }
  237. if(GameMng._userData.email_list==null||GameMng._userData.email_list==undefined){ //
  238. GameMng._userData.email_list = []
  239. for (let index = 1; index <=10; index++) {
  240. let u = new emailData()
  241. u.title = index+"号测试人员"
  242. GameMng._userData.email_list.push(u)
  243. }
  244. }
  245. }
  246. static add_chat_history(id){
  247. if(GameMng._userData.room!=null){
  248. if(GameMng._userData.room.chat_history_list===null||GameMng._userData.room.chat_history_list===undefined){
  249. GameMng._userData.room.chat_history_list = []
  250. }
  251. GameMng._userData.room.chat_history_list.push(id)
  252. }
  253. }
  254. static updateRoomData(room:roomData){
  255. if(GameMng._userData!=null){
  256. if(GameMng._userData.room==null){
  257. GameMng._userData.room = new roomData()
  258. }
  259. if(room.rival==null){
  260. GameMng._userData.room.rival =null;
  261. }
  262. // if(room.chess==null){
  263. // GameMng._userData.room.chess =null;
  264. // }
  265. for (const key in room) {
  266. if (Object.prototype.hasOwnProperty.call(room, key)) {
  267. const element = room[key];
  268. GameMng._userData.room[key] = element;
  269. }
  270. }
  271. }
  272. }
  273. static updateUserData(User_Data:userData){
  274. if(GameMng._userData!=null){
  275. for (const key in User_Data) {
  276. if (Object.prototype.hasOwnProperty.call(User_Data, key)) {
  277. const element = User_Data[key];
  278. GameMng._userData[key] = element;
  279. }
  280. }
  281. }
  282. }
  283. onLoad(){
  284. director.addPersistRootNode(this.node);
  285. var self = this;
  286. GameMng.Instance=this;
  287. self.unschedule(self.tick)
  288. self.schedule(self.tick,2.5)
  289. game.on(Game.EVENT_HIDE, function () {
  290. console.log("游戏进入后台");
  291. }, this);
  292. game.on(Game.EVENT_SHOW, function(){
  293. console.log("游戏进入前台");
  294. native_manager.check_jump_info()
  295. ClientEvent.dispatchEvent(Constant.EVENT_TYPE.MSG_HEART_BEAT)
  296. //ADSDK.ShareCallBack();
  297. },this);
  298. if(!GameMng.isFirstRunApp){
  299. GameMng.isFirstRunApp = true;
  300. native_manager.check_jump_info()
  301. }
  302. ClientEvent.on(Constant.EVENT_TYPE.MSG_EXTRUSION_LINE,this.extrusion_line,this)
  303. ClientEvent.on(Constant.EVENT_TYPE.MSG_HEART_BEAT,this.startTick,this)
  304. ClientEvent.on(Constant.EVENT_TYPE.MSG_UPDATE_USER_INFO,this.update_user_info,this)
  305. }
  306. update_user_info(User_Data:userData){
  307. UIManager.removeLoadingLayer()
  308. let isUpdateTongQian = false;
  309. isUpdateTongQian = User_Data.bag.tong_bi!=GameMng._userData.bag.tong_bi
  310. if(User_Data.bag.tong_bi>GameMng._userData.bag.tong_bi){
  311. UITips.show("获取铜钱"+(User_Data.bag.tong_bi -GameMng._userData.bag.tong_bi))
  312. }
  313. GameMng.updateUserData(User_Data);
  314. if(isUpdateTongQian){
  315. ClientEvent.dispatchEvent(Constant.UI_EVENT.UI_MSG_UPDATE_TONG_QIAN)
  316. }
  317. }
  318. extrusion_line(obj:any){
  319. let name = obj.type===0?"你的账号正在被登陆,是否重新登陆?":"请重新登陆"
  320. let isSHow = obj.type===0? true:false
  321. gameSocket.Instance.stopConnected = true;
  322. gameSocket.Instance.close()
  323. UIDialog.Show(()=>{
  324. GameMng.Instance.quit_view = quit_chess_show_view.login
  325. ScenceMng.Instance.load('GameLaunch');
  326. },()=>{
  327. },name,null,isSHow)
  328. console.log("被挤掉线",obj.type)
  329. }
  330. startTick(){
  331. this.unschedule(this.tick)
  332. if(GameMng._userData!=null){
  333. check_status = 'wait';
  334. gameSocket.Instance.is_online_status = false;
  335. this.heartbeat()
  336. this.loadingLayer = UILoading.deley_show(2.5)
  337. this.scheduleOnce(()=>{
  338. this.heartbeat()
  339. if(!gameSocket.Instance.is_online_status){
  340. }else{
  341. console.log("what the fuck?")
  342. UIManager.removeLoadingLayer()
  343. this.loadingLayer = null;
  344. }
  345. },1)
  346. }
  347. this.schedule(this.tick,2.5)
  348. }
  349. heartbeat(){
  350. if(check_status==='wait'){
  351. gameSocket.Instance.heartbeat();
  352. check_status = 'send'
  353. }else{
  354. if(check_status ==='send'){
  355. check_status = 'wait'
  356. if(this.check_heartbeat_succeed()){
  357. console.log("心跳成功")
  358. if(GameMng.reward_tong_bi){
  359. GameMng.reward_tong_bi = false;
  360. msgManager.ad_tongbi(1)
  361. }
  362. gameSocket.Instance.is_online_status = true;
  363. }else{
  364. gameSocket.Instance.is_online_status = false;
  365. console.log("心跳失败")
  366. // gameSocket.Instance.close()
  367. }
  368. }
  369. // this.scheduleOnce(()=>{
  370. // },2.5)
  371. }
  372. }
  373. check_heartbeat_succeed():boolean{
  374. return gameSocket.Instance.check_heartbeat_succeed;
  375. }
  376. tick(){
  377. console.log("schedule reConnect",gameSocket.Instance.is_connected)
  378. if(!gameSocket.Instance.is_connected||!gameSocket.Instance.is_online_status){
  379. if(this.loadingLayer ===null){
  380. this.loadingLayer = UIManager.AddPrefab(GameMng.Instance.uiloading)
  381. }else{
  382. if(gameSocket.Instance.stopConnected){
  383. this.loadingLayer.active = false;
  384. }
  385. }
  386. }else{
  387. if(this.loadingLayer&&gameSocket.Instance.is_online_status){
  388. UIManager.removeLoadingLayer()
  389. this.loadingLayer = null;
  390. }
  391. }
  392. if(gameSocket.Instance.is_connected){
  393. this.heartbeat()
  394. }else{
  395. msgManager.reConnect()
  396. }
  397. }
  398. }