GameRoleDataModel.ts 8.3 KB
import { ConfigManager } from "simba-config-manager";
import { DeepReadonlyObject } from "simba-utils";
import { GameRecord, PlotManager } from "../../avg/AVG";
import { FaceType, RoleType } from "../../avg/EditorEnums";
import { GameTextData } from "../../common/gameplay/gamedata/GameTextData";
import { UIManager } from "../../common/gameplay/managers/UIManager";
import { StringUtils } from "../../common/utils/StringUtils";
import { relationLevelConfig } from "../../config/RelationLevelConfig";
import { IRole } from "../../config/Role";
import { GameModelManager } from "./GameModelManager";

export default class GameRoleDataModel {
    private _cfg: DeepReadonlyObject<IRole> = undefined as unknown as DeepReadonlyObject<IRole>;
    private _curSkin: number = -1;
    private _skinMaps: Set<number> = new Set<number>();
    private _roleName: string = "";

    private _itemMaps: Map<number, number> = new Map<number, number>();

    public setConfig(cfg: DeepReadonlyObject<IRole>) {
        this._cfg = cfg;
        this.initDate();
    }

    public getConfig() {
        return this._cfg;
    }

    initDate() {
        this.initSkin();
        this.initData();
        this.initVariablesChange();
    }

    initVariablesChange() {
        let func = (scope: string, varName: string, prevValue: string | number, value: string | number) => {
            value = value === undefined ? 0 : value;
            prevValue = prevValue === undefined ? 0 : prevValue;
            let newLike = (value as number) - (prevValue as number);
            UIManager.showToast(StringUtils.format(GameModelManager.getLanguageTxt(GameTextData.GAME_TEXT_LIKE_CHANGE_VALUE), this._cfg.name, newLike));
        }

        GameRecord.onRecordVariableChange("like" + this._cfg.id, func);

        GameRecord.onGlobalVariableChange("like" + this._cfg.id, func);
    }

    hasSkin(id: number) {
        return this._skinMaps.has(id);
    }

    getRoleLike(): number {
        let g = GameRecord.globalVariables["like" + this._cfg.id];
        g = g ? g : 0;
        // let r = GameRecord.recordVariables["like" + this._cfg.id];
        let r = PlotManager.getAllRecordVariableValue("like" + this._cfg.id);
        r = r ? r : 0;
        let l = g + r;
        return l;
    }

    addRoleLike(like: number, globalRecord: boolean = true) {
        let pre = this.getRoleLike();
        let after = pre + like;
        after = after > 0 ? after : 0;
        let offset = after - pre;
        if (globalRecord) {
            let g = GameRecord.globalVariables["like" + this._cfg.id];
            g = g ? g : 0;
            g += offset;
            GameRecord.globalVariables["like" + this._cfg.id] = g;
        } else {
            let r = GameRecord.recordVariables["like" + this._cfg.id] as number;
            r = r ? r : 0;
            r += offset;
            GameRecord.recordVariables["like" + this._cfg.id] = r;
        }
        GameRecord.saveRecord();
    }

    getPortrait(face = FaceType.Normal, skin?: number) { // TODO 默认皮肤更改?
        if (skin === undefined && this._curSkin > 0) skin = this._curSkin;
        return "textures/portrait/" + this._cfg.id + "/" + (skin !== undefined ? skin + "/" : "") + face;
    }

    getBust(skin?: number) {
        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 = ConfigManager.getAllConfig(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: number) {
        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;
        }

        GameRecord.globalVariables["skins_" + this._cfg.id + "_skins"] = skinsstr;
        GameRecord.autoSave();
    }

    setCurSkin(id: number) {
        this._curSkin = id;
        GameRecord.globalVariables["curskin_" + this._cfg.id + "_skin"] = id;
        GameRecord.autoSave();
    }

    getCurSkin(): number {
        return this._curSkin;
    }

    initSkin() {
        this._curSkin = GameRecord.globalVariables["curskin_" + this._cfg.id + "_skin"];

        let skins = GameRecord.globalVariables["skins_" + this._cfg.id + "_skins"] as string;
        skins = skins === undefined ? "" : skins;
        let skinSpilt = skins.split(",");
        for (let index = 0; index < skinSpilt.length; ++index) {
            this._skinMaps.add(parseInt(skinSpilt[index]));
        }
    }

    getOwnedSkins(): Set<number> {
        return this._skinMaps;
    }

    initData() {
        this._itemMaps.clear();
        let props = GameModelManager.getItemConfigs();
        for (let key in props) {
            let value = props[key];
            let recordNum = GameRecord.globalVariables["prop_" + this._cfg.id + "_" + value.id + "_num"] as number;
            recordNum = recordNum === undefined ? 0 : recordNum;
            this._itemMaps.set(value.id, recordNum);
        }
    }

    public getProps(id: number) {
        return this._itemMaps.get(id);
    }

    public addProps(id: number, num: number): { result: boolean, errorinfo: string } {
        let ret: { result: boolean, errorinfo: string } = { result: false, errorinfo: "" };
        let cfg = GameModelManager.getItemConfig(id);
        let validProp: boolean = 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) as number;
        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);
        GameRecord.globalVariables["prop_" + this._cfg.id + "_" + id + "_num"] = value;
        GameRecord.saveRecord();
        return ret;
    }


    public getRoleName() {
        if (!this._roleName || this._roleName === "") {
            return this._cfg.name;
        } else {
            return this._roleName;
        }
    }

    public setRoleName(name: string) {
        this._roleName = name;
    }

    /**
     * 
     * @param skin 皮肤id
     */
    public getBagPortraitByRoleId(skin?: number) {
        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 + "/" : "") + FaceType.Normal;

        }
    }

}