ReplayEngine.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. tagRecordUserInfo = cc.Class({
  2. ctor:function() {
  3. this.dwUserID = 0;
  4. this.dwGameID = 0;
  5. this.wTableID = 0;
  6. this.wChairID = 0;
  7. this.cbGender = 0;
  8. this.llScore = 0;
  9. },
  10. });
  11. tagProgressData = cc.Class({
  12. ctor:function() {
  13. this.wChairID = 0;
  14. this.wMainCmdID = 0;
  15. this.wSubCmdID = 0;
  16. this.wDataSize = 0;
  17. this.pStruct = null;
  18. },
  19. });
  20. tagRecordFileHead = cc.Class({
  21. ctor:function() {
  22. this.dwVer = 0;
  23. this.wUserCount = 0;
  24. },
  25. });
  26. CMD_GameBase = cc.Class({
  27. ctor:function() {
  28. this._name = 'CMD_GameBase'
  29. this.InfoHead = new tagRecordFileHead();
  30. this.wInfoSize = 0; //房主ID
  31. this.roomInfo = new CMD_GF_RoomInfo(); //52
  32. this.wSceneSize = 0; //房主ID
  33. this.gameScene = 0;
  34. },
  35. });
  36. RePlayVer = 1;
  37. cc.RePlayEngine = cc.Class({
  38. extends: cc.BaseClass,
  39. properties: {
  40. },
  41. onLoad:function () {
  42. },
  43. Init :function (Hook) {
  44. this.m_Hook = Hook;
  45. this.m_PlayData = null;
  46. this.m_GameEngine = null;
  47. this.m_bInitScene = false;
  48. this.m_PGIndex = 0;
  49. this.m_PGProgress = 0;
  50. },
  51. SetData:function (Data, LookUser, bOnlyData) {
  52. this.m_bOnlyData = bOnlyData;
  53. this.m_PlayData = Data.split(',');
  54. for(var i in this.m_PlayData) this.m_PlayData[i] = parseInt(this.m_PlayData[i], 16);
  55. this.m_GameBase = new CMD_GameBase();
  56. this.m_GameBase.gameScene = GameDef.CMD_S_StatusFree();
  57. var DataIndex = gCByte.Bytes2Str( this.m_GameBase, this.m_PlayData);
  58. //游戏开始信息头
  59. if( this.m_GameBase.InfoHead.dwVer != RePlayVer) {
  60. if(window.LOG_NET_DATA)console.log("ParseData err dwVer")
  61. if(window.LOG_NET_DATA)console.log(this.GameData)
  62. return this.ShowTips("數據异常! 1");
  63. }
  64. //校验数据
  65. if(this.m_GameBase.wInfoSize != gCByte.GetSize(this.m_GameBase.roomInfo) || this.m_GameBase.wSceneSize != gCByte.GetSize(this.m_GameBase.gameScene)){
  66. return this.ShowTips("數據异常! 0")
  67. }
  68. //保存数据
  69. var StartIndex = gCByte.GetSize(this.m_GameBase.InfoHead) + 2;
  70. this.m_GameBase.RoomData = this.m_PlayData.slice(StartIndex, StartIndex + this.m_GameBase.wInfoSize);
  71. StartIndex += this.m_GameBase.wInfoSize + 2;
  72. this.m_GameBase.SceneData = this.m_PlayData.slice(StartIndex, StartIndex + this.m_GameBase.wSceneSize);
  73. var kernel = gClientKernel.get();
  74. if(!this.m_bOnlyData)kernel.OnGFConfigServer(LookUser,null);
  75. this.GameData = new Array;
  76. this.ParseData(DataIndex, 0);
  77. if(this.m_GameEngine) this.ClearScene();
  78. },
  79. SetGameEngine:function (GameEngine) {
  80. this.m_GameEngine = GameEngine;
  81. this.m_GameEngine.m_ReplayMode = true;
  82. this.m_GameEngine.LoadSound();
  83. this.m_GameView = this.m_GameEngine.m_GameClientView;
  84. var kernel = gClientKernel.get();
  85. kernel.SetClientKernelSink(this.m_GameEngine);
  86. },
  87. IsReady:function(){
  88. if(this.m_PlayData == null || this.m_GameEngine == null) return false;
  89. if(this.InitScene()) return true;
  90. return false;
  91. },
  92. //更新按钮状态
  93. ChangeUIState:function(){
  94. this.m_Hook.ChangeUIState( this.m_PGIndex, this.m_PGProgress,this.GameData.length - 1, this.GameData[this.m_PGIndex].length - 1)
  95. },
  96. ClearScene:function(){
  97. this.m_bInitScene = false;
  98. this.InitScene();
  99. this.ChangeUIState();
  100. },
  101. InitScene:function(){
  102. if(this.m_bInitScene) return true;
  103. this.m_bInitScene = true;
  104. if(window.LOG_NET_DATA) console.log("InitScene")
  105. var kernel = gClientKernel.get();
  106. //观看视角
  107. var tempUser = new tagUserInfo(); //dwUserID == 0
  108. //本局信息
  109. for(var i=0;i< this.GameData[this.m_PGIndex].user.length;i++){
  110. if( this.GameData[this.m_PGIndex].user[i].dwUserID != kernel.mUserAttribute.dwUserID) continue
  111. gCByte.StrSameMemCopy(tempUser, this.GameData[this.m_PGIndex].user[i]);
  112. }
  113. //半路加入 取加入时数据
  114. if(tempUser.dwUserID == 0){
  115. for(var i=this.m_PGIndex;i<this.GameData.length;i++){
  116. if(tempUser.dwUserID != 0) break;
  117. for(var j=0;j<this.GameData[i].user.length;j++){
  118. if( this.GameData[i].user[j].dwUserID != kernel.mUserAttribute.dwUserID) continue
  119. gCByte.StrSameMemCopy(tempUser, this.GameData[i].user[j]);
  120. }
  121. }
  122. }
  123. //非游戏玩家 取首位置初始化
  124. if(tempUser.dwUserID == 0){
  125. kernel.OnGFConfigServer(this.GameData[0].user[0], null);
  126. gCByte.StrSameMemCopy(tempUser, this.GameData[this.m_PGIndex].user[0]);
  127. }
  128. //观看视角玩家进入
  129. kernel.mUserManager.ActiveUserItem(tempUser/*, CustomFaceInfo*/);
  130. //其他玩家
  131. for(var i=0;i< this.GameData[this.m_PGIndex ].user.length;i++){
  132. if( this.GameData[this.m_PGIndex ].user[i].dwUserID == kernel.mUserAttribute.dwUserID) continue
  133. var tempUser = new tagUserInfo();
  134. gCByte.StrSameMemCopy(tempUser, this.GameData[this.m_PGIndex].user[i]);
  135. kernel.mUserManager.ActiveUserItem(tempUser/*, CustomFaceInfo*/);
  136. }
  137. //场景还原
  138. this.m_GameEngine.OnClearScene();
  139. //初始化场景
  140. this.m_GameEngine.OnCardRoomMessage(SUB_GF_ROOM_INFO, this.m_GameBase.RoomData, this.m_GameBase.wInfoSize);
  141. this.m_GameEngine.OnEventSceneMessage(GAME_STATUS_FREE, false, this.m_GameBase.SceneData, this.m_GameBase.wSceneSize) ;
  142. //进度信息
  143. if(this.GameData[this.m_PGIndex].RoomStatus) this.m_GameEngine.OnCardRoomMessage(SUB_GF_ROOM_STATUS, this.GameData[this.m_PGIndex].RoomStatus.ByteData, this.GameData[this.m_PGIndex].RoomStatus.wDataSize);
  144. this.ShowIndex(0);
  145. },
  146. //解析游戏数据
  147. ParseData:function(DataIndex, GameIndex){
  148. if(DataIndex >= this.m_PlayData.length) return this.DataParseFinish();
  149. this.GameData[GameIndex] = new Array();
  150. //游戏玩家数据
  151. this.GameData[GameIndex].user = new Array();
  152. for(var i=0;i< this.m_GameBase.InfoHead.wUserCount;i++){
  153. this.GameData[GameIndex].user[i] = new tagRecordUserInfo();
  154. }
  155. DataIndex += gCByte.Bytes2Str(this.GameData[GameIndex].user, this.m_PlayData, DataIndex);
  156. if(!this.m_bOnlyData){
  157. var kernel = gClientKernel.get();
  158. //其他玩家
  159. for(var i=0;i< this.GameData[GameIndex].user.length;i++){
  160. if( this.GameData[GameIndex].user[i].dwUserID == kernel.mUserAttribute.dwUserID) {
  161. this.m_GameBase.InfoHead.wLookChair = this.GameData[GameIndex].user[i].wChairID;
  162. }
  163. }
  164. }
  165. //游戏流程数据
  166. var tempSize = this.ParseGameData(DataIndex, GameIndex);
  167. //下一局数据
  168. if(tempSize != false) this.ParseData(DataIndex+tempSize, GameIndex+1);
  169. },
  170. GetUserChairID:function(dwUserID){
  171. if(this.GameData == null) return INVALID_CHAIR;
  172. if(this.GameData[0].user == null) return INVALID_CHAIR;
  173. for(var i in this.GameData[0].user){
  174. if(this.GameData[0].user[i].dwUserID == dwUserID) return this.GameData[0].user[i].wChairID;
  175. }
  176. return INVALID_CHAIR;
  177. },
  178. ParseGameData:function(DataIndex, GameIndex){
  179. var Size = 0;
  180. //放卡框架消息
  181. while(DataIndex + Size < this.m_PlayData.length){
  182. var TempData = new tagProgressData();
  183. Size += gCByte.Bytes2Str(TempData, this.m_PlayData, DataIndex + Size);
  184. //单句结束判断
  185. if(TempData.wMainCmdID == INVALID_WORD && TempData.wSubCmdID == INVALID_WORD) break;
  186. if(TempData.wMainCmdID == MDM_GF_CARDROOM){
  187. switch(TempData.wSubCmdID){
  188. case SUB_GF_ROOM_INFO: {
  189. var pRoomInfo = new CMD_GF_RoomInfo();
  190. if(datasize != gCByte.Bytes2Str(pRoomInfo,data) ) return false;
  191. if(gCByte.Bytes2Str(pRoomInfo, this.m_PlayData, DataIndex + Size) != TempData.wDataSize){
  192. console.error("MDM_GF_CARDROOM SizeErr SubID "+TempData.wSubCmdID)
  193. return false;
  194. }
  195. TempData.ByteData = this.m_PlayData.slice( DataIndex + Size, DataIndex + Size + TempData.wDataSize)
  196. break
  197. }
  198. case SUB_GF_ROOM_STATUS:{
  199. TempData.pStruct = new CMD_GF_RoomStatus();
  200. TempData.pStruct.bLockArr = new Array(GameDef.GAME_PLAYER);
  201. if(gCByte.Bytes2Str(TempData.pStruct, this.m_PlayData, DataIndex + Size) != TempData.wDataSize){
  202. console.error("MDM_GF_CARDROOM SizeErr SubID "+TempData.wSubCmdID)
  203. return false;
  204. }
  205. TempData.ByteData = this.m_PlayData.slice( DataIndex + Size, DataIndex + Size + TempData.wDataSize)
  206. this.GameData[GameIndex].RoomStatus = TempData;
  207. break
  208. }
  209. case SUB_GF_ROOM_GAME_FINISH:{
  210. TempData.pStruct = GameDef.CMD_S_GameCustomInfo();
  211. if(gCByte.Bytes2Str(TempData.pStruct , this.m_PlayData, DataIndex + Size) != TempData.wDataSize){
  212. console.error("MDM_GF_CARDROOM SizeErr SubID "+TempData.wSubCmdID)
  213. return false;
  214. }
  215. TempData.ByteData = this.m_PlayData.slice( DataIndex + Size, DataIndex + Size + TempData.wDataSize)
  216. this.GameData[GameIndex].push(TempData);
  217. //战绩显示数据
  218. this.GameData.CustomEnd = TempData.pStruct;
  219. break
  220. }
  221. default:{
  222. if(window.LOG_NET_DATA)console.log("MDM_GF_CARDROOM skip msg "+TempData.wSubCmdID)
  223. }
  224. }
  225. }
  226. //游戏私有消息
  227. if(TempData.wMainCmdID == MDM_GF_GAME){
  228. if(!this.CustomGameData(GameIndex, TempData, DataIndex+Size)) return false;
  229. }
  230. Size += TempData.wDataSize;
  231. }
  232. if( this.CustomGameDataParseEnd != null ){
  233. this.CustomGameDataParseEnd(GameIndex);
  234. }
  235. return Size
  236. },
  237. //解析完成
  238. DataParseFinish:function(){
  239. if(window.LOG_NET_DATA)console.log(this.GameData)
  240. this.m_PGIndex = 0;
  241. this.m_PGProgress = 0;
  242. if(!this.m_bOnlyData)this.ChangeUIState();
  243. },
  244. PlayNext:function(){
  245. if(this.m_PGProgress + 1 >= this.GameData[this.m_PGIndex].length) return
  246. this.ShowIndex(this.m_PGProgress+1)
  247. this.ChangeUIState();
  248. },
  249. PlayLast:function(){
  250. if(this.m_PGProgress == 0) return
  251. this.ShowIndex(this.m_PGProgress-1)
  252. this.ChangeUIState();
  253. },
  254. ShowIndex:function(wShowIndex){
  255. var pData = this.GameData[this.m_PGIndex];
  256. var StartIndex = this.m_PGProgress;
  257. if(this.m_PGProgress >= wShowIndex ) {
  258. this.m_GameEngine.OnClearScene();
  259. StartIndex = 0;
  260. }else{
  261. StartIndex += 1;
  262. }
  263. for(var i = StartIndex; i<=wShowIndex; i++){
  264. var pMsg = pData[i];
  265. this.m_GameEngine.m_bRollBack = i != wShowIndex;
  266. if(pMsg.wMainCmdID == MDM_GF_GAME) this.m_GameEngine.OnEventGameMessage(pMsg.wSubCmdID,pMsg.ByteData,pMsg.wDataSize)
  267. if(pMsg.wMainCmdID == MDM_GF_CARDROOM) this.m_GameEngine.OnCardRoomMessage(pMsg.wSubCmdID,pMsg.ByteData,pMsg.wDataSize);
  268. if(pMsg.wMainCmdID != MDM_GF_CARDROOM) this.ShowIndexCustom(i);
  269. }
  270. if( pData[wShowIndex].wMainCmdID != MDM_GF_CARDROOM) this.ShowEndIndexCustom(wShowIndex);
  271. this.m_PGProgress = wShowIndex;
  272. },
  273. });