import { ApiCall } from "tsrpc";
import { Reqrename, Resrename } from "../../monopoly/protocols/user/Ptlrename";
import { PlayerFun } from "../../public/player";
import { PayFun } from "../../public/pay";

export default async function (call: ApiCall<Reqrename, Resrename>) {
    let uid = call.req.data.playerId
    let _name = call.req.data.nickname

    // 名称已被使用
    let _user = await G.mongodb.collection('user').findOne({ "name": _name });
    if (_user) {
        return call.error('', { code: 1001, message: globalThis.lng.nameyicunzai });
    }

    // g123 后台修改不限次数，不扣资源
    // let num = call.conn.gud.changeNameNum || 0;
    // let need = G.gc.common.usercom.changename[num] || G.gc.common.usercom.changename.slice(-1)[0];

    // 无此玩家
    let gud = await G.mongodb.collection('user').findOne({ uid: uid });
    if (!gud) {
        return call.error('', { code: 1002, message: globalThis.lng.wuciwanjia });
    }
    let rescall = PayFun.getCall(gud);
    await PlayerFun.addAttr(rescall, { name: _name });
    G.server.sendMsgByUid(uid, 'msg_s2c/PlayerChange', { name: _name })

    call.succ({ message: "玩家昵称更新成功", code: 0 });

    G.emit('PLAYER_CHANGE_NAME', gud, _name);
}