GameMng.ts 13 KB

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