奖池数据结构 (allItems)
定义了所有可抽取物品的配置,包含名称、权重和分类信息。
权重决定了物品的抽取概率,分类用于区分普通、稀有和传说物品。
const allItems = [
{ name: "菜篮子*2", weight: 1525, category: "普通" },
{ name: "欢乐豆*10", weight: 1000, category: "普通" },
{ name: "史诗体验卡*1", weight: 1000, category: "普通" },
{ name: "手气卡*1", weight: 3000, category: "普通" },
{ name: "换将卡*1", weight: 3000, category: "普通" },
{ name: "进阶丹*1", weight: 300, category: "普通" },
{ name: "雁翎甲*1", weight: 100, category: "普通" },
{ name: "雁翎*1000", weight: 50, category: "稀有" },
{ name: "当期精品武将*1", weight: 20, category: "稀有" },
{ name: "当期传说皮肤动态包*1", weight: 4, category: "史诗" },
{ name: "当期史诗武将*1", weight: 1, category: "传说" }
];
单次抽奖逻辑 (singleDraw)
这是抽奖系统的核心函数,基于权重随机算法选择奖励物品。
function singleDraw() {
const totalWeight = allItems.reduce((sum, item) => sum + item.weight, 0);
const random = Math.random() * totalWeight;
let currentWeight = 0;
let selectedItem = null;
for (const item of allItems) {
currentWeight += item.weight;
if (random <= currentWeight) {
selectedItem = item;
break;
}
}
return selectedItem.name;
}
批量抽奖逻辑 (batchDraw)
处理批量抽奖操作,更新统计信息和花费计算。
function batchDraw(count) {
const results = {};
for (let i = 0; i < count; i++) {
const itemName = singleDraw();
results[itemName] = (results[itemName] || 0) + 1;
state.userSingleStats[itemName] = (state.userSingleStats[itemName] || 0) + 1;
state.userTotalStats[itemName] = (state.userTotalStats[itemName] || 0) + 1;
}
state.totalDraws += count;
state.silverCost += count * 100;
state.pearlCost += count * 0.01;
updateCostDisplay();
updateAllStatsTables();
return results;
}
用户抽奖函数 (userDrawOnce)
处理用户抽奖操作,包括统计重置和史诗物品检测。
function userDrawOnce(count) {
state.userSingleStats = initializeStats();
const results = batchDraw(count);
if (results["当期史诗武将*1"] && state.epicPopup) {
showEpicPopup("当期史诗武将*1");
}
}
史诗弹窗函数 (showEpicPopup)
显示抽到史诗物品的恭喜弹窗。
function showEpicPopup(itemName) {
const titleEl = document.getElementById('epic-title');
const contEl = document.getElementById('epic-content');
titleEl.textContent = '恭喜获得!';
contEl.textContent = `你获得了:${itemName}!`;
document.getElementById('epic-popup').style.display = 'flex';
}
核心设计特点
权重系统:基于权重的随机算法,确保高权重物品出现概率更高。
分类系统:物品分为普通、稀有和传说三个等级,提供清晰的稀有度区分。
成本计算:每次抽奖消耗固定数量的银币和史诗宝珠。
弹窗提示:抽到传说物品时显示恭喜弹窗,增强用户体验。
这套系统通过简单的权重算法和清晰的物品分类,为用户提供了直观的抽奖体验。