ServerHandler.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * @Author: mojunshou 1637302775@qq.com
  3. * @Date: 2025-04-11 10:16:41
  4. * @LastEditors: mojunshou 1637302775@qq.com
  5. * @LastEditTime: 2025-04-24 17:42:18
  6. * @Description:
  7. */
  8. // ServerHandler.ts
  9. import { smc } from '../SingletonModuleComp';
  10. import { GameEvent } from '../config/GameEvent';
  11. import { oops } from 'db://oops-framework/core/Oops';
  12. import { CocosHandler, CocosHandlerType } from './CocosHandler';
  13. import { ProtocolEvent } from './ProtocolEvent';
  14. import { DCHandler } from './DCHandler';
  15. export class ServerHandler {
  16. private static _inst: ServerHandler;
  17. public static get inst(): ServerHandler {
  18. if (!this._inst) {
  19. this._inst = new ServerHandler();
  20. }
  21. return this._inst;
  22. }
  23. private buildCallback(success: string, fail?: string) {
  24. const callback: any = { onSuccess: success };
  25. if (fail) callback.onFail = fail;
  26. return callback;
  27. }
  28. async sendMsgToServer(param: any) {
  29. const data: CocosHandlerType = {
  30. method: 'request.post',
  31. param: JSON.stringify(param)
  32. };
  33. return await CocosHandler.inst.sendMessageToAndroid(data, '服务器请求');
  34. }
  35. //微信登录
  36. wxLogin(code: string) {
  37. const param = {
  38. url: ProtocolEvent.WechatLogin,
  39. param: {
  40. code: code
  41. },
  42. callback: this.buildCallback('ServerHandler.inst.onWxLoginInfo', 'ServerHandler.inst.onRequestFail')
  43. };
  44. this.sendMsgToServer(param);
  45. }
  46. onWxLoginInfo(str: string) {
  47. console.log('[服务器] 微信登录返回', str);
  48. // let
  49. this.getAccountInfo();
  50. }
  51. getAccountInfo() {
  52. const param = {
  53. url: ProtocolEvent.AccountInfo,
  54. callback: this.buildCallback('ServerHandler.inst.onAccountInfo', 'ServerHandler.inst.onRequestFail')
  55. };
  56. this.sendMsgToServer(param);
  57. }
  58. onAccountInfo(str: string) {
  59. console.log('[服务器] 获取账号信息返回', str);
  60. const data = JSON.parse(str);
  61. const account = smc.account.AccountModel;
  62. const game = smc.game.GameModel;
  63. account.uid = data.uid;
  64. account.accountName = data.nickname;
  65. account.headUrl = data.headImgUrl;
  66. account.isLogined = data.isBand;
  67. account.curLevel = data.currentLevelInfo.level;
  68. game.curScore = data.currentLevelInfo.score;
  69. game.targetScore = data.currentLevelConf.score;
  70. game.curLevelConfig = data.currentLevelConf;
  71. game.costInfo = data.handlingChargeConf;
  72. game.eventType = data.currentLevelConf.eventType || "";
  73. game.popupType = data.currentLevelConf.popupType || "";
  74. account.wxCoin = data.props['1005'];
  75. account.hbCoin = data.props['1004'];
  76. account.goldCoin = data.props['1006'];
  77. console.log("account.goldCoin", account.goldCoin)
  78. game.popupShow = data.currentLevelInfo.popupStatus
  79. oops.message.dispatchEvent(GameEvent.UserLogin);
  80. }
  81. getDailyReward(level: number) {
  82. const param = {
  83. url: ProtocolEvent.GetDailyReward,
  84. param: {
  85. level: level
  86. },
  87. callback: this.buildCallback('ServerHandler.inst.onDailyReward', 'ServerHandler.inst.onRequestFail')
  88. };
  89. this.sendMsgToServer(param);
  90. }
  91. onDailyReward(str: string) {
  92. console.log('[服务器] 每日奖励返回', str);
  93. let result = JSON.parse(str);
  94. smc.account.AccountModel.hbCoin = result.props["1004"];
  95. smc.account.AccountModel.xcCount = result.props["1007"]; //消除次数
  96. smc.account.AccountModel.goldCoin = result.props["1006"] //金砖数量
  97. smc.game.GameModel.wxCash = result.money;
  98. //status2在最后1在最前边0排第二
  99. let taskList = result.taskList.sort((a: any, b: any) => {
  100. if (a.status === 1 && b.status !== 1) return -1; // a 在前
  101. })
  102. smc.game.GameModel.taskList = taskList;
  103. oops.message.dispatchEvent(GameEvent.updateRedPackeTaskList);
  104. }
  105. //获取红包页信息
  106. getHbTxInfo() {
  107. const param = {
  108. url: ProtocolEvent.GetHbWithdrawInfo,
  109. callback: this.buildCallback('ServerHandler.inst.onHbTxInfo', 'ServerHandler.instonRequestFail')
  110. };
  111. this.sendMsgToServer(param);
  112. }
  113. onHbTxInfo(str: string) {
  114. console.log('[服务器] 获取红包页面信息返回', str);
  115. let result = JSON.parse(str);
  116. smc.account.AccountModel.hbCoin = result.props["1004"] || 0;
  117. smc.account.AccountModel.xcCount = result.props["1007"] || 0; //消除次数
  118. smc.account.AccountModel.goldCoin = result.props["1006"] || 0; //金砖数量
  119. //要根据taskList的status排序,可领取再在前边,领取完在最后0 进行中 1 可领取 2已经领取
  120. let taskList = result.taskList.sort((a: any, b: any) => {
  121. if (a.status === 1 && b.status !== 1) return -1; // a 在前
  122. })
  123. smc.game.GameModel.taskList = taskList;
  124. smc.game.GameModel.wxCash = result.money;
  125. oops.message.dispatchEvent(GameEvent.openView, "openRedBagView");
  126. }
  127. //获取用户信息
  128. getUserItemInfo() {
  129. const param = {
  130. url: ProtocolEvent.UserItemInfo,
  131. callback: this.buildCallback('ServerHandler.inst.onUserItemInfo', 'ServerHandler.instonRequestFail')
  132. };
  133. this.sendMsgToServer(param);
  134. }
  135. onUserItemInfo(str: string) {
  136. const result = JSON.parse(str);
  137. const props = result.data.props;
  138. const account = smc.account.AccountModel;
  139. const game = smc.game.GameModel;
  140. account.wxCoin = props['1005'] || 0;
  141. account.hbCoin = props['1004'] || 0;
  142. account.goldCoin = props['1006'] || 0;
  143. game.handlingCharge = props['1009'] || 0;
  144. //返回成功才登录
  145. }
  146. //获取提现记录
  147. getRecordList() {
  148. const param = {
  149. url: ProtocolEvent.GetWithdrawRecord,
  150. param: {
  151. offset: 0,
  152. limit: 10
  153. },
  154. callback: this.buildCallback('ServerHandler.inst.onRecordList', 'ServerHandler.inst.onRequestFail')
  155. };
  156. this.sendMsgToServer(param);
  157. }
  158. //提现列表
  159. onRecordList(str: string) {
  160. console.log('[服务器] 提现列表返回', str);
  161. const result = JSON.parse(str);
  162. if (result.count > 0) {
  163. smc.game.GameModel.recordList = result.list;
  164. }
  165. oops.message.dispatchEvent(GameEvent.openView, "openRecordView");
  166. }
  167. getGameAwardInfo() {
  168. const param = {
  169. url: ProtocolEvent.GetGameAward,
  170. callback: this.buildCallback('ServerHandler.inst.onGameAwardInfo', 'ServerHandler.inst.onRequestFail')
  171. };
  172. this.sendMsgToServer(param);
  173. }
  174. onGameAwardInfo(str: string) {
  175. const result = JSON.parse(str);
  176. smc.game.GameModel.passViewInfo = result;
  177. oops.message.dispatchEvent(GameEvent.openView, 'openPassView');
  178. }
  179. getTxbfInfo() {
  180. const param = {
  181. url: ProtocolEvent.GetWithdrawReward,
  182. callback: this.buildCallback('ServerHandler.inst.onRebates', 'ServerHandler.inst.onRequestFail')
  183. };
  184. this.sendMsgToServer(param);
  185. }
  186. //提现返利
  187. onRebates(str: string) {
  188. console.log('[服务器] 提现返利信息', str);
  189. let result = JSON.parse(str);
  190. smc.game.GameModel.cashNum = result.props["1004"];
  191. smc.account.AccountModel.hbCoin = result.props["1004"];
  192. smc.game.GameModel.changeHbCoin = result.changes["1004"];
  193. oops.message.dispatchEvent(GameEvent.openView, "openRebateView")
  194. }
  195. //获取双倍奖励返回
  196. getDoubleSurprise() {
  197. const param = {
  198. url: ProtocolEvent.GetDoubleAwardInfo,
  199. callback: this.buildCallback('ServerHandler.inst.onDoubleSurprise', 'ServerHandler.inst.onRequestFail')
  200. };
  201. this.sendMsgToServer(param);
  202. }
  203. //双倍惊喜返回
  204. onDoubleSurprise(str: string) {
  205. console.log('[服务器] 恭喜翻倍信息', str);
  206. let result = JSON.parse(str);
  207. smc.game.GameModel.doubleRewardInfo = result;
  208. oops.message.dispatchEvent(GameEvent.openView, "openDoubleSurprise");
  209. }
  210. //更新消除奖励
  211. updateEliminationReward(data: { level: number, score: number, count: number }) {
  212. const param = {
  213. url: ProtocolEvent.GetEliminationReward,
  214. param: {
  215. eliminationCount: data.count,
  216. level: data.level,
  217. score: data.score
  218. },
  219. callback: this.buildCallback('ServerHandler.inst.onEliminationSuccess', 'ServerHandler.inst.onRequestFail')
  220. };
  221. this.sendMsgToServer(param);
  222. }
  223. onEliminationSuccess(str: string) {
  224. console.log('[服务器] 消除成功返回', str);
  225. let result = JSON.parse(str);
  226. if (result?.props && result?.changes) {
  227. const props = result.props;
  228. const changes = result.changes;
  229. const account = smc.account.AccountModel;
  230. const game = smc.game.GameModel;
  231. //全部取小数点后两位
  232. account.hbCoin = props['1004'] || 0;
  233. account.wxCoin = props['1005'] || 0;
  234. game.changeHbCoin = changes['1004'] || 0;
  235. game.changeWxCoin = changes['1005'] || 0;
  236. oops.message.dispatchEvent(GameEvent.showCoinAnimation);
  237. }
  238. }
  239. getSign(price: number) {
  240. const param = {
  241. url: ProtocolEvent.AdVideoStart,
  242. param: { ecpm: price },
  243. callback: this.buildCallback('ServerHandler.inst.onSign')
  244. };
  245. this.sendMsgToServer(param);
  246. }
  247. onSign(str: string) {
  248. console.log('[服务器] 签名返回', str);
  249. let result = JSON.parse(str);
  250. smc.game.GameModel.sign = result?.sign || "";
  251. }
  252. //直接领取通关奖励
  253. getPassRewards() {
  254. const level = smc.account.AccountModel.curLevel;
  255. const param = {
  256. url: ProtocolEvent.GetPassReward,
  257. param: {
  258. level: level,
  259. },
  260. callback: this.buildCallback('ServerHandler.inst.onGetPassRewards', 'ServerHandler.inst.onRequestFail')
  261. }
  262. this.sendMsgToServer(param);
  263. }
  264. //直接领取通关奖励返回
  265. onGetPassRewards(str: string) {
  266. console.log('[服务器] 直接领取通关奖励成功返回', str);
  267. let result = JSON.parse(str);
  268. //全部取小数点后两位
  269. smc.game.GameModel.changeHbCoin = result.changes['1004'] || 0;
  270. smc.game.GameModel.changeWxCoin = result.changes['1005'] || 0;
  271. smc.account.AccountModel.hbCoin = result.props["1004"] || 0;
  272. smc.account.AccountModel.wxCoin = result.props["1005"] || 0;
  273. oops.message.dispatchEvent(GameEvent.showCoinAnimation);
  274. //请求下一局
  275. //如果是提现点,就展示提现,然后展示提现返利
  276. if (smc.game.GameModel.curLevelConfig.eventType && smc.game.GameModel.curLevelConfig.eventType == "WITHDRAW_POINT") {
  277. //展示提现信息
  278. this.WechatReward();
  279. smc.game.GameModel.curLevelConfig.eventType = "";
  280. } else {
  281. this.getNextLevel();
  282. }
  283. }
  284. //少量领取翻倍奖励
  285. getLittleRewards() {
  286. const level = smc.account.AccountModel.curLevel;
  287. const param = {
  288. url: ProtocolEvent.GetLittlePassReward,
  289. param: {
  290. level: level,
  291. },
  292. callback: this.buildCallback('ServerHandler.inst.onGetDoubleRewards', 'ServerHandler.inst.onRequestFail')
  293. }
  294. this.sendMsgToServer(param);
  295. }
  296. //少量领取翻倍奖励返回
  297. onGetDoubleRewards(str: string) {
  298. console.log('[服务器] 直接领取通关奖励成功返回', str);
  299. let result = JSON.parse(str);
  300. smc.game.GameModel.changeHbCoin = result.changes['1004'] || 0;
  301. smc.game.GameModel.changeWxCoin = result.changes['1005'] || 0;
  302. smc.account.AccountModel.hbCoin = result.props["1004"] || 0;
  303. smc.account.AccountModel.wxCoin = result.props["1005"] || 0;
  304. oops.message.dispatchEvent(GameEvent.showCoinAnimation);
  305. }
  306. //获取视频奖励
  307. getVideorReward() {
  308. const sign = smc.game.GameModel.sign;
  309. const level = smc.account.AccountModel.curLevel;
  310. let type = smc.game.GameModel.viewType;
  311. console.log("获取视频奖励类型", type)
  312. const param = {
  313. url: ProtocolEvent.GetVideorReward,
  314. param: {
  315. level: level,
  316. type: type,
  317. transId: "",
  318. sign: sign
  319. },
  320. callback: this.buildCallback('ServerHandler.inst.onGetVideorReward', 'ServerHandler.inst.onRequestFail')
  321. }
  322. this.sendMsgToServer(param);
  323. }
  324. onGetVideorReward(str: string) {
  325. console.log('[服务器] 获取视频奖励放回', str);
  326. let result = JSON.parse(str);
  327. if (result.tipThreshold) {
  328. oops.gui.toast("今日奖励已领取,请明天再来吧")
  329. return
  330. }
  331. if (result?.props && !result.tipThreshold) {
  332. console.log(">>>>>>>>>>>>>>发放奖励了")
  333. const props = result.props;
  334. const changes = result.changes;
  335. const account = smc.account.AccountModel;
  336. const game = smc.game.GameModel;
  337. account.wxCoin = props['1005'] || 0;
  338. account.hbCoin = props['1004'] || 0;
  339. game.changeHbCoin = changes['1004'] || 0;
  340. game.changeWxCoin = changes['1005'] || 0;
  341. //是不是通关奖励点击的
  342. if (smc.game.GameModel.viewType == "pass_reward") {
  343. smc.game.GameModel.viewType = "";
  344. this.getNextLevel();
  345. }
  346. oops.message.dispatchEvent(GameEvent.showCoinAnimation);
  347. }
  348. }
  349. //下一关
  350. getNextLevel() {
  351. const level = smc.account.AccountModel.curLevel;
  352. const param = {
  353. url: ProtocolEvent.NextLevel,
  354. param: {
  355. nextLevel: level + 1
  356. },
  357. callback: this.buildCallback('ServerHandler.inst.onGetNextLevel', 'ServerHandler.inst.onRequestFail')
  358. }
  359. this.sendMsgToServer(param);
  360. }
  361. onGetNextLevel(str: string) {
  362. console.log('[服务器] 下一关数据返回', str);
  363. let result = JSON.parse(str);
  364. smc.account.AccountModel.curLevel = result.currentLevelData.level;
  365. smc.game.GameModel.curScore = result.currentLevelData.score;
  366. smc.game.GameModel.targetScore = result.currentLevelConf.score;
  367. smc.game.GameModel.eventType = result.currentLevelConf.eventType || "";
  368. smc.game.GameModel.curLevelConfig = result.currentLevelConf;
  369. smc.account.AccountModel.goldCoin = result.props["1006"] //金砖数量
  370. smc.game.GameModel.popupType = result.currentLevelConf.popupType || "";
  371. smc.game.GameModel.popupShow = result.currentLevelData.popupStatus
  372. oops.message.dispatchEvent(GameEvent.RestartGame);
  373. }
  374. //重新开始游戏
  375. RestartGame() {
  376. const param = {
  377. url: ProtocolEvent.RestartGame,
  378. callback: this.buildCallback('ServerHandler.inst.onRestartGame', 'ServerHandler.inst.onRequestFail')
  379. }
  380. this.sendMsgToServer(param);
  381. }
  382. onRestartGame(str: string) {
  383. console.log('[服务器] 重新开始游戏返回', str);
  384. let result = JSON.parse(str);
  385. smc.account.AccountModel.curLevel = result.currentLevelData.level;
  386. smc.game.GameModel.curScore = result.currentLevelData.score;
  387. smc.game.GameModel.targetScore = result.currentLevelConf.score;
  388. oops.message.dispatchEvent(GameEvent.RestartGame);
  389. }
  390. //微信提现页面
  391. WechatReward() {
  392. const param = {
  393. url: ProtocolEvent.GetWelfarePointCash,
  394. param: {
  395. level: smc.account.AccountModel.curLevel,
  396. riskToken: "",
  397. riskBusinessId: ""
  398. },
  399. callback: this.buildCallback('ServerHandler.inst.onWechatReward', 'ServerHandler.inst.onRequestFail')
  400. }
  401. this.sendMsgToServer(param);
  402. }
  403. onWechatReward(str: string) {
  404. console.log("[服务器] 微信提现请求成功", str);
  405. let result = JSON.parse(str);
  406. smc.game.GameModel.txNum = result.changes["8001"];
  407. smc.game.GameModel.txType = 1;
  408. oops.message.dispatchEvent(GameEvent.openView, "openCashWithdrawalView")
  409. const curLevel = smc.account.AccountModel.curLevel;
  410. DCHandler.inst.reportData(3000101, curLevel);
  411. }
  412. //红包币页提现
  413. HbReward() {
  414. const param = {
  415. url: ProtocolEvent.GetRedPacketCash,
  416. param: {
  417. riskToken: "",
  418. riskBusinessId: ""
  419. },
  420. callback: this.buildCallback('ServerHandler.inst.onHbReward', 'ServerHandler.inst.onHbRewardFail')
  421. }
  422. this.sendMsgToServer(param);
  423. }
  424. onHbReward(str: string) {
  425. console.log('[服务器] 红包币提现返回', str);
  426. let result = JSON.parse(str);
  427. const wxCash = result.changes["8001"];
  428. smc.game.GameModel.txNum = wxCash;
  429. smc.game.GameModel.txType = 2;
  430. oops.message.dispatchEvent(GameEvent.openView, "openCashWithdrawalView");
  431. //更新红包页面的数值
  432. smc.account.AccountModel.hbCoin = result.props["1004"];
  433. smc.account.AccountModel.wxCoin = result.props["1005"];
  434. smc.game.GameModel.wxCash = result.money || 0;
  435. oops.message.dispatchEvent(GameEvent.updateHbAndWxCoin);
  436. }
  437. onHbRewardFail(code: number, str: string) {
  438. oops.gui.toast(str);
  439. }
  440. //获取微信提现页信息
  441. getWechatTxInfo() {
  442. const param = {
  443. url: ProtocolEvent.GetWechatTxInfo,
  444. callback: this.buildCallback('ServerHandler.inst.onGetWechatTxInfo', 'ServerHandler.inst.onRequestFail')
  445. }
  446. this.sendMsgToServer(param);
  447. }
  448. onGetWechatTxInfo(str: string) {
  449. console.log('[服务器] 获取微信提现页信息返回', str);
  450. let result = JSON.parse(str);
  451. smc.game.GameModel.wechat_tx_info = result;
  452. oops.message.dispatchEvent(GameEvent.openView, "openWechatWithdrawalView");
  453. }
  454. //获取二倍速时长信息
  455. getDoubleSpeedTime() {
  456. const param = {
  457. url: ProtocolEvent.GetDoubleSpeedAward,
  458. callback: this.buildCallback('ServerHandler.inst.onGetDoubleSpeedTime', 'ServerHandler.inst.onRequestFail')
  459. }
  460. this.sendMsgToServer(param);
  461. }
  462. onGetDoubleSpeedTime(str: string) {
  463. console.log('[服务器] 获取二倍速时长返回', str);
  464. let result = JSON.parse(str);
  465. smc.game.GameModel.doubleSpeedTime = result.duration;
  466. oops.message.dispatchEvent(GameEvent.openView, "openDoubleSpeedView");
  467. }
  468. //获取引导界面信息
  469. getGuideInfo() {
  470. const param = {
  471. url: ProtocolEvent.GetGuideInfo,
  472. callback: this.buildCallback('ServerHandler.inst.onGetGuideInfo', 'ServerHandler.inst.onRequestFail')
  473. }
  474. this.sendMsgToServer(param);
  475. }
  476. onGetGuideInfo(str: string) {
  477. console.log('[服务器] 获取引导界面信息返回', str);
  478. let result = JSON.parse(str);
  479. smc.game.GameModel.loadbarInfo = result;
  480. }
  481. //更新弹出事件
  482. async updatePopupState(data: { level: number, type: string }) {
  483. const param = {
  484. url: ProtocolEvent.UpdatePopUpState,
  485. param: {
  486. level: data.level,
  487. type: data.type,
  488. callback: this.buildCallback('ServerHandler.inst.onUpdatePopupState', 'ServerHandler.inst.onRequestFail')
  489. },
  490. }
  491. this.sendMsgToServer(param);
  492. }
  493. onUpdatePopupState(str: string) {
  494. console.log('[服务器] 更新弹出事件返回', str);
  495. let result = JSON.parse(str);
  496. }
  497. //领取手续费
  498. getFee() {
  499. const param = {
  500. url: ProtocolEvent.GetFee,
  501. param: {
  502. level: smc.account.AccountModel.curLevel,
  503. },
  504. callback: this.buildCallback('ServerHandler.inst.onGetFee', 'ServerHandler.inst.onRequestFail')
  505. }
  506. this.sendMsgToServer(param);
  507. }
  508. onGetFee(str: string) {
  509. console.log('[服务器] 领取手续费返回', str);
  510. let result = JSON.parse(str);
  511. // smc.game.GameModel.fee = result.fee;
  512. }
  513. onRequestFail(code: number, str: string) {
  514. console.log('[服务器] 请求失败', code, str);
  515. }
  516. }
  517. window["ServerHandler"] = ServerHandler;