import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../public/player';
import { ZhanLingTasks } from '../../public/zhanling';
import { ReqFast, ResFast } from "../../shared/protocols/ganhai/PtlFast";
import { HongDianChange } from "../hongdian/fun";
import { GanHaiRed, calculatePrize } from './ApiOpen';

export default async function (call: ApiCall<ReqFast, ResFast>) {
    let dbData = await G.mongodb.collection('ganhai').findOne({ uid: call.uid });

    if (dbData.ship == null) return call.error('', { code: -1 });
    if (dbData.ship.sTime + G.gc.ganhai.ships[dbData.ship.index].time < G.time) return call.error('', { code: -2 });

    await PlayerFun.checkNeedIsMeet(call, G.gc.ganhai.ships[dbData.ship.index].kshsNeed);
    await PlayerFun.cutNeed(call, G.gc.ganhai.ships[dbData.ship.index].kshsNeed);
    let prize = calculatePrize(dbData.ship, G.time);
    await PlayerFun.sendPrize(call, prize);

    G.mongodb.collection('ganhai').updateOne({ uid: call.uid }, { $set: { ship: null } });
    HongDianChange.sendChangeKey(call.uid, ['gonghuihd']);
    ZhanLingTasks.addLog(call.uid, 'week', `ganhai_finish`);
    GanHaiRed.addList(call.uid, null);

    call.succ({
        prize: prize,
        change: {
            ship: null,
        }
    });
}