注册 登录
n/program · 2024-12-21 · 401 次浏览
u/Str荣誉会员王者
n/programu/Str 荣誉会员 · 2024-12-21

刚刚又搓了个用户数量显示器,加了二分法

```js
const fs = require('fs');
const fetch = require('node-fetch');

let userId = 100;
let rf = false;
let cache = null;

try {
userId = require('./config.json');
} catch (error) {}

async function save() {
if (userId === cache) return;
fs.writeFileSync('./config.json', JSON.stringify(userId), 'utf-8');
cache = userId;
}

async function isUser(id) {
try {
let req = await fetch(https://www.nodeloc.com/api/users/${id});
return !!(await req.json()).data;
} catch (error) {
return await isUser(id);
}
}

async function sub(id, retry = 0) {
if (await isUser(id)) {
if (retry > 0) console.log(警告:此数字此时可能并不准确!);
if (retry > 10 && !rf) {
console.log(警告:已失去同步,二分法同步中...);
await ef();
return userId;
}
return await sub(id + 1, retry + 1);
}
return id - 1;
}

async function ef() {
rf = true;

let pending = true;
let min = userId;
let max = userId + 10000;

while(pending) {
let id = Math.floor((max + min) / 2);
if (await isUser(id)) {
min = id;
} else {
max = id;
}
console.log([${min}, ${max}] (${id}));
if (max - min == 1) {
userId = min;
pending = false;
}
}

await save();
rf = false;
}

async function updateUserId() {
userId = await sub(userId + 1);
await save();
}

async function render() {
if (rf) {
setTimeout(rendr, 1000);
return;
}
await updateUserId();
console.clear();
console.log(UserId: ${userId});
render();
}

render();
```

使用方法照旧,新版本不需要很长的同步时间

```
警告:此数字此时可能并不准确!
警告:已失去同步,二分法同步中...
[10000, 15000] (15000)
[10000, 12500] (12500)
[10000, 11250] (11250)
[10000, 10625] (10625)
[10000, 10312] (10312)
[10000, 10156] (10156)
[10000, 10078] (10078)
[10000, 10039] (10039)
[10019, 10039] (10019)
[10019, 10029] (10029)
[10019, 10024] (10024)
[10021, 10024] (10021)
[10021, 10022] (10022)
```
```
UserId: 10021
```
0
1
登录后参与讨论。
u/udc2b6nr 广告
QuicksilverVPN

快银 VPN,一键连接全球节点,线路稳定、延迟低。支持 Windows、macOS 等多平台,TUN 全局模式覆盖全部应用流量。无需复杂配置,下载即用。

umami.dnsl.top 了解更多

1 条回复

排序
  1. yucho 荣誉会员 钻石 2024-12-21

    当前用户数:[img src=https://y.eo.mk/NLUserCount/?type=img width=100]

    0