def post(self, *args, **kwargs): ''' Reset the Game ''' errors = [] success = None try: users = User.all() for user in users: user.money = 0 teams = Team.all() for team in teams: if options.banking: team.money = options.starting_team_money else: team.money = 0 team.flags = [] team.hints = [] team.boxes = [] team.items = [] team.purchased_source_code = [] level_0 = GameLevel.by_number(0) if not level_0: level_0 = GameLevel.all()[0] team.game_levels = [level_0] self.dbsession.add(team) self.dbsession.commit() self.dbsession.flush() for team in teams: for paste in team.pastes: self.dbsession.delete(paste) for shared_file in team.files: shared_file.delete_data() self.dbsession.delete(shared_file) self.dbsession.commit() self.dbsession.flush() Penalty.clear() Notification.clear() snapshot = Snapshot.all() for snap in snapshot: self.dbsession.delete(snap) self.dbsession.commit() snapshot_team = SnapshotTeam.all() for snap in snapshot_team: self.dbsession.delete(snap) self.dbsession.commit() game_history = GameHistory.instance() game_history.take_snapshot() # Take starting snapshot flags = Flag.all() for flag in flags: flag.value = flag._original_value if flag._original_value else flag.value self.dbsession.add(flag) self.dbsession.commit() self.dbsession.flush() success = "Successfully Reset Game" self.render('admin/reset.html', success=success, errors=errors) except BaseException as e: errors.append("Failed to Reset Game") logging.error(str(e)) self.render('admin/reset.html', success=None, errors=errors)
class ScoreboardHistorySocketHandler(WebSocketHandler): connections = set() game_history = GameHistory.instance() def initialize(self): self.last_message = datetime.now() def open(self): ''' When we receive a new websocket connect ''' self.connections.add(self) history_length = int(self.get_argument('length', 29)) self.write_message(self.get_history(history_length)) def on_message(self, message): ''' We ignore messages if there are more than 1 every 3 seconds ''' if self.application.settings['freeze_scoreboard']: self.write_message("pause") elif datetime.now() - self.last_message > timedelta(seconds=3): self.last_message = datetime.now() self.write_message(self.game_history[:-1]) def on_close(self): ''' Lost connection to client ''' self.connections.remove(self) def get_history(self, length=29): ''' Send history in JSON ''' length = abs(length) + 1 return json.dumps({'history': self.game_history[(-1 * length):]})
class ScoreboardHistorySocketHandler(WebSocketHandler): connections = set() game_history = GameHistory.instance() def initialize(self): self.last_message = datetime.now() def open(self): ''' When we receive a new websocket connect ''' self.connections.add(self) self.write_message(self.get_history()) def on_message(self, message): ''' We ignore messages if there are more than 1 every 5 seconds ''' if self.last_message - datetime.now() > timedelta(seconds=5): self.last_message = datetime.now() self.write_message(self.game_history[:-1]) def on_close(self): ''' Lost connection to client ''' self.connections.remove(self) def get_history(self, length=29): ''' Send history in JSON ''' length = abs(length) + 1 return json.dumps({'history': self.game_history[(-1 * length):]})
class ScoreboardHistorySocketHandler(WebSocketHandler): connections = set() game_history = GameHistory.instance() def initialize(self): self.game_history._load() self.last_message = datetime.now() def open(self): """ When we receive a new websocket connect """ self.connections.add(self) history_length = int(self.get_argument("length", 29)) self.write_message(self.get_history(history_length)) def on_message(self, message): """ We ignore messages if there are more than 1 every 3 seconds """ if self.application.settings["hide_scoreboard"]: self.write_message("pause") elif datetime.now() - self.last_message > timedelta(seconds=3): self.last_message = datetime.now() self.write_message(self.get_history(1)) def on_close(self): """ Lost connection to client """ self.connections.remove(self) def get_history(self, length=29): """ Send history in JSON """ length = abs(length) + 1 return json.dumps({"history": self.game_history[(-1 * length):]})
def start_server(): ''' Main entry point for the application ''' sockets = netutil.bind_sockets(config.listen_port) server = HTTPServer(app) server.add_sockets(sockets) io_loop = IOLoop.instance() scoring = PeriodicCallback(scoring_round, int(5 * 60 * 1000), io_loop=io_loop) scoring.start() try: sys.stdout.write("\r" + INFO + "The game has begun, good hunting!\n") if config.debug: sys.stdout.write(WARN + "WARNING: Debug mode is enabled.\n") sys.stdout.flush() game_history = GameHistory.Instance() history_callback = PeriodicCallback(game_history.take_snapshot, int(60 * 1000), io_loop=io_loop) history_callback.start() io_loop.start() except KeyboardInterrupt: print('\r' + WARN + 'Shutdown Everything!') except: logging.exception("Main i/o loop threw exception") finally: io_loop.stop() if config.debug and \ raw_input(PROMPT + "Flush Memcache? [Y/n]: ").lower() == 'y': print(INFO + 'Flushing cache ...'), FileCache.flush() print('OK') _exit(0)
def bots(self): game_history = GameHistory.Instance() history = {} for team in Team.all(): history[team.name] = game_history.get_bot_history_by_name( team.name, -30 ) self.render('scoreboard/history/bots.html', history=history)
def refresh_app_config(self): # Update default theme self.application.ui_modules["Theme"].theme = Theme.by_name( options.default_theme) # Callback functions - updates and starts/stops the botnet callback self.application.settings["score_bots_callback"].stop() self.application.score_bots_callback = PeriodicCallback( score_bots, options.bot_reward_interval) if options.use_bots: logging.info("Starting botnet callback function") self.application.settings["score_bots_callback"].start() logging.info("Restarting history callback function") game_history = GameHistory.instance() self.application.settings["history_callback"].stop() self.application.history_callback = PeriodicCallback( game_history.take_snapshot, options.history_snapshot_interval) self.application.settings["history_callback"].start()
def start_server(): ''' Main entry point for the application ''' server = HTTPServer(app) sockets = netutil.bind_sockets(config.listen_port) server.add_sockets(sockets) io_loop = IOLoop.instance() try: if config.debug: # Print a nice verbose warning sys.stdout.write(WARN + "WARNING: Debug mode is enabled in " + config.filename) sys.stdout.write( bold + "\n\n\t>>> Debug Mode Disables Some Security Measures <<<" + W + "\n\n") sys.stdout.flush() game_history = GameHistory.Instance() history_callback = PeriodicCallback(game_history.take_snapshot, config.history_snapshot_interval, io_loop=io_loop) scoring_callback = PeriodicCallback(score_bots, config.bot_reward_interval, io_loop=io_loop) bot_ping_callback = PeriodicCallback(ping_bots, 30000, io_loop=io_loop) # Start ALL THE THINGS! bot_ping_callback.start() history_callback.start() scoring_callback.start() io_loop.start() except KeyboardInterrupt: sys.stdout.write('\r' + WARN + 'Shutdown Everything!\n') except: logging.exception("Main i/o loop threw exception") finally: io_loop.stop() if config.debug and raw_input( PROMPT + "Flush Memcache? [Y/n]: ").lower() == 'y': sys.stdout.write(INFO + 'Flushing cache ... '), FileCache.flush() sys.stdout.write('okay\n') _exit(0)
from handlers.AdminHandlers import * from handlers.ErrorHandlers import * from handlers.PublicHandlers import * from handlers.MarketHandlers import * from handlers.UpgradeHandlers import * from handlers.MissionsHandler import * from handlers.PastebinHandlers import * from handlers.ScoreboardHandlers import * from handlers.FileUploadHandlers import * from handlers.NotificationHandlers import * from handlers.StaticFileHandler import StaticFileHandler from tornado.options import options # Singletons io_loop = IOLoop.instance() game_history = GameHistory.instance() def get_cookie_secret(): if options.debug: return "Don't use this in production" else: return urandom(32).encode("hex") # Main URL Configuration # First get base URLs that all game types will require urls = [ # Public handlers - PublicHandlers.py (r"/login", LoginHandler), (r"/registration", RegistrationHandler),
from handlers.PastebinHandlers import * from handlers.ScoreboardHandlers import * from handlers.FileUploadHandlers import * from handlers.NotificationHandlers import * from handlers.MaterialsHandler import * from handlers.ChefHandler import * from handlers.StaticFileHandler import StaticFileHandler try: from urllib.parse import unquote except ImportError: from urllib import unquote # Singletons io_loop = IOLoop.instance() game_history = GameHistory.instance() def get_cookie_secret(): if options.debug: return "Don't use this in production" else: return encode(urandom(32), "hex") # Main URL Configuration # First get base URLs that all game types will require urls = [ # Public handlers - PublicHandlers.py (r"/login", LoginHandler), (r"/registration", RegistrationHandler),
def post(self, *args, **kwargs): """ Reset the Game """ errors = [] success = None try: users = User.all() for user in users: user.money = 0 teams = Team.all() for team in teams: if options.banking: team.money = options.starting_team_money else: team.money = 0 team.flags = [] team.hints = [] team.boxes = [] team.items = [] team.purchased_source_code = [] level_0 = GameLevel.by_number(0) if not level_0: level_0 = GameLevel.all()[0] team.game_levels = [level_0] self.dbsession.add(team) self.dbsession.commit() self.dbsession.flush() for team in teams: for paste in team.pastes: self.dbsession.delete(paste) for shared_file in team.files: shared_file.delete_data() self.dbsession.delete(shared_file) self.dbsession.commit() self.dbsession.flush() Penalty.clear() Notification.clear() snapshot = Snapshot.all() for snap in snapshot: self.dbsession.delete(snap) self.dbsession.commit() snapshot_team = SnapshotTeam.all() for snap in snapshot_team: self.dbsession.delete(snap) self.dbsession.commit() game_history = GameHistory.instance() game_history.take_snapshot() # Take starting snapshot flags = Flag.all() for flag in flags: # flag.value = flag.value allows a fallback to when original_value was used # Allows for the flag value to be reset if dynamic scoring was used # Can be removed after depreciation timeframe flag.value = flag.value self.dbsession.add(flag) self.dbsession.commit() self.dbsession.flush() self.event_manager.push_score_update() self.flush_memcached() success = "Successfully Reset Game" self.render("admin/reset.html", success=success, errors=errors) except BaseException as e: errors.append("Failed to Reset Game") logging.error(str(e)) self.render("admin/reset.html", success=None, errors=errors)
def initialize(self): ''' Setup sessions ''' self.manager = EventManager.Instance() self.game_history = GameHistory.Instance()
def initialize(self): ''' Setup sessions ''' self.manager = EventManager.instance() self.game_history = GameHistory.instance()