ShakeModel.ts 7.84 KB
import BaseModel from "./BaseModel";
import { AppSdk } from "../../sdk/AppSdk";
import DateUtils from "../../uitl/DateUtils";
import Util, { DataKey } from "../../uitl/Util";
import ObjectInstance from "../../uitl/ObjectInstance";

/**
 * 
 */
export default class ShakeModel extends BaseModel {

    private conf: any;
    private eleProbabilityArr: number[] = [];
    private totalWeight: number = 0;
    private proArrLen: number = 0;
    /**红包出现的总次数每日 */
    private red_day_limit: number = 0;
    private cur_red_day_limit: number = 0;
    /**摇一摇出现的总次数 */
    private shake_day_limit: number = 0;

    private cur_shake_day_limit: number = 0;
    /**
     * red_day_limit	红包每日极限产出	
    *  shake_day_limit	摇一摇每日极限产出	
    * interstitial_cnt 插屏计数(每出现x次通用获得物品界面后,出现插屏)	
    * interstitial_delay	插屏出现延时(毫秒)	
     */
   
    /**
     *  // let result = {
        //     data: {
        //         gameConfig: {
        //             "config": {
        //                 "red_day_limit": 10,
        //                 "shake_day_limit": 50,
        //                 "interstitial_cnt": 2,
        //                 "interstitial_delay": 500
        //             },
        //             "shake": [
        //                 {
        //                     "id": 1,
        //                     "type": 1,
        //                     "weight": 1000,
        //                     "num": 0
        //                 },
        //                 {
        //                     "id": 2,
        //                     "type": 2,
        //                     "weight": 400,
        //                     "num": 0
        //                 },
        //                 {
        //                     "id": 3,
        //                     "type": 3,
        //                     "weight": 300,
        //                     "num": 1
        //                 },
        //                 {
        //                     "id": 4,
        //                     "type": 3,
        //                     "weight": 100,
        //                     "num": 2
        //                 },
        //                 {
        //                     "id": 5,
        //                     "type": 4,
        //                     "weight": 300,
        //                     "num": 1
        //                 },
        //                 {
        //                     "id": 6,
        //                     "type": 4,
        //                     "weight": 100,
        //                     "num": 2
        //                 },
        //                 {
        //                     "id": 7,
        //                     "type": 5,
        //                     "weight": 300,
        //                     "num": 1
        //                 },
        //                 {
        //                     "id": 8,
        //                     "type": 5,
        //                     "weight": 100,
        //                     "num": 2
        //                 },
        //                 {
        //                     "id": 9,
        //                     "type": 6,
        //                     "weight": 300,
        //                     "num": 1
        //                 },
        //                 {
        //                     "id": 10,
        //                     "type": 6,
        //                     "weight": 100,
        //                     "num": 2
        //                 },
        //                 {
        //                     "id": 11,
        //                     "type": 7,
        //                     "weight": 300,
        //                     "num": 1
        //                 },
        //                 {
        //                     "id": 12,
        //                     "type": 7,
        //                     "weight": 100,
        //                     "num": 2
        //                 },
        //                 {
        //                     "id": 13,
        //                     "type": 8,
        //                     "weight": 500,
        //                     "num": 0
        //                 }
        //             ],
        //             'coin_config': []
        //         }
        //     }
        // }
     */
    async init() {
        let result = await AppSdk.I.getGameConfig();
        console.log('---------------ShakeModel-------'+JSON.stringify(result.data))
        
        let data = result.data;
        if (data['gameConfig']) {
            let gameData = data['gameConfig'];
            this.update(this.initData(gameData));
        }
    }

    initData(gameData) {
        let data = new ShakeData;
        let lastTime = Util.I.getItem(DataKey.shakeLastTime) || '0';
        let lt = parseInt(lastTime);
        let last = DateUtils.timeDay(lt);
        let today = DateUtils.today;

        if (last != today) {
            this.cur_shake_day_limit = 0;
            this.cur_red_day_limit = 0;
            lastTime = DateUtils.nowTime.toString();
            Util.I.setItem(DataKey.cur_shake_day_limit, 0);
            Util.I.setItem(DataKey.cur_red_day_limit, 0);
            Util.I.setItem(DataKey.shakeLastTime, lastTime);

        } else {
            let cur_red_day_limit = Util.I.getItem(DataKey.cur_red_day_limit) || '0';
            this.cur_red_day_limit = parseInt(cur_red_day_limit);
            let cur_shake_day_limit = Util.I.getItem(DataKey.cur_shake_day_limit) || '0';
            this.cur_shake_day_limit = parseInt(cur_shake_day_limit);
        }

        data.config = gameData['config'];
        gameData['shake'].forEach(ele => {
            data.shakeWeightList.push(ele);
        });
        this.conf = [];
        this.conf = data.shakeWeightList;
        this.initProbability();
        this.shake_day_limit = data.config.shake_day_limit;
        this.red_day_limit = data.config.red_day_limit;
        return data;
    }

    initProbability() {
        this.totalWeight = 0;
        this.conf.forEach((ele, index) => {
            this.totalWeight += ele.weight;
            let itemW = ele.weight;
            if (index > 0)
                itemW += this.eleProbabilityArr[index - 1];
            this.eleProbabilityArr = [...this.eleProbabilityArr, itemW];
        })
        this.proArrLen = this.eleProbabilityArr.length;

    }

    getShakeType() {
        ++this.cur_shake_day_limit;
        Util.I.setItem(DataKey.cur_shake_day_limit, this.cur_shake_day_limit);

        if (this.cur_shake_day_limit > this.shake_day_limit) {
            /**'次数达到上限,请明天再来*/

            /**在此处可以弹框等处理 */
            return false;
        }
        let ran = Math.floor(Math.random() * this.totalWeight);
        for (let index = 0; index < this.proArrLen; index++) {
            const ele = this.eleProbabilityArr[index];
            if (ele >= ran) {
                if (0 === index || 1 === index) {
                    ++this.cur_red_day_limit;
                    Util.I.setItem(DataKey.cur_red_day_limit, this.cur_red_day_limit);
                    if (this.cur_red_day_limit > this.red_day_limit) {
                        return this.conf[this.proArrLen - 1];

                    }
                }
                return this.conf[index];
            }
        }
        return this.conf[this.proArrLen - 1];
    }

    get data(): ShakeData {
        return this._data;
    }

    static get I(): ShakeModel {
        return ObjectInstance.get(ShakeModel) as ShakeModel;
    }
}


class ShakeData {
    config: ShakeConfig;
    shakeWeightList: ShakeWeightData[] = [];
}
interface ShakeConfig {
    red_day_limit: number,
    shake_day_limit: number,
    interstitial_cnt: number,
    interstitial_delay: number
}

interface ShakeWeightData {
    id: number;
    type: number;
    weight: number;
    num: number;
}