Download trading.js from Fadi49/My: direct link, hf CLI and curl.
- Browser
- Download file 6.25 kB
-
https://huggingface.co/spaces/Fadi49/My/resolve/main/trading.js
- Command line
-
hf download hf://spaces/Fadi49/My/trading.js
-
curl -L -o trading.js https://huggingface.co/spaces/Fadi49/My/resolve/main/trading.js
6.25 kB
| ; | |
| /** | |
| * Trading System — نظام التجارة | |
| * يتبادل الأغراض مع اللاعبين، يعرض صرف، يتفاوض | |
| */ | |
| const { EventEmitter } = require('events'); | |
| const sleep = ms => new Promise(r => setTimeout(r, Math.max(0, ms))); | |
| const rand = (a, b) => Math.floor(Math.random() * (b - a + 1)) + a; | |
| const chance = pct => Math.random() * 100 < pct; | |
| const pick = arr => arr[Math.floor(Math.random() * arr.length)]; | |
| const TRADE_OFFERS = [ | |
| { give: 'cobblestone', want: 'diamond', ratio: 64, desc: '64 حجر ↔ 1 ماس' }, | |
| { give: 'iron_ingot', want: 'diamond', ratio: 8, desc: '8 حديد ↔ 1 ماس' }, | |
| { give: 'coal', want: 'iron_ingot', ratio: 8, desc: '8 فحم ↔ 1 حديد' }, | |
| { give: 'bread', want: 'iron_ingot', ratio: 4, desc: '4 خبز ↔ 1 حديد' }, | |
| { give: 'oak_planks', want: 'cobblestone', ratio: 4, desc: '4 خشب ↔ 4 حجر' }, | |
| ]; | |
| const TRADE_MESSAGES = [ | |
| 'مين يبي يتبادل معاي؟', | |
| 'عنددي موارد أبيعها', | |
| 'أي حد يبي حديد؟', | |
| 'أنا أبيع خشب رخيص', | |
| 'مين يبي خبز؟', | |
| 'أعرض مواردي للبيع', | |
| 'تبي نتبادل؟', | |
| ]; | |
| const TRADE_RESPONSES = { | |
| accept: ['تمام! يلا نتبادل','أكيد','موافق','يلا','ما عندي مانع'], | |
| reject: ['لا شكراً','مش مهتم','بعدين','ما أبي'], | |
| counter: ['لا بس أبي أكثر','غالي علي','أنا أverse لك أكثر','ما تقبل؟'], | |
| }; | |
| class TradingSystem extends EventEmitter { | |
| constructor(bot, config = {}) { | |
| super(); | |
| this.bot = bot; | |
| this.config = { | |
| offerInterval: 25000, | |
| maxTradeDistance: 4, | |
| ...config, | |
| }; | |
| this._active = false; | |
| this._timers = []; | |
| this._trading = false; | |
| this._tradesCompleted = 0; | |
| this._tradeOffers = 0; | |
| this._log('INIT', 'نظام التجارة جاهز'); | |
| } | |
| _log(tag, msg) { | |
| const ts = new Date().toISOString().replace('T', ' ').slice(0, 19); | |
| console.log(`[${ts}] [TRADE] ${tag} ${msg}`); | |
| } | |
| start() { | |
| if (this._active) return; | |
| this._active = true; | |
| this._log('START', 'بدء نظام التجارة'); | |
| // عرض تبادل عشوائي | |
| this._addTimer(() => this._offerTrade(), this.config.offerInterval); | |
| // استمع لرسائل التجارة | |
| this.bot.on('messagestr', (msg) => this._onTradeMessage(msg)); | |
| } | |
| stop() { | |
| this._active = false; | |
| this._trading = false; | |
| this._timers.forEach(t => { try { clearInterval(t); clearTimeout(t); } catch(_) {} }); | |
| this._timers = []; | |
| this._log('STOP', 'إيقاف نظام التجارة'); | |
| } | |
| _addTimer(fn, interval) { | |
| const t = setInterval(() => { if (this._active) fn(); }, interval); | |
| this._timers.push(t); | |
| } | |
| // ═══ عرض تبادل ═══ | |
| async _offerTrade() { | |
| if (this._trading || !this.bot?.entity) return; | |
| if (chance(60)) return; | |
| const nearbyPlayers = this._getNearbyPlayers(); | |
| if (nearbyPlayers.length === 0) return; | |
| const offer = pick(TRADE_OFFERS); | |
| const giveItem = this.bot.inventory.items().find(i => i.name === offer.give); | |
| if (!giveItem || giveItem.count < offer.ratio) return; | |
| await this._sendChat(pick(TRADE_MESSAGES)); | |
| this._tradeOffers++; | |
| this._log('💰', `عرض: ${offer.desc}`); | |
| this.emit('offered', offer); | |
| } | |
| // ═══ رد على رسالة ═══ | |
| async _onTradeMessage(msg) { | |
| if (!this._active) return; | |
| const cleaned = msg.toString().replace(/<[^>]+>/g, '').trim().toLowerCase(); | |
| if (cleaned.includes('تبادل') || cleaned.includes('swap') || cleaned.includes('trade')) { | |
| if (chance(50)) { | |
| await this._sendChat(pick(TRADE_RESPONSES.accept)); | |
| this._trading = true; | |
| await sleep(rand(1000, 2000)); | |
| await this._executeTrade(); | |
| } else { | |
| await this._sendChat(pick(TRADE_RESPONSES.reject)); | |
| } | |
| } | |
| } | |
| // ═══ تنفيذ التبادل ═══ | |
| async _executeTrade() { | |
| if (!this.bot?.entity) return; | |
| try { | |
| // دور على لاعب قريب | |
| const player = this._getNearbyPlayers()[0]; | |
| if (!player) return; | |
| // امشي للاعب | |
| await this._walkTo(player.entity.position); | |
| // فتح التبادل | |
| await sleep(rand(500, 1000)); | |
| // mineflayer ما عنده تبادل مباشر، بس نقدر نرمي أغراض | |
| const offer = pick(TRADE_OFFERS); | |
| const item = this.bot.inventory.items().find(i => i.name === offer.give); | |
| if (item) { | |
| await this.bot.tossStack(item); | |
| this._tradesCompleted++; | |
| this._log('✅', `تبادل: ${item.name}`); | |
| this.emit('traded', item.name); | |
| } | |
| } catch(_) {} | |
| finally { | |
| this._trading = false; | |
| } | |
| } | |
| // ═══ رمي أغراض ═══ | |
| async tossItem(itemName, count) { | |
| if (!this.bot) return; | |
| const item = this.bot.inventory.items().find(i => i.name === itemName); | |
| if (item) { | |
| await this.bot.tossStack(item); | |
| this._log('扔', `رمى ${itemName}`); | |
| } | |
| } | |
| async _walkTo(position) { | |
| if (!this.bot?.entity) return; | |
| const yaw = Math.atan2( | |
| position.z - this.bot.entity.position.z, | |
| position.x - this.bot.entity.position.x | |
| ); | |
| this.bot.look(yaw, 0, false); | |
| this.bot.setControlState('forward', true); | |
| await sleep(rand(200, 500)); | |
| this.bot.setControlState('forward', false); | |
| } | |
| async _sendChat(msg) { | |
| if (!this.bot) return; | |
| try { | |
| const safe = msg.replace(/[^\x20-\x7E\u0600-\u06FF ]/g, '').slice(0, 100); | |
| this.bot.chat(safe); | |
| this._log('💬', `قال: ${safe}`); | |
| } catch(_) {} | |
| } | |
| _getNearbyPlayers() { | |
| if (!this.bot?.entity) return []; | |
| return Object.values(this.bot.players || {}) | |
| .filter(p => p.username !== this.bot.username && p.entity && | |
| p.entity.position?.distanceTo(this.bot.entity.position) < 16); | |
| } | |
| getStats() { | |
| return { | |
| tradesCompleted: this._tradesCompleted, | |
| tradeOffers: this._tradeOffers, | |
| trading: this._trading, | |
| active: this._active, | |
| }; | |
| } | |
| destroy() { | |
| this.stop(); | |
| this.removeAllListeners(); | |
| } | |
| } | |
| module.exports = { TradingSystem, TRADE_OFFERS }; | |