GameDotMgr.ts 12 KB
import { SDK, TimeManager } from "simba-sdk";
import { RoleType } from "../avg/EditorEnums";
import { GameRecord } from "../avg/AVG";
import { ConfigManager } from "simba-config-manager";
import { role } from "../config/Role";
import { channel, GameConfig } from "../GameConfig";
import { CostType, DotShopTotalType, DotShopType, EventLocation, EventOperationType, ExposureType, GuideLocation, GuideOperationType, LoadingState, MomentOptionType, PlotSessionState, ShareLocation, ShowType, SpecialDateContentStatus, SpecialDateContentType, TaskDotType, TutorialState, TutorialType } from "./Enums";
import { GameModelManager } from "./model/GameModelManager";

interface BaseDotElements {
    energy: number,
    gold: number,
    silver: number,
    grace: { roleId: number, grace: number }[],
    user_register_version: string,
    login_duration: number,
    total_duration: number,
    daily_duration: number,
}

export default class GameDotMgr {
    private static readonly _instance: GameDotMgr = new GameDotMgr();
    private _majorIds: number[] = [];

    public static getInstance(): GameDotMgr {
        return GameDotMgr._instance;
    }

    public async dot(eventKey: string, data: any, category?: string) {
        // if (channel === "dummy") return;
        // category = category ? category : "yybs_event";
        // let baseElements = this.getBaseElements();
        // let biRet = await SDK.bi(category, eventKey, baseElements, data);
    }

    private getBaseElements(): BaseDotElements {
        let regV = GameRecord.globalVariables["register_version"];
        if (!regV) {
            regV = GameConfig.gameVersion;
            GameRecord.globalVariables["register_version"] = regV;
            GameRecord.saveRecord();
        }
        let player = GameModelManager.getPlayerData();
        let base: BaseDotElements = {
            energy: player.getEnergy(),
            gold: player.getGoldCoin(),
            silver: player.getClothCoin(),
            grace: [],
            user_register_version: regV,
            login_duration: TimeManager.getGameTime(),
            total_duration: TimeManager.getTotalGameTime(),
            daily_duration: TimeManager.getTodayGameTime(),
        };
        let gArr: { roleId: number, grace: number }[] = [];
        if (!this._majorIds || 0 === this._majorIds.length) {
            let r = ConfigManager.getAllConfig(role);
            for (let id in r) {
                let c = r[id];
                if (c && c.RoleType === RoleType.Role_Major) {
                    this._majorIds.push(c.id);
                }
            }
        }
        for (let i = 0; i < this._majorIds.length; i++) {
            let rid = this._majorIds[i];
            let role = GameModelManager.getRoleData(rid);
            let gv = role?.getRoleLike();
            gv = gv ? gv : 0;
            let item: { roleId: number, grace: number } = {
                roleId: rid,
                grace: gv
            }
            gArr.push(item);
        }
        base.grace = gArr;
        return base;
    }

    public async dotClickUI(uiName: string) {
        let eventKey = "click_ui";
        let recordKey = "click_ui_count_" + uiName + "_t_" + GameModelManager.getCurDayStr();
        let clickCount = GameRecord.globalVariables[recordKey] as number;
        if (undefined === clickCount) {
            clickCount = 0;
        }
        clickCount += 1;
        GameRecord.globalVariables[recordKey] = clickCount;
        GameRecord.saveRecord();
        // console.log("dotClickUI = ", { click_ui: clickCount, ui_location: uiName });
        await this.dot(eventKey, { click_ui: clickCount, ui_location: uiName }, "click_ui");
    }

    public async dotLoading(state: LoadingState) {
        let eventKey = "loading";
        await this.dot(eventKey, { loading: state }, "loading");
    }

    /**
     * 
     * @param playerId 玩家的PlayerId
     * @param gameConstData 兑换码兑换的道具ID
     */
    public async dotExchangeCodeSuccess(playerId: string, gameConstData?: string) {
        let eventKey = "exchange_code_success";
        await this.dot(eventKey, { exchange_code_success: playerId, gameConstData: gameConstData }, "exchange_code_success");
    }

    public async dotCost(cType: CostType, cNum: number) {
        // let eventKey = "cost";
        // let recordKey = cType + GameModelManager.getCurDayStr();
        // let count = GameRecord.globalVariables[recordKey] as number;
        // if (undefined === count) {
        //     count = 0;
        // }
        // count = count + cNum;
        // GameRecord.globalVariables[recordKey] = count;
        // GameRecord.saveRecord();
        // let data = {};
        // data[cType] = count;
        // // console.log("dotCost = ", data);
        // await this.dot(eventKey, data, "cost");
    }

    public async dotPlot(session: number, state: PlotSessionState) {
        let eventKey = "plot";
        let v = session + "_" + state;
        // console.log("dotPlot = ", { plot_state: v });
        await this.dot(eventKey, { plot_state: v }, "plot");
    }

    public async dotExposure(eType: ExposureType | string) {
        // let eventKey = "show";
        // // console.log("dotExposure = ", { show: eType });
        // await this.dot(eventKey, { show: eType }, "show");
    }

    public async dotShop(sType: DotShopType, param?: any) {
        // let eventKey = "shop";
        // let data = {};

        // switch (sType) {
        //     case DotShopType.LuckyDraw:
        //         {
        //             data[sType] = param.location;
        //         }
        //         break;
        //     case DotShopType.Clothes:
        //         {
        //             if (param.isWear) {
        //                 data[sType] = "wear_" + param.sid + "_" + param.rid;
        //             } else {
        //                 data[sType] = "have_" + param.sid + "_" + param.rid;
        //             }
        //         }
        //         break;
        // }
        // // console.log("dotShop = ", data);
        // await this.dot(eventKey, data, "shop");
    }

    public async dotShopTotal(sType: DotShopTotalType, param?: any) {
        // let eventKey = "shop_total";
        // let data = {};
        // switch (sType) {
        //     case DotShopTotalType.ShowGift:
        //         {
        //             let rKey = "g_i_t_show_c" + GameModelManager.getCurDayStr();;
        //             let total = GameRecord.globalVariables[rKey] as number;
        //             total = total ? total : 0;
        //             total++;
        //             data[sType] = {
        //                 show_id: "show_item_" + param.id,
        //                 total_show: total
        //             }
        //             GameRecord.globalVariables[rKey] = total;
        //         }
        //         break;
        //     case DotShopTotalType.BuyItem:
        //         {
        //             let rKey = "g_i_t_buy_c" + GameModelManager.getCurDayStr();;
        //             let total = GameRecord.globalVariables[rKey] as number;
        //             total = total ? total : 0;
        //             total++;
        //             data[sType] = {
        //                 buy_id: "buy_" + param.id,
        //                 total_buy: total
        //             }
        //             GameRecord.globalVariables[rKey] = total;
        //         }
        //         break;
        //     case DotShopTotalType.SendGift:
        //         {
        //             let rKey = "g_i_t_send_c" + GameModelManager.getCurDayStr();;
        //             let total = GameRecord.globalVariables[rKey] as number;
        //             total = total ? total : 0;
        //             total++;
        //             data[sType] =
        //             {
        //                 send_id: param.gid,
        //                 role_id: param.rid,
        //                 total_send: total
        //             };
        //             GameRecord.globalVariables[rKey] = total;
        //         }
        //         break;
        //     case DotShopTotalType.Popup:
        //         {
        //             data[sType] = param.popType;
        //         }
        //         break;
        // }
        // GameRecord.autoSave();
        // // console.log("dotShopTotal = ", data);
        // await this.dot(eventKey, data, "shop_total");
    }

    public async dotTask(taskDotType: TaskDotType, tId: number) {
        // let eventKey = "task";
        // let data = {};
        // data["daily_task"] = {};
        // switch (taskDotType) {
        //     case TaskDotType.CompleteTask:
        //         {
        //             data["daily_task"] = "completed_" + tId;
        //         }
        //         break;
        //     case TaskDotType.GetTaskRewards:
        //         {
        //             data["daily_task"] = "get_rewards_" + tId;
        //         }
        //         break;
        // }
        // await this.dot(eventKey, data, "task");
    }

    public async dotMoment(id: number, optionType: MomentOptionType) {
        // let eventKey = "circle_of_friends";
        // let v = "circle_" + optionType;
        // await this.dot(eventKey, { circle: v }, "circle_of_friends");
    }

    public async dotTutorial(tType: TutorialType, state: TutorialState, param: any) {
        let eventKey = "tutorial";
        let k = "" + tType;
        let v = "" + param.session;
        switch (state) {
            case TutorialState.SelectSentence:
                {
                    v += state + param.plotId;
                }
                break;
            case TutorialState.SelectSentenceRet:
                {
                    k += "_plot_select";
                    v += TutorialState.SelectSentence + param.plotId + state + param.select;
                }
                break;
            case TutorialState.Progress:
                {
                    k += "_plot_progress";
                    v += state + param.progress;
                }
                break;
        }
        let data = {};
        data[k] = v;
        // console.log("dotTutorial = ", data);
        await this.dot(eventKey, data, "tutorial");
    }

    public async dotShare(shareType: ShowType, shareLocation: ShareLocation | string) {
        let eventKey = "share";
        // console.log("dotShare = ", { share_type: shareType, share_location: shareLocation });
        await this.dot(eventKey, { share_type: shareType, share_location: shareLocation }, "share");
    }

    public async dotVideo(videoType: ShowType, videoLocation: string) {
        let eventKey = "video";
        // console.log("dotVideo = ", { video_type: videoType, video_location: videoLocation });
        await this.dot(eventKey, { video_type: videoType, video_location: videoLocation }, "video")
    }

    //todo
    public async dotShareRes(location: ShareLocation, titleId: number, imgId: number) {
        let eventKey = "share_source";
        let v = location + "_" + titleId + "_" + imgId;
        await this.dot(eventKey, { share_source: v }, "share_source");
    }

    public async dotGuide(oprType: GuideOperationType, location: GuideLocation) {
        let eventKey = "guide";
        let v = location + oprType;
        // console.log("dotGuide = ",{ guide: v });
        await this.dot(eventKey, { guide: v }, "guide");
    }


    public async dotEvent(oprType: EventOperationType, location: EventLocation) {
        let eventKey = 'event';
        let v = location;
        let data = {};
        data[oprType] = v;
        // console.log("dotEvent = ",data);
        await this.dot(eventKey, data, 'event');
    }

    public async dotSpecialPlot(sceneId: number, plotId: number, selectIndex: number, contenType: SpecialDateContentType, contentStatus: SpecialDateContentStatus) {
        let eventKey = 'special_plot';
        let data = {
            scene: sceneId,
            plot: plotId,
            select: selectIndex,
            contentype: contenType,
            status: contentStatus
        }
        // console.log("dotSpecialPlot = ", data);
        await this.dot(eventKey, data, 'special_plot');
    }

}