UserScript/mcbbs/vending-machine.user.js

307 lines
12 KiB
JavaScript
Raw Normal View History

2020-11-09 17:24:49 +00:00
// ==UserScript==
// @name MCBBS自动售卖机
2020-11-10 09:14:44 +00:00
// @namespace https://miaowoo.cc/
2021-09-03 08:08:10 +00:00
// @version 0.8.0
2020-11-09 17:24:49 +00:00
// @description 自动售卖道具
// @author MiaoWoo
// @match https://www.mcbbs.net/home.php?mod=magic**
// @grant none
// ==/UserScript==
2020-12-26 15:25:22 +00:00
/* global jq */
2020-11-09 17:24:49 +00:00
(async function () {
let store = window.localStorage
const lastOrderKey = 'circlebox:lastorder'
2020-12-21 14:28:34 +00:00
let params = 0
2020-11-09 17:24:49 +00:00
let checkTimes = 0
2020-12-10 16:15:31 +00:00
let stock = 0
2020-12-11 02:50:41 +00:00
let messagetext = undefined
2020-12-21 14:28:34 +00:00
let storeUsername = undefined
2020-11-09 17:24:49 +00:00
async function sleep(time) {
return new Promise((resolve, reject) => setTimeout(resolve, time))
}
2020-12-28 03:12:37 +00:00
function createMenu() {
let master = document.createElement('a')
master.href = "/home.php?mod=magic&action=mybox&nodeType=master"
master.innerText = "售卖机主节点"
let slave = document.createElement('a')
slave.href = "/home.php?mod=magic&action=mybox&nodeType=slave"
slave.innerText = "售卖机子节点"
let masterLi = document.createElement('li')
masterLi.append(master)
let slaveLi = document.createElement('li')
slaveLi.append(slave)
let tbn = document.getElementsByClassName('tbn')[0].children[1]
tbn.append(masterLi)
tbn.append(slaveLi)
}
2020-11-09 17:24:49 +00:00
async function main() {
2021-03-14 06:36:32 +00:00
params = location.href.split('?')[1].split('&').map(s => s.split('=')).reduce((pre, cur, index) => { pre[cur[0]] = cur[1]; return pre }, {})
2021-09-03 08:08:10 +00:00
let errorh1 = document.getElementsByTagName('h1')[0]
if (errorh1 && errorh1.innerText.startsWith(50)) {
return refresh()
}
2021-09-03 08:08:10 +00:00
let taskId = setTimeout(refresh, 60000)
2020-12-28 03:12:37 +00:00
createMenu()
2021-09-03 08:08:10 +00:00
if (!params.nodeType) { return console.log('nodeType is undefined skip...') }
if (!params.action) { return refresh() }
2020-12-21 14:28:34 +00:00
console.log(params)
2020-11-10 04:11:14 +00:00
try {
2020-12-21 14:28:34 +00:00
storeUsername = document.getElementsByClassName('username')[0].innerText
2020-12-11 02:50:41 +00:00
messagetext = document.getElementById('messagetext')
if (messagetext) {
updateStatus(messagetext.innerText)
return
} else {
updateStatus('')
}
2021-09-03 08:08:10 +00:00
clearTimeout(taskId)
2020-12-21 14:28:34 +00:00
switch (params.nodeType) {
case "master":
master(params)
2020-11-10 04:11:14 +00:00
break
2020-12-21 14:28:34 +00:00
case "slave":
slave(params)
2020-11-10 04:11:14 +00:00
break
}
} catch (ex) {
await sleep(1500)
2020-12-21 14:28:34 +00:00
refresh()
}
}
async function master(params) {
switch (params.action || "shop") {
case "shop":
await shopAction(params)
break
case "mybox":
stock = await updateStock()
await myboxAction(params)
break
case "log":
await logAction(params)
break
}
}
async function slave(params) {
switch (params.action || "shop") {
case "shop":
case "log":
refresh()
break
case "mybox":
stock = await updateStock()
await needReplenishment()
break
}
}
async function needReplenishment() {
let timeoutTask = setTimeout(() => { refresh() }, 120000)
2020-12-26 15:25:22 +00:00
if (stock == 0) { return }
2022-10-22 02:27:27 +00:00
let result = await fetch('https://s.yumc.pw/mcbbs/needReplenishment').then(r => r.json())
2020-12-21 14:28:34 +00:00
if (result.code != 200) {
if (++checkTimes > 5) { return refresh() }
clearTimeout(timeoutTask)
return setTimeout(() => needReplenishment(), 2000)
2020-11-09 17:24:49 +00:00
}
2020-12-21 14:28:34 +00:00
let masterInfo = result.data
sendItem(() => window.showWindow('magics', 'home.php?mod=magic&action=mybox&operation=give&magicid=32', 'get', 0), masterInfo.username, masterInfo.amount || stock)
}
async function refresh() {
try {
await fetch('/api/mobile/index.php?version=4&module=credit')
location.href = '/home.php?mod=magic&action=mybox&nodeType=' + params.nodeType
} catch (error) {
setTimeout(refresh, 5000)
}
2020-11-09 17:24:49 +00:00
}
async function shopAction() {
2020-11-10 04:11:14 +00:00
await sendOrderAction(() => window.showWindow('magics', 'home.php?mod=magic&action=shop&operation=give&mid=mcbbs_mcserver_plus:serverBump'), true)
2020-11-09 17:24:49 +00:00
}
async function myboxAction() {
2020-11-10 04:11:14 +00:00
await sendOrderAction(() => window.showWindow('magics', 'home.php?mod=magic&action=mybox&operation=give&magicid=32', 'get', 0))
2020-11-09 17:24:49 +00:00
}
2020-12-09 02:42:46 +00:00
async function updateStock() {
let serverBump = document.getElementById('magic_mcbbs_mcserver_plus:serverBump')
let stock = 0
if (serverBump) {
stock = serverBump.nextElementSibling.nextElementSibling.children[0].innerText
}
2022-10-22 02:27:27 +00:00
await fetch(`https://s.yumc.pw/mcbbs/updateStock/username/${storeUsername}/stock/${stock}`).then(r => r.json())
2020-12-09 02:42:46 +00:00
return stock
}
2020-12-10 16:07:59 +00:00
async function updateStatus(message) {
2022-10-22 02:27:27 +00:00
await fetch(`https://s.yumc.pw/mcbbs/updateStatus/username/${storeUsername}/message/${message}`).then(r => r.json())
2020-12-10 16:07:59 +00:00
}
2020-12-09 02:42:46 +00:00
async function lowStockNotify(stock, order_id, goods_amount) {
2022-10-22 02:27:27 +00:00
await fetch(`https://s.yumc.pw/mcbbs/lowStockNotify/username/${storeUsername}/stock/${stock}/order_id/${order_id}/goods_amount/${goods_amount}`).then(r => r.json())
2020-11-09 17:24:49 +00:00
}
2020-12-09 02:42:46 +00:00
async function clearOrderCache(order_id) {
2022-10-22 02:27:27 +00:00
await fetch('https://s.yumc.pw/mcbbs/clearOrderCache/order_id/' + order_id).then(r => r.json())
2020-11-10 04:11:14 +00:00
}
2020-12-09 02:42:46 +00:00
async function handleLowStock(order, stock) {
if (order.allowAutoBuy) {
await clearOrderCache(order.order_id)
await sleep(1500)
2020-12-21 14:28:34 +00:00
location.href = '/home.php?mod=magic&action=shop&nodeType=' + params.nodeType
2020-12-09 02:42:46 +00:00
} else {
await lowStockNotify(stock, order.order_id, order.goods_amount)
}
2020-11-09 17:24:49 +00:00
}
2020-12-21 14:28:34 +00:00
async function sendItem(openFn, usernameValue, amountValue) {
try { openFn() } catch (ex) { }
amountValue = Math.min(stock, amountValue)
let username = undefined
let count = 0
while ((username = jq('#selectedusername')[0]) == undefined) {
2022-10-22 02:27:27 +00:00
if (count > 5) {
2020-12-21 14:28:34 +00:00
console.log('检测窗口超时!')
return false
}
await sleep(800)
count++
}
username.value = usernameValue
let amount = username.parentNode.parentNode.nextElementSibling.childNodes[3].childNodes[0]
amount.value = amountValue
let remark = username.parentNode.parentNode.nextElementSibling.nextElementSibling.childNodes[3].childNodes[0]
remark.value = `子节点补货 补货账号: ${storeUsername} 补货数量: ${amountValue}`
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 && error.startsWith('抱歉,对方道具包容量不足')) {
amountValue = error.replace(/[^0-9]/ig, "")
if (amountValue == 0) {
return refresh()
}
await sleep(200)
amount.value = amountValue
2020-12-21 14:40:38 +00:00
remark.value = `子节点补货 补货账号: ${storeUsername} 补货数量: ${amountValue}`
2020-12-21 14:28:34 +00:00
await sleep(300)
jq('#operatesubmit').click()
await sleep(500)
jq('#fwin_dialog_submit').click()
}
await sleep(800)
refresh()
}
2020-11-09 17:24:49 +00:00
async function sendOrderAction(openFn, back = false) {
2022-10-22 02:27:27 +00:00
let timeoutTask = setTimeout(() => { refresh() }, 45000)
2020-11-09 17:24:49 +00:00
let count = 0
2021-01-09 12:59:47 +00:00
if (stock < 50) {
setTimeout(() => { refresh() }, 15000)
}
2022-10-22 02:27:27 +00:00
document.getElementById('scbar_txt').value = "订单 第"+(checkTimes+1)+"次 检查中..."
let result = await fetch('https://s.yumc.pw/mcbbs/needSendOrder').then(r => r.json())
2020-12-10 16:12:05 +00:00
if (result.code != 200) {
2022-10-22 02:27:27 +00:00
if (++checkTimes > 10 || back) {
2020-12-21 14:28:34 +00:00
refresh()
2020-11-09 17:24:49 +00:00
}
2020-11-10 09:14:44 +00:00
clearTimeout(timeoutTask)
2020-12-09 02:42:46 +00:00
return setTimeout(() => sendOrderAction(openFn), 2000)
2020-11-09 17:24:49 +00:00
}
let order = result.data
2021-09-03 08:08:10 +00:00
if (store.getItem(lastOrderKey) == order.order_id) {
2022-10-22 02:27:27 +00:00
let finish = await fetch('https://s.yumc.pw/mcbbs/updateOrder', {
2021-09-03 08:08:10 +00:00
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"order_id": order.order_id,
"status": 2,
"remark": "当前订单可能重复发货 如未到帐 请联系管理员!"
})
}).then(r => r.json())
return
}
try { openFn() } catch (ex) { }
2020-12-10 16:12:05 +00:00
if (!order || !order.order_id) { return setTimeout(() => sendOrderAction(openFn), 2000) }
2020-11-09 17:24:49 +00:00
console.log(`开始发送订单 ${order.order_id}`)
let username = undefined
while ((username = jq('#selectedusername')[0]) == undefined) {
2022-10-22 02:27:27 +00:00
if (count > 8) {
2020-11-09 17:24:49 +00:00
console.log('检测窗口超时!')
2020-12-09 02:42:46 +00:00
if (!back) { await handleLowStock(order, stock) }
2020-11-09 17:24:49 +00:00
return
}
2020-11-10 09:14:44 +00:00
await sleep(800)
2020-11-09 17:24:49 +00:00
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]
2020-12-21 14:28:34 +00:00
remark.value = `圈云盒子 自动发货订单: ${order.order_id} 物品: ${order.goods_name} 数量: ${order.goods_amount} 发货账号: ${storeUsername}`
2020-11-09 17:24:49 +00:00
await sleep(300)
jq('#operatesubmit').click()
await sleep(500)
jq('#fwin_dialog_submit').click()
let info = ''
let error = ''
while ((info = jq('.alert_info').text()) == '') {
2022-10-22 02:27:27 +00:00
if (count > 8) {
2020-11-09 17:24:49 +00:00
error = jq('#ntcwin').text()
break
}
2020-11-10 09:14:44 +00:00
await sleep(800)
2020-11-09 17:24:49 +00:00
count++
}
2020-11-10 09:14:44 +00:00
if (error == "抱歉,操作数量不合法") {
2020-12-21 14:28:34 +00:00
if (order.goods_amount > stock) {
error = `发货账号: ${storeUsername} 库存 ${stock} 不足 ${order.goods_amount} 已通知管理员(QQ: 502647092) 请等待...`
}
await handleLowStock(order, stock)
2020-11-10 09:14:44 +00:00
}
2020-11-09 17:24:49 +00:00
try {
2021-09-03 08:08:10 +00:00
if (!info && !error) {
store.setItem(lastOrderKey, order.order_id)
}
2022-10-22 02:27:27 +00:00
let finish = await fetch('https://s.yumc.pw/mcbbs/updateOrder', {
2020-11-09 17:24:49 +00:00
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)
2022-10-22 02:27:27 +00:00
} finally {
await sleep(800)
refresh()
2020-11-09 17:24:49 +00:00
}
}
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()
})()