def pleblist_add_song(self, bot, source, message, **rest): if not message: return False # 1. Find youtube ID in message msg_split = message.split(" ") youtube_id = find_youtube_id_in_string(msg_split[0]) force = False try: if msg_split[1] == "force" and source.level >= 500: force = True except: pass if youtube_id is False: bot.whisper(source, "Could not find a valid youtube ID in your argument.") return False # 2. Make sure the stream is live stream_id = StreamHelper.get_current_stream_id() if stream_id is None or stream_id is False: bot.whisper(source, "You cannot request songs while the stream is offline.") return False ScheduleManager.execute_now(self.bg_pleblist_add_song, args=[stream_id, youtube_id, force, bot, source])
def award_tokens(self, tokens, redis=None, force=False): """ Returns True if tokens were awarded properly. Returns False if not. Tokens can only be rewarded once per stream ID. """ streamer = StreamHelper.get_streamer() stream_id = StreamHelper.get_current_stream_id() if stream_id is False: return False if redis is None: redis = RedisManager.get() key = '{streamer}:{username}:tokens'.format(streamer=streamer, username=self.username) if force: res = True redis.hset(key, stream_id, tokens) else: res = True if redis.hsetnx(key, stream_id, tokens) == 1 else False if res is True: HandlerManager.trigger('on_user_gain_tokens', self, tokens) return res
def award_tokens(self, tokens, redis=None, force=False): """ Returns True if tokens were awarded properly. Returns False if not. Tokens can only be rewarded once per stream ID. """ streamer = StreamHelper.get_streamer() stream_id = StreamHelper.get_current_stream_id() if stream_id is False: return False if redis is None: redis = RedisManager.get() key = '{streamer}:{username}:tokens'.format( streamer=streamer, username=self.username) if force: res = True redis.hset(key, stream_id, tokens) else: res = True if redis.hsetnx(key, stream_id, tokens) == 1 else False if res is True: HandlerManager.trigger('on_user_gain_tokens', self, tokens) return res
def progress_quest(self, amount, limit, completion_reward): """ Progress the quest for `stream_id` by `amount`. We load data from redis in case no progress has been made yet. """ streamer = StreamHelper.get_streamer() stream_id = StreamHelper.get_current_stream_id() if stream_id is False: return False redis = RedisManager.get() quest_progress_key = '{streamer}:{stream_id}:{username}:quest_progress'.format( streamer=streamer, stream_id=stream_id, username=self.username) if stream_id not in self.quest_progress: # Load the old progress, or set it to 0 if no progress was found self.init_quest_progress() if self.quest_progress[stream_id] >= limit: # The user has already completed this quest. return False self.quest_progress[stream_id] += amount if self.quest_progress[stream_id] >= limit: # The user just completed the quest for the first time self.award_tokens(completion_reward, redis=redis) return False redis.set(quest_progress_key, self.quest_progress[stream_id])
def pleblist_add_song(self, **options): message = options['message'] bot = options['bot'] source = options['source'] if message: # 1. Find youtube ID in message msg_split = message.split(' ') youtube_id = find_youtube_id_in_string(msg_split[0]) force = False try: if msg_split[1] == 'force' and source.level >= 500: force = True except: pass if youtube_id is False: bot.whisper(source.username, 'Could not find a valid youtube ID in your argument.') return False # 2. Make sure the stream is live stream_id = StreamHelper.get_current_stream_id() if stream_id is None or stream_id is False: bot.whisper(source.username, 'You cannot request songs while the stream is offline.') return False ScheduleManager.execute_now(self.bg_pleblist_add_song, args=[stream_id, youtube_id, force], kwargs=options)
def add_song(self, bot, source, message, **rest): if not message: self.bot.whisper( source, "Could not find a valid youtube ID in your argument.") return False # 1. Find youtube ID in message msg_split = message.split(" ") youtube_id = find_youtube_id_in_string(msg_split[0]) if youtube_id is False: youtube_id = find_youtube_video_by_search(message) if youtube_id is None: self.bot.whisper( source, "Could not find a valid youtube ID in your argument.") return False # 2. Make sure the stream is live stream_id = StreamHelper.get_current_stream_id() if stream_id is None or stream_id is False: self.bot.whisper( source, "You cannot request songs while the stream is offline.") return False return self.create_song_request_queue(youtube_id, bot, source)
def get_quest_progress(self): stream_id = StreamHelper.get_current_stream_id() if stream_id is False: return False if stream_id not in self.quest_progress: self.init_quest_progress() return self.quest_progress[stream_id]
def _to_histroy(self, db_session, skipped_by_id=None): stream_id = StreamHelper.get_current_stream_id() if not stream_id: self._remove(db_session) return None history = SongrequestHistory._create( db_session, stream_id, self.video_id, self.requested_by.id if self.requested_by else None, skipped_by_id, self.skip_after, ) self._remove(db_session) return history
def init_quest_progress(self, redis=None): """ Initialize quest progress for the current stream. """ streamer = StreamHelper.get_streamer() stream_id = StreamHelper.get_current_stream_id() if redis is None: redis = RedisManager.get() quest_progress_key = '{streamer}:{stream_id}:{username}:quest_progress'.format( streamer=streamer, stream_id=stream_id, username=self.username) old_progress = 0 try: old_progress = int(redis.get(quest_progress_key)) except (TypeError, ValueError): pass self.quest_progress[stream_id] = old_progress
def finish_quest(self, redis, user): stream_id = StreamHelper.get_current_stream_id() # Load user's finished quest status val = redis.hget(self.quest_finished_key, user.username) if val: quests_finished = json.loads(val) else: quests_finished = [] if stream_id in quests_finished: # User has already completed this quest return reward_type = self.quest_module.settings['reward_type'] reward_amount = self.quest_module.settings['reward_amount'] if reward_type == 'tokens' and user.tokens > self.quest_module.settings[ 'max_tokens']: message = 'You finished todays quest, but you have more than the max tokens allowed already. Spend some tokens!' pajbot.managers.handler.HandlerManager.trigger( 'send_whisper', user.username, message) return # Mark the current stream ID has finished quests_finished.append(stream_id) redis.hset(self.quest_finished_key, user.username, json.dumps(quests_finished, separators=(',', ':'))) # Award the user appropriately if reward_type == 'tokens': user.tokens += reward_amount else: user.points += reward_amount # Notify the user that they've finished today's quest message = 'You finished todays quest! You have been awarded with {} {}.'.format( reward_amount, reward_type) pajbot.managers.handler.HandlerManager.trigger('send_whisper', user.username, message) # XXX: this can safely be removed once points are moved to redis user.save()
def finish_quest(self, redis, user): if not self.quest_module: log.error("Quest module not initialized") return stream_id = StreamHelper.get_current_stream_id() # Load user's finished quest status val = redis.hget(self.quest_finished_key, user.id) if val: quests_finished = json.loads(val) else: quests_finished = [] if stream_id in quests_finished: # User has already completed this quest return reward_type = self.quest_module.settings["reward_type"] reward_amount = self.quest_module.settings["reward_amount"] if reward_type == "tokens" and user.tokens > self.quest_module.settings[ "max_tokens"]: message = ( "You finished todays quest, but you have more than the max tokens allowed already. Spend some tokens!" ) self.bot.whisper(user, message) return # Mark the current stream ID has finished quests_finished.append(stream_id) redis.hset(self.quest_finished_key, user.id, json.dumps(quests_finished, separators=(",", ":"))) # Award the user appropriately if reward_type == "tokens": user.tokens += reward_amount else: user.points += reward_amount # Notify the user that they've finished today's quest message = f"You finished todays quest! You have been awarded with {reward_amount} {reward_type}." self.bot.whisper(user, message)
def finish_quest(self, redis, user): stream_id = StreamHelper.get_current_stream_id() # Load user's finished quest status val = redis.hget(self.quest_finished_key, user.username) if val: quests_finished = json.loads(val) else: quests_finished = [] if stream_id in quests_finished: # User has already completed this quest return reward_type = self.quest_module.settings['reward_type'] reward_amount = self.quest_module.settings['reward_amount'] if reward_type == 'tokens' and user.tokens > self.quest_module.settings['max_tokens']: message = 'You finished todays quest, but you have more than the max tokens allowed already. Spend some tokens!' pajbot.managers.handler.HandlerManager.trigger('send_whisper', user.username, message) return # Mark the current stream ID has finished quests_finished.append(stream_id) redis.hset(self.quest_finished_key, user.username, json.dumps(quests_finished, separators=(',', ':'))) # Award the user appropriately if reward_type == 'tokens': user.tokens += reward_amount else: user.points += reward_amount # Notify the user that they've finished today's quest message = 'You finished todays quest! You have been awarded with {} {}.'.format(reward_amount, reward_type) pajbot.managers.handler.HandlerManager.trigger('send_whisper', user.username, message) # XXX: this can safely be removed once points are moved to redis user.save()