Spaces:
Paused
Paused
Update endpoints/antibot.js
Browse files- endpoints/antibot.js +42 -94
endpoints/antibot.js
CHANGED
|
@@ -2,50 +2,8 @@ const fs = require('fs');
|
|
| 2 |
const path = require('path');
|
| 3 |
const { extractTextFromImage } = require('./imageProcessor');
|
| 4 |
|
| 5 |
-
function
|
| 6 |
-
|
| 7 |
-
'0': 'o', '1': 'i', '2': 'z', '3': 'e', '4': 'a',
|
| 8 |
-
'5': 's', '6': 'g', '7': 't', '8': 'b', '9': 'g',
|
| 9 |
-
'z': '2', 'o': '0', 'i': '1', 'e': '3', 'a': '4',
|
| 10 |
-
's': '5', 'g': '6', 't': '7', 'b': '8'
|
| 11 |
-
};
|
| 12 |
-
|
| 13 |
-
return leetStr.split('').map(char => leetMap[char.toLowerCase()] || char).join('');
|
| 14 |
-
}
|
| 15 |
-
|
| 16 |
-
function calculateMathExpression(expression) {
|
| 17 |
-
try {
|
| 18 |
-
expression = expression.replace(/\s/g, '');
|
| 19 |
-
|
| 20 |
-
if (expression.includes('*')) {
|
| 21 |
-
const parts = expression.split('*');
|
| 22 |
-
if (parts.length === 2) {
|
| 23 |
-
const num1 = parseInt(parts[0]);
|
| 24 |
-
const num2 = parseInt(parts[1]);
|
| 25 |
-
if (!isNaN(num1) && !isNaN(num2)) {
|
| 26 |
-
return (num1 * num2).toString();
|
| 27 |
-
}
|
| 28 |
-
}
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
if (expression.includes('+')) {
|
| 32 |
-
const parts = expression.split('+');
|
| 33 |
-
if (parts.length === 2) {
|
| 34 |
-
const num1 = parseInt(parts[0]);
|
| 35 |
-
const num2 = parseInt(parts[1]);
|
| 36 |
-
if (!isNaN(num1) && !isNaN(num2)) {
|
| 37 |
-
return (num1 + num2).toString();
|
| 38 |
-
}
|
| 39 |
-
}
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
const result = eval(expression);
|
| 43 |
-
if (!isNaN(result)) {
|
| 44 |
-
return result.toString();
|
| 45 |
-
}
|
| 46 |
-
} catch (e) {}
|
| 47 |
-
|
| 48 |
-
return expression;
|
| 49 |
}
|
| 50 |
|
| 51 |
function isValueMatch(value, targetSoal) {
|
|
@@ -58,19 +16,11 @@ function isValueMatch(value, targetSoal) {
|
|
| 58 |
'VI': '6', 'VII': '7', 'VIII': '8', 'IX': '9', 'X': '10'
|
| 59 |
};
|
| 60 |
|
| 61 |
-
const
|
| 62 |
-
const normalizedValue = calculatedValue.toLowerCase().trim();
|
| 63 |
const normalizedSoal = targetSoal.toLowerCase().trim();
|
| 64 |
|
| 65 |
if (normalizedValue === normalizedSoal) return true;
|
| 66 |
|
| 67 |
-
const leetValue = leetToText(normalizedValue);
|
| 68 |
-
const leetSoal = leetToText(normalizedSoal);
|
| 69 |
-
|
| 70 |
-
if (leetValue === normalizedSoal) return true;
|
| 71 |
-
if (normalizedValue === leetSoal) return true;
|
| 72 |
-
if (leetValue === leetSoal) return true;
|
| 73 |
-
|
| 74 |
const mappedValue = numberMap[normalizedValue] || numberMap[normalizedValue.toUpperCase()];
|
| 75 |
const mappedSoal = numberMap[normalizedSoal] || numberMap[normalizedSoal.toUpperCase()];
|
| 76 |
|
|
@@ -89,13 +39,7 @@ async function antibot(data) {
|
|
| 89 |
const mainText = await extractTextFromImage(mainBuffer);
|
| 90 |
|
| 91 |
const soalText = mainText.response;
|
| 92 |
-
|
| 93 |
-
let soalArray;
|
| 94 |
-
if (soalText.replace(/[,;:.]/g, ' ').split(' ').filter(n => n.trim()).length === 1) {
|
| 95 |
-
soalArray = soalText.split('').filter(n => n.trim());
|
| 96 |
-
} else {
|
| 97 |
-
soalArray = soalText.replace(/[,;:.]/g, ' ').split(' ').filter(n => n.trim());
|
| 98 |
-
}
|
| 99 |
|
| 100 |
const soalRaw = soalArray;
|
| 101 |
const soalLeet = soalArray;
|
|
@@ -108,10 +52,12 @@ async function antibot(data) {
|
|
| 108 |
const botBuffer = Buffer.from(bot.img, 'base64');
|
| 109 |
const botText = await extractTextFromImage(botBuffer);
|
| 110 |
|
|
|
|
|
|
|
| 111 |
botResults.push({
|
| 112 |
id: bot.id,
|
| 113 |
text: botText.response,
|
| 114 |
-
value:
|
| 115 |
});
|
| 116 |
|
| 117 |
} catch (error) {
|
|
@@ -125,14 +71,12 @@ async function antibot(data) {
|
|
| 125 |
}
|
| 126 |
}
|
| 127 |
|
| 128 |
-
|
| 129 |
-
console.log("Soal:", soalArray);
|
| 130 |
-
console.log("Bot Results:", botResults.map(b => `${b.id}: '${b.value}'`));
|
| 131 |
-
|
| 132 |
const result = [];
|
| 133 |
const usedIds = new Set();
|
| 134 |
-
let
|
| 135 |
|
|
|
|
| 136 |
for (let i = 0; i < soalArray.length; i++) {
|
| 137 |
const targetSoal = soalArray[i];
|
| 138 |
let foundId = null;
|
|
@@ -142,8 +86,7 @@ async function antibot(data) {
|
|
| 142 |
isValueMatch(bot.value, targetSoal)) {
|
| 143 |
foundId = bot.id;
|
| 144 |
usedIds.add(bot.id);
|
| 145 |
-
|
| 146 |
-
console.log(`✅ Match found: Bot ${bot.id} ('${bot.value}') → Soal '${targetSoal}'`);
|
| 147 |
break;
|
| 148 |
}
|
| 149 |
}
|
|
@@ -151,46 +94,51 @@ async function antibot(data) {
|
|
| 151 |
result.push({ id: foundId });
|
| 152 |
}
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
console.log("🔄 Filling empty slots with remaining bots...");
|
| 158 |
-
|
| 159 |
for (let i = 0; i < result.length; i++) {
|
| 160 |
if (!result[i].id) {
|
| 161 |
for (const bot of botResults) {
|
| 162 |
if (!usedIds.has(bot.id) && bot.value && bot.value.trim() !== '') {
|
| 163 |
result[i].id = bot.id;
|
| 164 |
usedIds.add(bot.id);
|
| 165 |
-
console.log(`➡️ Filled slot ${i} with bot ${bot.id}`);
|
| 166 |
break;
|
| 167 |
}
|
| 168 |
}
|
| 169 |
}
|
| 170 |
}
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
soalLeet: soalLeet,
|
| 177 |
-
botResults: botResults,
|
| 178 |
-
result: result
|
| 179 |
}
|
| 180 |
-
}
|
| 181 |
} else {
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
soal: soalRaw,
|
| 187 |
-
soalLeet: soalLeet,
|
| 188 |
-
botResults: botResults,
|
| 189 |
-
result: "invalid"
|
| 190 |
-
}
|
| 191 |
-
};
|
| 192 |
}
|
| 193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
} catch (error) {
|
| 195 |
console.error("Antibot function error:", error);
|
| 196 |
return {
|
|
@@ -199,7 +147,7 @@ async function antibot(data) {
|
|
| 199 |
data: {
|
| 200 |
soal: [],
|
| 201 |
botResults: [],
|
| 202 |
-
result:
|
| 203 |
}
|
| 204 |
};
|
| 205 |
}
|
|
|
|
| 2 |
const path = require('path');
|
| 3 |
const { extractTextFromImage } = require('./imageProcessor');
|
| 4 |
|
| 5 |
+
function mapAnswer(soalArray, jawaban, botIndex) {
|
| 6 |
+
return jawaban;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
}
|
| 8 |
|
| 9 |
function isValueMatch(value, targetSoal) {
|
|
|
|
| 16 |
'VI': '6', 'VII': '7', 'VIII': '8', 'IX': '9', 'X': '10'
|
| 17 |
};
|
| 18 |
|
| 19 |
+
const normalizedValue = value.toLowerCase().trim();
|
|
|
|
| 20 |
const normalizedSoal = targetSoal.toLowerCase().trim();
|
| 21 |
|
| 22 |
if (normalizedValue === normalizedSoal) return true;
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
const mappedValue = numberMap[normalizedValue] || numberMap[normalizedValue.toUpperCase()];
|
| 25 |
const mappedSoal = numberMap[normalizedSoal] || numberMap[normalizedSoal.toUpperCase()];
|
| 26 |
|
|
|
|
| 39 |
const mainText = await extractTextFromImage(mainBuffer);
|
| 40 |
|
| 41 |
const soalText = mainText.response;
|
| 42 |
+
const soalArray = soalText.replace(/[,;]/g, ' ').split(' ').filter(n => n.trim());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
const soalRaw = soalArray;
|
| 45 |
const soalLeet = soalArray;
|
|
|
|
| 52 |
const botBuffer = Buffer.from(bot.img, 'base64');
|
| 53 |
const botText = await extractTextFromImage(botBuffer);
|
| 54 |
|
| 55 |
+
const mappedValue = mapAnswer(soalArray, botText.response, i);
|
| 56 |
+
|
| 57 |
botResults.push({
|
| 58 |
id: bot.id,
|
| 59 |
text: botText.response,
|
| 60 |
+
value: mappedValue
|
| 61 |
});
|
| 62 |
|
| 63 |
} catch (error) {
|
|
|
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
| 74 |
+
// LOGIC BARU: Hitung yang cocok dulu
|
|
|
|
|
|
|
|
|
|
| 75 |
const result = [];
|
| 76 |
const usedIds = new Set();
|
| 77 |
+
let matchedCount = 0;
|
| 78 |
|
| 79 |
+
// STEP 1: CARI YANG COCOK
|
| 80 |
for (let i = 0; i < soalArray.length; i++) {
|
| 81 |
const targetSoal = soalArray[i];
|
| 82 |
let foundId = null;
|
|
|
|
| 86 |
isValueMatch(bot.value, targetSoal)) {
|
| 87 |
foundId = bot.id;
|
| 88 |
usedIds.add(bot.id);
|
| 89 |
+
matchedCount++;
|
|
|
|
| 90 |
break;
|
| 91 |
}
|
| 92 |
}
|
|
|
|
| 94 |
result.push({ id: foundId });
|
| 95 |
}
|
| 96 |
|
| 97 |
+
// STEP 2: ISI BERDASARKAN LOGIC BARU
|
| 98 |
+
if (matchedCount >= 2) {
|
| 99 |
+
// Kalo ada 2 atau 3 yang cocok, isi yang kosong dengan sisa bot
|
|
|
|
|
|
|
| 100 |
for (let i = 0; i < result.length; i++) {
|
| 101 |
if (!result[i].id) {
|
| 102 |
for (const bot of botResults) {
|
| 103 |
if (!usedIds.has(bot.id) && bot.value && bot.value.trim() !== '') {
|
| 104 |
result[i].id = bot.id;
|
| 105 |
usedIds.add(bot.id);
|
|
|
|
| 106 |
break;
|
| 107 |
}
|
| 108 |
}
|
| 109 |
}
|
| 110 |
}
|
| 111 |
+
} else if (matchedCount === 1) {
|
| 112 |
+
// Kalo cuma 1 yang cocok, yang kosong kasih "invalid"
|
| 113 |
+
for (let i = 0; i < result.length; i++) {
|
| 114 |
+
if (!result[i].id) {
|
| 115 |
+
result[i].id = 'invalid';
|
|
|
|
|
|
|
|
|
|
| 116 |
}
|
| 117 |
+
}
|
| 118 |
} else {
|
| 119 |
+
// Kalo ga ada yang cocok, semua kasih "invalid"
|
| 120 |
+
for (let i = 0; i < result.length; i++) {
|
| 121 |
+
result[i].id = 'invalid';
|
| 122 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
}
|
| 124 |
|
| 125 |
+
// Pastikan semua slot terisi
|
| 126 |
+
for (let i = 0; i < result.length; i++) {
|
| 127 |
+
if (!result[i].id) {
|
| 128 |
+
result[i].id = 'invalid';
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
return {
|
| 133 |
+
success: true,
|
| 134 |
+
data: {
|
| 135 |
+
soal: soalRaw,
|
| 136 |
+
soalLeet: soalLeet,
|
| 137 |
+
botResults: botResults,
|
| 138 |
+
result: result
|
| 139 |
+
}
|
| 140 |
+
};
|
| 141 |
+
|
| 142 |
} catch (error) {
|
| 143 |
console.error("Antibot function error:", error);
|
| 144 |
return {
|
|
|
|
| 147 |
data: {
|
| 148 |
soal: [],
|
| 149 |
botResults: [],
|
| 150 |
+
result: []
|
| 151 |
}
|
| 152 |
};
|
| 153 |
}
|