Skip to content

Commit

Permalink
feat: added new function to ctstime to simplify checking if something…
Browse files Browse the repository at this point in the history
… can run yet (checkIntervalElapsed)

fix: slowed down how often affordability checks are run
fix: remove some logging from main.qml
  • Loading branch information
kaneryu committed Nov 14, 2024
1 parent 73a78b5 commit c54251d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
16 changes: 16 additions & 0 deletions src/createthesun/ctstime.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,19 @@ def getTimeSinceTimestampMs(timestamp: float) -> float:

def getTimeSinceTimestampSec(timestamp: float) -> float:
return getTimeSec() - timestamp

runEveryMsCheck_ = {}

def checkIntervalElapsed(timeToWait: int, id: str, debug: bool = False) -> bool:
if id not in runEveryMsCheck_:
runEveryMsCheck_[id] = 0

if debug: print("runEveryMsCheck_:", runEveryMsCheck_)
if debug: print("id:", id)
if getTimeSinceTimestampMs(runEveryMsCheck_[id]) >= timeToWait:
runEveryMsCheck_[id] = getTimeMs()
if debug: print("success, id:", id)
return True

if debug: print("not yet, id:", id)
return False
23 changes: 10 additions & 13 deletions src/createthesun/gamedefine.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,28 @@ def maxAllPurchase(item): ...
def setBuyMultiplier(self, mult): ...

ItemGameLogic = IGLSkeleton
class items_:
class caseInsensitiveDict(dict):
"""Case Insensitive dictionary (for the keys)
You can use this grab the items, without worrying about what case you're using.
Access the dict directy by items.data
"""
def __init__(self):
self.data = {}

super().__init__()

def __getitem__(self, key: str) -> _Item:
if type(key) == str:
return self.data[key.lower()]
return super().__getitem__(key.lower())
else:
return self.data[key]
return super().__getitem__(key)

def __setitem__(self, key: str, value: _Item) -> None:
if type(key) == str:
self.data[key.lower()] = value
return super().__setitem__(key.lower(), value)
else:
print("Key supplied to items_.__setitem__, but type was ", type(key))
self.data[key] = value

def __repr__(self) -> str:
return str(self.data)
items = items_()
super().__setitem__(key, value)


items = caseInsensitiveDict()



Expand Down
2 changes: 0 additions & 2 deletions src/createthesun/main~2x3x.qml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ ApplicationWindow {

function onLoadComplete() {
console.log("Loaded")
console.log(ItemsModel)
console.log(Backend.activeTab)
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/createthesun/qmlver.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,19 @@ def __init__(self):
self.running = True

def run(self):
self.lastElectronIncreaseTime = 0
while self.running:

time.sleep(1/60) # 60hz

if not gamedefine.items["Electrons"].amount >= gamedefine.items["Electrons"].maxAmount and ctstime.getTimeSinceTimestampMs(self.lastElectronIncreaseTime) >= gamedefine.items["Electrons"].waitTime:
if not gamedefine.items["Electrons"].amount >= gamedefine.items["Electrons"].maxAmount and ctstime.checkIntervalElapsed(gamedefine.items["Electrons"].waitTime, "Electrons-bgworker"):
gamedefine.items["Electrons"].amount = gamedefine.items["Electrons"].increaseAmount + gamedefine.items["Electrons"].amount
self.lastElectronIncreaseTime = ctstime.getTimeMs()

if gamedefine.items["Electrons"].amount <= gamedefine.items["Electrons"].minAmount:
gamedefine.items["Electrons"].amount = gamedefine.items["Electrons"].minAmount

for i in list(gamedefine.items.values()):
print(i, i.name)
i.periodicalChecks()
if ctstime.checkIntervalElapsed(100, "periodicalChecks"):
for i in list(gamedefine.items.values()):
i.periodicalChecks()


def stop(self):
Expand All @@ -76,7 +74,7 @@ class Tab:
class Items(QObject):
def __init__(self):
super().__init__()
for i in gamedefine.items.data:
for i in gamedefine.items:
setattr(self, i.lower(), gamedefine.items[i])

@Slot(str, result=QObject)
Expand Down

0 comments on commit c54251d

Please sign in to comment.