Certainly! Below, I'll provide you with the code for the "Artifact Drop Calculator" module, integrating it into your existing PyQt5 application. I'll ensure the code is clean, well-organized, and matches your project's style and criteria.
Here's what we'll do:
-
Create the UI for the Artifact Drop Calculator: We'll design a new window that allows the user to select the artifact's rarity, type, input their desired stats, and calculate the probability.
-
Implement the Logic for Probability Calculation: We'll write functions that compute the probability based on the game's mechanics you've described.
-
Integrate the Module into Your Main Application: We'll modify main.py to open the new window when the "Artifact Drop Calculator" button is clicked.
Below is the complete code for the new module, along with explanations.
1. Create the UI for the Artifact Drop Calculator
First, we'll create drop_calculator_ui.py, which contains the UI design for the drop calculator window. We'll match the style you've used in main_menu_ui.py.
# drop_calculator_ui.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_DropCalculatorWindow(object):
def setupUi(self, DropCalculatorWindow):
DropCalculatorWindow.setObjectName("DropCalculatorWindow")
DropCalculatorWindow.resize(500, 600)
DropCalculatorWindow.setFixedSize(500, 600)
DropCalculatorWindow.setWindowIcon(QtGui.QIcon("resources/icons/drop_calculator_icon.png"))
# Central Widget
self.centralwidget = QtWidgets.QWidget(DropCalculatorWindow)
self.centralwidget.setObjectName("centralwidget")
# Title Label
self.titleLabel = QtWidgets.QLabel(self.centralwidget)
self.titleLabel.setGeometry(QtCore.QRect(50, 20, 400, 50))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
self.titleLabel.setFont(font)
self.titleLabel.setAlignment(QtCore.Qt.AlignCenter)
self.titleLabel.setObjectName("titleLabel")
# Rarity Selection
self.rarityLabel = QtWidgets.QLabel(self.centralwidget)
self.rarityLabel.setGeometry(QtCore.QRect(50, 90, 150, 30))
font.setPointSize(12)
font.setBold(False)
self.rarityLabel.setFont(font)
self.rarityLabel.setObjectName("rarityLabel")
self.rarityComboBox = QtWidgets.QComboBox(self.centralwidget)
self.rarityComboBox.setGeometry(QtCore.QRect(210, 90, 240, 30))
self.rarityComboBox.setFont(font)
self.rarityComboBox.setObjectName("rarityComboBox")
self.rarityComboBox.addItem("Epic (4★)")
self.rarityComboBox.addItem("Legendary (5★)")
# Artifact Type Selection
self.typeLabel = QtWidgets.QLabel(self.centralwidget)
self.typeLabel.setGeometry(QtCore.QRect(50, 140, 150, 30))
self.typeLabel.setFont(font)
self.typeLabel.setObjectName("typeLabel")
self.typeComboBox = QtWidgets.QComboBox(self.centralwidget)
self.typeComboBox.setGeometry(QtCore.QRect(210, 140, 240, 30))
self.typeComboBox.setFont(font)
self.typeComboBox.setObjectName("typeComboBox")
self.typeComboBox.addItems(["Flower", "Plume", "Sands", "Goblet", "Circlet"])
# Initial Substats Selection
self.initSubstatsLabel = QtWidgets.QLabel(self.centralwidget)
self.initSubstatsLabel.setGeometry(QtCore.QRect(50, 190, 150, 30))
self.initSubstatsLabel.setFont(font)
self.initSubstatsLabel.setObjectName("initSubstatsLabel")
self.initSubstatsComboBox = QtWidgets.QComboBox(self.centralwidget)
self.initSubstatsComboBox.setGeometry(QtCore.QRect(210, 190, 240, 30))
self.initSubstatsComboBox.setFont(font)
self.initSubstatsComboBox.setObjectName("initSubstatsComboBox")
# Desired Substats
self.desiredSubstatsLabel = QtWidgets.QLabel(self.centralwidget)
self.desiredSubstatsLabel.setGeometry(QtCore.QRect(50, 240, 400, 30))
self.desiredSubstatsLabel.setFont(font)
self.desiredSubstatsLabel.setObjectName("desiredSubstatsLabel")
# Substat Checkboxes
self.substatOptions = ["ATK%", "DEF%", "HP%", "Energy Recharge", "Elemental Mastery",
"Crit Rate", "Crit Damage", "Flat ATK", "Flat DEF", "Flat HP"]
self.substatCheckboxes = []
y_position = 280
for i, substat in enumerate(self.substatOptions):
checkbox = QtWidgets.QCheckBox(self.centralwidget)
checkbox.setGeometry(QtCore.QRect(70 + (i % 2) * 220, y_position + (i // 2) * 30, 200, 30))
checkbox.setFont(font)
checkbox.setText(substat)
checkbox.setObjectName(f"checkbox{substat.replace(' ', '')}")
self.substatCheckboxes.append(checkbox)
# Calculate Button
self.calculateButton = QtWidgets.QPushButton(self.centralwidget)
self.calculateButton.setGeometry(QtCore.QRect(150, 500, 200, 50))
font.setBold(True)
self.calculateButton.setFont(font)
self.calculateButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.calculateButton.setObjectName("calculateButton")
# Result Label
self.resultLabel = QtWidgets.QLabel(self.centralwidget)
self.resultLabel.setGeometry(QtCore.QRect(50, 560, 400, 30))
font.setBold(False)
self.resultLabel.setFont(font)
self.resultLabel.setAlignment(QtCore.Qt.AlignCenter)
self.resultLabel.setObjectName("resultLabel")
DropCalculatorWindow.setCentralWidget(self.centralwidget)
# Apply Stylesheet
self.applyStyles()
self.retranslateUi(DropCalculatorWindow)
QtCore.QMetaObject.connectSlotsByName(DropCalculatorWindow)
def retranslateUi(self, DropCalculatorWindow):
_translate = QtCore.QCoreApplication.translate
DropCalculatorWindow.setWindowTitle(_translate("DropCalculatorWindow", "Artifact Drop Calculator"))
self.titleLabel.setText(_translate("DropCalculatorWindow", "Artifact Drop Calculator"))
self.rarityLabel.setText(_translate("DropCalculatorWindow", "Artifact Rarity:"))
self.typeLabel.setText(_translate("DropCalculatorWindow", "Artifact Type:"))
self.initSubstatsLabel.setText(_translate("DropCalculatorWindow", "Initial Substats:"))
self.desiredSubstatsLabel.setText(_translate("DropCalculatorWindow", "Select Desired Substats:"))
self.calculateButton.setText(_translate("DropCalculatorWindow", "Calculate Probability"))
self.resultLabel.setText(_translate("DropCalculatorWindow", ""))
def applyStyles(self):
style = """
QWidget {
background-color: #3B4252;
color: #ECEFF4;
}
QLabel {
color: #ECEFF4;
}
QComboBox {
background-color: #4C566A;
color: #ECEFF4;
border-radius: 5px;
padding: 5px;
}
QComboBox QAbstractItemView {
background-color: #4C566A;
selection-background-color: #5E81AC;
}
QCheckBox {
color: #ECEFF4;
}
QPushButton {
background-color: #5E81AC;
border: none;
border-radius: 10px;
color: #ECEFF4;
}
QPushButton:hover {
background-color: #81A1C1;
}
"""
self.centralwidget.setStyleSheet(style)
2. Implement the Logic for Probability Calculation
Next, we'll create drop_calculator_window.py, which includes the logic to compute the probability based on the user's inputs.
# drop_calculator_window.py
from PyQt5.QtWidgets import QMainWindow, QMessageBox
from PyQt5.QtCore import Qt
from drop_calculator_ui import Ui_DropCalculatorWindow
import math
class DropCalculatorWindow(QMainWindow, Ui_DropCalculatorWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
# Connect signals and slots
self.rarityComboBox.currentTextChanged.connect(self.update_initial_substats)
self.calculateButton.clicked.connect(self.calculate_probability)
# Initialize initial substats based on default rarity
self.update_initial_substats()
def update_initial_substats(self):
rarity = self.rarityComboBox.currentText()
self.initSubstatsComboBox.clear()
if rarity == "Epic (4★)":
self.initSubstatsComboBox.addItems(["2 Substats", "3 Substats"])
else:
self.initSubstatsComboBox.addItems(["3 Substats", "4 Substats"])
def calculate_probability(self):
rarity = self.rarityComboBox.currentText()
init_substats_text = self.initSubstatsComboBox.currentText()
init_substats = int(init_substats_text[0])
# Collect desired substats
desired_substats = []
for checkbox in self.substatCheckboxes:
if checkbox.isChecked():
desired_substats.append(checkbox.text())
if not desired_substats:
QMessageBox.warning(self, "No Substats Selected", "Please select at least one desired substat.")
return
# Calculate probability
total_probability = self.compute_probability(rarity, init_substats, desired_substats)
# Display result
self.resultLabel.setText(f"Probability: {total_probability:.5%}")
def compute_probability(self, rarity, init_substats, desired_substats):
# Total possible substats
all_substats = ["ATK%", "DEF%", "HP%", "Energy Recharge", "Elemental Mastery",
"Crit Rate", "Crit Damage", "Flat ATK", "Flat DEF", "Flat HP"]
num_desired = len(desired_substats)
num_possible = len(all_substats)
# Determine the number of upgrades based on rarity
if rarity == "Epic (4★)":
max_level = 16
else:
max_level = 20
num_upgrades = (max_level // 4)
# For 4★ max substats at level 0 is 4, for 5★ it's 4
max_substats = 4
# Starting substats
num_initial_substats = init_substats
num_new_substats = max_substats - num_initial_substats
# Number of times existing substats are upgraded
num_upgrades_existing = num_upgrades - num_new_substats
# Probability of desired substats appearing in initial substats
prob_initial = self.combination_probability(num_possible, num_initial_substats, num_desired)
# Probability of desired substats being added as new substats
prob_new_substats = self.combination_probability(num_possible - num_initial_substats, num_new_substats, num_desired - num_initial_substats)
# Probability of upgrades going into desired substats
prob_upgrades = self.upgrade_probability(num_initial_substats + num_new_substats, num_desired, num_upgrades_existing)
# Total probability is the product of the probabilities
total_probability = prob_initial * prob_new_substats * prob_upgrades
return total_probability
def combination_probability(self, total_substats, num_slots, num_desired_substats):
# Calculate the probability that among num_slots slots, num_desired_substats desired substats appear
if num_desired_substats > num_slots or num_desired_substats < 0:
return 0.0
favorable = math.comb(num_desired_substats, num_desired_substats) * math.comb(total_substats - num_desired_substats, num_slots - num_desired_substats)
total = math.comb(total_substats, num_slots)
probability = favorable / total
return probability
def upgrade_probability(self, total_substats, num_desired_substats, num_upgrades):
# Probability that upgrades go into desired substats
# Assuming each upgrade randomly selects one of the substats
if num_desired_substats == 0:
return 0.0
single_upgrade_prob = num_desired_substats / total_substats
# Probability that all upgrades go into desired substats
probability = single_upgrade_prob ** num_upgrades
return probability
Explanation of the Logic:
-
Initial Substats Probability (prob_initial):
- We calculate the probability that the initial substats include the desired substats.
- This is a combination problem, where we select
num_initial_substats from num_possible substats.
- We calculate the favorable combinations where desired substats appear.
-
New Substats Probability (prob_new_substats):
- For the new substats added at level 4, we calculate the probability that they are the desired substats.
- We adjust the total substats by excluding those already considered.
-
Upgrade Probability (prob_upgrades):
- We calculate the probability that the stat upgrades (procs) go into desired substats.
- Each upgrade has a chance equal to the fraction of desired substats over total substats.
Note: This is a simplified model and assumes independence between events, which may not be completely accurate due to the game's mechanics. You may need to adjust the computation to match the exact probabilistic model of the game's mechanics.
3. Integrate the Module into Your Main Application
Finally, we'll modify your main.py to include the new module.
# main.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
from critmass_calculator.calculator_window import CalculatorWindow
from team_builder.team_builder_window import TeamBuilderWindow
from drop_calculator_window import DropCalculatorWindow # Import DropCalculatorWindow
from main_menu_ui import Ui_MainMenuWindow
class MainMenu(QMainWindow, Ui_MainMenuWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.calculatorButton.clicked.connect(self.open_calculator)
self.teamBuilderButton.clicked.connect(self.open_team_builder)
self.dropCalculatorButton.clicked.connect(self.open_drop_calculator)
def open_calculator(self):
self.calculator = CalculatorWindow()
self.calculator.show()
def open_team_builder(self):
self.team_builder = TeamBuilderWindow()
self.team_builder.show()
def open_drop_calculator(self):
self.drop_calculator = DropCalculatorWindow()
self.drop_calculator.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
main_menu = MainMenu()
main_menu.show()
sys.exit(app.exec_())
Additional Notes:
-
Resources Folder: Ensure that all icons and resources are in the resources/icons/ directory, as referenced in the UI code.
-
PyQt5 Signals and Slots: The connect methods link UI elements to functions. For example, when the user clicks the "Calculate Probability" button, it triggers the calculate_probability method.
-
Code Organization: We've separated the UI code and the logic by using drop_calculator_ui.py and drop_calculator_window.py. This keeps the UI design and functionality modular and organized.
-
Error Handling: The code includes basic error handling, such as checking if the user has selected any desired substats.
-
Styling: The UI styling matches your existing application, promoting a consistent look and feel.
Testing the Module:
- Run your main application.
- Click on "Artifact Drop Calculator" from the main menu.
- In the drop calculator window:
- Select the artifact rarity and type.
- Choose the number of initial substats.
- Check the desired substats.
- Click "Calculate Probability".
- The probability will be displayed at the bottom.
Conclusion:
The code provided should integrate seamlessly into your existing project. It meets the requirements you've specified and follows clean coding practices. If you have any questions or need further adjustments, feel free to ask!