(Lion): Make economy bonus logic explicit.
This commit is contained in:
@@ -1,12 +1,11 @@
|
|||||||
import pytz
|
import pytz
|
||||||
|
from functools import reduce
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from meta import client
|
from meta import client
|
||||||
from data import tables as tb
|
from data import tables as tb
|
||||||
from settings import UserSettings, GuildSettings
|
from settings import UserSettings, GuildSettings
|
||||||
|
|
||||||
# Give modules the ability to intercept addCoin() calls
|
|
||||||
_lion_add_coins_callbacks: list = []
|
|
||||||
|
|
||||||
class Lion:
|
class Lion:
|
||||||
"""
|
"""
|
||||||
@@ -22,6 +21,9 @@ class Lion:
|
|||||||
# Lion cache. Currently lions don't expire
|
# Lion cache. Currently lions don't expire
|
||||||
_lions = {} # (guildid, userid) -> Lion
|
_lions = {} # (guildid, userid) -> Lion
|
||||||
|
|
||||||
|
# Extra methods supplying an economy bonus
|
||||||
|
_economy_bonuses = []
|
||||||
|
|
||||||
def __init__(self, guildid, userid):
|
def __init__(self, guildid, userid):
|
||||||
self.guildid = guildid
|
self.guildid = guildid
|
||||||
self.userid = userid
|
self.userid = userid
|
||||||
@@ -122,6 +124,24 @@ class Lion:
|
|||||||
|
|
||||||
return int(coins)
|
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
|
@property
|
||||||
def session(self):
|
def session(self):
|
||||||
"""
|
"""
|
||||||
@@ -220,10 +240,7 @@ class Lion:
|
|||||||
"""
|
"""
|
||||||
Add coins to the user, optionally store the transaction in pending.
|
Add coins to the user, optionally store the transaction in pending.
|
||||||
"""
|
"""
|
||||||
for cb in _lion_add_coins_callbacks:
|
self._pending_coins += amount * (1 if ignorebonus else self.economy_bonus)
|
||||||
[self, amount, flush, ignorebonus] = cb(self, amount, flush, ignorebonus)
|
|
||||||
|
|
||||||
self._pending_coins += amount
|
|
||||||
self._pending[self.key] = self
|
self._pending[self.key] = self
|
||||||
if flush:
|
if flush:
|
||||||
self.flush()
|
self.flush()
|
||||||
@@ -256,12 +273,3 @@ class Lion:
|
|||||||
for lion in lions:
|
for lion in lions:
|
||||||
lion._pending_coins -= int(lion._pending_coins)
|
lion._pending_coins -= int(lion._pending_coins)
|
||||||
cls._pending.pop(lion.key, None)
|
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 LionContext import register_reply_callback, unregister_reply_callback
|
||||||
from bot.data.conditions import NOT
|
from bot.data.conditions import NOT
|
||||||
from meta.client import client
|
from meta.client import client
|
||||||
from core.lion import register_addcoins_callback, unregister_addcoins_callback
|
from core.lion import Lion
|
||||||
|
|
||||||
from .utils import *
|
from .utils import *
|
||||||
from .webhook import init_webhook
|
from .webhook import init_webhook
|
||||||
@@ -16,14 +16,14 @@ upvote_info = "You have a boost available {}, to support our project and earn **
|
|||||||
async def register_hook(client):
|
async def register_hook(client):
|
||||||
init_webhook()
|
init_webhook()
|
||||||
register_reply_callback(reply)
|
register_reply_callback(reply)
|
||||||
register_addcoins_callback(cb_addCoins)
|
Lion.register_economy_bonus(economy_bonus)
|
||||||
|
|
||||||
client.log("Registered LionContext reply util hook.", context="Topgg" )
|
client.log("Registered LionContext reply util hook.", context="Topgg" )
|
||||||
|
|
||||||
@module.unload_task
|
@module.unload_task
|
||||||
async def unregister_hook(client):
|
async def unregister_hook(client):
|
||||||
unregister_reply_callback(reply)
|
unregister_reply_callback(reply)
|
||||||
unregister_addcoins_callback(cb_addCoins)
|
Lion.unregister_economy_bonus(economy_bonus)
|
||||||
|
|
||||||
client.log("Unregistered LionContext reply util hook.", context="Topgg" )
|
client.log("Unregistered LionContext reply util hook.", context="Topgg" )
|
||||||
|
|
||||||
@@ -57,12 +57,5 @@ def reply(util_func, *args, **kwargs):
|
|||||||
return [args, kwargs]
|
return [args, kwargs]
|
||||||
|
|
||||||
|
|
||||||
def cb_addCoins(self, amount, flush, ignorebonus):
|
def economy_bonus(lion):
|
||||||
|
return 1.25 if get_last_voted_timestamp(lion.userid) else 1
|
||||||
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]
|
|
||||||
|
|||||||
Reference in New Issue
Block a user