MatchMaster/server/laf-cloud/functions/drawCat.ts
2026-07-30 12:22:02 +08:00

76 lines
2.2 KiB
TypeScript

import cloud from '@lafjs/cloud';
import Utils from "@/Utils";
const db = cloud.database();
const level1 = [4, 5, 6, 7, 8];
const level2 = [9, 10];
const level3 = [11];
const DEFAULT_CAT_ARR = [1, 2, 3];
const FILM_CONSUME_TABLE = [5, 10, 25, 35, 55, 70, 100, 120];
/**猫咪抽卡 */
export default async function (ctx: FunctionContext) {
const uid = ctx.body.uid;
const dbname = ctx.body.gameName == "iaa" ? "usersAd" : "users";
const res = await db.collection(dbname).where({ _id: uid }).getOne();
if (!res.data) {
return { code: 0, data: null, msg: "未获取到玩家信息" };
}
if (res.data.token && !Utils.checkToken(ctx.body.token, res.data.token)) {
return { code: 0, data: null, msg: "token校验失败" };
}
let film = res.data.film;
if (!film && film != 0) {
film = res.data.levelAmount;
}
const catArr = Array.isArray(res.data.catArr)
? [...res.data.catArr]
: [...DEFAULT_CAT_ARR];
const list1 = level1.filter(catId => !catArr.includes(catId));
const list2 = level2.filter(catId => !catArr.includes(catId));
const list3 = level3.filter(catId => !catArr.includes(catId));
const total = list1.length * 150 + list2.length * 50 + list3.length * 8;
if (total == 0) {
return { code: 0, data: null, msg: "已抽取所有猫咪" };
}
const totalCount = level1.length + level2.length + level3.length;
const ownedCount = totalCount - (list1.length + list2.length + list3.length);
const filmCost = FILM_CONSUME_TABLE[Math.min(ownedCount, 7)];
film = Number(film) || 0;
if (film < filmCost) {
return {
code: 0,
data: { film, cost: filmCost },
msg: "代币不足,闯关获取更多代币!",
};
}
film -= filmCost;
const weightRoll = Math.floor(Math.random() * total);
let candidates;
if (weightRoll < list1.length * 150) {
candidates = list1;
} else if (weightRoll < list1.length * 150 + list2.length * 50) {
candidates = list2;
} else {
candidates = list3;
}
const reward = candidates[Math.floor(Math.random() * candidates.length)];
catArr.push(reward);
await db.collection(dbname).where({ _id: uid }).update({ catArr, film });
return {
code: 1,
data: { catArr, reward, film },
msg: "成功",
};
}