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):]})
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.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 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()
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()