Skip to content

Commit b29a575

Browse files
Integrate Pawtect protection into WPlace API
1 parent 608e4cb commit b29a575

File tree

1 file changed

+120
-44
lines changed

1 file changed

+120
-44
lines changed

src/Auto-Image.js

Lines changed: 120 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import {
44
EMBEDDED_LANGUAGES,
55
} from 'embedded-assets';
66

7+
import {
8+
enhancedAPI,
9+
PawtectWASM,
10+
FingerprintGenerator,
11+
utils
12+
} from './pawtect-wasm.js';
13+
714
(async () => {
815
// CONFIGURATION CONSTANTS
916
const CONFIG = {
@@ -3264,74 +3271,97 @@ import {
32643271
}
32653272
}
32663273

3267-
// WPLACE API SERVICE
3274+
// ENHANCED WPLACE API SERVICE WITH PAWTECT PROTECTION
32683275
const WPlaceService = {
3276+
// Initialize enhanced API on first use
3277+
async _ensureInitialized() {
3278+
if (!enhancedAPI.isInitialized) {
3279+
console.log('🚀 Initializing Enhanced WPlace API with Pawtect protection...');
3280+
await enhancedAPI.initialize();
3281+
}
3282+
},
3283+
32693284
async paintPixelInRegion(regionX, regionY, pixelX, pixelY, color) {
32703285
try {
3286+
// Ensure enhanced API is initialized
3287+
await this._ensureInitialized();
3288+
3289+
// Ensure Turnstile token is available
32713290
await ensureToken();
3272-
if (!turnstileToken) return 'token_error';
3273-
const payload = {
3274-
coords: [pixelX, pixelY],
3275-
colors: [color],
3276-
t: turnstileToken,
3277-
};
3278-
const res = await fetch(
3279-
`https://backend.wplace.live/s0/pixel/${regionX}/${regionY}`,
3280-
{
3281-
method: 'POST',
3282-
headers: { 'Content-Type': 'text/plain;charset=UTF-8' },
3283-
credentials: 'include',
3284-
body: JSON.stringify(payload),
3285-
}
3291+
if (!turnstileToken) {
3292+
console.error('❌ No Turnstile token available');
3293+
return 'token_error';
3294+
}
3295+
3296+
console.log('🎯 Painting pixel with enhanced protection system');
3297+
console.log(`📍 Region: ${regionX}, ${regionY}`);
3298+
console.log(`🔲 Pixel: ${pixelX}, ${pixelY}`);
3299+
console.log(`🎨 Color: ${color}`);
3300+
3301+
// Use enhanced API with Pawtect protection
3302+
const result = await enhancedAPI.paintPixelInRegion(
3303+
regionX, regionY, pixelX, pixelY, color, turnstileToken
32863304
);
3287-
if (res.status === 403) {
3288-
console.error(
3289-
'❌ 403 Forbidden. Turnstile token might be invalid or expired.'
3290-
);
3305+
3306+
if (result === 'token_error') {
3307+
console.warn('⚠️ Token error - invalidating current token');
32913308
turnstileToken = null;
32923309
tokenPromise = new Promise(resolve => {
32933310
_resolveToken = resolve;
32943311
});
32953312
return 'token_error';
32963313
}
3297-
const data = await res.json();
3298-
return data?.painted === 1;
3299-
} catch (e) {
3300-
console.error('Paint request failed:', e);
3314+
3315+
if (result === true) {
3316+
console.log('✅ Pixel painted successfully with enhanced protection');
3317+
} else {
3318+
console.warn('⚠️ Pixel painting failed');
3319+
}
3320+
3321+
return result;
3322+
} catch (error) {
3323+
console.error('❌ Enhanced paint request failed:', error);
33013324
return false;
33023325
}
33033326
},
33043327

33053328
async getCharges() {
3306-
const defaultResult = {
3307-
charges: 0,
3308-
max: 1,
3309-
cooldown: CONFIG.COOLDOWN_DEFAULT,
3310-
};
3311-
33123329
try {
3313-
const res = await fetch('https://backend.wplace.live/me', {
3314-
credentials: 'include',
3315-
});
3316-
3317-
if (!res.ok) {
3318-
console.error(`Failed to get charges: HTTP ${res.status}`);
3319-
return defaultResult;
3320-
}
3330+
// Ensure enhanced API is initialized
3331+
await this._ensureInitialized();
33213332

3322-
const data = await res.json();
3333+
// Use enhanced API for charges
3334+
const result = await enhancedAPI.getCharges();
33233335

3336+
// Add backward compatibility fields
33243337
return {
3325-
charges: data.charges?.count ?? 0,
3326-
max: data.charges?.max ?? 1,
3327-
cooldown:
3328-
data.charges?.cooldownMs ?? CONFIG.COOLDOWN_DEFAULT,
3338+
charges: result.charges,
3339+
max: result.max,
3340+
cooldown: result.cooldown,
3341+
// Additional fields from enhanced API
3342+
id: result.id,
3343+
droplets: result.droplets
33293344
};
3330-
} catch (e) {
3331-
console.error('Failed to get charges:', e);
3345+
} catch (error) {
3346+
console.error('❌ Failed to get charges with enhanced API:', error);
3347+
3348+
// Fallback to basic implementation
3349+
const defaultResult = {
3350+
charges: 0,
3351+
max: 1,
3352+
cooldown: CONFIG.COOLDOWN_DEFAULT,
3353+
id: null,
3354+
droplets: 0
3355+
};
3356+
33323357
return defaultResult;
33333358
}
33343359
},
3360+
3361+
// Get status of enhanced protection systems
3362+
getProtectionStatus() {
3363+
return enhancedAPI.getStatus();
3364+
}
33353365
};
33363366

33373367
// Desktop Notification Manager
@@ -4982,6 +5012,8 @@ import {
49825012

49835013
try {
49845014
await updateStats(true);
5015+
// Also update protection status when refreshing
5016+
setTimeout(updateProtectionStatus, 100);
49855017
} catch (error) {
49865018
console.error('Error refreshing charges:', error);
49875019
} finally {
@@ -5955,6 +5987,14 @@ import {
59555987
</div>
59565988
<div class="wplace-stat-value" id="wplace-stat-fullcharge-value">--:--:--</div>
59575989
</div>
5990+
<div class="wplace-stat-item">
5991+
<div class="wplace-stat-label">
5992+
<i class="fas fa-shield-alt"></i> ${Utils.t('protectionStatus')}
5993+
</div>
5994+
<div class="wplace-stat-value" id="wplace-stat-protection-value">
5995+
<span id="pawtect-status">🔐 Checking...</span>
5996+
</div>
5997+
</div>
59585998
${
59595999
state.colorsChecked
59606000
? `
@@ -5974,6 +6014,9 @@ import {
59746014

59756015
// should be after statsArea.innerHTML = '...'. todo make full stats ui update partial
59766016
updateChargeStatsDisplay(intervalMs);
6017+
6018+
// Update protection status after stats are rendered
6019+
setTimeout(updateProtectionStatus, 100);
59776020
};
59786021

59796022
updateDataButtons = () => {
@@ -5982,6 +6025,39 @@ import {
59826025
saveToFileBtn.disabled = !hasImageData;
59836026
};
59846027

6028+
// Update protection status display
6029+
updateProtectionStatus = () => {
6030+
const protectionElement = document.getElementById('pawtect-status');
6031+
if (!protectionElement) return;
6032+
6033+
try {
6034+
const protectionStatus = WPlaceService.getProtectionStatus();
6035+
6036+
if (protectionStatus.initialized && protectionStatus.pawtectStatus.initialized) {
6037+
protectionElement.innerHTML = `
6038+
<span style="color: #28a745;">🔐 ${Utils.t('protectionEnhanced')}</span>
6039+
`;
6040+
protectionElement.title = Utils.t('protectionEnhancedDesc');
6041+
} else if (protectionStatus.initialized) {
6042+
protectionElement.innerHTML = `
6043+
<span style="color: #ffc107;">⚡ ${Utils.t('protectionBasic')}</span>
6044+
`;
6045+
protectionElement.title = Utils.t('protectionBasicDesc');
6046+
} else {
6047+
protectionElement.innerHTML = `
6048+
<span style="color: #dc3545;">⚠️ ${Utils.t('protectionMinimal')}</span>
6049+
`;
6050+
protectionElement.title = Utils.t('protectionMinimalDesc');
6051+
}
6052+
} catch (error) {
6053+
console.warn('Failed to update protection status:', error);
6054+
protectionElement.innerHTML = `
6055+
<span style="color: #6c757d;">❓ ${Utils.t('protectionUnknown')}</span>
6056+
`;
6057+
protectionElement.title = 'Protection status unknown';
6058+
}
6059+
};
6060+
59856061
updateDataButtons();
59866062

59876063
function showResizeDialog(processor) {

0 commit comments

Comments
 (0)