From 84f550a355a3f21e96051067a200129ea0694d3b Mon Sep 17 00:00:00 2001 From: moneromooo Date: Wed, 21 Jan 2015 18:15:49 +0000 Subject: [PATCH] Add ban/unban commands --- tipbot/betutils.py | 72 ++++++++++++++++++++++++++++++++++++- tipbot/modules/blackjack.py | 2 +- tipbot/modules/dice.py | 2 +- 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/tipbot/betutils.py b/tipbot/betutils.py index a51ad9a..514ee03 100644 --- a/tipbot/betutils.py +++ b/tipbot/betutils.py @@ -17,7 +17,25 @@ import tipbot.coinspecs as coinspecs from tipbot.command_manager import * from utils import * -def IsBetAmountValid(amount,minbet,maxbet,potential_loss,max_loss,max_loss_ratio): +def IsBanned(link): + try: + banned = redis_hget('banned',link.identity()) + if not banned: + return False, None + banned = float(banned) + now = time.time() + if banned < now: + redis_hdel('banned',link.identity()) + return False, None + return True, 'You are banned for %s' % TimeToString(banned-now) + except Exception,e: + log_error('Failed to check bannishment for %s: %s' % (link.identity(),str(e))) + return False, None + +def IsBetValid(link,amount,minbet,maxbet,potential_loss,max_loss,max_loss_ratio): + banned,reason = IsBanned(link) + if banned: + return False, reason try: amount = float(amount) except Exception,e: @@ -302,6 +320,44 @@ def ReserveBalance(link,cmd): return link.send("Reserve balance set") +def Ban(link,cmd): + t = 3600 + try: + sidentity = GetParam(cmd,1) + if sidentity: + sidentity=IdentityFromString(link,sidentity) + if sidentity!=link.identity() and not IsAdmin(link): + log_error('%s is not admin, cannot ban %s' % (link.identity(),sidentity)) + link.send('Access denied') + return + else: + sidentity=link.identity() + + banned = redis_hget('banned',sidentity) + now=time.time() + if banned and float(banned) > now+t: + link.send('%s is already banned for %s' % (NickFromIdentity(sidentity), TimeToString(banned-now))) + else: + redis_hset('banned',sidentity,now+t) + link.send('%s is banned for %s' % (NickFromIdentity(sidentity), TimeToString(t))) + except Exception,e: + log_error('Failed to ban %s: %s' % (sidentity,str(e))) + link.send('An error occured') + return + +def Unban(link,cmd): + try: + sidentity=GetParam(cmd,1) + if not sidentity: + sidentity=link.identity() + sidentity=IdentityFromString(link,sidentity) + redis_hdel('banned',sidentity) + link.send('%s was unbanned' % (sidentity)) + except Exception,e: + log_error('Failed to unban %s: %s' % (sidentity,str(e))) + link.send('An error occured') + return + RegisterCommand({ 'module': 'betting', 'name': 'reserve_balance', @@ -318,3 +374,17 @@ RegisterCommand({ 'registered': True, 'help': "get the house balance" }) +RegisterCommand({ + 'module': 'betting', + 'name': 'ban', + 'function': Ban, + 'registered': True, + 'help': "ban yourself from playing for an hour" +}) +RegisterCommand({ + 'module': 'betting', + 'name': 'unban', + 'function': Unban, + 'admin': True, + 'help': "unban someone from playing" +}) diff --git a/tipbot/modules/blackjack.py b/tipbot/modules/blackjack.py index 3e52a9c..0de719e 100644 --- a/tipbot/modules/blackjack.py +++ b/tipbot/modules/blackjack.py @@ -567,7 +567,7 @@ def Blackjack(link,cmd): potential_loss = amount * 1.5 + sidebets["potential_loss"] / coinspecs.atomic_units potential_units_loss = long (potential_loss * coinspecs.atomic_units) log_info('%s bets a total of %s, potential loss %s, side bets %s' % (identity, AmountToString(total_units_wagered), AmountToString(potential_units_loss), str(sidebets))) - valid,reason = IsBetAmountValid(total_amount_wagered,config.blackjack_min_bet,config.blackjack_max_bet,potential_loss,config.blackjack_max_loss,config.blackjack_max_loss_ratio) + valid,reason = IsBetValid(link,total_amount_wagered,config.blackjack_min_bet,config.blackjack_max_bet,potential_loss,config.blackjack_max_loss,config.blackjack_max_loss_ratio) if not valid: log_info("Dice: %s's bet refused: %s" % (identity, reason)) link.send("%s: %s" % (link.user.nick, reason)) diff --git a/tipbot/modules/dice.py b/tipbot/modules/dice.py index adc8ad2..ce34788 100644 --- a/tipbot/modules/dice.py +++ b/tipbot/modules/dice.py @@ -68,7 +68,7 @@ def Dice(link,cmd): log_info("Dice: %s wants to bet %s at x%f, %s target" % (identity, AmountToString(units), multiplier, "under" if under else "over")) potential_loss = amount * multiplier - valid,reason = IsBetAmountValid(amount,config.dice_min_bet,config.dice_max_bet,potential_loss,config.dice_max_loss,config.dice_max_loss_ratio) + valid,reason = IsBetValid(link,amount,config.dice_min_bet,config.dice_max_bet,potential_loss,config.dice_max_loss,config.dice_max_loss_ratio) if not valid: log_info("Dice: %s's bet refused: %s" % (identity, reason)) link.send("%s: %s" % (link.user.nick, reason))