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