/* * @Author: mojunshou 1637302775@qq.com * @Date: 2025-04-11 10:16:41 * @LastEditors: mojunshou 1637302775@qq.com * @LastEditTime: 2025-04-11 11:34:08 * @Description: */ // ServerHandler.ts import { smc } from '../SingletonModuleComp'; import { GameEvent } from '../config/GameEvent'; import { oops } from 'db://oops-framework/core/Oops'; import { CocosHandler, CocosHandlerType } from './CocosHandler'; import { ProtocolEvent } from './ProtocolEvent'; export class ServerHandler { private static _inst: ServerHandler; public static get inst(): ServerHandler { if (!this._inst) { this._inst = new ServerHandler(); } return this._inst; } private buildCallback(success: string, fail?: string) { const callback: any = { onSuccess: success }; if (fail) callback.onFail = fail; return callback; } async sendMsgToServer(param: any) { const data: CocosHandlerType = { method: 'request.post', param: JSON.stringify(param) }; return await CocosHandler.inst.sendMessageToAndroid(data, '服务器请求'); } getAccountInfo() { const param = { url: ProtocolEvent.AccountInfo, callback: this.buildCallback('ServerHandler.inst.onAccountInfo', 'ServerHandler.inst.onRequestFail') }; this.sendMsgToServer(param); } onAccountInfo(str: string) { const data = JSON.parse(str); const account = smc.account.AccountModel; const game = smc.game.GameModel; account.uid = data.uid; account.accountName = data.nickname; account.headUrl = data.headImgUrl; account.isLogined = data.isBand; account.curLevel = data.currentLevelInfo.level; game.curScore = data.currentLevelInfo.score; game.targetScore = data.currentLevelConf.score; game.curLevelConfig = data.currentLevelConf; game.costInfo = data.handlingChargeConf; oops.message.dispatchEvent(GameEvent.UserLogin); } getDailyReward(level: number) { const param = { url: ProtocolEvent.GetDailyReward, param: { level }, callback: this.buildCallback('ServerHandler.inst.onDailyReward', 'ServerHandler.inst.onRequestFail') }; this.sendMsgToServer(param); } onDailyReward(str: string) { console.log('[服务器] 每日奖励返回', str); } getDailyTaskReward() { this.getUserItemInfo(); const param = { url: ProtocolEvent.GetDailyTask, callback: this.buildCallback('ServerHandler.inst.onDailyTaskReward', 'ServerHandler.inst.onRequestFail') }; this.sendMsgToServer(param); } onDailyTaskReward(str: string) { console.log('[服务器] 红包任务数据返回', str); oops.message.dispatchEvent(GameEvent.openView, 'openRedBagView'); } getUserItemInfo() { const param = { url: ProtocolEvent.UserItemInfo, callback: this.buildCallback('ServerHandler.inst.onUserItemInfo', 'ServerHandler.instonRequestFail') }; this.sendMsgToServer(param); } onUserItemInfo(str: string) { const result = JSON.parse(str); const props = result.data.props; const account = smc.account.AccountModel; const game = smc.game.GameModel; account.gameCoin = props['1005']; account.cashCoin = props['1004']; account.goldCoin = props['1006']; game.handlingCharge = props['1009']; } getRecordList() { const param = { url: ProtocolEvent.GetWithdrawRecord, param: { offset: 0, limit: 50 }, callback: this.buildCallback('ServerHandler.inst.onRecordList', 'ServerHandler.inst.onRequestFail') }; this.sendMsgToServer(param); } onRecordList(str: string) { const result = JSON.parse(str); if (result.count > 0) { smc.game.GameModel.recordList = result.data.list; } oops.message.dispatchEvent(GameEvent.openRecordView); } getGameAwardInfo() { const param = { url: ProtocolEvent.getGameAward, callback: this.buildCallback('ServerHandler.inst.onGameAwardInfo', 'ServerHandler.inst.onRequestFail') }; this.sendMsgToServer(param); } onGameAwardInfo(str: string) { const result = JSON.parse(str); smc.game.GameModel.passViewInfo = result; oops.message.dispatchEvent(GameEvent.openView, 'openPassView'); } getTxbfInfo() { const param = { url: ProtocolEvent.GetWithdrawReward, callback: this.buildCallback('ServerHandler.inst.onRebates', 'ServerHandler.inst.onRequestFail') }; this.sendMsgToServer(param); } onRebates(str: string) { console.log('[服务器] 提现返利信息', str); } getDoubleSurprise() { const param = { url: ProtocolEvent.getDoubleAwardInfo, callback: this.buildCallback('ServerHandler.inst.onDoubleSurprise', 'ServerHandler.inst.onRequestFail') }; this.sendMsgToServer(param); } onDoubleSurprise(str: string) { console.log('[服务器] 恭喜翻倍信息', str); } updateEliminationReward(data: { level: number, score: number, count: number }) { const param = { url: ProtocolEvent.GetEliminationReward, param: { eliminationCount: data.count, level: data.level, score: data.score }, callback: this.buildCallback('ServerHandler.inst.onEliminationSuccess', 'ServerHandler.inst.onRequestFail') }; this.sendMsgToServer(param); } onEliminationSuccess(str: string) { console.log('[服务器] 消除成功返回', str); } getSign(price: number) { const param = { url: ProtocolEvent.AdVideoStart, param: { ecpm: price }, callback: this.buildCallback('ServerHandler.inst.onSign') }; this.sendMsgToServer(param); } onSign(str: string) { console.log('[服务器] 签名返回', str); let result = JSON.parse(str); smc.game.GameModel.sign = result?.sign || ""; } onRequestFail(code: number, str: string) { console.log('[服务器] 请求失败', code, str); } } window["ServerHandler"] = ServerHandler;