import { ApiCall } from "tsrpc";
import { RedisCollections2 } from '../../module/redis';
import { ReqGetList, ResGetList } from "../../shared/protocols/equip/PtlGetList";

export default async function (call: ApiCall<ReqGetList, ResGetList>) {
    let list: ResGetList['list'] = {};
    //let kvList: k_v<RedisCollections2['equip']> = {};
    let arrList = await G.mongodb.collection('equip').find({ uid: call.uid }).toArray();

    let equipCon = G.gc.equip
    let color = {}
    let maxequiplv = 0
    arrList.forEach(v => {
        let d = G.mongodb.conversionIdObj(v);
        //kvList[G.formatRedisKey(d._id)] = d;
        list[d._id] = d;

        if (v.lv > maxequiplv) maxequiplv = v.lv
        if (!color[equipCon[v.equipId].colour]) color[equipCon[v.equipId].colour] = 0
        color[equipCon[v.equipId].colour] += 1

    });
    // 记录玩家最大等级，注册任务用
    await G.mongodb.collection('playerInfo', 'usertasklog').updateOne({ uid: call.conn.uid, type: 'usertasklog' },
        { $set: { maxequiplv: maxequiplv, equipcolor: color } }, { upsert: true })


    let recLshd = await G.mongodb.collection('playerInfo', 'lshd_equip').findOne({ uid: call.conn.uid, type: 'lshd_equip' });
    let { uid, _id, type, ...equips } = (recLshd || {});

    call.conn.lshd.equip = equips || {};
    call.succ({
        list: list,
        lshd: equips || {}
    });
}