Drop: Select quality of cards based on drop rates

This commit is contained in:
2022-09-21 11:17:11 +02:00
parent fc45908419
commit 73a2df6e74
3 changed files with 89 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ const { SlashCommandBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, Compo
const { Card, User, Character, DropHistory, sequelize } = require("../models");
const { customAlphabet } = require("nanoid");
const { CardUtils, UserUtils, ReplyUtils, GeneralUtils, Rendering } = require("../util");
const { QUALITY } = require("../config/constants");
const card = require("../models/card");
module.exports = {
@@ -33,6 +34,28 @@ module.exports = {
});
for (let i = 0; i < 3; i++) {
//generate quality based on drop rate
let quality = undefined;
let roll = Math.random() * 100;
if (roll <= 45.0) {
quality = QUALITY.BAD; //45%
}
if (roll > 45.0) {
quality = QUALITY.OK; //25%
}
if (roll > 70.0) {
quality = QUALITY.GOOD; //15%
}
if (roll > 85.0) {
quality = QUALITY.GREAT; //10%
}
if (roll > 95.0) {
quality = QUALITY.EXCELLENT; //5%
}
if (roll > 99.9) {
quality = QUALITY.SHINY; //0.1%
}
//get number of characters in database
const characterId = characters[i].id
console.log(`characterId: ${characterId}`);
@@ -40,7 +63,7 @@ module.exports = {
let newCard = await Card.create({
characterId: characterId,
identifier: CardUtils.generateIdentifier(),
quality: Math.floor(Math.random() * 6) + 1,
quality: quality,
printNr: await CardUtils.getNextPrintNumber(characterId),
});

20
config/constants.js Normal file
View File

@@ -0,0 +1,20 @@
const QUALITY = {
BAD : 1,
OK : 2,
GOOD : 3,
GREAT : 4,
EXCELLENT : 5,
SHINY : 6
}
const QUALITY_NAMES = {
1 : "Bad",
2 : "Ok",
3 : "Good",
4 : "Great",
5 : "Excellent",
6 : "Shiny"
}
exports.QUALITY = QUALITY;
exports.QUALITY_NAMES = QUALITY_NAMES;

45
dropRateTest.js Normal file
View File

@@ -0,0 +1,45 @@
const { QUALITY, QUALITY_NAMES } = require('./config/constants');
let results = [];
let drops = 1000;
let header = `⫭=== Quality distribution for ${drops.toLocaleString()} drops: ===⫬`;
console.log(header);
console.log(`|${' '.repeat(header.length-2)}|`);
for ( let i = 0 ; i < drops ; i++) {
//generate rarity based on drop rate
let quality = undefined;
let roll = Math.random() * 100;
if (roll <= 45.0) {
quality = QUALITY.BAD;
}
if (roll > 45.0) {
quality = QUALITY.OK;
}
if (roll > 70.0) {
quality = QUALITY.GOOD;
}
if (roll > 85.0) {
quality = QUALITY.GREAT;
}
if (roll > 95.0) {
quality = QUALITY.EXCELLENT;
}
if (roll > 99.9) {
quality = QUALITY.SHINY;
}
results.push(quality);
}
//count the number of times each quality appears
let counts = {};
results.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });
//print results with percentages
let p_total = 0;
for (let key in counts) {
let p = counts[key]/results.length * 100;
p_total += p;
let cpStr = `${counts[key].toLocaleString()} (${p}%)`;
let fStr = `| ${key}/${QUALITY_NAMES[key]}${' '.repeat(10-QUALITY_NAMES[key].length)}: ${cpStr}`;
console.log(`${fStr}${' '.repeat(header.length-fStr.length-1)}|`);
}
console.log(`${'_'.repeat(header.length-2)}`);