From ec68b193a0384b9d87c4bfe83f63263d729a86ec Mon Sep 17 00:00:00 2001 From: dy Date: Tue, 19 Dec 2023 22:06:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E8=B7=AF=E7=8B=82=E9=A3=99=E6=80=A7?= =?UTF-8?q?=E8=83=BD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api_s2c/gonglukuangbiao/ApiOpen.ts | 50 ++++++++++++++++++-------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/src/api_s2c/gonglukuangbiao/ApiOpen.ts b/src/api_s2c/gonglukuangbiao/ApiOpen.ts index 419f224..bb484b4 100644 --- a/src/api_s2c/gonglukuangbiao/ApiOpen.ts +++ b/src/api_s2c/gonglukuangbiao/ApiOpen.ts @@ -50,19 +50,42 @@ export default async function (call: ApiCall) { call.succ(db as any); } -export async function refreshEnemys(call: ApiCall) { - let section = [call.conn.gud.mapId - 8, call.conn.gud.mapId + 8]; - //let allUser = Object.values(await G.redis.get('user')).filter(u => u.mapId >= section[0] && u.mapId <= section[1] && u.uid != call.uid); - //let dbUser = await G.mongodb.collection('wanted').find({uid: {$in: allUser.map(u => u.uid)}}).toArray(); +let lastRefreshTime: number +let enemyObject: object = {} - let allUser = await G.mongodb.find('user', { - $and: [ - {mapId: {$gte: section[0]}}, - {mapId: {$lte: section[1]}}, - {uid: {$ne: call.uid}} - ] - }, ['uid']); - let dbUser = await G.mongodb.find('wanted', {uid: {$in: allUser.map(u => u.uid)}}, ['uid', 'wanted']); +/** + * 每小时刷新公路狂飙用户,功能超过30级开放,只查询30级以上的用户,超过80级的,80级到100级为一档 + */ +async function checkAndRefreshEnemy() { + if (lastRefreshTime + 3600 > G.time) return + lastRefreshTime = G.time + for (let i = 3; i <= 8; i ++) { + let users = await G.mongodb.collection('user').aggregate([ + { + $match: { + $and: [ + {lv: {$gte: i * 10}}, + {lv: {$lt: i == 8 ? 101 : (i + 1) * 10}}, + ] + } + }, + { + $project: {uid: 1} + }, + { + $sample: {size: 100} + } + ]).toArray() + let dbUser = await G.mongodb.find('wanted', {uid: {$in: users.map(u => u.uid)}}, ['uid', 'wanted']); + + enemyObject[i] = dbUser || [] + } +} + +export async function refreshEnemys(call: ApiCall) { + if (!lastRefreshTime) await checkAndRefreshEnemy() + + let dbUser = enemyObject[Math.floor(call.conn.gud.lv / 10)] let enemys: joinFightData[] = []; @@ -74,7 +97,6 @@ export async function refreshEnemys(call: ApiCall) { let filter = dbUser.filter(u => conf.need <= u.wanted && u.wanted < needMax); let uids = filter.map(f => f.uid); dbUser.remove(u => uids.includes(u.uid)); - let a = configs[0].npc.random() let enemy = uids.length > 0 ? await FightFun.getPlayerFightData(uids.random()) : formatNpcData(configs[0].npc.random()); if (!enemy) enemy = formatNpcData(configs[0].npc.random()) if (enemy.player.isNpc) enemy.player.name = G.gc.gonglukuangbiao.npcname.random() @@ -83,7 +105,7 @@ export async function refreshEnemys(call: ApiCall) { PublicShared.randomNum(G.gc.gonglukuangbiao.wanted[0].wanted[0], G.gc.gonglukuangbiao.wanted[0].wanted[1]) enemys.push(enemy); } - + checkAndRefreshEnemy() return enemys.reverse(); }