GameProcessManager.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. import Connect from './Connect';
  2. import onfire from '../framework/onfire';
  3. import Common from '../common/Common';
  4. import Event from '../net/Event';
  5. import ConstMsgId from '../net/ConstMsgId';
  6. import LogicUnitManager from '../framework/LogicUnitManager';
  7. import DataModelManager from '../framework/DataModelManager';
  8. import NetConfig from '../net/NetConfig';
  9. import Define from '../common/Define';
  10. cc.game.config.showFPS = false
  11. export default class GameProcessManager
  12. {
  13. constructor()
  14. {
  15. cc.log("constructor GameProcessManager start ")
  16. if( GameProcessManager.unique !== undefined ){
  17. return GameProcessManager.unique;
  18. }
  19. if(CC_WECHATGAME)
  20. {
  21. // H5SDK.init({
  22. // wxappid: Common.appid,
  23. // gameVersion:'1.0.3'
  24. // });
  25. } //初始化监听回调函数
  26. this._gameConnect = new Connect("",false)
  27. this._lum = LogicUnitManager;
  28. this._ddm = DataModelManager;
  29. this.socketType = Define.SOCKET_TYPE.GAME
  30. this.playerInfoModel = this._ddm.getModel('PlayerInfoModel');
  31. //this.loadProto();
  32. this._socketConnectOkCallback = null
  33. // if(CC_JSB && cc.sys.OS_ANDROID === cc.sys.os)
  34. // {
  35. // if(NetConfig.AndroidMainAddress.length<=1)
  36. // {
  37. // this.createSocket(NetConfig.AndroidMainAddress[0].address,null,Define.SOCKET_TYPE.PLAZA);
  38. // }
  39. // else
  40. // {
  41. // var idx = Math.floor((Math.random() * NetConfig.AndroidMainAddress.length)) ;
  42. // this.createSocket(NetConfig.AndroidMainAddress[idx].address,null,Define.SOCKET_TYPE.PLAZA);
  43. // }
  44. // }
  45. // else
  46. // {
  47. // if(NetConfig.mainAddress.length<=1)
  48. // {
  49. // this.createSocket(NetConfig.mainAddress[0].address,null,Define.SOCKET_TYPE.PLAZA);
  50. // }
  51. // else
  52. // {
  53. // var idx = Math.floor((Math.random() * NetConfig.mainAddress.length)) ;
  54. // this.createSocket(NetConfig.mainAddress[idx].address,null,Define.SOCKET_TYPE.PLAZA);
  55. // }
  56. // }
  57. cc.log("constructor GameProcessManager end ")
  58. GameProcessManager.unique = this
  59. //this._onProtoLoaded()
  60. }
  61. createSocket(url,successCallback,socketType)
  62. {
  63. // if(this._gameConnect)
  64. // {
  65. // this._gameConnect.close()
  66. // }
  67. this.socketType = socketType
  68. this._socketConnectOkCallback = successCallback
  69. this._gameConnect._url = url
  70. this._gameConnect.open(false);
  71. this._gameConnect._socketType = socketType
  72. this._gameConnect.off(Event.SOCKET_MSG.DATA)
  73. this._gameConnect.off(Event.SOCKET_MSG.OPEN)
  74. this._gameConnect.off(Event.SOCKET_MSG.RECONNECT_START)
  75. this._gameConnect.off(Event.SOCKET_MSG.RECONNECT_OK)
  76. this._gameConnect.on(Event.SOCKET_MSG.DATA,this.onData.bind(this),this)
  77. this._gameConnect.on(Event.SOCKET_MSG.OPEN,this.onOpen.bind(this),this)
  78. this._gameConnect.on(Event.SOCKET_MSG.RECONNECT_START,this.onReConnectStart.bind(this),this)
  79. this._gameConnect.on(Event.SOCKET_MSG.RECONNECT_OK,this.onConnectOk.bind(this),this)
  80. }
  81. closeSocket()
  82. {
  83. this._gameConnect.close()
  84. }
  85. getSocketState()
  86. {
  87. return this._gameConnect.getConnectState()
  88. }
  89. onData(sender,msg)
  90. {
  91. var wMainCmdID = msg[0]
  92. var wSubCmdID = msg[1]
  93. var buf = msg[2]
  94. // console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%');
  95. // Common.Print("GameProcessManager onData msg wMainCmdID :" + wMainCmdID + "wSubCmdID:" + wSubCmdID);
  96. // if(wMainCmdID === 0 && wSubCmdID === 1)
  97. // {
  98. // this._gameConnect.close()
  99. // }
  100. if(this.socketType === Define.SOCKET_TYPE.PLAZA ) // 大厅socket
  101. {
  102. if (ConstMsgId.MDM_MB_LOGON === wMainCmdID) {
  103. if (ConstMsgId.SUB_MB_LOGON_WX === wSubCmdID) {
  104. var str = Common.ab2str(buf)
  105. Common.InspectPrint(str)
  106. this._lum.getBehavior('LoginBehavior').setData(wMainCmdID,wSubCmdID,str)
  107. this._lum.getBehavior('LoginBehavior').emit(Event.COM_MSG.AUTH_OK,str)
  108. } else if (ConstMsgId.SUB_MB_LOGON_SUCCESS === wSubCmdID) {
  109. this._lum.getBehavior('LoginBehavior').setData(wMainCmdID,wSubCmdID,buf)
  110. this._lum.getBehavior('LoginBehavior').emit(Event.CPT_MSG.LOGON_SUCCESS,buf)
  111. } else if (ConstMsgId.SUB_MB_LOGON_FAILURE === wSubCmdID) {
  112. this._lum.getBehavior('LoginBehavior').setData(wMainCmdID,wSubCmdID,buf);
  113. }
  114. } else if (ConstMsgId.MDM_GP_LOGON === wMainCmdID) {
  115. this._lum.getBehavior('LoginBehavior').setData(wMainCmdID,wSubCmdID,buf)
  116. } else if (ConstMsgId.MDM_MB_SERVER_LIST === wMainCmdID) {
  117. if (ConstMsgId.SUB_MB_LIST_SERVER === wSubCmdID) {
  118. this._lum.getBehavior('BattleBehavior').setData(wMainCmdID,wSubCmdID,buf)
  119. this._lum.getBehavior('BattleBehavior').emit(Event.CPT_MSG.SERVER_LIST,buf)
  120. }
  121. } else if (ConstMsgId.MDM_GP_USER_SERVICE === wMainCmdID) {
  122. if (ConstMsgId.SUB_GP_GROWLEVEL_PARAMETER === wSubCmdID) {
  123. this._lum.getBehavior('LoginBehavior').setData(wMainCmdID, wSubCmdID, buf);
  124. }
  125. }
  126. }
  127. else if(this.socketType === Define.SOCKET_TYPE.GAME) //游戏socket
  128. {
  129. if (ConstMsgId.MDM_GR_LOGON === wMainCmdID)//
  130. {
  131. console.log("ConstMsgId.MDM_GR_LOGON")
  132. this._lum.getBehavior('BattleBehavior').setData(wMainCmdID,wSubCmdID,buf)
  133. }
  134. else if (ConstMsgId.MDM_GR_CONFIG === wMainCmdID)//
  135. {
  136. this._lum.getBehavior('BattleBehavior').setData(wMainCmdID,wSubCmdID,buf)
  137. }
  138. else if (ConstMsgId.MDM_GR_USER === wMainCmdID)//
  139. {
  140. this._lum.getBehavior('BattleBehavior').setData(wMainCmdID,wSubCmdID,buf)
  141. }
  142. else if(ConstMsgId.MDM_GR_STATUS === wMainCmdID) //桌子状态
  143. {
  144. this._lum.getBehavior('BattleBehavior').setData(wMainCmdID,wSubCmdID,buf)
  145. }
  146. else if(ConstMsgId.MDM_GF_FRAME === wMainCmdID) //游戏状态
  147. {
  148. this._lum.getBehavior('BattleBehavior').setData(wMainCmdID,wSubCmdID,buf)
  149. }
  150. else if(ConstMsgId.MDM_GF_GAME === wMainCmdID)// 游戏协议
  151. {
  152. this._lum.getBehavior('BattleBehavior').setData(wMainCmdID,wSubCmdID,buf)
  153. }
  154. else if(ConstMsgId.MDM_GR_INSURE === wMainCmdID)
  155. {
  156. this._lum.getBehavior('BattleBehavior').setData(wMainCmdID,wSubCmdID,buf)
  157. }
  158. }
  159. // else if (msgId == ConstMsgId.SC.StartRoundNotify)// 开始第n轮(第n题)答题
  160. // {
  161. // this._lum.getBehavior('BattleBehavior').emit(Event.CPT_MSG.ROUND_START,pb);
  162. // }
  163. // else if (msgId == ConstMsgId.SC.AnswerResponse)// 收到自己第n轮(第n题)答题反馈
  164. // {
  165. // this._lum.getBehavior('BattleBehavior').setData(msgId, pb);
  166. // this._lum.getBehavior('BattleBehavior').emit(Event.CPT_MSG.ANSWER_RESPONCE,pb);
  167. // }
  168. // else if (msgId == ConstMsgId.SC.RoundAnswerEndNotify)// 收到第n轮答题结束
  169. // {
  170. // this._lum.getBehavior('BattleBehavior').setData(msgId, pb);
  171. // this._lum.getBehavior('BattleBehavior').emit(Event.CPT_MSG.ROUND_END,pb);
  172. // }
  173. // else if (msgId == ConstMsgId.SC.AnswerBattleEndingNotify)// 收到答题对局结束
  174. // {
  175. // this._lum.getBehavior('BattleBehavior').setData(msgId, pb);
  176. // this._lum.getBehavior('BattleBehavior').emit(Event.CPT_MSG.BATTLE_END,pb);
  177. // }
  178. // else if (msgId == ConstMsgId.SC.UnMatchResponse)
  179. // {
  180. // this._lum.getBehavior('BattleBehavior').emit(Event.CPT_MSG.UNMATCH,pb);
  181. // }
  182. // else if (msgId == ConstMsgId.SC.BattleAgainResponse)
  183. // {
  184. // this._lum.getBehavior('BattleBehavior').emit(Event.CPT_MSG.BATTLE_AGAIN, pb);
  185. // }
  186. // else if (msgId == ConstMsgId.SC.LeaveBattleResponse)
  187. // {
  188. // this._lum.getBehavior('BattleBehavior').emit(Event.CPT_MSG.LEAVE_BATTLE, pb);
  189. // }
  190. // else if (msgId == ConstMsgId.SC.PlayerLeaveNotify)
  191. // {
  192. // this._lum.getBehavior('BattleBehavior').emit(Event.CPT_MSG.PLAVER_LEAVE, pb);
  193. // }
  194. // else if (msgId == ConstMsgId.SC.PlayerBattleAgainNotify)
  195. // {
  196. // this._lum.getBehavior('BattleBehavior').emit(Event.CPT_MSG.BATTLE_AGAIN_READY, pb);
  197. // }
  198. // else if (msgId == ConstMsgId.SC.RoleBaseInfoNotify)
  199. // {
  200. // this._lum.getBehavior('LoginBehavior').setData(msgId,pb);
  201. // this._lum.getBehavior('LoginBehavior').emit(Event.CPT_MSG.ROLE_BASEINFO_NOTIFY,pb);
  202. // }
  203. // else if (msgId == ConstMsgId.SC.CreateFriendPkRoomResponse)
  204. // {
  205. // this._lum.getBehavior('LoginBehavior').emit(Event.CPT_MSG.CREATE_FRIEND_PK_ROOM, pb);
  206. // }
  207. // else if (msgId == ConstMsgId.SC.EnterFriendPkRoomResponse)
  208. // {
  209. // console.log('Event.CPT_MSG.ENTER_FRIEND_PK_ROOM');
  210. // this._lum.getBehavior('LoginBehavior').setData(msgId,pb);
  211. // this._lum.getBehavior('LoginBehavior').emit(Event.CPT_MSG.ENTER_FRIEND_PK_ROOM, pb);
  212. // }
  213. // // else if (msgId == ConstMsgId.SC.PullStrengthInfoResponse)
  214. // // {
  215. // // this._lum.getBehavior('LoginBehavior').setData(msgId,pb);
  216. // // this._lum.getBehavior('LoginBehavior').emit(Event.CPT_MSG.REFRESH_STRENGTH, pb);
  217. // // }
  218. // else if(msgId == ConstMsgId.SC.InformResponse)
  219. // {
  220. // this._lum.getBehavior('BattleBehavior').emit(Event.CPT_MSG.INFORM_RESPONSE, pb);
  221. // }
  222. // else if(msgId == ConstMsgId.SC.PlayerLoadingRateNotify)
  223. // {
  224. // this._lum.getBehavior('BattleBehavior').emit(Event.CPT_MSG.LOADING_RATE_NOTIFY, pb);
  225. // }
  226. // else if(msgId == ConstMsgId.SC.PlayerLoadingCompleteNotify)
  227. // {
  228. // this._lum.getBehavior('BattleBehavior').emit(Event.CPT_MSG.LOADING_COMPLETE_NOTIFY, pb);
  229. // }
  230. // else if(msgId == ConstMsgId.SC.CreateTeamResponse)
  231. // {
  232. // this._lum.getBehavior('LoginBehavior').emit(Event.CPT_MSG.CREATE_TEAM_RESPONSE, pb);
  233. // }
  234. // else if(msgId == ConstMsgId.SC.JoinTeamResponse)
  235. // {
  236. // this._lum.getBehavior('LoginBehavior').emit(Event.CPT_MSG.JOIN_TEAM_RESPONSE, pb);
  237. // }
  238. // else if(msgId == ConstMsgId.SC.JoinTeamNotify)
  239. // {
  240. // this._lum.getBehavior('LoginBehavior').emit(Event.CPT_MSG.JOIN_TEAM_NOTIFY, pb);
  241. // }
  242. // else if(msgId == ConstMsgId.SC.LeaveTeamNotify)
  243. // {
  244. // this._lum.getBehavior('LoginBehavior').emit(Event.CPT_MSG.LEAVE_TEAM_NOTIFY, pb);
  245. // }
  246. // else if (msgId == ConstMsgId.SC.WeeklySettleNotify)
  247. // {
  248. // this._lum.getBehavior('LoginBehavior').setData(msgId, pb);
  249. // }
  250. }
  251. showWXUserinfoBtn()
  252. {
  253. this._lum.getBehavior('LoginBehavior').emit(Event.COM_MSG.SHOW_WX_LOGIN_BTN, {show:true});
  254. var that = this;
  255. let userinfoBtn = H5SDK.createUserInfoButton({
  256. type: 'text',
  257. text: '',
  258. style: {
  259. left: 0,
  260. top: 0,
  261. width: Common.wxWindowWidth,
  262. height: Common.wxwindowHeight,
  263. lineHeight: 40
  264. // backgroundColor: '#00ff00',
  265. // color: '#ffffff',
  266. // textAlign: 'center',
  267. // fontSize: 16,
  268. // borderRadius: 4,
  269. }
  270. });
  271. userinfoBtn.onTap(function (res) {
  272. console.log('userinfoBtn.onTap:' + JSON.stringify(res));
  273. if(res.hasOwnProperty('userInfo'))
  274. {
  275. userinfoBtn.hide()
  276. console.log('res.userInfo: ' + JSON.stringify(res.userInfo));
  277. that.loginWX(res.userInfo);
  278. that.playerInfoModel.setSelfWXPlayerInfo(res.userInfo);
  279. that._lum.getBehavior('LoginBehavior').emit(Event.COM_MSG.AUTH_OK,{});
  280. that.login(false);
  281. }
  282. else
  283. {
  284. console.log('userinfoBtn.onTap: auth fail' );
  285. }
  286. });
  287. }
  288. loginWX(userInfo)
  289. {
  290. console.log('loginWX(userInfo):' + JSON.stringify(userInfo));
  291. this._lum.getBehavior("LoginBehavior").hasLoginWX = true;
  292. console.log('loginWX(userInfo) 1:' );
  293. console.log('loginWX(userInfo) 2:' );
  294. //this.login(false);
  295. }
  296. onOpen()
  297. {
  298. // if (CC_WECHATGAME)
  299. // {
  300. // this.getCode(false);
  301. // }
  302. // else
  303. // {
  304. // this.login(false);
  305. // }
  306. console.log("onOpen")
  307. if(this.socketType == Define.SOCKET_TYPE.PLAZA)
  308. {
  309. // this._lum.getBehavior('LoginBehavior').login()
  310. }
  311. else if(this.socketType == Define.SOCKET_TYPE.GAME)
  312. {
  313. this._lum.getBehavior('BattleBehavior').onSocketLoginGameServer()
  314. }
  315. this._lum.getBehavior('LoginBehavior').emit(Event.SOCKET_MSG.OPEN, {});
  316. if( (this._socketConnectOkCallback && typeof(this._socketConnectOkCallback)==="function"))
  317. {
  318. this._socketConnectOkCallback()
  319. this._socketConnectOkCallback = null
  320. }
  321. }
  322. onReConnectStart()
  323. {
  324. }
  325. onConnectOk()
  326. {
  327. // this._lum.getBehavior('BattleBehavior').needCheckConnectGameing = true
  328. this._lum.getBehavior('BattleBehavior').emit(Event.SOCKET_MSG.RECONNECT_OK, {});
  329. }
  330. loadProto()
  331. {
  332. var protoFiles = [
  333. ["resources/proto/client_request.proto", "./client_request.proto"],
  334. ["resources/proto/client_response.proto", "./client_response.proto"]
  335. ];
  336. this._gameConnect.loadProto(protoFiles,this._onProtoLoaded.bind(this));
  337. }
  338. _onProtoLoaded()
  339. {
  340. this._gameConnect.open();
  341. }
  342. sendMsg(msg)
  343. {
  344. this._gameConnect.sendMsg(msg);
  345. }
  346. }