(Lion): Make economy bonus logic explicit.
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import pytz
|
||||
from functools import reduce
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from meta import client
|
||||
from data import tables as tb
|
||||
from settings import UserSettings, GuildSettings
|
||||
|
||||
# Give modules the ability to intercept addCoin() calls
|
||||
_lion_add_coins_callbacks: list = []
|
||||
|
||||
class Lion:
|
||||
"""
|
||||
@@ -22,6 +21,9 @@ class Lion:
|
||||
# Lion cache. Currently lions don't expire
|
||||
_lions = {} # (guildid, userid) -> Lion
|
||||
|
||||
# Extra methods supplying an economy bonus
|
||||
_economy_bonuses = []
|
||||
|
||||
def __init__(self, guildid, userid):
|
||||
self.guildid = guildid
|
||||
self.userid = userid
|
||||
@@ -122,6 +124,24 @@ class Lion:
|
||||
|
||||
return int(coins)
|
||||
|
||||
@property
|
||||
def economy_bonus(self):
|
||||
"""
|
||||
Economy multiplier
|
||||
"""
|
||||
return reduce(
|
||||
lambda x, y: x * y,
|
||||
[func(self) for func in self._economy_bonuses]
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def register_economy_bonus(cls, func):
|
||||
cls._economy_bonuses.append(func)
|
||||
|
||||
@classmethod
|
||||
def unregister_economy_bonus(cls, func):
|
||||
cls._economy_bonuses.remove(func)
|
||||
|
||||
@property
|
||||
def session(self):
|
||||
"""
|
||||
@@ -220,10 +240,7 @@ class Lion:
|
||||
"""
|
||||
Add coins to the user, optionally store the transaction in pending.
|
||||
"""
|
||||
for cb in _lion_add_coins_callbacks:
|
||||
[self, amount, flush, ignorebonus] = cb(self, amount, flush, ignorebonus)
|
||||
|
||||
self._pending_coins += amount
|
||||
self._pending_coins += amount * (1 if ignorebonus else self.economy_bonus)
|
||||
self._pending[self.key] = self
|
||||
if flush:
|
||||
self.flush()
|
||||
@@ -256,12 +273,3 @@ class Lion:
|
||||
for lion in lions:
|
||||
lion._pending_coins -= int(lion._pending_coins)
|
||||
cls._pending.pop(lion.key, None)
|
||||
|
||||
|
||||
# TODO Expand this callback system to other functions
|
||||
# Note: callbacks MUST return [self, amount, flush, ignorebonus] modified/unmodified
|
||||
def register_addcoins_callback(func):
|
||||
_lion_add_coins_callbacks.append(func)
|
||||
|
||||
def unregister_addcoins_callback(func):
|
||||
_lion_add_coins_callbacks.remove(func)
|
||||
|
||||
@@ -3,7 +3,7 @@ from LionModule import LionModule
|
||||
from LionContext import register_reply_callback, unregister_reply_callback
|
||||
from bot.data.conditions import NOT
|
||||
from meta.client import client
|
||||
from core.lion import register_addcoins_callback, unregister_addcoins_callback
|
||||
from core.lion import Lion
|
||||
|
||||
from .utils import *
|
||||
from .webhook import init_webhook
|
||||
@@ -16,15 +16,15 @@ upvote_info = "You have a boost available {}, to support our project and earn **
|
||||
async def register_hook(client):
|
||||
init_webhook()
|
||||
register_reply_callback(reply)
|
||||
register_addcoins_callback(cb_addCoins)
|
||||
Lion.register_economy_bonus(economy_bonus)
|
||||
|
||||
client.log("Registered LionContext reply util hook.", context="Topgg" )
|
||||
|
||||
@module.unload_task
|
||||
async def unregister_hook(client):
|
||||
unregister_reply_callback(reply)
|
||||
unregister_addcoins_callback(cb_addCoins)
|
||||
|
||||
Lion.unregister_economy_bonus(economy_bonus)
|
||||
|
||||
client.log("Unregistered LionContext reply util hook.", context="Topgg" )
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ def reply(util_func, *args, **kwargs):
|
||||
value=(
|
||||
upvote_info_formatted
|
||||
),
|
||||
inline=False
|
||||
inline=False
|
||||
)
|
||||
elif 'content' in args and args['content']:
|
||||
args['content'] += '\n\n' + upvote_info_formatted
|
||||
@@ -57,12 +57,5 @@ def reply(util_func, *args, **kwargs):
|
||||
return [args, kwargs]
|
||||
|
||||
|
||||
def cb_addCoins(self, amount, flush, ignorebonus):
|
||||
|
||||
client.log('cb_addCoins hook with amount={} ignorebonux={}'.format(amount, ignorebonus), context='Topgg')
|
||||
|
||||
if not ignorebonus and amount > 0 and get_last_voted_timestamp(self.userid):
|
||||
amount *= 1.25
|
||||
client.log('cb_addCoins with bonus={}'.format(amount), context='Topgg')
|
||||
|
||||
return [self, amount, flush, ignorebonus]
|
||||
def economy_bonus(lion):
|
||||
return 1.25 if get_last_voted_timestamp(lion.userid) else 1
|
||||
|
||||
Reference in New Issue
Block a user