import { ApiCall } from "tsrpc";
import { ReqUpUser, ResUpUser } from "../../cross/protocols/kbzz/PtlUpUser";
import { RankKbzz } from '../../public/rank/rank_kbzz';

export default async function (call: ApiCall<ReqUpUser, ResUpUser>) {


    G.mongodb.collection('kbzzGroupUser').updateOne(
        { uid: call.req.uid },
        {
            $inc: { score: call.req.addScore },
            $set: call.req.info == null ? {} : { info: call.req.info }
        }
    );

    G.mongodb.collection('kbzzGroupTroop').updateOne(
        { group: call.req.group, troop: call.req.troop },
        {
            $inc: { score: call.req.addScore }
        }
    );

    G.mongodb.collection('kbzzGroupLog').updateOne(
        { group: call.req.group },
        {
            $push: {
                log: {
                    $each: [call.req.log],
                    $slice: -50
                }
            }
        },
        { upsert: true }
    );

    if (call.req.info != null) {
        RankKbzz.groupList[call.req.group].addNew({
            ...call.req.info,
            valArr: [call.req.allScore]
        });
    }

    call.succ({});
}