Compare commits
4 Commits
7aa6c55863
...
088bba8309
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
088bba8309 | ||
|
|
6d8b8b2928 | ||
|
|
7b5b648e97 | ||
|
|
5d88861b8c |
97
cooldowns.py
97
cooldowns.py
@ -9,80 +9,113 @@ from time import sleep
|
||||
|
||||
class Cooldown_Action:
|
||||
def __init__(self):
|
||||
self.name:str = ""
|
||||
self.tag:str = ""
|
||||
self.time = datetime.datetime.now()
|
||||
|
||||
class Cooldown:
|
||||
def __init__(self):
|
||||
self.cooldownName:str = ""
|
||||
self.cooldownActionLimit:int = 0
|
||||
self.cooldownDuration:int = 0 #Seconds
|
||||
|
||||
self.actionList:list = []
|
||||
|
||||
def setupCooldown(self, name, limit, duration):
|
||||
self.cooldownName = name
|
||||
self.cooldownActionLimit = limit
|
||||
self.cooldownDuration = duration
|
||||
|
||||
class Cooldowns_Module:
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.coolDownName:str = ""
|
||||
self.coolDownActionLimit:int = 0
|
||||
self.coolDownDuration:int = 0 #Seconds
|
||||
|
||||
self.actionList:list = []
|
||||
|
||||
self.cooldownList:list = []
|
||||
|
||||
def setupCooldown(self, name, limit, duration):
|
||||
self.coolDownName = name
|
||||
self.coolDownActionLimit = limit
|
||||
self.coolDownDuration = duration
|
||||
|
||||
newCD:Cooldown = Cooldown()
|
||||
newCD.setupCooldown(name, limit, duration)
|
||||
self.cooldownList.append(newCD)
|
||||
|
||||
def isCooldownActive(self):
|
||||
isCoolDownActivated:bool = False
|
||||
|
||||
def getCooldown(self, name:str):
|
||||
returnCD:bool = False
|
||||
for cd in self.cooldownList:
|
||||
if cd.cooldownName == name:
|
||||
returnCD = True
|
||||
return cd
|
||||
if returnCD == False:
|
||||
return None
|
||||
|
||||
def isCooldownActive(self, name:str):
|
||||
isCooldownActivated:bool = False
|
||||
|
||||
selectedCooldown:Cooldown = self.getCooldown(name)
|
||||
|
||||
if selectedCooldown == None:
|
||||
return None
|
||||
|
||||
timenow = datetime.datetime.now()
|
||||
|
||||
if len(self.actionList) >= self.coolDownActionLimit:
|
||||
actionCount = len(self.actionList)
|
||||
if len(selectedCooldown.actionList) >= selectedCooldown.cooldownActionLimit:
|
||||
actionCount = len(selectedCooldown.actionList)
|
||||
|
||||
maxTmpIndex = actionCount - self.coolDownActionLimit
|
||||
maxRecentAction:Cooldown_Action = self.actionList[maxTmpIndex]
|
||||
maxTmpIndex = actionCount - selectedCooldown.cooldownActionLimit
|
||||
maxRecentAction:Cooldown_Action = selectedCooldown.actionList[maxTmpIndex]
|
||||
|
||||
timeDiff = timenow - maxRecentAction.time
|
||||
maxTimeAllowed = timedelta(seconds = self.coolDownDuration)
|
||||
maxTimeAllowed = timedelta(seconds = selectedCooldown.cooldownDuration)
|
||||
|
||||
if timeDiff < maxTimeAllowed:
|
||||
isCoolDownActivated = True
|
||||
isCooldownActivated = True
|
||||
|
||||
return isCoolDownActivated
|
||||
return isCooldownActivated
|
||||
|
||||
def actionTrigger(self, name:str = ""):
|
||||
def actionTrigger(self, name:str = "", tag:str = ""):
|
||||
newAction = Cooldown_Action()
|
||||
newAction.name = name
|
||||
self.actionList.append(newAction)
|
||||
newAction.tag = tag
|
||||
targetCD = self.getCooldown(name)
|
||||
if targetCD != None:
|
||||
targetCD.actionList.append(newAction)
|
||||
|
||||
if __name__ == "__main__":
|
||||
testCD = Cooldowns_Module()
|
||||
testCD.setupCooldown("test", 20, 2)
|
||||
cdName = "test"
|
||||
testCD.setupCooldown(cdName, 20, 2)
|
||||
|
||||
print("CD Test 0: ")
|
||||
for x in range(20):
|
||||
testCD.actionTrigger("cdName")
|
||||
sleep(0)
|
||||
print(testCD.isCooldownActive("cdName"))
|
||||
print("//Test Done//")
|
||||
sleep(2)
|
||||
|
||||
print("CD Test 1: ")
|
||||
for x in range(20):
|
||||
testCD.actionTrigger()
|
||||
testCD.actionTrigger(cdName)
|
||||
sleep(0)
|
||||
print(testCD.isCooldownActive())
|
||||
print(testCD.isCooldownActive("test"))
|
||||
print("//Test Done//")
|
||||
sleep(2)
|
||||
|
||||
print("CD Test 2: ")
|
||||
for x in range(10):
|
||||
testCD.actionTrigger()
|
||||
testCD.actionTrigger(cdName)
|
||||
sleep(0)
|
||||
print(testCD.isCooldownActive())
|
||||
print(testCD.isCooldownActive(cdName))
|
||||
print("//Test Done//")
|
||||
sleep(2)
|
||||
|
||||
print("CD Test 3: ")
|
||||
for x in range(20):
|
||||
testCD.actionTrigger()
|
||||
testCD.actionTrigger(cdName)
|
||||
sleep(0.05)
|
||||
print(testCD.isCooldownActive())
|
||||
print(testCD.isCooldownActive(cdName))
|
||||
print("//Test Done//")
|
||||
sleep(2)
|
||||
|
||||
print("CD Test 4: ")
|
||||
for x in range(20):
|
||||
testCD.actionTrigger()
|
||||
testCD.actionTrigger(cdName)
|
||||
sleep(0.6)
|
||||
print(testCD.isCooldownActive())
|
||||
print(testCD.isCooldownActive(cdName))
|
||||
print("//Test Done//")
|
||||
@ -60,9 +60,9 @@ class Twitch_Module():
|
||||
self.chat.irc.socket.close()
|
||||
|
||||
def send_message(self, message):
|
||||
if self.twitchChat_cooldown.isCooldownActive() == False:
|
||||
if self.twitchChat_cooldown.isCooldownActive("twitchChat") == False:
|
||||
self.chat.send(message)
|
||||
self.twitchChat_cooldown.actionTrigger()
|
||||
self.twitchChat_cooldown.actionTrigger("twitchChat")
|
||||
|
||||
def send_whisper(self, user, message):
|
||||
pass
|
||||
@ -73,7 +73,7 @@ class Twitch_Module():
|
||||
if message.channel == "thecuriousnerd":
|
||||
|
||||
if not self.isSenderBot(message):
|
||||
if self.twitchChat_cooldown.isCooldownActive() == False:
|
||||
if self.twitchChat_cooldown.isCooldownActive("twitchChat") == False:
|
||||
self.eval_commands(message)
|
||||
#elif message.channel == message.sender:
|
||||
#self.eval_commands(message)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user