import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../public/player';
import { ReqRefresh, ResRefresh } from "../../shared/protocols/slzd/PtlRefresh";
import { getEnemy } from './fun';

export default async function (call: ApiCall<ReqRefresh, ResRefresh>) {
    if (!G.gc.slzd.forts[call.req.fort]) return call.error(globalThis.lng.shiwu_6);
    let fort = call.req.fort;
    let dbData = await G.mongodb.collection('slzdUser').findOne({ uid: call.uid });

    if (dbData.data.useRefreshNum >= G.gc.slzd.refreshNum) {
        await PlayerFun.checkNeedIsMeet(call, G.gc.slzd.refreshNeed);
        await PlayerFun.cutNeed(call, G.gc.slzd.refreshNeed);
    }

    let enemy = (await getEnemy(call.conn.gud, fort)).map(e => {
        return {
            data: e,
            result: null
        };
    });

    G.mongodb.collection('slzdUser').updateOne(
        { uid: call.uid },
        { $set: G.mongodb.createTreeObj({ key: 'data.enemy.' + fort, val: enemy }, { key: 'data.useRefreshNum', val: dbData.data.useRefreshNum + 1 }) }
    );

    call.succ({
        enemy: enemy
    });
}