From a1651e690dc9a85931e74de7bef2eb6706e208bf Mon Sep 17 00:00:00 2001 From: 502647092 Date: Tue, 10 Nov 2020 04:11:14 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20'mcbbs/vending-machine.use?= =?UTF-8?q?r.js'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mcbbs/vending-machine.user.js | 77 +++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 26 deletions(-) diff --git a/mcbbs/vending-machine.user.js b/mcbbs/vending-machine.user.js index 52f75dd..dbd4328 100644 --- a/mcbbs/vending-machine.user.js +++ b/mcbbs/vending-machine.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name MCBBS自动售卖机 // @namespace http://tampermonkey.net/ -// @version 0.1 +// @version 0.2 // @description 自动售卖道具 // @author MiaoWoo // @match https://www.mcbbs.net/home.php?mod=magic** @@ -16,59 +16,72 @@ return new Promise((resolve, reject) => setTimeout(resolve, time)) } async function main() { - let params = location.href.split('?')[1].split('&').map(s => s.split('=')).reduce((pre, cur, index) => { pre[cur[0]] = cur[1]; return pre }, {}) - if (!params.action) { return location.href = '/home.php?mod=magic&action=mybox' } - console.log(params) - switch (params.action || "shop") { - case "shop": - await shopAction(params) - break - case "mybox": - await myboxAction(params) - break - case "log": - await logAction(params) - break + try { + let params = location.href.split('?')[1].split('&').map(s => s.split('=')).reduce((pre, cur, index) => { pre[cur[0]] = cur[1]; return pre }, {}) + if (!params.action) { return location.href = '/home.php?mod=magic&action=mybox' } + console.log(params) + switch (params.action || "shop") { + case "shop": + await shopAction(params) + break + case "mybox": + await myboxAction(params) + break + case "log": + await logAction(params) + break + } + } catch (ex) { + await sleep(1500) + location.href = '/home.php?mod=magic&action=mybox' } } async function shopAction() { - sendOrderAction(() => window.showWindow('magics', 'home.php?mod=magic&action=shop&operation=give&mid=mcbbs_mcserver_plus:serverBump'), true) + await sendOrderAction(() => window.showWindow('magics', 'home.php?mod=magic&action=shop&operation=give&mid=mcbbs_mcserver_plus:serverBump'), true) } async function myboxAction() { - sendOrderAction(() => window.showWindow('magics', 'home.php?mod=magic&action=mybox&operation=give&magicid=32', 'get', 0)) + await sendOrderAction(() => window.showWindow('magics', 'home.php?mod=magic&action=mybox&operation=give&magicid=32', 'get', 0)) } async function lowStockNotify() { await fetch('https://reward.yumc.pw/mcbbs/lowStockNotify').then(r => r.json()) } + async function clearOrderCache() { + await fetch('https://reward.yumc.pw/mcbbs/clearOrderCache').then(r => r.json()) + } async function duplicateNotify(order_id) { - await fetch('https://reward.yumc.pw/mcbbs/duplicateNotify/order_id' + order_id).then(r => r.json()) + await fetch('https://reward.yumc.pw/mcbbs/duplicateNotify/order_id/' + order_id).then(r => r.json()) } async function sendOrderAction(openFn, back = false) { let count = 0 let result = await fetch('https://reward.yumc.pw/mcbbs/needSendOrder').then(r => r.json()) if (result.code != 200) { - if (++checkTimes > 30) { + if (++checkTimes > 30 || back) { location.href = '/home.php?mod=magic&action=mybox' } return setTimeout(() => sendOrderAction(openFn), 15000) } try { openFn() } catch (ex) { } let order = result.data - if (store.getItem(lastOrderKey) == order.order_id) { - console.error('重复发送订单: ' + order.order_id + ' 5秒后刷新重试...') - duplicateNotify(order.order_id) - return setTimeout(() => location.href = '/home.php?mod=magic&action=mybox', 5000) - } + // if (store.getItem(lastOrderKey) == order.order_id) { + // console.error('重复发送订单: ' + order.order_id + ' 5秒后刷新重试...') + // duplicateNotify(order.order_id) + // await sleep(5000) + // return location.href = '/home.php?mod=magic&action=mybox' + // } console.log(`开始发送订单 ${order.order_id}`) let username = undefined while ((username = jq('#selectedusername')[0]) == undefined) { - if (count > 5) { + if (count > 3) { console.log('检测窗口超时!') if (!back) { if (order.allowAutoBuy) { + await clearOrderCache() + await sleep(1500) location.href = '/home.php?mod=magic&action=shop' } else { - lowStockNotify() + await lowStockNotify() + await sleep(10000) + location.href = '/home.php?mod=magic&action=mybox' } } return @@ -91,6 +104,18 @@ while ((info = jq('.alert_info').text()) == '') { if (count > 5) { error = jq('#ntcwin').text() + if (error == "抱歉,操作数量不合法") { + if (order.allowAutoBuy) { + await clearOrderCache() + await sleep(1500) + location.href = '/home.php?mod=magic&action=shop' + return + } else { + lowStockNotify() + await sleep(10000) + location.href = '/home.php?mod=magic&action=mybox' + } + } break } await sleep(1000) @@ -114,7 +139,7 @@ console.log(ex) } await sleep(800) - if (back) { + if (back || error) { return location.href = '/home.php?mod=magic&action=mybox' } jq('#fwin_dialog_submit').click()