0a3cee0e-d616-496c-ac8f-17c583b5ee07.js 8 KB
"use strict";
cc._RF.push(module, '0a3ce4O1hZJbKyPF8WDte4H', 'GameRoleDataModel');
// script/game/model/GameRoleDataModel.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const simba_config_manager_1 = require("simba-config-manager");
const AVG_1 = require("../../avg/AVG");
const EditorEnums_1 = require("../../avg/EditorEnums");
const GameTextData_1 = require("../../common/gameplay/gamedata/GameTextData");
const UIManager_1 = require("../../common/gameplay/managers/UIManager");
const StringUtils_1 = require("../../common/utils/StringUtils");
const RelationLevelConfig_1 = require("../../config/RelationLevelConfig");
const GameModelManager_1 = require("./GameModelManager");
class GameRoleDataModel {
    constructor() {
        this._cfg = undefined;
        this._curSkin = -1;
        this._skinMaps = new Set();
        this._roleName = "";
        this._itemMaps = new Map();
    }
    setConfig(cfg) {
        this._cfg = cfg;
        this.initDate();
    }
    getConfig() {
        return this._cfg;
    }
    initDate() {
        this.initSkin();
        this.initData();
        this.initVariablesChange();
    }
    initVariablesChange() {
        let func = (scope, varName, prevValue, value) => {
            value = value === undefined ? 0 : value;
            prevValue = prevValue === undefined ? 0 : prevValue;
            let newLike = value - prevValue;
            UIManager_1.UIManager.showToast(StringUtils_1.StringUtils.format(GameModelManager_1.GameModelManager.getLanguageTxt(GameTextData_1.GameTextData.GAME_TEXT_LIKE_CHANGE_VALUE), this._cfg.name, newLike));
        };
        AVG_1.GameRecord.onRecordVariableChange("like" + this._cfg.id, func);
        AVG_1.GameRecord.onGlobalVariableChange("like" + this._cfg.id, func);
    }
    hasSkin(id) {
        return this._skinMaps.has(id);
    }
    getRoleLike() {
        let g = AVG_1.GameRecord.globalVariables["like" + this._cfg.id];
        g = g ? g : 0;
        // let r = GameRecord.recordVariables["like" + this._cfg.id];
        let r = AVG_1.PlotManager.getAllRecordVariableValue("like" + this._cfg.id);
        r = r ? r : 0;
        let l = g + r;
        return l;
    }
    addRoleLike(like, globalRecord = true) {
        let pre = this.getRoleLike();
        let after = pre + like;
        after = after > 0 ? after : 0;
        let offset = after - pre;
        if (globalRecord) {
            let g = AVG_1.GameRecord.globalVariables["like" + this._cfg.id];
            g = g ? g : 0;
            g += offset;
            AVG_1.GameRecord.globalVariables["like" + this._cfg.id] = g;
        }
        else {
            let r = AVG_1.GameRecord.recordVariables["like" + this._cfg.id];
            r = r ? r : 0;
            r += offset;
            AVG_1.GameRecord.recordVariables["like" + this._cfg.id] = r;
        }
        AVG_1.GameRecord.saveRecord();
    }
    getPortrait(face = EditorEnums_1.FaceType.Normal, skin) {
        if (skin === undefined && this._curSkin > 0)
            skin = this._curSkin;
        return "textures/portrait/" + this._cfg.id + "/" + (skin !== undefined ? skin + "/" : "") + face;
    }
    getBust(skin) {
        if (skin === undefined && this._curSkin > 0)
            skin = this._curSkin;
        let ret = "textures/bust/" + this._cfg.id;
        if (skin) {
            ret += skin;
        }
        return ret;
    }
    getHeadIcon() {
        let ret = "/textures/head_icon/" + this._cfg.id;
        if (this._curSkin > 0) {
            ret += "_" + this._curSkin;
        }
        return ret;
    }
    getNameIcon() {
        let ret = "/textures/name_icon/" + this._cfg.id;
        return ret;
    }
    getRoleLikeLevel() {
        let configs = simba_config_manager_1.ConfigManager.getAllConfig(RelationLevelConfig_1.relationLevelConfig);
        let level = 0;
        let like = this.getRoleLike();
        const maxLevel = Object.keys(configs).length;
        let levelValue = 0;
        for (let id = 1; id <= maxLevel; id++) {
            levelValue = configs[id].relation_value;
            if (like >= levelValue) {
                like -= levelValue;
                level = id;
            }
            else {
                break;
            }
        }
        return { level, currLike: like, nextLevelLike: levelValue, maxLevel };
    }
    addSkin(id) {
        this._skinMaps.add(id);
        let skinsstr = "";
        let index = 0;
        for (let value of this._skinMaps) {
            skinsstr += value;
            if (index != this._skinMaps.size - 1) {
                skinsstr += ",";
            }
            ++index;
        }
        AVG_1.GameRecord.globalVariables["skins_" + this._cfg.id + "_skins"] = skinsstr;
        AVG_1.GameRecord.autoSave();
    }
    setCurSkin(id) {
        this._curSkin = id;
        AVG_1.GameRecord.globalVariables["curskin_" + this._cfg.id + "_skin"] = id;
        AVG_1.GameRecord.autoSave();
    }
    getCurSkin() {
        return this._curSkin;
    }
    initSkin() {
        this._curSkin = AVG_1.GameRecord.globalVariables["curskin_" + this._cfg.id + "_skin"];
        let skins = AVG_1.GameRecord.globalVariables["skins_" + this._cfg.id + "_skins"];
        skins = skins === undefined ? "" : skins;
        let skinSpilt = skins.split(",");
        for (let index = 0; index < skinSpilt.length; ++index) {
            this._skinMaps.add(parseInt(skinSpilt[index]));
        }
    }
    getOwnedSkins() {
        return this._skinMaps;
    }
    initData() {
        this._itemMaps.clear();
        let props = GameModelManager_1.GameModelManager.getItemConfigs();
        for (let key in props) {
            let value = props[key];
            let recordNum = AVG_1.GameRecord.globalVariables["prop_" + this._cfg.id + "_" + value.id + "_num"];
            recordNum = recordNum === undefined ? 0 : recordNum;
            this._itemMaps.set(value.id, recordNum);
        }
    }
    getProps(id) {
        return this._itemMaps.get(id);
    }
    addProps(id, num) {
        let ret = { result: false, errorinfo: "" };
        let cfg = GameModelManager_1.GameModelManager.getItemConfig(id);
        let validProp = cfg ? true : false;
        if (!validProp) {
            ret.errorinfo = "prop id " + id + " not exist";
            return ret;
        }
        let max = cfg['max_num'] ? cfg['max_num'] : Number.MAX_VALUE;
        if (Math.abs(num) > max) {
            ret.errorinfo = "input num over max , num abs value limit " + max;
            return ret;
        }
        let value = this._itemMaps.get(id);
        if (value >= max && num > 0) {
            ret.result = false;
            ret.errorinfo = "cur value over or equal max value limit " + max;
            return ret;
        }
        else {
            value += num;
            ret.result = true;
        }
        if (value > max) {
            ret.errorinfo = "value over max , set num as max";
        }
        else if (value === max) {
            ret.errorinfo = "value equal max";
        }
        else if (value < 0) {
            ret.errorinfo = "value less 0 , set num as 0";
        }
        else {
            ret.errorinfo = "succeed";
        }
        value = value > max ? max : value;
        value = value < 0 ? 0 : value;
        this._itemMaps.set(id, value);
        AVG_1.GameRecord.globalVariables["prop_" + this._cfg.id + "_" + id + "_num"] = value;
        AVG_1.GameRecord.saveRecord();
        return ret;
    }
    getRoleName() {
        if (!this._roleName || this._roleName === "") {
            return this._cfg.name;
        }
        else {
            return this._roleName;
        }
    }
    setRoleName(name) {
        this._roleName = name;
    }
    /**
     *
     * @param skin 皮肤id
     */
    getBagPortraitByRoleId(skin) {
        if (this._cfg.id == 2) { //自己
            return "textures/portrait/" + this._cfg.id + "/" + (skin !== undefined ? skin + "/" : "") + 7;
        }
        else {
            return "textures/portrait/" + this._cfg.id + "/" + (skin !== undefined ? skin + "/" : "") + EditorEnums_1.FaceType.Normal;
        }
    }
}
exports.default = GameRoleDataModel;

cc._RF.pop();