def cron(channel): import globals try: channel = channel.lstrip("#") from src.lib.twitch import get_stream_status, get_hosts from src.lib.channel_data import ChannelData from src.lib.queries import Database cd = ChannelData(channel) if get_stream_status(): channel_id = cd.get_channel_id_from_db()[0] hosts = get_hosts(channel_id) unthanked_users = [] for host in hosts: host_data = cd.get_channel_data_by_user(host["host_login"], "host") if not host_data: cd.insert_channel_data(host["host_login"], "host") db = Database() db.add_user([host["host_login"]], channel) db.modify_points(host["host_login"], channel, 100) unthanked_users.append(host["host_login"]) if len(unthanked_users) == 1: user = unthanked_users[0] resp = "Thank you {0} for the host! Here's 100 cash!".format(user) globals.irc.send_message("#" + channel, resp) elif len(unthanked_users) > 1: resp = "The following users are receiving 100 cash for hosting: " + ", ".join(unthanked_users) + "!" globals.irc.send_message("#" + channel, resp) elif len(unthanked_users) > 10: resp = "Thanks to the {0} people hosting! Each of you get 100 cash!".format( len(unthanked_users)) globals.irc.send_message("#" + channel, resp) else: cd.remove_channel_data("host") except Exception as error: print error
def check_for_sub(channel, username, message): # >> :[email protected] PRIVMSG #curvyllama :KiefyWonder subscribed for 5 months in a row! # >> :[email protected] PRIVMSG #curvyllama :KiefyWonder just subscribed! # Photo_phocus just subscribed to jonsandman! # HermanNugent subscribed to JonSandman for 7 months in a row! # first sub points = 1000 # resub = 250 db = Database() try: channel = channel.lstrip("#") message_split = message.rstrip("!").split() subbed_user = message_split[0].lower() if message_split[1] == "just" and len(message_split) < 4: points = 1000 db.add_user([subbed_user], channel) db.modify_points(subbed_user, channel, points) resp = "/me {0} just subscribed for the first time!\ {1} cash for you!".format(subbed_user, points) self.irc.send_message("#" + channel, resp) elif message_split[1] == "subscribed" and len(message_split) < 9: months_subbed = message_split[3] points = 250 db.add_user([subbed_user], channel) db.modify_points(subbed_user, channel, points) resp = "/me {0} has just resubscribed for {1} months \ straight and is getting {2} cash!".format(subbed_user, months_subbed, points) self.irc.send_message("#" + channel, resp) except Exception as error: print error
class Bets: def __init__(self): self.irc = globals.irc self.channel = globals.CURRENT_CHANNEL self.chan = "#" + self.channel self.db = Database() #self.bets_started = datetime.strptime(self.db.get_bets_started(self.channel)[0], "%Y %m %d %H %M %S") #Generates list of tuples(username, amount, outcome, date) that only consists of bidders that bet after the bets have started self.bets_data = [x for x in self.db.get_bets_data(self.channel) if datetime.strptime(x[3], "%Y %m %d %H %M %S") > datetime.strptime(self.db.get_bets_started(self.channel)[0], "%Y %m %d %H %M %S")] def calculate_total_points(self): total_points = 0 for bidder in self.bets_data: total_points += bidder[1] return total_points def calculate_winning_points(self, outcome): winning_points = 0 for bidder in self.bets_data: if bidder[2] == outcome: winning_points += bidder[1] return winning_points #Called from outcome.py def distribute_profit(self, outcome): bets_total = self.calculate_total_points() bets_winning = self.calculate_winning_points(outcome) try: profit = float(bets_total)/bets_winning except ZeroDivisionError: msg = "Sorry, there are no winners" self.irc.send_message(self.chan, msg) return try: for bidder in self.bets_data: if bidder[2] == outcome: winnings = int(profit*bidder[1]) self.db.modify_points(bidder[0], winnings) msg = "/w {0} Congrats! You won {1} points!".format(bidder[0], winnings) self.irc.send_message(self.chan, msg) else: msg = "/w {0} Feelsbadman, maybe you will win next time".format(bidder[0]) self.irc.send_message(self.chan, msg) except Exception as error: print error msg = "Sorry, there was an error distributing profit" self.irc.send_message(self.chan, msg) return msg = "Winnings have been released!" self.irc.send_message(self.chan, msg)
class Gamble: def __init__(self, channel="testchannel", user="******", points=0): self.db = Database() self.channel = channel self.user = user self.points = points def initiate_gamble(self): if self.channel not in globals.channel_info: globals.channel_info[self.channel]["gamble"] = { "time": None, "users": {}, "points": 0} globals.channel_info[self.channel]['gamble'] = { "time": time.time(), "users": { self.user: True}, "points": self.points} def terminate_gamble(self): globals.channel_info[self.channel]['gamble']["time"] = None def check_gamble(self): if self.channel not in globals.channel_info: globals.channel_info[self.channel]["gamble"] = { "time": None, "users": {}} if globals.channel_info[self.channel]['gamble']["time"]: return globals.channel_info[self.channel]['gamble']["time"] else: return None def get_gamble_user(self, user): user_value = globals.channel_info[globals.CURRENT_CHANNEL]['gamble'][ "users"].get(user) if user_value: return True else: return False def rob_yield(self, multiplier=1): points_yield = random.choice(range(1, 11)) points = 0 if points_yield > 9: points = random.choice(range(1, 301)) * multiplier elif points_yield <= 9 and points_yield > 5: points = random.choice(range(1, 21)) * multiplier elif points_yield <= 5 and points_yield > 1: points = random.choice(range(1, 11)) * multiplier else: points = self.points if bool(random.getrandbits(1)): points *= -1 return points def apply_yield(self, channel, user, points): self.db.modify_points(user, channel, points)
def chance(): db = Database() channel = globals.CURRENT_CHANNEL user = globals.CURRENT_USER g = Gamble(channel) points = abs(g.rob_yield(multiplier=1)) db.add_user([user], channel) db.modify_points(user, channel, points) print user, points, channel if points == 0: resp = "Nothing this time! Try again in a half hour?" else: resp = "You got {0} cash!".format(points) return resp
def win(args): db = Database() channel = globals.CURRENT_CHANNEL bets = db.are_bets(channel)[0] if bets: username = globals.CURRENT_USER if db.get_user(username) == None: db.add_user([username]) db.modify_points(username, 1000) current_points = db.get_points(username)[0] if (args[0][-1] == "%"): try: percentage = int(args[0].strip("%")) except Exception as error: print error return "Sorry, but your betting amount should be a number or a percentage" if percentage > 100: return "Sorry, but you can't bet more than a 100%" else: multiply = float(percentage) / 100 amount = int(current_points * multiply) else: try: amount = int(args[0]) except Exception as error: print error return "Sorry, but your betting amount should be a number or a percentage" if amount > current_points: return "You currently have {0} points, use them wisely.".format( current_points) date = datetime.strftime(datetime.utcnow(), "%Y %m %d %H %M %S") db.add_bidder(channel, username, amount, "win", date) db.modify_points(username, -amount) return "Bet successfully placed! You bet {0} that {1} will win".format( amount, channel) else: return "Sorry, bets were not started yet"
def lose(args): db = Database() channel = globals.CURRENT_CHANNEL bets = db.are_bets(channel)[0] if bets: username = globals.CURRENT_USER if db.get_user(username) == None: db.add_user([username]) db.modify_points(username, 1000) current_points = db.get_points(username)[0] if (args[0][-1] == "%"): try: percentage = int(args[0].strip("%")) except Exception as error: print error return "Sorry, but your betting amount should be a number or a percentage" if percentage > 100: return "Sorry, but you can't bet more than a 100%" else: multiply = float(percentage) / 100 amount = int(current_points * multiply) else: try: amount = int(args[0]) except Exception as error: print error return "Sorry, but your betting amount should be a number or a percentage" if amount > current_points: return "You currently have {0} points, use them wisely.".format(current_points) date = datetime.strftime(datetime.utcnow(), "%Y %m %d %H %M %S") db.add_bidder(channel, username, amount, "lose", date) db.modify_points(username, -amount) return "Bet successfully placed! You bet {0} that {1} will lose".format(amount, channel) else: return "Sorry, bets were not started yet"
def gift(args): user = globals.CURRENT_USER recipient = args[0].lower().lstrip("@") channel = globals.CURRENT_CHANNEL try: amount = abs(int(args[1])) except: return "Amount has to be a number!" if recipient == user: return "You can't gift yourself cash!" db = Database() if db.get_user(user, channel): if db.get_user(user, channel)[2] >= amount and db.get_user( recipient, channel): db.modify_points(recipient, channel, amount) db.modify_points(user, channel, amount * -1) return "{0} cash has been debited to {1}!".format(amount, recipient) else: return "Those numbers just don't add up. Check your spelling!" else: return "You don't even have any cash!"
def gift(args): user = globals.CURRENT_USER recipient = args[0].lower().lstrip("@") channel = globals.CURRENT_CHANNEL try: amount = abs(int(args[1])) except: return "Amount has to be a number!" if recipient == user: return "You can't gift yourself cash!" db = Database() if db.get_user(user, channel): if db.get_user(user, channel)[2] >= amount and db.get_user( recipient, channel): db.modify_points(recipient, channel, amount) db.modify_points(user, channel, amount * -1) return "{0} cash has been debited to {1}!".format( amount, recipient) else: return "Those numbers just don't add up. Check your spelling!" else: return "You don't even have any cash!"
def cron(channel): try: channel = channel.lstrip("#") from src.lib.twitch import get_stream_status, get_hosts from src.lib.channel_data import ChannelData from src.lib.queries import Database cd = ChannelData(channel) if get_stream_status(): channel_id = cd.get_channel_id_from_db()[0] hosts = get_hosts(channel_id) unthanked_users = [] for host in hosts: host_data = cd.get_channel_data_by_user( host["host_login"], "host") if not host_data: cd.insert_channel_data(host["host_login"], "host") db = Database() db.add_user([host["host_login"]], channel) db.modify_points(host["host_login"], channel, 100) unthanked_users.append(host["host_login"]) if len(unthanked_users) == 1: user = unthanked_users[0] resp = "Thank you {0} for the host! Here's 100 cash!".format( user) globals.irc.send_message("#" + channel, resp) elif len(unthanked_users) > 1: import globals resp = "The following users are receiving 100 cash for hosting: " + ", ".join( unthanked_users) + "!" globals.irc.send_message("#" + channel, resp) elif len(unthanked_users) > 10: resp = "Thanks to the {0} people hosting! Each of you get 100 cash!".format( len(unthanked_users)) globals.irc.send_message("#" + channel, resp) else: cd.remove_channel_data("host") except Exception as error: print error
class Cash: def __init__(self, channel): self.db = Database() self.channel = channel def add_all(self, points): user_dict, all_users = get_dict_for_users(self.channel) self.db.add_user(all_users, self.channel) for user in all_users: self.db.modify_points([user], self.channel, points) return {"users": all_users, "channel": self.channel, "points": points} def add(self, users, points): self.db.add_user(users, self.channel) self.db.modify_points(users[0], self.channel, points) return {"user": users[0], "channel": self.channel, "points": points} def modify(self, users, points): self.db.add_user(users, self.channel) self.db.modify_points(users[0], self.channel, points) return {"user": users[0], "channel": self.channel, "points": points} def get(self, user): # (3, u'testuser', 5, u'mod') user_data = self.db.get_user(user, self.channel) if user_data: return { "user": user_data[1], "channel": self.channel, "points": user_data[2] } else: return {"user": user, "channel": self.channel, "points": 0} def rank(self, user): user_data = self.db.get_cash_rank(user, self.channel) if user_data: return { "user": user_data[0], "channel": self.channel, "points": user_data[1], "rank": user_data[3] } else: return { "user": user, "channel": self.channel, "points": 0, "rank": None }