import { ApiCall } from "tsrpc";
import { ReqOpen, ResOpen } from "../../shared/protocols/shootGame/PtlOpen";
import { PublicShared } from '../../shared/public/public';

export default async function (call: ApiCall<ReqOpen, ResOpen>) {

    const data = await G.mongodb.collection('playerInfo', 'shootGame').findOne({ uid: call.uid, type: 'shootGame' });

    if (!data) {
        G.mongodb.collection('playerInfo', 'shootGame').insertOne({
            passList: {},
            receiveList: [],
            refreshTime: G.time,
            uid: call.uid,
            type: 'shootGame'
        });
        return call.succ({
            passList: {},
            receiveList: []
        });
    }

    if (PublicShared.getToDayZeroTime() > data.refreshTime) {
        data.receiveList = [];
        data.refreshTime = G.time;
        G.mongodb.collection('playerInfo', 'shootGame').updateOne({ uid: call.uid, type: 'shootGame' }, { $set: { receiveList: [], refreshTime: G.time } });
    }

    call.succ({
        passList: data.passList,
        receiveList: data.receiveList
    });
}