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:
|
class Cooldown_Action:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.name:str = ""
|
self.tag:str = ""
|
||||||
self.time = datetime.datetime.now()
|
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:
|
class Cooldowns_Module:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.coolDownName:str = ""
|
self.cooldownList:list = []
|
||||||
self.coolDownActionLimit:int = 0
|
|
||||||
self.coolDownDuration:int = 0 #Seconds
|
|
||||||
|
|
||||||
self.actionList:list = []
|
|
||||||
|
|
||||||
|
|
||||||
def setupCooldown(self, name, limit, duration):
|
def setupCooldown(self, name, limit, duration):
|
||||||
self.coolDownName = name
|
newCD:Cooldown = Cooldown()
|
||||||
self.coolDownActionLimit = limit
|
newCD.setupCooldown(name, limit, duration)
|
||||||
self.coolDownDuration = 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()
|
timenow = datetime.datetime.now()
|
||||||
|
|
||||||
if len(self.actionList) >= self.coolDownActionLimit:
|
if len(selectedCooldown.actionList) >= selectedCooldown.cooldownActionLimit:
|
||||||
actionCount = len(self.actionList)
|
actionCount = len(selectedCooldown.actionList)
|
||||||
|
|
||||||
maxTmpIndex = actionCount - self.coolDownActionLimit
|
maxTmpIndex = actionCount - selectedCooldown.cooldownActionLimit
|
||||||
maxRecentAction:Cooldown_Action = self.actionList[maxTmpIndex]
|
maxRecentAction:Cooldown_Action = selectedCooldown.actionList[maxTmpIndex]
|
||||||
|
|
||||||
timeDiff = timenow - maxRecentAction.time
|
timeDiff = timenow - maxRecentAction.time
|
||||||
maxTimeAllowed = timedelta(seconds = self.coolDownDuration)
|
maxTimeAllowed = timedelta(seconds = selectedCooldown.cooldownDuration)
|
||||||
|
|
||||||
if timeDiff < maxTimeAllowed:
|
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 = Cooldown_Action()
|
||||||
newAction.name = name
|
newAction.tag = tag
|
||||||
self.actionList.append(newAction)
|
targetCD = self.getCooldown(name)
|
||||||
|
if targetCD != None:
|
||||||
|
targetCD.actionList.append(newAction)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
testCD = Cooldowns_Module()
|
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: ")
|
print("CD Test 1: ")
|
||||||
for x in range(20):
|
for x in range(20):
|
||||||
testCD.actionTrigger()
|
testCD.actionTrigger(cdName)
|
||||||
sleep(0)
|
sleep(0)
|
||||||
print(testCD.isCooldownActive())
|
print(testCD.isCooldownActive("test"))
|
||||||
print("//Test Done//")
|
print("//Test Done//")
|
||||||
sleep(2)
|
sleep(2)
|
||||||
|
|
||||||
print("CD Test 2: ")
|
print("CD Test 2: ")
|
||||||
for x in range(10):
|
for x in range(10):
|
||||||
testCD.actionTrigger()
|
testCD.actionTrigger(cdName)
|
||||||
sleep(0)
|
sleep(0)
|
||||||
print(testCD.isCooldownActive())
|
print(testCD.isCooldownActive(cdName))
|
||||||
print("//Test Done//")
|
print("//Test Done//")
|
||||||
sleep(2)
|
sleep(2)
|
||||||
|
|
||||||
print("CD Test 3: ")
|
print("CD Test 3: ")
|
||||||
for x in range(20):
|
for x in range(20):
|
||||||
testCD.actionTrigger()
|
testCD.actionTrigger(cdName)
|
||||||
sleep(0.05)
|
sleep(0.05)
|
||||||
print(testCD.isCooldownActive())
|
print(testCD.isCooldownActive(cdName))
|
||||||
print("//Test Done//")
|
print("//Test Done//")
|
||||||
sleep(2)
|
sleep(2)
|
||||||
|
|
||||||
print("CD Test 4: ")
|
print("CD Test 4: ")
|
||||||
for x in range(20):
|
for x in range(20):
|
||||||
testCD.actionTrigger()
|
testCD.actionTrigger(cdName)
|
||||||
sleep(0.6)
|
sleep(0.6)
|
||||||
print(testCD.isCooldownActive())
|
print(testCD.isCooldownActive(cdName))
|
||||||
print("//Test Done//")
|
print("//Test Done//")
|
||||||
@ -60,9 +60,9 @@ class Twitch_Module():
|
|||||||
self.chat.irc.socket.close()
|
self.chat.irc.socket.close()
|
||||||
|
|
||||||
def send_message(self, message):
|
def send_message(self, message):
|
||||||
if self.twitchChat_cooldown.isCooldownActive() == False:
|
if self.twitchChat_cooldown.isCooldownActive("twitchChat") == False:
|
||||||
self.chat.send(message)
|
self.chat.send(message)
|
||||||
self.twitchChat_cooldown.actionTrigger()
|
self.twitchChat_cooldown.actionTrigger("twitchChat")
|
||||||
|
|
||||||
def send_whisper(self, user, message):
|
def send_whisper(self, user, message):
|
||||||
pass
|
pass
|
||||||
@ -73,7 +73,7 @@ class Twitch_Module():
|
|||||||
if message.channel == "thecuriousnerd":
|
if message.channel == "thecuriousnerd":
|
||||||
|
|
||||||
if not self.isSenderBot(message):
|
if not self.isSenderBot(message):
|
||||||
if self.twitchChat_cooldown.isCooldownActive() == False:
|
if self.twitchChat_cooldown.isCooldownActive("twitchChat") == False:
|
||||||
self.eval_commands(message)
|
self.eval_commands(message)
|
||||||
#elif message.channel == message.sender:
|
#elif message.channel == message.sender:
|
||||||
#self.eval_commands(message)
|
#self.eval_commands(message)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user