MatchMaster/server/laf-cloud/functions/drawHead.ts

112 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import cloud from '@lafjs/cloud';
import Utils from "@/Utils";
const db = cloud.database();
const level1 = [19, 20, 21, 22, 23];
const level2 = [24, 25];
const level3 = [26];
/**头像抽卡 */
export default async function (ctx: FunctionContext) {
const uid = ctx.body.uid;
let dbname = "users";
const gameName = ctx.body.gameName;
if (gameName == "iaa") {
dbname = "usersAd";
}
let res = await db.collection(dbname).where({ _id: uid }).getOne();
if (!res.data) {
return {
code: 0, data: null, msg: "未获取到玩家信息"
}
}
const token1 = ctx.body.token;
if (res.data && res.data.token) {
let istoken = Utils.checkToken(token1, res.data.token);
if (!istoken) {
return { code: 0, data: null, msg: "token校验失败" };
}
}
let film = res.data.film;
if (!film && film != 0) {
film = res.data.levelAmount;
}
let headArr = res.data.headArr;
if (!headArr) {
headArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] ];
}
let list1 = [];
let list2 = [];
let list3 = [];
for (let i = 0; i < level1.length; i++) {
if (!headArr.includes(level1[i])) {
list1.push(level1[i]);
}
}
for (let i = 0; i < level2.length; i++) {
if (!headArr.includes(level2[i])) {
list2.push(level2[i]);
}
}
for (let i = 0; i < level3.length; i++) {
if (!headArr.includes(level3[i])) {
list3.push(level3[i]);
}
}
let total = list1.length * 150 + list2.length * 50 + list3.length * 8;
if (total == 0) {
return { code: 0, data: null, msg: "已抽取所有头像" }
}
let totalCount = level1.length + level2.length + level3.length;
// 计算
let count = totalCount - (list1.length + list2.length + list3.length);
// 阶梯消耗表索引0对应消耗5索引1对应消耗10以此类推
let filmConsumeTable = [5, 10, 25, 35, 55, 70, 100, 120];
// 根据数量获取对应的消耗数量超过7时取最后一个120
let filmCost = filmConsumeTable[Math.min(count, 7)];
film = Number(film) || 0;
if (film < filmCost) {
return { code: 0, data: { film: film, cost: filmCost }, msg: "代币不足,闯关获取更多代币!" }
}
// 检查通过后再扣除,余额始终以服务端数据为准。
film -= filmCost;
let num = Math.floor(Math.random() * total);
let list = [];
if (num < list1.length * 150) {
list = list1;
} else if (num < list1.length * 150 + list2.length * 50) {
list = list2;
} else {
list = list3;
}
let num2 = Math.floor(Math.random() * list.length);
if (list[num2] == 26) {
for (let i = 0; i < level1.length; i++) {
if (!headArr.includes(level1[i])) {
headArr.push(level1[i]);
}
}
for (let i = 0; i < level2.length; i++) {
if (!headArr.includes(level2[i])) {
headArr.push(level2[i]);
}
}
for (let i = 0; i < level3.length; i++) {
if (!headArr.includes(level3[i])) {
headArr.push(level3[i]);
}
}
} else {
if (!headArr.includes(list[num2])) {
headArr.push(list[num2]);
}
}
await db.collection(dbname).where({ _id: uid }).update({ headArr: headArr, film: film });
return { code: 1, data: { headArr: headArr, reward: list[num2], film: film }, msg: "成功" };
}