ClientKernel.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. CClientKernel = cc.Class({
  2. ctor:function() {
  3. //游戏属性
  4. this.mGameAttribute = new tagGameAttribute();
  5. this.mFirstPlaying = false;
  6. this.mMeUserItem = 0;
  7. this.mServerItem = null;
  8. this.mUserManager = new CGameUserManager(this);
  9. },
  10. //内核配置
  11. Init:function() {
  12. this.mGameAttribute.dwClientVersion = cc.VERSION_PLAZA;
  13. //this.mServerItem.OnGFGameReady();
  14. return true;
  15. },
  16. SetServerItem:function(pServerItem){
  17. this.mServerItem = pServerItem;
  18. },
  19. ResetUserItem:function(){
  20. this.mUserManager.ResetUserItem(this.mMeUserItem);
  21. },
  22. Intermit:function(iExitCode){
  23. this.mServerItem.OnGFGameClose(iExitCode);
  24. gClientKernel.destory();
  25. return true;
  26. },
  27. //房间属性
  28. GetServerAttribute:function () {
  29. return this.mServerAttribute;
  30. },
  31. //自己位置
  32. GetMeUserItem :function(){
  33. return this.mMeUserItem;
  34. },
  35. //自己位置
  36. GetMeChairID :function(){
  37. if (this.mMeUserItem == 0) return INVALID_CHAIR;
  38. return this.mMeUserItem.GetChairID();
  39. },
  40. //设置接口
  41. SetClientKernelSink :function(pIClientKernelSink){
  42. this.mIClientKernelSink = pIClientKernelSink;
  43. return true;
  44. },
  45. //设置接口
  46. SetStringMessageSink:function(pIStringMessageSink) {
  47. this.mIStringMessageSink = pIStringMessageSink;
  48. return true;
  49. },
  50. OnUserItemAcitve :function(pIClientUserItem){
  51. if (pIClientUserItem == 0) return;
  52. if (this.mUserAttribute.dwUserID == pIClientUserItem.GetUserID()){
  53. this.mMeUserItem = pIClientUserItem;
  54. }
  55. if (this.mIClientKernelSink)
  56. this.mIClientKernelSink.OnEventUserEnter(pIClientUserItem, pIClientUserItem.GetUserStatus() == US_LOOKON);
  57. },
  58. OnUserItemDelete:function (pIClientUserItem) {
  59. if (pIClientUserItem == 0) return;
  60. if (this.mIClientKernelSink)
  61. this.mIClientKernelSink.OnEventUserLeave(pIClientUserItem, pIClientUserItem.GetUserStatus() == US_LOOKON);
  62. },
  63. //用户积分
  64. OnGFUserScore:function (dwUserID, UserScore){
  65. //寻找用户
  66. var pIClientUserItem = this.mUserManager.SearchUserByUserID(dwUserID);
  67. //用户判断
  68. if (pIClientUserItem==null || pIClientUserItem==0) return;
  69. if (this.mIClientKernelSink)
  70. this.mIClientKernelSink.OnEventScoreUpdare(pIClientUserItem, pIClientUserItem.GetUserStatus() == US_LOOKON);
  71. },
  72. //用户状态
  73. OnGFUserStatus :function (dwUserID, UserStatus) {
  74. var pIClientUserItem = this.mUserManager.SearchUserByUserID(dwUserID);
  75. //用户判断
  76. if (pIClientUserItem==0) return;
  77. //状态定义
  78. var cbUserStatus=UserStatus.cbUserStatus;
  79. //离开判断
  80. if ((cbUserStatus==US_NULL)||(cbUserStatus==US_FREE)) {
  81. if (this.mMeUserItem.GetUserID()==pIClientUserItem.GetUserID()) {
  82. //设置变量
  83. this.mAllowLookon=false;
  84. this.mGameStatus=GAME_STATUS_FREE;
  85. //重置游戏
  86. //if (this.mIClientKernelSink)
  87. // this.mIClientKernelSink.ResetGameClient();
  88. //删除用户
  89. //this.mUserManager.ResetUserItem( this.mMeUserItem );
  90. }
  91. else
  92. {
  93. //删除用户
  94. this.mUserManager.DeleteUserItem(pIClientUserItem);
  95. }
  96. return;
  97. }
  98. if (this.mUserManager)
  99. this.mUserManager.UpdateUserItemStatus(pIClientUserItem, UserStatus);
  100. },
  101. //房间配置
  102. OnGFConfigServer:function (UserAttribute, ServerAttribute){
  103. this.mUserAttribute = UserAttribute;
  104. this.mServerAttribute = ServerAttribute;
  105. },
  106. //用户进入
  107. OnGFUserEnter:function (pIClientUserItem){
  108. //用户判断
  109. if (pIClientUserItem==null) return;
  110. if (this.mUserManager!=null) {
  111. this.mUserManager.ActiveUserItem(pIClientUserItem.GetUserInfo(),pIClientUserItem.GetCustomFaceInfo());
  112. }
  113. },
  114. //配置完成
  115. OnGFConfigFinish:function (){
  116. if ( !this.mFirstPlaying ){
  117. this.mFirstPlaying = true;
  118. if (this.mIClientKernelSink && !this.mIClientKernelSink.SetupGameClient()) {
  119. return;
  120. }
  121. }
  122. this.SendGameOption();
  123. },
  124. //////////////////////////////////////////////////////////////////////////
  125. //网络接口
  126. //发送函数
  127. SendSocketClass:function (wMainCmdID, wSubCmdID, Obj) {
  128. return this.mServerItem.SendSocketClass(wMainCmdID, wSubCmdID, Obj);
  129. },
  130. //发送函数
  131. SendSocketData:function (wMainCmdID, wSubCmdID, data, dataSize) {
  132. return this.mServerItem.GFSendData(wMainCmdID, wSubCmdID, data, dataSize);
  133. },
  134. //////////////////////////////////////////////////////////////////////////
  135. //功能接口
  136. //发送进入场景
  137. SendGameOption:function (){
  138. //变量定义
  139. var GameOption = new CMD_GF_GameOption();
  140. //构造数据
  141. GameOption.cbAllowLookon = 1;
  142. GameOption.dwFrameVersion = cc.VERSION_PLAZA;
  143. GameOption.dwClientVersion = this.mGameAttribute.dwClientVersion;
  144. //发送数据
  145. this.SendSocketClass(MDM_GF_FRAME, SUB_GF_GAME_OPTION, GameOption);
  146. },
  147. OnGFEventSocket:function (main, sub, data, dataSize){
  148. //游戏消息
  149. if (main==MDM_GF_GAME) {
  150. if (this.mIClientKernelSink==0) return false;
  151. if (USER_CARD_TEST && this.mIClientKernelSink.OnEventCardTestMessage(sub,data,dataSize)) return true;
  152. return this.mIClientKernelSink.OnEventGameMessage(sub,data,dataSize);
  153. }
  154. //游戏消息
  155. if (main==MDM_GR_INSURE) return true;
  156. //框架处理
  157. if (main==MDM_GF_FRAME){
  158. return this.OnEventGameFrameMessage(sub,data,dataSize)
  159. }
  160. if (main==MDM_GF_CARDROOM) {
  161. if (this.mIClientKernelSink==0) return false;
  162. return this.mIClientKernelSink.OnCardRoomMessage(sub,data,dataSize);
  163. }
  164. return false;
  165. },
  166. OnEventGameFrameMessage:function (sub,data,dataSize){
  167. switch (sub){
  168. case SUB_GF_GAME_STATUS: //游戏状态
  169. {
  170. return this.OnSocketSubGameStatus(data,dataSize);
  171. }
  172. case SUB_GF_GAME_SCENE: //游戏场景
  173. {
  174. return this.OnSocketSubGameScene(data,dataSize);
  175. }
  176. case SUB_GF_LOOKON_STATUS: //旁观状态
  177. {
  178. return OnSocketSubLookonStatus(data,dataSize);
  179. }
  180. case SUB_GF_SYSTEM_MESSAGE: //系统消息
  181. {
  182. return this.OnSocketSubSystemMessage(data,dataSize);
  183. }
  184. case SUB_GF_ACTION_MESSAGE: //动作消息
  185. {
  186. return OnSocketSubActionMessage(data,dataSize);
  187. }
  188. case SUB_GF_GPS_GET_RES: //动作消息
  189. {
  190. return this.OnSocketSubGPSInfo(data,dataSize);
  191. }
  192. case SUB_GF_USER_READY: //用户准备
  193. {
  194. if(this.mMeUserItem ==0 || this.mMeUserItem.GetUserStatus()>=US_READY)
  195. return true;
  196. this.SendFrameData(SUB_GF_USER_READY);
  197. if (this.mIClientKernelSink)
  198. this.mIClientKernelSink.OnGFMatchWaitTips(0);
  199. return true;
  200. }
  201. case SUB_GR_MATCH_INFO: //比赛信息
  202. case SUB_GR_MATCH_WAIT_TIP: //等待提示
  203. case SUB_GR_MATCH_RESULT: //比赛结果
  204. {
  205. return true;
  206. }
  207. case SUB_GF_USER_EXPRESSION: // 快捷短语
  208. {
  209. return this.mIClientKernelSink.OnSubUserPhrase(data, dataSize);
  210. }
  211. case SUB_GF_USER_CHAT: // 用户聊天
  212. {
  213. return this.mIClientKernelSink.OnSubUserChat(data, dataSize);
  214. }
  215. case SUB_GF_USER_VOICE: //语音
  216. {
  217. return this.mIClientKernelSink.OnSubUserVoice(data,dataSize);
  218. }
  219. case SUB_GF_USER_VOICE_A: //语音
  220. {
  221. return this.mIClientKernelSink.OnSubUserVoice_A(data,dataSize);
  222. }
  223. }
  224. return true;
  225. },
  226. //游戏状态
  227. OnSocketSubGameStatus:function (data, dataSize){
  228. var pGameStatus = new CMD_GF_GameStatus();
  229. //效验参数
  230. if (dataSize!=gCByte.Bytes2Str(pGameStatus, data)) return false;
  231. //设置变量
  232. this.mGameStatus=pGameStatus.bGameStatus;
  233. this.mAllowLookon=pGameStatus.bAllowLookon?true:false;
  234. return true;
  235. },
  236. //系统消息
  237. OnSocketSubSystemMessage:function (data, dataSize){
  238. //变量定义
  239. var pSystemMessage = new CMD_CM_SystemMessage();
  240. gCByte.Bytes2Str(pSystemMessage, data);
  241. //显示消息
  242. if ((pSystemMessage.wType&SMT_CHAT) && (this.mIStringMessageSink != null)){
  243. this.mIStringMessageSink.InsertSystemString(pSystemMessage.szString);
  244. }
  245. //弹出消息
  246. if ((pSystemMessage.wType&SMT_EJECT) && (this.mIStringMessageSink != 0)){
  247. if(pSystemMessage.wType&SMT_CLOSE_GAME) this.mIStringMessageSink.InsertPromptStringAndClose(pSystemMessage.szString);
  248. else this.mIStringMessageSink.InsertPromptString(pSystemMessage.szString);
  249. }
  250. //关闭处理
  251. if ((pSystemMessage.wType&SMT_CLOSE_GAME) != 0){
  252. //中断连接
  253. //this.Intermit(GameExitCode_Normal);
  254. }
  255. //关闭房间
  256. if (pSystemMessage.wType&SMT_CLOSE_GAME){
  257. //if (g_pClientLand!=null)g_pClientLand->CloseGameClient();
  258. }
  259. return true;
  260. },
  261. OnSocketSubGPSInfo:function (data, dataSize){
  262. //变量定义
  263. var InfoArr = new Array();
  264. InfoArr[0] = new tagUserGps();
  265. var size = gCByte.GetSize(InfoArr[0]);
  266. if(dataSize == 0 || dataSize < size || dataSize % size != 0 ){
  267. if(window.LOG_NET_DATA)console.log("OnSocketSubGPSInfo dataSize err "+dataSize+" % "+size)
  268. return true;
  269. }
  270. var Cnt = dataSize/size;
  271. for(var i = 1;i<Cnt;i++){
  272. InfoArr[i] = new tagUserGps();
  273. }
  274. InfoArr._name = "GPS_ARR"
  275. gCByte.Bytes2Str(InfoArr, data);
  276. this.mIClientKernelSink.OnGetTableGPSRes(InfoArr);
  277. return true;
  278. },
  279. //游戏场景
  280. OnSocketSubGameScene:function (data, dataSize){
  281. //效验状态
  282. if (this.mMeUserItem==0) return true;
  283. if (this.mMeUserItem==null) return true;
  284. if (this.mIClientKernelSink==0) return true;
  285. //场景处理
  286. var bLookonUser=(this.mMeUserItem.GetUserStatus()==US_LOOKON);
  287. this.mIClientKernelSink.ShowLookOnView(bLookonUser)
  288. this.mIClientKernelSink.OnClearScene();
  289. var bRet = this.mIClientKernelSink.OnEventSceneMessage(this.mGameStatus, bLookonUser, data, dataSize);
  290. return bRet;
  291. },
  292. //旁观状态
  293. IsLookonMode:function (){
  294. if (this.mMeUserItem == 0) return true;
  295. return this.mMeUserItem.GetUserStatus() == US_LOOKON;
  296. },
  297. //用户更新
  298. OnUserItemUpdate:function (pIClientUserItem, UserAttrib){
  299. if (this.pIClientUserItem == 0) return;
  300. if (this.mIClientKernelSink)
  301. this.mIClientKernelSink.OnEventUserStatus(pIClientUserItem, pIClientUserItem.GetUserStatus() == US_LOOKON);
  302. },
  303. //游戏用户
  304. GetTableUserItem:function (wChairID){
  305. return this.mUserManager.GetTableUserItem(wChairID);
  306. },
  307. //游戏用户
  308. GetTableLookOnUserArr:function (){
  309. return this.mUserManager.GetTableLookOnUserArr();
  310. },
  311. });
  312. IClientKernel__ = cc.Class({
  313. ctor:function(){
  314. this.__gClentKernelRefCount = 0;
  315. this.__gClientKernel = null;
  316. },
  317. create:function() {
  318. if (this.__gClentKernelRefCount == 0) {
  319. this.__gClientKernel = new CClientKernel();
  320. }
  321. this.__gClentKernelRefCount++;
  322. return this.__gClientKernel;
  323. },
  324. destory:function() {
  325. if (this.__gClentKernelRefCount > 0){
  326. this.__gClentKernelRefCount--;
  327. if (this.__gClentKernelRefCount <= 0){
  328. this.__gClentKernelRefCount = 0;
  329. this.__gClientKernel = null;
  330. }
  331. }
  332. },
  333. get:function() {
  334. return this.__gClientKernel;
  335. },
  336. });
  337. window.gClientKernel = new IClientKernel__();