本WIKI于2021年03月04日通过了时雨音的接管申请,编辑权限开放。

欢迎收藏本WIKI防止迷路,也希望有爱的小伙伴和我们一起编辑哟~

建议使用PC版页面访问本WIKI以获得更佳体验!

编辑帮助WIKI建议加入我们使用哔哩哔哩APP打开WIKI

全站通知:

用户:646195980/Gacha.js

来自坎特伯雷公主与骑士唤醒冠军之剑的奇幻冒险WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:Ctrl-F5
/**
 * Gacha.js | 仅供娱乐,请勿较真。结果没有参考价值。
 * Author: 飞小RAN (https://wiki.biligame.com/gt/User:646195980)
 * License: See https://wiki.biligame.com/gt/User:646195980#License
 * 另一种抽卡模拟器?
 **/
async function gacha(){
    // Part.1 Heros Pull Database
    if (!window.GachaData) {
        console.log('[Gacha] 不存在GachaData,尝试获取');
        try {
            const response = await fetch(`https://wiki.biligame.com/gt/api.php?action=ask&query=[[分类:角色]]|?名字|?稀有度|?职业|?属性|?简称|format=json|limit=520|offset=0|link=all|headers=show|type=full|sort=名字|order=asc|eq=yes&api_version=2&utf8=1&format=json`);
            const jsondata = await response.json();
            window.GachaData = jsondata.query;
        } catch (e) {
            console.error(e);
        }
    }
    // Part.2 Heros Data
    let heros = {
        1: [],
        2: [],
        3: [],
        4: []
    };
    for (const key in window.GachaData.results) {
        const item = window.GachaData.results[key];
        const { fulltext, printouts } = item;
        const { 名字, 稀有度, 职业, 属性, 简称 } = printouts;
        const data = {
            角色: fulltext || null,
            名字: 名字[0] || null,
            稀有度: 稀有度[0] || null,
            职业: 职业[0] || null,
            属性: 属性[0] || null,
            简称: 简称[0] || null
        };
        await heros[data.稀有度].push(data);
    }
    // Part.3 Heros Gacha
    let list = {
        heros: [],
        rank: [],
        times: await Number(document.querySelector(".gachatable .times").innerText) || 0,
        mileage: await Number(document.querySelector(".gachatable .mileage").innerText) || 0,
        gem: await Number(document.querySelector(".gachatable .gem").innerText) || 0,
        G1: await Number(document.querySelector(".gachatable .G1").innerText) || 0,
        G2: await Number(document.querySelector(".gachatable .G2").innerText) || 0,
        G3: await Number(document.querySelector(".gachatable .G3").innerText) || 0
    };
    list.gem += 2700;
    list.times += 10;
    for (let i = 0; i < 10; i++) {
        const sum = list.rank.reduce((acc, cur) => acc + cur, 0);
        if ( i === 9 && sum < 10) {
            await list.rank.push(2);
        } else {
            const rank = await random();
            list[`G${rank}`] += 1;
            await list.rank.push(rank);
        }
    }
    for (let i = 1; i < list.G1; i++) {
        list.mileage += 1;
    }
    for (let i = 1; i < list.G2; i++) {
        list.mileage += 8;
    }
    for (let i = 1; i < list.G3; i++) {
        list.mileage += 50;
    }
    await list.rank.forEach(i => {
        list.heros.push(heros[i][Math.floor(Math.random() * heros[i].length)]);
    });
    // Part.4 Heros Parse
    document.querySelector(".gachatable .times").innerText = list.times;
    document.querySelector(".gachatable .mileage").innerText = list.mileage;
    document.querySelector(".gachatable .gem").innerText = list.gem;
    document.querySelector(".gachatable .G1").innerText = list.G1;
    document.querySelector(".gachatable .G2").innerText = list.G2;
    document.querySelector(".gachatable .G3").innerText = list.G3;
    let text = '';
    await list.heros.forEach(i => {
        text += `{{角色图鉴/行|${i.角色}|${i.名字}|${i.稀有度}|${i.职业}|${i.属性}|${i.简称}}}`;
    });
    try {
        const response = await fetch(`https://wiki.biligame.com/gt/api.php?action=parse&format=json&title=模拟召唤&text=${text}&wrapoutputclass=gacha&disablelimitreport=1&disableeditsection=1&disablestylededuplication=1&contentmodel=wikitext&utf8=1`);
        const data = await response.json();
        // 输出
        document.querySelector("div.gacha").outerHTML = data.parse.text["*"];
    } catch (e) {
        console.error(e);
    }
}
// Part.X 稀有度
function random() {
    const random = Math.random(); // 生成一个0到1之间的随机数
    if (random <= 0.7825) {
        return 1; // 78.25%的概率返回1
    } else if (random <= 0.9725) {
        return 2; // 19.00%的概率返回2(0.7825 + 0.19 = 0.9725)
    } else {
        return 3; // 2.75%的概率返回3
    }
}
// Part.0 Heros Button
document.querySelector(".gacha.btn").outerHTML = `<a class="btn btn-default" href="javascript:gacha()" role="button">${document.querySelector(".gacha.btn").innerText}</a>`;