/******************************************************************************* * * 活动管理 * * * * 2023.06.25 ******************************************************************************/ const BridgeController = require( "bridge.controller" ); const ActDefine = require( "acts.define" ); const ActManager = cc.Class( { extends: BridgeController, // 初始化配置数据 initConfig: function() { // 插件 nx.plugin.add( this, [ "view" ] ); // 视图创建 this.vbuild( ActDefine.ViewActs ); // 全局支持 nx.bridge.acts = this; }, // 注册监听事件 registerEvents: function() { }, // 注册协议接受事件 registerProtocals: function() { // 前端准备完毕 this.RegisterProtocal( 16600, this.onBaseData.bind( this ) ); // 活动基础信息 this.RegisterProtocal( 16603, this.handle16603.bind( this ) ); // 整点更新 this.RegisterProtocal( 16607, this.onFreshActs.bind( this ) ); // 奖励更新 this.RegisterProtocal( 16602, this.onFreshActsRewards.bind( this ) ); this.RegisterProtocal( 16606, this.onUpdateActsRewards.bind( this ) ); // 推送弹窗 this.RegisterProtocal( 16621, this.onPushPopup.bind( this ) ); this.RegisterProtocal( 16622, this.onPushPopupClose.bind( this ) ); // 活动引导 this.RegisterProtocal( 16623, this.onActGuide.bind( this ) ); this.RegisterProtocal( 16624, this.onActGuideDone.bind( this ) ); // 活动抽奖 this.RegisterProtocal( 31111, this.handle31111.bind( this ) ); }, // 从服务器初始化数据 reqBaseFromServer: function( _cb ) { // 配置加载 let cfgs = [ "ad_show", "login_popup_data" ]; this.vreset(); this.loadConfigs( cfgs, ( _ret, _data ) => { this.reqBaseData( _cb ); } ); }, // ============================================================ // 基本数据 // ============================================================ // 请求基本数据 reqBaseData: function( _cb ) { this.SendProtocal( 16600, {}, _cb ); }, // 基本数据 onBaseData: function( _data ) { if( !this.isGoodData( _data ) ) { return; } let themes = _data.holiday_theme_list; themes.sort( ( _a, _b ) => { return _a.sort - _b.sort; } ); this.vset( "themes", themes, false ); // 活动模块更新 this.buildActMods(); // 重构提示链 this.buildTipsView(); // 奖励信息 this.reqActsRewards(); // 通知 this.vnotify( "themes" ); this.freshForeshow(); }, // 活动模块更新 buildActMods: function() { let self = this; let MODS = ActDefine.ActsMods; let themes = this.vget( "themes" ); // 子活动统计 let ids = []; let temp = {}; for( let i = 0; i < themes.length; ++i ) { let theme = themes[ i ]; theme.theme_holiday_list.forEach( _t => { ids.push( _t.camp_id ); temp[ _t.camp_id ] = nx.dt.objClone( _t ); } ); } // 活动列表是否改变 if( nx.dt.objNEmpty( this.acts ) ) { let ids2 = []; for( let id in this.acts ) { ids.push( parseInt( id ) ); } // 如果活动发生了变化,则需要重建列表 ids.sort( ( _a, _b ) => { return _a - _b; } ); ids2.sort( ( _a, _b ) => { return _a - _b; } ); if( ids.toString() != ids2.toString() ) { this.acts = {}; nx.debug( "$Acts:活动列表发生改变:" ); nx.debug( "$Acts:老:", ids2.toString() ); nx.debug( "$Acts:新:", ids.toString() ); } } // 列表重建 if( nx.dt.objEmpty( this.acts ) ) { let push = function( _data ) { let cfg = MODS[ _data.source ]; if( nx.dt.objEmpty( cfg ) ) { nx.warn( "$Acts:无效活动:", _data.source ); return; } if( nx.dt.strNEmpty( cfg.mod ) ) { let ref = require( cfg.mod ); let mod = ref.getInstance( _data.source, cfg.mod, true ); if( mod ) { nx.debug( "$ActMods:" + cfg.mod ); mod.bindData( self, _data ); self.acts[ _data.camp_id ] = mod; } } } this.acts = {}; for( let i = 0; i < themes.length; ++i ) { let theme = themes[ i ]; theme.theme_holiday_list.forEach( _t => { _t.theme_id = theme.theme_id; try { push( _t ); } catch( e ) { nx.error( "[异常]:" + ( e ? e.message + "\n" + e.stack : "未知错误!" ) ); if( e && cc.sys.isNative && window && window.__errorHandler ) { window.__errorHandler( e.message, "", "", e.stack ); } } } ); } nx.debug( "$Acts:创建子活动%s个", nx.dt.objLen( this.acts ) ); } for( let id in this.acts ) { let at = this.acts[ id ]; if( at ) { at.reqBaseFromServer(); } } }, // 获取指定主题信息 queryTheme: function( _theme ) { let themes = this.vget( "themes" ); for( let i in themes ) { let tm = themes[ i ]; if( tm && tm.theme_id == _theme ) { return tm; } } }, // 获得指定source的主题信息 queryThemeBySource: function( _source ) { let themes = this.vget( "themes" ); for( let i in themes ) { let tm = themes[ i ]; if( tm && tm.source == _source ) { return tm; } } }, // 查询活动信息 searchAct: function( _camp_id ) { let themes = this.vget( "themes" ); for( let i in themes ) { let theme = themes[ i ]; if( !theme || nx.dt.arrEmpty( theme.theme_holiday_list ) ) { continue; } for( let k in theme.theme_holiday_list ) { let act = theme.theme_holiday_list[ k ]; if( act && act.camp_id == _camp_id ) { return { theme: theme, infos: act, }; } } } nx.error( "$Acts:子活动信息不存在:", _camp_id ); return null; }, // 获取指定主题信息 queryThemeByType: function( _type ) { let themes = this.vget( "themes" ); for( let i in themes ) { let tm = themes[ i ]; if( tm && tm.type == _type ) { return tm; } } }, // 查询子活动 queryAct: function( _camp_id, _warn = true ) { if( !this.acts ) { return; } let act = this.acts[ _camp_id ]; if( !act && _warn ) { nx.error( "$Acts:子活动不存在:", _camp_id ); return null; } return act; }, // ============================================================ // 活动信息 // ============================================================ // 获取活动基础信息 reqActData: function( _camp_id, _cb ) { this.SendProtocal( 16603, { bid: _camp_id }, ( _ret, _data ) => { if( _ret && _data.bid == _camp_id ) { nx.dt.fnInvoke( _cb, true, _data ); } } ); }, // 请求基本信息返回 handle16603: function( _data ) { if( !this.isGoodData( _data ) ) { return; } }, // ============================================================ // "主题卡池-招募(抽奖) // type1: 1-道具抽 // type2: 1-单抽,2-+连抽 // ============================================================ // 卡池抽取 reqDrawFromPool: function( _gid, _type1, _type2, _cb ) { this.SendProtocal( 31111, { group_id: _gid, type1: _type1, type2: _type2, }, _cb ); }, // 卡池抽取返回 handle31111: function( _data ) { if( !this.isGoodData( _data ) ) { return; } }, // ============================================================ // 活动奖励 // ============================================================ // 请求所有活动未领取奖励 reqActsRewards: function( _cb ) { this.SendProtocal( 16602, {}, _cb ); }, // 请求所有活动未领取奖励 onFreshActsRewards: function( _data ) { if( this.isGoodData( _data ) ) { this.onUpdateActsRewards( _data.holiday_list ); } }, // 领取奖励返回结果 onUpdateActsRewards: function( _data ) { if( nx.dt.objGood( _data ) ) { let act = this.queryAct( _data.bid ); if( act ) { act.openTip( "reward", _data.can_get_num != 0 ); } return; } if( nx.dt.arrEmpty( _data ) ) { return; } for( let i = 0; i < _data.length; ++i ) { let ifo = _data[ i ]; let act = this.queryAct( ifo.bid ); if( !act ) { nx.warn( "$Acts:无效活动 ", ifo.bid ); continue; } act.openTip( "reward", ifo.can_get_num != 0 ); } }, // ============================================================ // 活动预告 // ============================================================ // 刷新 freshForeshow: function() { let themes = this.vget( "themes" ); let DATA = game.configs.ad_show; if( nx.dt.arrEmpty( themes ) || nx.dt.objEmpty( DATA ) ) { this.vset( "foreshow", [] ); return; } let ads = []; let find = function( _theme, _camp ) { // 0为常驻 if( _theme == 0 ) { return true; } // 999为不显示 if( _theme == 999 ) { return false; } for( let i in themes ) { let tm = themes[ i ]; if( !tm || tm.theme_id != _theme ) { continue; } for( let k in tm.theme_holiday_list ) { let at = tm.theme_holiday_list[ k ]; if( at && at.camp_id == _camp ) { return true; } } } return false; }; for( let id in DATA.data_adshow_roll ) { let roll = DATA.data_adshow_roll[ id ]; let func = DATA.data_adshow_func[ id ]; if( !roll || !func || !find( func.total_id, func.camp_id ) ) { continue; } let actitem = this.queryAct( func.camp_id ); let ret = nx.bridge.jumper.checkSource( actitem.data.source ); if( nx.dt.objEmpty( ret ) ){ ads.push( { id: id, sc: roll.img, sk: roll.link, name: func.name, img: func.img, link: func.link, is_new: func.is_new, theme_id: func.total_id, camp_id: func.camp_id, ico: func.cli_icon } ); } } this.vset( "foreshow", ads ); }, // ============================================================ // 推送弹窗 // ============================================================ // 今日推送 reqPushPopup: function() { this.SendProtocal( 16621, {} ); }, // 今日推送 onPushPopup: function( _data ) { if( !this.isGoodData( _data ) ) { this.vset( "pushPopup", 0 ); this.vset( "pushToday", false ); return; } this.vset( "pushPopup", _data.id ); this.vset( "pushToday", true ); }, // 关闭推送 reqClosePushPopup: function( _cb ) { this.SendProtocal( 16622, {}, _cb ); }, // 关闭推送回复 onPushPopupClose: function( _data ) { if( !this.isGoodData( _data ) ) { return; } this.vset( "pushToday", false ); }, // ============================================================ // 活动更新通知 // ============================================================ // 固定时间更新 onFreshActs: function() { for( let id in this.acts ) { let act = this.acts[ id ]; if( act ) { act.reqBaseFromServer(); } } }, // ============================================================ // 活动红点提示 // ============================================================ // 组建红点结构 buildTipsView: function() { let themes = this.vget( "themes" ); let tree = { key: "acts", sub: {} }; // 全主题遍历 for( let i in themes ) { let tm = themes[ i ]; let tk = `acts.${ tm.theme_id }`; let ifo = { key: tk, sub: {} }; // 全活动遍历 let arr = tm.theme_holiday_list || []; for( let k in arr ) { let temp = arr[ k ]; let cateId = `${ temp.cate }`; let campId = `${ temp.camp_id }`; let act = this.queryAct( campId, false ); if( !act ) { continue; } // 活动前缀格式: acts.{主题编号}.{Cate类别}.{活动编号} if( !ifo.sub[ cateId ] ) { ifo.sub[ cateId ] = { key: `${ tk }.${ cateId }`, sub: {} }; } if( !ifo.sub[ cateId ].sub[ campId ] ) { ifo.sub[ cateId ].sub[ campId ] = { key: `${ tk }.${ cateId }.${ campId }`, sub: {} }; } let keys = act.tipKeys(); keys = [ "reward" ].concat( keys ); keys.forEach( _key => { ifo.sub[ cateId ].sub[ campId ].sub[ _key ] = { key: `${ tk }.${ cateId }.${ campId }.${ _key }` }; } ); } tree.sub[ `${ tm.theme_id }` ] = ifo; } nx.mTip.append( tree ); return tree; }, // 获取主题提示键 getThemeTipKey: function( _theme_id ) { return "acts." + _theme_id; }, // ============================================================ // 活动引导 // ============================================================ // 活动引导 needGuide: function( _camp_id, _cb ) { this.SendProtocal( 16623, { camp_id: _camp_id }, _cb ); }, // 活动引导 onActGuide: function() { }, // 引导完成上传 reqDoneGuide: function( _camp_id, _cb ) { this.SendProtocal( 16624, { camp_id: _camp_id }, _cb ); }, // 引导上传 onActGuideDone: function() { }, } ); // 模块导出 module.exports = ActManager;