import { player } from '../../../shared/protocols/user/type';

export function checkPlayerGift(old: player, cur: Partial<player>) {
    let payIds: string[] = [];
    let keys = Object.keys(G.gc.xianshilibao);

    for (let [k, v] of Object.entries(cur)) {
        if (keys.filter(key => key.indexOf(k) != -1).length < 1) continue;

        for (let tag = old[k] + 1; tag <= v; tag++) {
            if (G.gc.xianshilibao[k + tag]) payIds.push(k + tag);
        }
    }

    payIds.length > 0 && addGift(old.uid, payIds);
}

export async function addGift(uid: string, payIds: string[]) {

    let insert = payIds.map(id => {
        return {
            payId: id,
            eTime: G.time + G.gc.xianshilibao[id].time
        };
    });

    G.mongodb.collection('syncBtns').updateOne(
        { uid: uid },
        {
            //@ts-ignore
            $push: {
                'xianshilibao.pays': { $each: insert }
            }
        },
        { upsert: true }
    );

    G.server.sendMsgByUid(uid, 'msg_s2c/Xianshilibao', insert);
}