import { ApiCall } from "tsrpc";
import { DxltFun } from '../../public/dxlt';
import { ReqOpen, ResOpen } from "../../shared/protocols/dixialeitai/PtlOpen";
import { PublicShared } from '../../shared/public/public';
import { HongDianChange } from "../hongdian/fun";

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

    let collection = await G.mongodb.collection('dxlt').findOne({ uid: call.uid });
    if (!collection) return call.error('', { code: 2 });

    let { _id, uid, ...data } = collection;

    if (Object.keys(data).length == 2) {//说明第一次进入游戏
        data.buff = {};
        data.item = {};
        data.over = false;
        data.curLayer = 1;
        data.maxLayer = 0;
        data.curMaxLayer = 0;
        data.toDayLayer = 0;
        data.recordLayer = 0;
        data.mibao = 0;
        data.enemyState = {};
        data.killPrizeRec = [];
        data.passPrizeRec = [];
        data.shop = {};
        data.killBoss = 0;
        call.succ(data);
    } else if (PublicShared.getToDayZeroTime() >= PublicShared.getToDayZeroTime(data.resetTime) + 2 * 24 * 3600) {//每两天重置
        data.item = {};
        data.buff = {};
        data.toDayLayer = 0;
        data.enemyState = {};
        data.killPrizeRec = [];
        data.shop = {};
        data.killBoss = 0;
        DxltFun.backLayer(data);
        call.error('', { code: 2 });
        HongDianChange.sendChangeKey(call.uid, ['dxlthd']);
    } else {
        call.succ(data);
    }
    G.ioredis.set(`dxlt:${call.uid}`, JSON.stringify(data));
    G.mongodb.collection('dxlt').updateOne({ uid: call.uid }, { $set: { ...data } });
}