Skip to content

Commit

Permalink
Add backend for items, make disabling on buttons actually work, and o…
Browse files Browse the repository at this point in the history
…ther changes
  • Loading branch information
kaneryu committed Aug 7, 2024
1 parent aa7532e commit b635d51
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 180 deletions.
313 changes: 174 additions & 139 deletions src/createthesun/gameLogic/itemGameLogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,158 +7,193 @@
from .. import gamedefine


def canAfford(item: str, doBuyMultiply=True) -> bool:
"""
Checks if you can afford an item.
from PySide6.QtCore import QObject
from PySide6.QtCore import Signal , Slot

# first argment is the item, second is misc

Check failure on line 13 in src/createthesun/gameLogic/itemGameLogic.py

View workflow job for this annotation

GitHub Actions / test

Ruff (I001)

src\createthesun\gameLogic\itemGameLogic.py:1:1: I001 Import block is un-sorted or un-formatted
_instance = None
class ItemGameLogic(QObject):



@staticmethod
def getInstance():
global _instance
if _instance is None:
_instance = ItemGameLogic()
return _instance

def __new__(cls, *args, **kwargs):
global _instance
if not _instance:
_instance = super(ItemGameLogic, cls).__new__(cls, *args, **kwargs)
return _instance

def __init__(self):
super().__init__()

@Slot(str, result=bool)
def canAfford(self, item: str, doBuyMultiply=True) -> bool:
"""
Checks if you can afford an item.
Args:
item (str): The item to check.
Returns:
bool: Whether you can afford the item or not.
"""

buyMultiplier = gamedefine.game.mainTabBuyMultiple if doBuyMultiply else 1

if gamedefine.items[item].costEquation == "":
return True

costs = gamedefine.items[item].cost
ongoing = True

for i in costs:
if i["what"].amount < i["amount"] * buyMultiplier:
ongoing = False


return ongoing

@Slot(str, result=None)
def _purchase(self, item: str, doBuyMultiply=False) -> None:
"""
Purchases an item.
Args:
item (str): The item to purchase.
"""

buyMultiplier = gamedefine.game.mainTabBuyMultiple if doBuyMultiply else 1

if gamedefine.items[item].costEquation == "":

gamedefine.items[item].amount += 1
print(f"purchased {item}, now have {gamedefine.items[item].amount}")
return


costs = gamedefine.items[item].cost
gives = gamedefine.items[item].gives


for i in costs:
i["what"].amount -= i["amount"] * buyMultiplier
for i in gives:
i["what"].amount += i["amount"] * buyMultiplier
print(f"purchased {item}, now have {gamedefine.items[item].amount}, with buy multiplier {buyMultiplier}")

Args:
item (str): The item to check.
Returns:
bool: Whether you can afford the item or not.
"""

buyMultiplier = gamedefine.gamedefine.mainTabBuyMultiple if doBuyMultiply else 1

if gamedefine.gamedefine.itemInternalDefine[item]["costEquation"] == "":
return True

whatItCosts = gamedefine.gamedefine.itemInternalDefine[item]["whatItCosts"]

ongoing = True

for i in whatItCosts:
if gamedefine.gamedefine.amounts[i["what"]] < i["amount"] * buyMultiplier:
ongoing = False

return ongoing


def purchase(item: str, doBuyMultiply=False) -> None:
"""
Purchases an item.
Args:
item (str): The item to purchase.
"""

buyMultiplier = gamedefine.gamedefine.mainTabBuyMultiple if doBuyMultiply else 1

if gamedefine.gamedefine.itemInternalDefine[item]["costEquation"] == "":
gamedefine.gamedefine.amounts[item] += 1
print(f"purchased {item}, now have {gamedefine.gamedefine.amounts[item]}")
return

whatItCosts = gamedefine.gamedefine.itemInternalDefine[item]["whatItCosts"]
whatItGives = gamedefine.gamedefine.itemInternalDefine[item]["whatItGives"]

for i in whatItCosts:
gamedefine.gamedefine.amounts[i["what"]] -= i["amount"] * buyMultiplier
for i in whatItGives:
gamedefine.gamedefine.amounts[i["what"]] += i["amount"] * buyMultiplier
print(f"purchased {item}, now have {gamedefine.gamedefine.amounts[item]}, with buy multiplier {buyMultiplier}")

return


def getCurrentCost(item: str, _round: bool | None = False, eNotation: bool | None = True) -> dict:
"""
Returns the current cost of an item.
@Slot(str, result=None)
def purchase(self, item: str) -> None:
if self.canAfford(item):
self._purchase(item)

Args:
item (str): The item to get the cost of.
_round (bool | None): Whether to round the cost or not. Defaults to False.
@Slot(str, result=None)
def getCurrentCost(self, item: str, _round: bool | None = False, eNotation: bool | None = True) -> float:
"""
Returns the current cost of an item.
Returns:
dict: The current cost of the item.
"""
Args:
item (str): The item to get the cost of.
_round (bool | None): Whether to round the cost or not. Defaults to False.
buyMultiplier = gamedefine.gamedefine.mainTabBuyMultiple
Returns:
dict: The current cost of the item.
"""

equation = gamedefine.gamedefine.itemInternalDefine[item]["costEquation"]
if equation == "":
return {"failed": True}
buyMultiplier = gamedefine.game.mainTabBuyMultiple

whatItCosts = gamedefine.gamedefine.itemInternalDefine[item]["whatItCosts"][0]
equation = gamedefine.gamedefine.itemInternalDefine[item]["costEquation"]
if equation == "":
return {"failed": True}

currentCost = int(numberLogic.evaluateCostEquation(equation, 1))
whatItCosts = gamedefine.gamedefine.itemInternalDefine[item]["whatItCosts"][0]

if _round:
return {whatItCosts: round(currentCost) * buyMultiplier}
else:
return {whatItCosts: currentCost * buyMultiplier}
currentCost = int(numberLogic.evaluateCostEquation(equation, 1))

# if _round:
# return {whatItCosts: round(currentCost) * buyMultiplier}
# else:
# return {whatItCosts: currentCost * buyMultiplier}

def parseCost(name):
buyMultiplier = gamedefine.gamedefine.mainTabBuyMultiple

if not name == "quarks":
what = gamedefine.gamedefine.itemInternalDefine[name]["whatItCosts"]
get = gamedefine.gamedefine.itemInternalDefine[name]["whatItGives"]
string = ["Purchase "]
else:
return "Free"
exempt = ["hydrogen"]
for i in get:
string.append(str(i["amount"] * buyMultiplier) + " ")

if i["amount"] * buyMultiplier == 1 and not i["what"] in exempt:
string.append(i["what"][:-1])
else:
string.append(i["what"])

if get.index(i) < len(get) - 2:
string.append(", ")
elif get.index(i) == len(get) - 2:
string.append(" and ")
if _round:
return round(currentCost * buyMultiplier)
else:
string.append(" for ")
return currentCost * buyMultiplier

for i in what:
string.append(str(i["amount"] * buyMultiplier) + " ")
@Slot(str, result=str)
def parseCost(self, item: str) -> str:
buyMultiplier = gamedefine.game.mainTabBuyMultiple

if i["amount"] * buyMultiplier == 1 and not i["what"] in exempt:
string.append(i["what"][:-1])
if not gamedefine.items[item].defaultCost == -1:
cost = gamedefine.items[item].cost
gives = gamedefine.items[item].gives
string = ["Purchase "]
else:
string.append(i["what"])

if what.index(i) < len(what) - 2:
string.append(", ")
elif what.index(i) == len(what) - 2:
string.append(" and ")
else:
string.append(".")

return "".join(string)


def maxAll():
def maxAllPurchase(item):
whatItCosts = gamedefine.gamedefine.itemInternalDefine[item]["whatItCosts"]
whatItGives = gamedefine.gamedefine.itemInternalDefine[item]["whatItGives"]

maxAmountPossible = 1

for i in whatItCosts:
maxAmountPossible = gamedefine.gamedefine.amounts[i["what"]] // i["amount"]

for i in whatItCosts:
gamedefine.gamedefine.amounts[i["what"]] -= int(i["amount"] * maxAmountPossible)
for i in whatItGives:
gamedefine.gamedefine.amounts[i["what"]] += int(i["amount"] * maxAmountPossible)

print(f"max all purchased {maxAmountPossible} of {item}, now have {gamedefine.gamedefine.amounts[item]}")

return

affordList = []
for i in gamedefine.gamedefine.itemInternalDefine:
if not gamedefine.gamedefine.itemInternalDefine[i]["whatItCosts"][0]["what"] == "nothing":
if i in gamedefine.gamedefine.purchaseToCreate:
if canAfford(i, doBuyMultiply=False):
affordList.append(i)

for i in affordList:
maxAllPurchase(
i
) # this fixes the 'cascade effect' of the max all button by not actually purchasing it until the end
return "Free"
exempt = ["Hydrogen"]
for i in gives:
string.append(str(numberLogic.humanReadableNumber(i["amount"] * buyMultiplier)) + " ")

string.append(i["what"].getName_(i["amount"] * buyMultiplier).lower())
numberLogic.humanReadableNumber
if gives.index(i) < len(gives) - 2:
string.append(", ")
elif gives.index(i) == len(gives) - 2:
string.append(" and ")
else:
string.append(" for ")

for i in cost:
string.append(str(numberLogic.humanReadableNumber(i["amount"] * buyMultiplier)) + " ")

string.append(i["what"].getName_(i["amount"] * buyMultiplier).lower())

if cost.index(i) < len(cost) - 2:
string.append(", ")
elif cost.index(i) == len(cost) - 2:
string.append(" and ")
else:
string.append(".")

return "".join(string)

@Slot(result=None)
def maxAll(self):
def maxAllPurchase(item):
costs = gamedefine.items[item].cost
gives = gamedefine.items[item].gives

maxAmountPossible = 1

for i in costs:
maxAmountPossible = gamedefine.items[i].amount // i["amount"]

for i in costs:
gamedefine.items[i].amount -= int(i["amount"] * maxAmountPossible)
for i in gives:
gamedefine.items[i].amount += int(i["amount"] * maxAmountPossible)

print(f"max all purchased {maxAmountPossible} of {item}, now have {gamedefine.items[item].amount}")

return

affordList = []
for i in gamedefine.items:
if not gamedefine.items[i].cost[0]["what"] == "nothing":
if i in gamedefine.game.purchaseToCreate:
if self.canAfford(i, doBuyMultiply=False):
affordList.append(i)

for i in affordList:
maxAllPurchase(
i
) # this fixes the 'cascade effect' of the max all button by not actually purchasing it until the end

gamedefine.itemGameLogic = ItemGameLogic
Loading

0 comments on commit b635d51

Please sign in to comment.