ServerHandler.ts 18 KB

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