refactor Points class + tests
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-12-11 16:19:05 +01:00
parent c4ba63b83b
commit 25e38b6b5f
2 changed files with 193 additions and 42 deletions
+56 -41
View File
@@ -18,6 +18,9 @@ class Points(commands.Cog):
self.BANK_ID = 42
self.init()
self.pointsCount = {}
self.reloadPointsCount()
def __check(self, ctx):
return True
@@ -31,6 +34,8 @@ class Points(commands.Cog):
De eerste 10 punten zijn on the house. (Als er genoeg in de bank zit.)
"""
self.reloadPointsCount()
if points == 0:
yield from ctx.channel.send("0 punten? Gul hoor.")
return
@@ -58,21 +63,26 @@ class Points(commands.Cog):
return
# Execute the transaction:
# First, try to give a away (at most 10) bankpoints
areBeingGivenFromBank = min(points, self.getUsersRemainingBankPointsToGiveAwayToday(ctx.author.id))
areBeingGivenFromBank = min(areBeingGivenFromBank, self.getBankBalance())
if areBeingGivenFromBank > 0:
self.transferPoints(self.BANK_ID, userObj.id, areBeingGivenFromBank)
self.increaseUsersBankPointGivenAwayToday(ctx.author.id, areBeingGivenFromBank)
remainingPointsToGive = points - areBeingGivenFromBank
else:
remainingPointsToGive = points
# The remaining points come out of your own pocket
if remainingPointsToGive > 0:
self.transferPoints(ctx.author.id, userObj.id, remainingPointsToGive)
self.execute_geefpunten(ctx.author.id, userObj.id, points)
self.savePointsCount()
message = self.getMessageForAwardingPoints(ctx.author.name, userObj.name, points)
yield from ctx.channel.send(message)
def execute_geefpunten(self, giverId, recieverId, points):
# First, try to give away (at most 10) bankpoints
areBeingGivenFromBank = min(points, self.getUsersRemainingBankPointsToGiveAwayToday(giverId))
areBeingGivenFromBank = min(areBeingGivenFromBank, self.getBankBalance())
if areBeingGivenFromBank > 0:
self.transferPoints(self.BANK_ID, recieverId, areBeingGivenFromBank)
self.increaseUsersBankPointGivenAwayToday(giverId, areBeingGivenFromBank)
remainingPointsToGive = points - areBeingGivenFromBank
else:
remainingPointsToGive = points
# The remaining points come out of your own pocket
if remainingPointsToGive > 0:
self.transferPoints(giverId, recieverId, remainingPointsToGive)
@commands.command(pass_context=True, hidden=False)
@@ -85,6 +95,8 @@ class Points(commands.Cog):
Let op: je moet iemand taggen om punten weg te nemen.
"""
self.reloadPointsCount()
if points < 1:
yield from ctx.channel.send("Volgens mij snap je het niet.")
return
@@ -110,11 +122,16 @@ class Points(commands.Cog):
return
# Execute the transaction:
self.increaseUsersPointTakenOnDate(ctx.author.id, points)
self.transferPoints(userObj.id, self.BANK_ID, points)
self.execute_neempunten(ctx.author.id, userObj.id, points);
self.savePointsCount()
message = self.getMessageForTakingPoints(userObj.name, ctx.author.name, points)
yield from ctx.channel.send(message)
def execute_neempunten(self, takerId, victimId, points):
self.increaseUsersPointTakenOnDate(takerId, points)
self.transferPoints(victimId, self.BANK_ID, points)
@commands.command(pass_context=True, hidden=False)
@@ -123,14 +140,14 @@ class Points(commands.Cog):
"""Hoeveel punten heeft iedereen?
"""
self.reloadPointsCount()
message = "**De puntentelling:** \n```"
pointsCount = np.load('pointscount.npy', allow_pickle=True).item()
pointsCountSorted = sorted(pointsCount.items(), key=lambda i: i[1]['points'], reverse=True)
pointsCountSorted = sorted(self.pointsCount.items(), key=lambda i: i[1]['points'], reverse=True)
for user in pointsCountSorted:
if user[0] != self.BANK_ID:
message += str(user[1]["points"]).rjust(4) + " " + user[1]["name"] + "\n"
message += str(pointsCount[self.BANK_ID]["points"]).rjust(4) + " " + pointsCount[self.BANK_ID]["name"] + "```"
#message += "```"
message += str(self.pointsCount[self.BANK_ID]["points"]).rjust(4) + " " + self.pointsCount[self.BANK_ID]["name"] + "```"
yield from ctx.channel.send(message)
@@ -146,40 +163,42 @@ class Points(commands.Cog):
except:
print("No permission to send DM")
# Get the points count object from the file
def reloadPointsCount(self):
self.pointsCount = np.load('pointscount.npy', allow_pickle=True).item()
# Save the points count to the file
def savePointsCount(self):
np.save("pointscount.npy", self.pointsCount)
# Transfer points form one user to another
def transferPoints(self, fromUserId, toUserId, points):
pointsCount = np.load('pointscount.npy', allow_pickle=True).item()
pointsCount[fromUserId]["points"] -= points
pointsCount[toUserId]["points"] += points
np.save("pointscount.npy", pointsCount)
self.pointsCount[fromUserId]["points"] -= points
self.pointsCount[toUserId]["points"] += points
# Do we know this user?
def isUserOnList(self, user):
pointsCount = np.load('pointscount.npy', allow_pickle=True).item()
return user.id in pointsCount.keys()
return user.id in self.pointsCount.keys()
# Get a user's point balance
def getUserBalance(self, user):
pointsCount = np.load('pointscount.npy', allow_pickle=True).item()
return pointsCount[user.id]["points"]
return self.pointsCount[user.id]["points"]
# Get the balance of the bank
def getBankBalance(self):
pointsCount = np.load('pointscount.npy', allow_pickle=True).item()
return pointsCount[self.BANK_ID]["points"]
return self.pointsCount[self.BANK_ID]["points"]
# To keep track if a user used some points today, we look at when the last time was he/she used them
# If this was in the past: reset
# Also, return the updated object. Why not.
def refreshDateVarsAndReturnUserVars(self, userId):
pointsCount = np.load('pointscount.npy', allow_pickle=True).item()
currentDateStr = datetime.now().strftime("%Y%m%d")
if pointsCount[userId]["theDate"] != currentDateStr:
pointsCount[userId]["theDate"] = currentDateStr
pointsCount[userId]["pointTakenOnDate"] = 0
pointsCount[userId]["bankPointsGivenOnDate"] = 0
np.save("pointscount.npy", pointsCount)
return pointsCount[userId]
if self.pointsCount[userId]["theDate"] != currentDateStr:
self.pointsCount[userId]["theDate"] = currentDateStr
self.pointsCount[userId]["pointTakenOnDate"] = 0
self.pointsCount[userId]["bankPointsGivenOnDate"] = 0
return self.pointsCount[userId]
# How many points can this user still take away today?
def getUsersRemainingPointsToTakeToday(self, userId):
@@ -194,16 +213,12 @@ class Points(commands.Cog):
# Increase the number of points this user has taken away today.
# Warning: assumes you executed refreshDateVarsAndReturnUserVars()
def increaseUsersPointTakenOnDate(self, userId, points):
pointsCount = np.load('pointscount.npy', allow_pickle=True).item()
pointsCount[userId]["pointTakenOnDate"] += points
np.save("pointscount.npy", pointsCount)
self.pointsCount[userId]["pointTakenOnDate"] += points
# Increase the number of bank points this user has given today
# Warning: assumes you executed refreshDateVarsAndReturnUserVars()
def increaseUsersBankPointGivenAwayToday(self, userId, points):
pointsCount = np.load('pointscount.npy', allow_pickle=True).item()
pointsCount[userId]["bankPointsGivenOnDate"] += points
np.save("pointscount.npy", pointsCount)
self.pointsCount[userId]["bankPointsGivenOnDate"] += points
# Generate a semi-random text to tell somebody they got some shiny new points