- // ==UserScript==
- // [url=home.php?mod=space&uid=668096]@Name[/url] 方块 Roll 奖项概率计算器
- // @namespace https://tampermonkey.net/
- // @version 1.0
- // @description 输入已 Roll 到的方块颜色,计算最终获得一等奖、二等奖、三等奖的概率
- // @author huiguo
- // [url=home.php?mod=space&uid=700810]@Match[/url] https://www.gamemale.com/*
- // @grant none
- // ==/UserScript==
- (function () {
- 'use strict';
- // ==============================
- // 方块概率配置
- // ==============================
- const cubeProb = {
- "金色": 0.01,
- "黑色": 0.04,
- "白色": 0.04,
- "绿色": 0.13,
- "蓝色": 0.13,
- "粉色": 0.13,
- "橙色": 0.13,
- "红色": 0.13,
- "紫色": 0.13,
- "青色": 0.13
- };
- const colors = Object.keys(cubeProb);
- // ==============================
- // 创建悬浮面板
- // ==============================
- const panel = document.createElement("div");
- panel.id = "roll-calc-panel";
- panel.innerHTML = `
- <div id="roll-calc-header">
- <span>方块 Roll 概率计算器</span>
- <button id="roll-calc-toggle">-</button>
- </div>
- <div id="roll-calc-body">
- <label>第一次 Roll</label>
- <select id="roll1">
- <option value="">未输入</option>
- </select>
- <label>第二次 Roll</label>
- <select id="roll2">
- <option value="">未输入</option>
- </select>
- <label>第三次 Roll</label>
- <select id="roll3">
- <option value="">未输入</option>
- </select>
- <button id="roll-calc-btn">计算概率</button>
- <button id="roll-reset-btn">重置</button>
- <div id="roll-result">
- 请按顺序输入已 Roll 到的颜色。
- </div>
- </div>
- `;
- document.body.appendChild(panel);
- // ==============================
- // 添加样式
- // ==============================
- const style = document.createElement("style");
- style.textContent = `
- #roll-calc-panel {
- position: fixed;
- right: 20px;
- bottom: 20px;
- width: 310px;
- background: #ffffff;
- color: #222;
- border-radius: 14px;
- box-shadow: 0 6px 24px rgba(0, 0, 0, 0.22);
- z-index: 999999;
- font-family: "Microsoft YaHei", Arial, sans-serif;
- overflow: hidden;
- border: 1px solid #ddd;
- }
- #roll-calc-header {
- background: #4a6cf7;
- color: white;
- padding: 10px 12px;
- font-size: 15px;
- font-weight: bold;
- display: flex;
- justify-content: space-between;
- align-items: center;
- cursor: move;
- user-select: none;
- }
- #roll-calc-toggle {
- background: white;
- color: #4a6cf7;
- border: none;
- border-radius: 6px;
- width: 26px;
- height: 24px;
- font-size: 16px;
- cursor: pointer;
- font-weight: bold;
- }
- #roll-calc-body {
- padding: 14px;
- }
- #roll-calc-body label {
- display: block;
- margin-top: 9px;
- margin-bottom: 4px;
- font-size: 14px;
- font-weight: bold;
- }
- #roll-calc-body select {
- width: 100%;
- padding: 7px;
- border-radius: 8px;
- border: 1px solid #ccc;
- font-size: 14px;
- background: white;
- color: #222;
- }
- #roll-calc-btn,
- #roll-reset-btn {
- width: 100%;
- margin-top: 10px;
- padding: 9px;
- border: none;
- border-radius: 8px;
- font-size: 14px;
- cursor: pointer;
- }
- #roll-calc-btn {
- background: #4a6cf7;
- color: white;
- }
- #roll-reset-btn {
- background: #eeeeee;
- color: #333;
- }
- #roll-calc-btn:hover {
- background: #3454d1;
- }
- #roll-reset-btn:hover {
- background: #dddddd;
- }
- #roll-result {
- margin-top: 12px;
- padding: 10px;
- background: #f0f3ff;
- border-radius: 10px;
- font-size: 13px;
- line-height: 1.7;
- max-height: 220px;
- overflow-y: auto;
- }
- .roll-warning {
- color: #c0392b;
- font-weight: bold;
- }
- .roll-prize {
- color: #4a6cf7;
- font-weight: bold;
- }
- `;
- document.head.appendChild(style);
- // ==============================
- // 初始化下拉框
- // ==============================
- function initSelects() {
- const selects = [
- document.getElementById("roll1"),
- document.getElementById("roll2"),
- document.getElementById("roll3")
- ];
- selects.forEach(select => {
- colors.forEach(color => {
- const option = document.createElement("option");
- option.value = color;
- option.textContent = color + "方块";
- select.appendChild(option);
- });
- });
- }
- initSelects();
- // ==============================
- // 判断最终奖项
- // 优先级:一等奖 > 二等奖 > 三等奖
- // ==============================
- function judgePrize(rolls) {
- const counts = {};
- rolls.forEach(color => {
- counts[color] = (counts[color] || 0) + 1;
- });
- const uniqueColors = Object.keys(counts);
- const countValues = Object.values(counts);
- // 一等奖:只要有金色
- if (rolls.includes("金色")) {
- return "一等奖";
- }
- // 一等奖:三个颜色相同
- if (uniqueColors.length === 1) {
- return "一等奖";
- }
- // 二等奖:两个相同 + 一个不同
- if (countValues.includes(2)) {
- return "二等奖";
- }
- // 二等奖:黑色 + 白色 + 其他颜色
- if (
- rolls.includes("黑色") &&
- rolls.includes("白色") &&
- uniqueColors.length === 3
- ) {
- return "二等奖";
- }
- // 三等奖:三个不同颜色
- if (uniqueColors.length === 3) {
- return "三等奖";
- }
- return "未中奖";
- }
- // ==============================
- // 枚举剩余 Roll 的所有可能
- // ==============================
- function enumerateRolls(currentRolls, remainingCount, currentProb, resultProb) {
- if (remainingCount === 0) {
- const prize = judgePrize(currentRolls);
- resultProb[prize] += currentProb;
- return;
- }
- colors.forEach(color => {
- enumerateRolls(
- [...currentRolls, color],
- remainingCount - 1,
- currentProb * cubeProb[color],
- resultProb
- );
- });
- }
- // ==============================
- // 计算概率
- // ==============================
- function calculate() {
- const r1 = document.getElementById("roll1").value;
- const r2 = document.getElementById("roll2").value;
- const r3 = document.getElementById("roll3").value;
- const resultBox = document.getElementById("roll-result");
- const inputs = [r1, r2, r3];
- if (!r1 && (r2 || r3)) {
- resultBox.innerHTML = `
- <span class="roll-warning">
- 请按顺序输入:必须先输入第一次 Roll 的颜色。
- </span>
- `;
- return;
- }
- if (!r2 && r3) {
- resultBox.innerHTML = `
- <span class="roll-warning">
- 请按顺序输入:必须先输入第二次 Roll 的颜色。
- </span>
- `;
- return;
- }
- const knownRolls = inputs.filter(x => x !== "");
- const remainingCount = 3 - knownRolls.length;
- const resultProb = {
- "一等奖": 0,
- "二等奖": 0,
- "三等奖": 0,
- "未中奖": 0
- };
- enumerateRolls(knownRolls, remainingCount, 1, resultProb);
- const first = (resultProb["一等奖"] * 100).toFixed(2);
- const second = (resultProb["二等奖"] * 100).toFixed(2);
- const third = (resultProb["三等奖"] * 100).toFixed(2);
- const none = (resultProb["未中奖"] * 100).toFixed(2);
- const inputText = knownRolls
- .map((c, i) => `第 ${i + 1} 次:${c}方块`)
- .join("<br>");
- if (remainingCount === 0) {
- const prize = judgePrize(knownRolls);
- resultBox.innerHTML = `
- <strong>你已经输入了三次 Roll 结果:</strong><br>
- ${inputText}<br><br>
- 最终结果:<span class="roll-prize">${prize}</span>
- `;
- } else {
- resultBox.innerHTML = `
- <strong>当前已知结果:</strong><br>
- ${inputText}<br><br>
- 剩余还要 Roll <strong>${remainingCount}</strong> 次。<br><br>
- 最终获得 <strong>一等奖</strong> 的概率:${first}%<br>
- 最终获得 <strong>二等奖</strong> 的概率:${second}%<br>
- 最终获得 <strong>三等奖</strong> 的概率:${third}%<br>
- 最终 <strong>未中奖</strong> 的概率:${none}%
- `;
- }
- }
- // ==============================
- // 重置
- // ==============================
- function reset() {
- document.getElementById("roll1").value = "";
- document.getElementById("roll2").value = "";
- document.getElementById("roll3").value = "";
- document.getElementById("roll-result").innerHTML =
- "请按顺序输入已 Roll 到的颜色。";
- }
- document.getElementById("roll-calc-btn").addEventListener("click", calculate);
- document.getElementById("roll-reset-btn").addEventListener("click", reset);
- // ==============================
- // 收起 / 展开
- // ==============================
- const toggleBtn = document.getElementById("roll-calc-toggle");
- const body = document.getElementById("roll-calc-body");
- toggleBtn.addEventListener("click", function () {
- if (body.style.display === "none") {
- body.style.display = "block";
- toggleBtn.textContent = "-";
- } else {
- body.style.display = "none";
- toggleBtn.textContent = "+";
- }
- });
- // ==============================
- // 面板拖拽
- // ==============================
- const header = document.getElementById("roll-calc-header");
- let isDragging = false;
- let offsetX = 0;
- let offsetY = 0;
- header.addEventListener("mousedown", function (e) {
- isDragging = true;
- const rect = panel.getBoundingClientRect();
- offsetX = e.clientX - rect.left;
- offsetY = e.clientY - rect.top;
- panel.style.right = "auto";
- panel.style.bottom = "auto";
- panel.style.left = rect.left + "px";
- panel.style.top = rect.top + "px";
- });
- document.addEventListener("mousemove", function (e) {
- if (!isDragging) return;
- panel.style.left = e.clientX - offsetX + "px";
- panel.style.top = e.clientY - offsetY + "px";
- });
- document.addEventListener("mouseup", function () {
- isDragging = false;
- });
- })();
复制代码 |