import { ApiCall } from "tsrpc";
import { Reqdata, Resdata } from "../../monopoly/protocols/user/Ptldata";
import { PayFun } from "../../public/pay";
import { PlayerFun } from "../../public/player";

export default async function (call: ApiCall<Reqdata, Resdata>) {
    let uid = call.req.data.playerId
    let upData = call.req.data.playerData

    // 无此玩家
    let getgud = await G.mongodb.collection('user').findOne({ uid: uid });
    if (!getgud) {
        return call.error('', { code: 1002, message: globalThis.lng.wuciwanjia });
    }

    // 无法修改的数据
    let falseData = ["name", "uid", "bindUid"]
    // 需要修改的数据key
    let _tmp = Object.keys(upData)
    let _setData = {}
    for (let index = 0; index < _tmp.length; index++) {
        const element = _tmp[index];
        if (upData[element] == getgud[element] || falseData.indexOf(element) != -1) {
            continue
        }
        _setData[element] = upData[element]
    }

    if (Object.keys(_setData).length > 0) {
        let renamecall = PayFun.getCall(getgud);
        await PlayerFun.addAttr(renamecall, _setData);
        G.server.sendMsgByUid(uid, 'msg_s2c/PlayerChange', _setData)
    }

    call.succ({ message: "玩家导入数据成功", code: 0 });
}