import { ApiCall } from "tsrpc";
import { ReqOpenFort, ResOpenFort } from "../../shared/protocols/slzd/PtlOpenFort";
import { getEnemy } from './fun';

export default async function (call: ApiCall<ReqOpenFort, ResOpenFort>) {
    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.enemy[fort]) {
        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 }) }
        );

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