def ping_server(self): if self.sent_ping: return self.sendData({"type": "PING"}) self.sent_ping = True ScheduleManager.execute_delayed(30, self.check_ping)
def start(action_queue): # We add up to 1 minute of jitter to try to alleviate CPU spikes when multiple pajbot instances restart at the same time. # The jitter is added to both the initial refresh, and the scheduled one every 5 minutes. # Initial refresh ScheduleManager.execute_delayed( UserRanksRefreshManager._jitter(), lambda: action_queue.submit(UserRanksRefreshManager._refresh, action_queue), )
def disable_showemote(self, bot, source, args, **rest): if not self.showemote_disabled: self.showemote_disabled = True bot.whisper(source, f"Successfully disabled the show emote feature") ScheduleManager.execute_delayed(self.settings["disable_duration"], self.enable_showemote) return True bot.whisper(source, f"Show emote is already disabled!") return False
def _refresh(action_queue): try: with DBManager.create_dbapi_cursor_scope( autocommit=True) as cursor: cursor.execute( "REFRESH MATERIALIZED VIEW CONCURRENTLY user_rank") cursor.execute("VACUUM user_rank") finally: # Queue up the refresh in 5-6 minutes ScheduleManager.execute_delayed( UserRanksRefreshManager.delay + UserRanksRefreshManager._jitter(), lambda: action_queue.submit(UserRanksRefreshManager._refresh, action_queue), )
def lock_bets(self): self.lock_schedule = None if self.close_schedule: try: self.close_schedule.remove() except: pass self.close_schedule = None with DBManager.create_session_scope() as db_session: current_game = self.get_current_game(db_session) if current_game.bets_closed: return False points_by_outcome = current_game.get_points_by_outcome(db_session) investment_ratios = self.get_odds_ratio(points_by_outcome) winRatio = investment_ratios[BetGameOutcome.win] lossRatio = investment_ratios[BetGameOutcome.loss] self.bot.me( f"The betting for the current game has been closed! Winners can expect {winRatio} (win bettors) or {lossRatio} (loss bettors) point return on their bet" ) self.bot.websocket_manager.emit( event="notification", widget_id=WIDGET_ID, data={ "message": "The betting for the current game has been closed!" }, ) self.close_schedule = ScheduleManager.execute_delayed( 15, self.bot.websocket_manager.emit, args=["bet_close_game", WIDGET_ID]) current_game.bets_closed = True
def spread_points(self, gameResult): with DBManager.create_session_scope() as db_session: # What is faster? Doing it like this or with a generator afterwards? winners = 0 losers = 0 current_game = self.get_current_game(db_session, with_bets=True, with_users=True) current_game.outcome = gameResult points_by_outcome = current_game.get_points_by_outcome(db_session) investment_ratios = self.get_odds_ratio(points_by_outcome) total_winnings = 0 total_losings = 0 for bet in current_game.bets: if bet.outcome == current_game.outcome: winners += 1 investment_ratio = investment_ratios[bet.outcome] bet.profit = int(bet.points * (investment_ratio - 1)) bet.user.points += int(bet.points + bet.profit) total_winnings += bet.profit self.bot.whisper( bet.user, f"You bet {bet.points} points on the correct outcome and gained an extra {bet.profit} points, you now have {bet.user.points} points PogChamp", ) else: losers += 1 bet.profit = -bet.points total_losings += -bet.profit self.bot.whisper( bet.user, f"You bet {bet.points} points on the wrong outcome, so you lost it all :( . You now have {bet.user.points} points sadKEK", ) startString = f"The game ended as a {gameResult.name}. {winners} users won an extra {total_winnings} points, while {losers} users lost {total_losings} points." if self.spectating: resultString = startString[:20] + "radiant " + startString[20:] else: resultString = startString # Just to make sure current_game.bets_closed = True self.spectating = False self.bot.websocket_manager.emit( event="notification", widget_id=WIDGET_ID, data={"message": resultString, "length": 8} ) self.bot.me(resultString) db_session.flush() self.bot.websocket_manager.emit(event="bet_show_bets", widget_id=WIDGET_ID) if self.close_schedule: try: self.close_schedule.remove() except: pass self.close_schedule = ScheduleManager.execute_delayed( 15, self.bot.websocket_manager.emit, args=["bet_close_game", WIDGET_ID] )
def command_close(self, bot, source, message, **rest): with DBManager.create_session_scope() as db_session: current_game = db_session.query(BetGame).filter( BetGame.is_running).one_or_none() if not current_game: bot.say(f"{source}, no bet currently exists") return False if current_game.betting_open: if self.lock_schedule: try: self.lock_schedule.remove() except: pass self.lock_schedule = None count_down = 15 if message and message.isdigit(): count_down = 15 if int(message) < 0 else int(message) if count_down > 0: bot.me( f"Betting will be locked in {count_down} seconds! Place your bets people monkaS" ) self.lock_schedule = ScheduleManager.execute_delayed( count_down, self.lock_bets) elif message: split_message = message.split(" ") outcome = None if len(split_message) > 0: for item in split_message: outcome = "l" if "l" in item.lower( ) or "dire" in item.lower() else None if not outcome: outcome = "w" if "w" in item.lower( ) or "radi" in item.lower() else None if outcome: break if outcome: if outcome == "l": bot.execute_now(self.spread_points, BetGameOutcome.loss) else: bot.execute_now(self.spread_points, BetGameOutcome.win) else: bot.say(f"Are you pretending {source}?") return False self.spectating = False else: bot.say("WTFF")
def seek_function(self, _time): if not self.module_state["enabled"]: return False if self.current_song_id: with DBManager.create_session_scope() as db_session: current_song = SongrequestQueue._from_id( db_session, self.current_song_id) current_song.played_for += _time - current_song.current_song_time db_session.merge(current_song) db_session.commit() self.remove_schedule() self.schedule_job_id = random.randint(1, 100000) self.current_song_schedule = ScheduleManager.execute_delayed( current_song.time_left + 10, self.load_song_schedule, args=[self.schedule_job_id]) self._seek(_time, current_song.webjsonify()) return True return False
def resume_function(self): if not self.module_state["enabled"] or not self.current_song_id: return False if self.module_state["paused"]: self.module_state["paused"] = False self._module_state() self._resume() with DBManager.create_session_scope() as db_session: song = SongrequestQueue._from_id(db_session, self.current_song_id) song.date_resumed = utils.now() self.schedule_job_id = random.randint(1, 100000) self.current_song_schedule = ScheduleManager.execute_delayed( song.time_left + 10, self.load_song_schedule, args=[self.schedule_job_id]) return True return False
def load_song(self, skipped_by_id=None): if not self.module_state["enabled"]: return False if self.current_song_id: with DBManager.create_session_scope() as db_session: current_song = SongrequestQueue._from_id( db_session, self.current_song_id) if current_song: if current_song.current_song_time > 5: self.previous_queue = 0 histroy = current_song._to_histroy( db_session, skipped_by_id) if not histroy: log.info( "History not added because stream is offline!") else: current_song._remove(db_session) self._stop_video() self._hide() db_session.commit() self._playlist_history() self.current_song_id = None self.schedule_job_id = None self.module_state["paused"] = False self._module_state() self.remove_schedule() if not self.module_state["requests_open"]: if self.previously_playing_spotify: self.bot.spotify_api.play(self.bot.spotify_token_manager) log.info("Resumed Spotify") self.previously_playing_spotify = False return False with DBManager.create_session_scope() as db_session: current_song = SongrequestQueue._get_current_song(db_session) if not current_song: current_song = SongrequestQueue._pop_next_song(db_session) if current_song: SongRequestQueueManager.update_song_playing_id(current_song.id) current_song.played_for = 0 current_song.date_resumed = utils.now() self.current_song_id = current_song.id self._volume() self._play(current_song.video_id, current_song.webjsonify()) self.schedule_job_id = random.randint(1, 100000) self.current_song_schedule = ScheduleManager.execute_delayed( current_song.time_left + 10, self.load_song_schedule, args=[self.schedule_job_id]) if self.settings["use_spotify"]: is_playing, song_name, artistsArr = self.bot.spotify_api.state( self.bot.spotify_token_manager) if is_playing: self.bot.spotify_api.pause( self.bot.spotify_token_manager) self.previously_playing_spotify = True if not current_song.requested_by_id: SongrequestQueue._create(db_session, current_song.video_id, current_song.skip_after, None, backup=True) db_session.commit() if current_song.requested_by_id: self._playlist() else: self._backup_playlist() return True if not current_song: SongRequestQueueManager.update_song_playing_id("") if self.settings["use_spotify"]: if self.previously_playing_spotify: self.bot.spotify_api.play(self.bot.spotify_token_manager) log.info("Resumed Spotify") self.previously_playing_spotify = False if self.is_video_showing: self._hide() return False
def on_disconnect(self, *args): log.error('Socket disconnected. Donations no longer monitored') self.bot.say('Socket disconnected. Donations no longer monitored') ScheduleManager.execute_delayed(30, DonationPointsModule.restartClass)
def epm_incr(self, code, count): new_epm = self.epm.get(code, 0) + count self.epm[code] = new_epm self.save_epm_record(code, new_epm) # TODO if we want to add epm records back, do it here ScheduleManager.execute_delayed(60, self.epm_decr, args=[code, count])
def check_connection(self): if not self.sent_ping: self.sendData({"type": "PING"}) self.sent_ping = True ScheduleManager.execute_delayed(15, self.check_ping)
def epm_incr(self, code, count): if code in self.epm: self.epm[code] += count else: self.epm[code] = count ScheduleManager.execute_delayed(60, self.epm_decr, args=[code, count])
def ready(self): self.resume_function() ScheduleManager.execute_delayed(2, self._volume)
def onDisconnect(self, *args): log.error("Socket disconnected. Donations no longer monitored") ScheduleManager.execute_delayed(15, self.reset)
def epm_incr(self, code, count): new_epm = self.epm.get(code, 0) + count self.epm[code] = new_epm self.save_epm_record(code, new_epm) ScheduleManager.execute_delayed(60, self.epm_decr, args=[code, count])
def on_close(self, ws): log.error("Socket disconnected. Donations no longer monitored") ScheduleManager.execute_delayed(10, self.reset)