
import { gameMethod } from "../common/gameMethod";
import { SevBack } from "../common/Xys";

export default class SevDataMng {
    static initSevback() {

    }
    static sevBackUpdate(_old: SevBack, update: SevBack): SevBack {
        if (update == null) {
            return _old
        }
        //拷贝一份 避免改到原数据(不能拷贝，玩家数据大的影响性能)
        let old = _old//gameMethod.objCopy(_old)
        let updateCp: SevBack = gameMethod.objCopy(update);
        if (old == null) {
            old = {}
        }

        for (const key1 in updateCp) {
            if (old[key1] == null) {
                old[key1] = updateCp[key1];
                // continue;
            }
            //不是obj，直接忽略
            if (typeof updateCp[key1] != "object") {
                continue
            }
            for (const key2 in updateCp[key1]) {
                if (old[key1][key2] == null) {
                    old[key1][key2] = updateCp[key1][key2];
                    // continue;
                }
                if (key2 == "a") {
                    old[key1][key2] = updateCp[key1][key2];
                    continue;
                }
                if (key2 == "u") {
                    if (old[key1]["a"] == null) {
                        old[key1]["a"] = updateCp[key1][key2];
                        continue;
                    }
                    for (const _id in updateCp[key1][key2]) {
                        old[key1]["a"][_id] = updateCp[key1][key2][_id];
                    }
                    continue;
                }
                if (key2 == "d") {
                    if (old[key1]["a"] == null) {
                        old[key1]["a"] = {};
                    }
                    for (const _id in updateCp[key1][key2]) {
                        delete old[key1]["a"][_id]
                    }
                    continue;
                }
                //看第三层是不是aud，不是不管
                let isAud = false;
                for (const key3 in updateCp[key1][key2]) {
                    if (key3 == "a") {
                        isAud = true;
                        old[key1][key2][key3] = updateCp[key1][key2][key3];
                    }
                    if (key3 == "u") {
                        isAud = true;
                        if (old[key1][key2]["a"] == null) {
                            old[key1][key2]["a"] = updateCp[key1][key2][key3];
                            break;
                        }
                        for (const _id in updateCp[key1][key2][key3]) {
                            old[key1][key2]["a"][_id] = updateCp[key1][key2][key3][_id];
                        }
                    }
                    if (key3 == "d") {
                        isAud = true;
                        if (old[key1][key2]["a"] == null) {
                            old[key1][key2]["a"] = {};
                        }
                        for (const _id in updateCp[key1][key2][key3]) {
                            delete old[key1][key2]["a"][_id]
                        }
                    }
                    continue;
                }
                if (!isAud) {
                    old[key1][key2] = updateCp[key1][key2]; //只有两层 全覆盖
                }
            }
        }

        return old
    }
}