158 lines
4.7 KiB
TypeScript
158 lines
4.7 KiB
TypeScript
import cloud from '@lafjs/cloud'
|
|
const db = cloud.database();
|
|
import Ip2region from 'ip2region';
|
|
const query = new Ip2region();
|
|
export default async function (ctx: FunctionContext) {
|
|
//console.log(ctx.body);
|
|
const uid = ctx.body.uid;
|
|
const unionid = ctx.body.unionid;
|
|
let useravatar = ctx.body.useravatar;
|
|
let username = ctx.body.username;
|
|
let useravatarIcon = ctx.body.useravatarIcon;
|
|
let userCat = ctx.body.userCat;
|
|
let count = Number(ctx.body.count) || 10;//今日通关数
|
|
let levelTJ = ctx.body.levelTJ || false;
|
|
if (!count) {
|
|
return { code: 0, data: null, msg: "未获取到levelAmount" };
|
|
}
|
|
let address = ctx.body.address || "北京";
|
|
let ranks = await rank(uid, count, address, levelTJ, useravatar, username, useravatarIcon, userCat, unionid);
|
|
return ranks;
|
|
}
|
|
async function rank(uid, add, address, levelTJ, useravatar, username, useravatarIcon, userCat, unionid) {
|
|
let rankInfo = await db.collection("sRank").where({ _id: "69fd2f048463a95668c5e952" }).getOne();
|
|
let total = rankInfo.data.total;
|
|
if (total != null) {
|
|
total = JSON.parse(total);
|
|
} else {
|
|
total = {};
|
|
}
|
|
if (total["all"]) {
|
|
let isHave = false;
|
|
for (let i = 0; i < total["all"].length; i++) {
|
|
if (total["all"][i].id == unionid) {
|
|
isHave = true;
|
|
total["all"][i].useravatar = useravatar,
|
|
total["all"][i].username = username,
|
|
total["all"][i].useravatarIcon = useravatarIcon,
|
|
total["all"][i].addLevel = add;
|
|
if (hasUserCat(userCat)) {
|
|
total["all"][i].userCat = String(userCat);
|
|
}
|
|
total["all"][i].time = Date.now();
|
|
break;
|
|
}
|
|
}
|
|
if (isHave == false) {
|
|
let length = total["all"].length;
|
|
if (length < 10 || add > total["all"][length - 1].addLevel) {
|
|
total["all"].push({
|
|
id: unionid,
|
|
addLevel: add,
|
|
time: Date.now(),
|
|
username: username,
|
|
useravatar: useravatar,
|
|
useravatarIcon: useravatarIcon,
|
|
...(hasUserCat(userCat) ? { userCat: String(userCat) } : {}),
|
|
});
|
|
}
|
|
}
|
|
} else {
|
|
total["all"] = [{
|
|
id: unionid,
|
|
addLevel: add,
|
|
time: Date.now(),
|
|
username: username,
|
|
useravatar: useravatar,
|
|
useravatarIcon: useravatarIcon,
|
|
...(hasUserCat(userCat) ? { userCat: String(userCat) } : {}),
|
|
}];
|
|
}
|
|
total["all"].sort((a, b) => {
|
|
if (a.addLevel !== b.addLevel) {
|
|
return b.addLevel - a.addLevel;
|
|
}
|
|
return a.time - b.time;
|
|
});
|
|
if (total["all"].length >= 10) {
|
|
total["all"] = total["all"].slice(0, 10);
|
|
}
|
|
if (total[address]) {
|
|
let isHave = false;
|
|
for (let i = 0; i < total[address].length; i++) {
|
|
if (total[address][i].id == unionid) {
|
|
isHave = true;
|
|
total[address][i].useravatar = useravatar,
|
|
total[address][i].username = username,
|
|
total[address][i].useravatarIcon = useravatarIcon,
|
|
total[address][i].addLevel = add;
|
|
if (hasUserCat(userCat)) {
|
|
total[address][i].userCat = String(userCat);
|
|
}
|
|
total[address][i].time = Date.now();
|
|
break;
|
|
}
|
|
}
|
|
if (isHave == false) {
|
|
let length = total[address].length;
|
|
if (length < 30 || add > total[address][length - 1].addLevel) {
|
|
total[address].push({
|
|
id: unionid,
|
|
addLevel: add,
|
|
time: Date.now(),
|
|
username: username,
|
|
useravatar: useravatar,
|
|
useravatarIcon: useravatarIcon,
|
|
...(hasUserCat(userCat) ? { userCat: String(userCat) } : {}),
|
|
});
|
|
}
|
|
}
|
|
} else {
|
|
total[address] = [{
|
|
id: unionid,
|
|
addLevel: add,
|
|
time: Date.now(),
|
|
username: username,
|
|
useravatar: useravatar,
|
|
useravatarIcon: useravatarIcon,
|
|
...(hasUserCat(userCat) ? { userCat: String(userCat) } : {}),
|
|
}];
|
|
}
|
|
total[address].sort((a, b) => {
|
|
if (a.addLevel !== b.addLevel) {
|
|
return b.addLevel - a.addLevel;
|
|
}
|
|
return a.time - b.time;
|
|
});
|
|
if (total[address].length >= 30) {
|
|
total[address] = total[address].slice(0, 30);
|
|
}
|
|
if (!total["role"]) {
|
|
total["role"] = {};
|
|
}
|
|
if (levelTJ == 0) {
|
|
if (!total["role"][address]) {
|
|
total["role"][address] = 1;
|
|
} else {
|
|
total["role"][address] += 1;
|
|
let length = total[address].length;
|
|
if (length < 10) {
|
|
total["role"][address] = length;
|
|
}
|
|
}
|
|
}
|
|
await db.collection("sRank").where({ _id: "69fd2f048463a95668c5e952" }).update({ total: JSON.stringify(total) });
|
|
let rank = -1;
|
|
for (let i = 0; i < total[address].length; i++) {
|
|
if (total[address][i].id == unionid) {
|
|
rank = i + 1;
|
|
break;
|
|
}
|
|
}
|
|
return rank;
|
|
}
|
|
|
|
function hasUserCat(userCat) {
|
|
return userCat !== undefined && userCat !== null && userCat !== "";
|
|
}
|