UserScript/mcbbs/vending-machine.user.js

161 lines
6.4 KiB
JavaScript

// ==UserScript==
// @name MCBBS自动售卖机
// @namespace https://miaowoo.cc/
// @version 0.5
// @description 自动售卖道具
// @author MiaoWoo
// @match https://www.mcbbs.net/home.php?mod=magic**
// @grant none
// ==/UserScript==
(async function () {
let store = window.localStorage
const lastOrderKey = 'circlebox:lastorder'
let checkTimes = 0
async function sleep(time) {
return new Promise((resolve, reject) => setTimeout(resolve, time))
}
async function main() {
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() {
await sendOrderAction(() => window.showWindow('magics', 'home.php?mod=magic&action=shop&operation=give&mid=mcbbs_mcserver_plus:serverBump'), true)
}
async function myboxAction() {
await sendOrderAction(() => window.showWindow('magics', 'home.php?mod=magic&action=mybox&operation=give&magicid=32', 'get', 0))
}
async function updateStock() {
let serverBump = document.getElementById('magic_mcbbs_mcserver_plus:serverBump')
let stock = 0
if (serverBump) {
stock = serverBump.nextElementSibling.nextElementSibling.children[0].innerText
await fetch(`https://reward.yumc.pw/mcbbs/updateStock/stock/${stock}`).then(r => r.json())
}
return stock
}
async function lowStockNotify(stock, order_id, goods_amount) {
await fetch(`https://reward.yumc.pw/mcbbs/lowStockNotify/stock/${stock}/order_id/${order_id}/goods_amount/${goods_amount}`).then(r => r.json())
}
async function clearOrderCache(order_id) {
await fetch('https://reward.yumc.pw/mcbbs/clearOrderCache/order_id/' + order_id).then(r => r.json())
}
async function duplicateNotify(order_id, goods_amount) {
await fetch(`https://reward.yumc.pw/mcbbs/duplicateNotify/order_id/${order_id}/goods_amount/${goods_amount}`).then(r => r.json())
}
async function handleLowStock(order, stock) {
if (order.allowAutoBuy) {
await clearOrderCache(order.order_id)
await sleep(1500)
location.href = '/home.php?mod=magic&action=shop'
} else {
await lowStockNotify(stock, order.order_id, order.goods_amount)
await sleep(120000)
location.href = '/home.php?mod=magic&action=mybox'
}
}
async function sendOrderAction(openFn, back = false) {
let timeoutTask = setTimeout(() => { location.href = '/home.php?mod=magic&action=mybox' }, 90000)
let count = 0
let stock = updateStock()
let result = await fetch('https://reward.yumc.pw/mcbbs/needSendOrder').then(r => r.json())
if (result.code != 200 && result.data.order_id) {
if (++checkTimes > 30 || back) {
location.href = '/home.php?mod=magic&action=mybox'
}
clearTimeout(timeoutTask)
return setTimeout(() => sendOrderAction(openFn), 2000)
}
try { openFn() } catch (ex) { }
let order = result.data
console.log(`开始发送订单 ${order.order_id}`)
let username = undefined
while ((username = jq('#selectedusername')[0]) == undefined) {
if (count > 3) {
console.log('检测窗口超时!')
if (!back) { await handleLowStock(order, stock) }
return
}
await sleep(800)
count++
}
username.value = order.reciver_name
let amount = username.parentNode.parentNode.nextElementSibling.childNodes[3].childNodes[0]
amount.value = order.goods_amount
let remark = username.parentNode.parentNode.nextElementSibling.nextElementSibling.childNodes[3].childNodes[0]
remark.value = `圈云盒子 自动发货订单: ${order.order_id} 物品: ${order.goods_name} 数量: ${order.goods_amount}`
store.setItem(lastOrderKey, order.order_id)
await sleep(300)
jq('#operatesubmit').click()
await sleep(500)
jq('#fwin_dialog_submit').click()
let info = ''
let error = ''
while ((info = jq('.alert_info').text()) == '') {
if (count > 3) {
error = jq('#ntcwin').text()
break
}
await sleep(800)
count++
}
if (error == "抱歉,操作数量不合法") {
return await handleLowStock(order, stock)
}
try {
let finish = await fetch('https://reward.yumc.pw/mcbbs/updateOrder', {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"order_id": order.order_id,
"status": error ? 3 : 2,
"remark": info || error || "未获取到结果!"
})
}).then(r => r.json())
console.log(finish)
} catch (ex) {
console.log(ex)
}
await sleep(800)
if (back || error) {
return location.href = '/home.php?mod=magic&action=mybox'
}
jq('#fwin_dialog_submit').click()
}
async function logAction(params) {
switch (params.operation) {
case "givelog":
await givelogOperation()
break
}
}
async function givelogOperation() {
let logs = jq('table.dt').children().children().map((index, item) => {
let line = jq(item).text().split('\n').filter(f => f)
return line.join(' ')
}).splice(1)//去除第一行无效数据
console.log(logs)
}
main()
})()