Widget:家具计算器/js
<script type='text/javascript'src="https://wiki.biligame.com/ys/MediaWiki:vue3-global.js?action=raw&ctype=text/javascript"></script> <script> var furnitureInitialData, otherData; var getDataFun = { getFurnitureData: function () { let newData = {}; let newCommonData = {}; $("#liubian_ertongjie tr").each(function (index, item) { const $element = $(this); let name = $element.find("td").eq(0).text().replace("\n", ""); let imgUrl = $element.find("td").eq(1).text().replace("\n", ""); let term = $element.find("td").eq(2).text().replace("\n", ""); let type = $element.find("td").eq(3).text().replace("\n", ""); let material = $element.find("td").eq(4).text().replace("\n", ""); let gold = $element.find("td").eq(5).text().replace("\n", ""); let newType = term.split("·"); material = material.split(",").reduce((acc, item) => { const [key, value] = item.split("*"); acc[key.trim()] = Number(value); return acc; }, {}); function isEmpty(e) { if (e == "") return undefined; return e; } let nowData = { name: isEmpty(name), imgUrl: isEmpty(imgUrl), term: isEmpty(term), type: isEmpty(type), material: isEmpty(material), gold: isEmpty(gold) }; // console.log(nowData); if (newType[1] == "通用") { if (!newCommonData?.[term]) { newCommonData[term] = [nowData]; } else { newCommonData[term].push(nowData); } } else { if (!newData?.[newType[1]]) { newData[newType[1]] = { [term]: [nowData] }; } else { if (!newData[newType[1]]?.[term]) { newData[newType[1]][term] = [nowData]; } else { newData[newType[1]][term].push(nowData); } } } }); $.each(newData, function (index, item) { newData[index] = Object.assign({}, newCommonData, item); }); return newData; }, }
var Counter = { data() { return { furnitureNumber: {}, mwData: { userId: (document.cookie.match(/DedeUserID=([^;]+)/) || [])[1] || "", wgGameName: typeof mw == "object" ? mw.config.get("wgGameName") : "yuan", }, toolsSet: { theadColor: "", roleOn: "", chooseTerm: "全部", chooseType: "全部", saveName: "", }, uploadBox: { onupload: false, uploadOver: false, createPageSuc: false, uploadImgSuc: false, imgAudit: false, imgRepetition: [false, []], pageUrl: "", file: null, imgurl: "https://patchwiki.biligame.com/images/ys/7/7f/eaj1pyft1pyhyd4v3164wp2nhkd7l3d.png", describe: "", author: "", tag: "", cardName: "", cardData: { data: {}, gold: 0, role: "", }, }, checkedBox: new Array(), wikiUserjs: "userjs-furnitureData", wikiData: null, }; }, computed: { chooseData() { let that = this; let newdata = JSON.parse(JSON.stringify(furnitureInitialData[that.toolsSet.roleOn])); newdata = Object.values(newdata).flat(); let chooseTerm = [...new Set(newdata.map((item) => item.term))]; let chooseType = [...new Set(newdata.map((item) => item.type))]; if (that.toolsSet.chooseTerm != "全部") { // newdata = newdata[chooseTerm]; newdata = newdata.flat().filter((item) => item.term == that.toolsSet.chooseTerm); } if (that.toolsSet.chooseType != "全部") { // newdata = newdata[chooseTerm]; newdata = newdata.flat().filter((item) => item.type == that.toolsSet.chooseType); } return { data: newdata, roleKey: Object.keys(furnitureInitialData), chooseTerm, chooseType }; }, // 计算所需素材 calculateMaterial() { let that = this; let compoundTable = Object.values(furnitureInitialData) .flatMap((item) => Object.values(item)) .flat(); let needBox = { 金币: 0 }; $.each(that.furnitureNumber, function (boxIndex, boxItem) { if (boxItem > 0) { let data = compoundTable.find((i) => i.name == boxIndex); $.each(data.material, function (index, item) { if (needBox[index]) { needBox[index] += item * boxItem; } else { needBox[index] = item * boxItem; } }); needBox.金币 += Number(data.gold) * boxItem; } }); let exportData = {}; $.each(needBox, function (boxIndex, boxItem) { if (otherData.compose[boxIndex]) { let data = otherData.compose[boxIndex]; $.each(data, function (index, item) { if (exportData[index]) { exportData[index] += item * boxItem; } else { exportData[index] = item * boxItem; } }); } else { exportData[boxIndex] = boxItem; } });
return exportData; }, // 卡片家具数量 cardFurnitureSum() { const sum = Object.values(this.uploadBox.cardData.data).reduce((acc, val) => acc + val, 0); const keyCount = Object.keys(this.uploadBox.cardData.data).length; const gold = this.uploadBox.cardData.gold.toLocaleString("en-US"); return { keyCount: keyCount, allSum: sum, gold: gold }; }, }, methods: { // 设置表头颜色 theadColor(role = this.toolsSet.roleOn) { // if (!role) role = this.toolsSet.roleOn; let color = otherData.roleColor?.[role]; if (color == undefined) color = ""; return color; }, // 转换素材格式 convertMaterial(data) { let exportData = JSON.parse(JSON.stringify(data)); delete exportData.金币; let out; out = Object.entries(exportData) .map(([key, value]) => `${key}*${value}`) .join(", "); return [out, out.replace(/, /g, "\n\r")]; }, // 输入数字判断 handleInput(key, $event) { let that = this; let value = $event.target.value; if (value < 0) { value = 0; } else if (value == "") { value = 0; } value = parseInt(value); that.furnitureNumber[key] = value; }, // 获取图标 getImgUrl(oldUrl) { if (!oldUrl) { oldUrl = `https://patchwiki.biligame.com/images/ys/7/7f/eaj1pyft1pyhyd4v3164wp2nhkd7l3d.png`; } return oldUrl; }, // 获取表格的颜色 getTrColor(name) { // let that = this; // let data = that.checkedBox.find((i) => i.name == name); if (this.furnitureNumber[name] > 0) { return "checked"; } }, // 点击 chooseFurniture(name) { let that = this; if (that.furnitureNumber[name] == 0) { that.furnitureNumber[name] = 1; } else if (that.furnitureNumber[name] == 1) { that.furnitureNumber[name] = 0; } // if (that.checkedBox.find((i) => i.name == data.name)) { // let index = that.checkedBox.findIndex((i) => i.name == data.name); // that.checkedBox.splice(index, 1); // } else { // that.checkedBox.push(data); // } }, // 切换筛选清空选中 selectChange() { let that = this; $.each(that.furnitureNumber, function (index, item) { that.furnitureNumber[index] = 0; }); that.toolsSet.chooseTerm = "全部"; that.toolsSet.chooseType = "全部"; }, // 显示数据 showWikiData() { try { if (Object.keys(this.wikiData).length == 0) { return false; } } catch {} return true; }, // 读取Wiki数据 loadWiki() { var that = this; if (typeof mw == "object") { that.showloading(); new mw.Api() .get({ action: "query", format: "json", meta: "userinfo", formatversion: "2", uiprop: "options", userId: that.mwData.userId, }) .then(function (data) { var options = data.query.userinfo.options; let loadData = options?.[that.wikiUserjs]; if (loadData) { that.wikiData = JSON.parse(loadData.replace(/\^/g, '"')); } else { console.log(`没有数据`); that.wikiData = {}; } that.hideloading(); }); } else { that.wikiData = {}; } }, // 保存到Wiki saveToWiki(type) { let that = this; let saveData = JSON.parse(JSON.stringify(that.furnitureNumber)); let newData; if (type == "delete") { save(); return; } function save() { new mw.Api() .postWithToken("csrf", { action: "options", format: "json", optionname: that.wikiUserjs, optionvalue: JSON.stringify(newData).replace(/\^/g, '"'), }) .then(function (message) { that.wikiData = newData; alert("储存成功"); }) .catch(function (err) { if (err == "notloggedin") { alert("储存失败,请登录账号"); } else { console.log(err); alert("储存失败"); } }); } $.each(saveData, function (index, item) { if (item <= 0) { delete saveData[index]; } }); if (Object.keys(saveData).length == 0) { alert(`没有添加家具`); return; } let saveWarn = that.convertMaterial(saveData)[0]; if (that.toolsSet.saveName == "") { alert(`请输入保存名称`); return; } if (Object.keys(that.wikiData).includes(that.toolsSet.saveName)) { if (confirm(`是否要覆盖 ${that.toolsSet.saveName} 的数据`)) { } else { return; } } if (confirm(`确认是否保存,有以下家具:\n\n${saveWarn.replace(/, /g, "\n")}`)) { if (that.wikiData) { newData = JSON.parse(JSON.stringify(that.wikiData)); newData[that.toolsSet.saveName] = { role: that.toolsSet.roleOn, data: saveData, gold: that.calculateMaterial.金币 }; } else { newData = { [that.toolsSet.saveName]: { role: that.toolsSet.roleOn, data: saveData, gold: that.calculateMaterial.金币 } }; } if (typeof mw == "object") { save(); } else { that.wikiData = newData; } } }, // 加载数据 loadData(name) { if (!confirm(`是否读取 ${name} 这会删除现有数据`)) { return; } let that = this; let newData = that.wikiData[name]; that.toolsSet.roleOn = newData.role; $.each(that.furnitureNumber, function (index, item) { if (index in newData.data) { that.furnitureNumber[index] = newData.data[index]; } else { that.furnitureNumber[index] = 0; } }); console.table(that.furnitureNumber); }, // 删除数据 deleteData(name) { let that = this; if (confirm(`是否删除 ${name}`)) { delete that.wikiData[name]; that.saveToWiki("delete"); } }, // 处理输出到图片的家具 cardFurniture() { console.log(123); }, // 打开上传卡片 pickCard(name) { let that = this; that.uploadBox.cardName = name; that.uploadBox.cardData = that.wikiData[name]; that.uploadBox.onupload = !that.uploadBox.onupload; if (that.uploadBox.onupload) { $("body").css({ overflow: "hidden" }); } else { $("body").css({ overflow: "" }); } }, // 关闭卡片 closeCard() { this.uploadBox.onupload = false; this.uploadBox.imgurl = "https://patchwiki.biligame.com/images/ys/7/7f/eaj1pyft1pyhyd4v3164wp2nhkd7l3d.png"; this.uploadBox.file = null; this.uploadBox.uploadOver = false; this.uploadBox.createPageSuc = false; this.uploadBox.uploadImgSuc = false; this.uploadBox.imgRepetition = [false, []]; this.uploadBox.pageUrl = "";
$("body").css({ overflow: "" }); }, // 上传图片到浏览器 uploadImg(event) { let that = this; const file = event.target.files[0]; if (file) { // 生成临时地址用于预览 that.uploadBox.imgurl = URL.createObjectURL(file); that.uploadBox.file = file; } }, // 获取分享的数据 getShareData() { let that = this; console.log(that.shareData); let shareData; new mw.Api() .get({ action: "ask", query: `${that.shareData}|?主角|?家具`, }) .then(function (data) { let furnitureBox = Object.values(data.query.results)[0]; role = furnitureBox.printouts.主角[0]; furnitureBox = furnitureBox.printouts.家具[0]; shareData = JSON.parse(furnitureBox); console.log(furnitureBox); that.toolsSet.roleOn = role; $.each(that.furnitureNumber, function (index, item) { if (index in shareData) { that.furnitureNumber[index] = shareData[index]; } else { that.furnitureNumber[index] = 0; } }); }) .catch(function (err) { console.log("报错", err); }); }, // 添加评论 addFlowthread(pageid,uid) { new mw.Api().postWithToken("csrf", { action: "flowthread", type: "post", pageid: pageid, content: `赞:${uid}` }).catch(function (err) { console.log("添加点赞评论报错", err); }); new mw.Api().postWithToken("csrf", { action: "flowthread", type: "post", pageid: pageid, content: `踩:${uid}` }).catch(function (err) { console.log("添加点踩评论报错", err); }); }, // 上传到Wiki uploadWiki() { if (!confirm(`是否分享此小屋到Wiki`)) { return; } let that = this; let suffix; if (that.uploadBox.file) { if (!that.uploadBox.file.type.startsWith("image/")) { return; } suffix = that.uploadBox.file.name.split(".").pop().toLowerCase(); } let uid = Number(Math.random().toString().substr(3, 3) + Date.now()).toString(36); let imgName = that.uploadBox.file ? `Furniture-hut-${uid}.${suffix}` : `无`; function nowtime() { const padZero = num => num.toString().padStart(2, '0'); const now = new Date(); const year = now.getFullYear(); const month = padZero(now.getMonth() + 1); // 补零月份 const date = padZero(now.getDate()); const hours = padZero(now.getHours()); const minutes = padZero(now.getMinutes()); const seconds = padZero(now.getSeconds());
return `${year}-${month}-${date} ${hours}:${minutes}`; } that.showloading(true); if (typeof mw == "object") { new mw.Api() .create( `小屋分享:${uid}`, { summary: `创建小屋分享-${that.uploadBox.cardName}` }, `

角色: ${that.uploadBox.cardData.role} | 套装名称:${that.uploadBox.cardName} |
上传用户:${that.uploadBox.author} | 上传时间:${nowtime()} |
家具种类:Lua错误:mw.text.jsonDecode: 语法错误。 家具总数:Lua错误:mw.text.jsonDecode: 语法错误。
|
标签:${that.uploadBox.tag} |
简介: ${that.uploadBox.describe} | |
所需家具: Lua错误:mw.text.jsonDecode: 语法错误。 |
`.replace(/\t/g, "")
)
.then(function () {
that.uploadBox.createPageSuc = true;
that.uploadBox.pageUrl = `小屋分享:${uid}`;
console.log("页面创建成功");
if (that.uploadBox.file) {
if (that.uploadBox.file.type.startsWith("image/")) {
new mw.Api()
.upload(that.uploadBox.file, { filename: `Furniture-hut-${uid}.${suffix}`, ignorewarnings: true })
.then(function () {
that.uploadBox.uploadImgSuc = true;
console.log("图片上传成功");
})
.catch(function (reject) {
if (typeof reject == "object") {
console.log("已经有相同图片");
that.uploadBox.uploadImgSuc = true;
that.uploadBox.imgRepetition = [true, reject];
} else if (reject.includes("Furniture")) {
console.log("已经有相同图片");
that.uploadBox.uploadImgSuc = true;
that.uploadBox.imgRepetition = [true, [reject]];
} else if (reject == "moderation-image-queued") {
console.log("等待图片审核");
that.uploadBox.imgAudit = true;
} else {
console.log(`图片上传失败:${reject}`);
}
})
.always(function () {
that.addFlowthread(28496,uid);
that.hideloading(true);
that.uploadBox.uploadOver = true;
new mw.Api().postWithToken("csrf", { action: "purge", titles: `小屋分享:${uid}` }).catch(function (err) {
console.log("报错", err);
});
new mw.Api().postWithToken("csrf", { action: "purge", pageids: 28496 }).catch(function (err) {
console.log("报错", err);
});
});
} else {
that.addFlowthread(28496,uid);
that.hideloading(true);
that.uploadBox.uploadOver = true;
}
} else {
that.addFlowthread(28496,uid);
that.hideloading(true);
that.uploadBox.uploadOver = true;
}
})
.catch(function (reject) {
console.log(`创建失败:${reject}`);
if (reject == "articleexists") {
console.log("有页面了 重新创建");
that.uploadWiki();
}
});
} else {
that.hideloading(true);
}
},
// 触发加载遮罩
showloading(shade = false) {
$("#onload").show();
if (shade) {
$("#onload").css({ "background-color": "#0000003d" });
}
},
// 隐藏加载遮罩
hideloading(shade = false) {
$("#onload").hide();
if (shade) {
$("#onload").css({ "background-color": "" });
}
},
},
created() {
let that = this;
that.toolsSet.roleOn = Object.keys(furnitureInitialData)[0];
that.furnitureNumber = Object.values(furnitureInitialData)
.flatMap((item) => Object.values(item))
.flat()
.reduce((acc, obj) => {
// acc[obj.name] = obj.number;
acc[obj.name] = 0;
return acc;
}, {});
let lsData = localStorage.getItem("wiki_yuan_importdata");
if (lsData) {
lsData = JSON.parse(lsData);
that.toolsSet.roleOn = lsData.role
$.each(lsData.data, function (index, item) {
that.furnitureNumber[index] = item;
});
localStorage.removeItem("wiki_yuan_importdata");
}
},
mounted() {
let that = this;
$("#onload").hide();
},
};
var App = Vue.createApp(Counter); function furnitureOnload() { if (typeof mw == "object") { otherData = JSON.parse($("#otherData").text()); furnitureInitialData = getDataFun.getFurnitureData(); } else { furnitureInitialData = file_furnitureData; otherData = otherData; } App.mount("#app"); }
(window.RLQ = window.RLQ || []).push([ "jquery", () => { furnitureOnload(); }, ]);
</script>