def __at__(self, index): ''' Get snapshot at specific index ''' key = Snapshot.to_key(index + 1) if self.cache.get(key) is not None: return self.cache.get(key) else: snapshot = Snapshot.by_id(index + 1) if snapshot is not None: self.cache.set(snapshot.key, snapshot.to_dict()) return snapshot.to_dict() return None
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)
def _load(self): ''' Moves snapshots from db into the cache ''' logging.info("Loading game history from database ...") if Snapshot.by_id(1) is None: self.__now__() # Take starting snapshot self.epoch = Snapshot.by_id(1).created try: max_index = len(self) start_index = 1 if len(self) <= 10 else max_index - 9 for index in range(start_index, max_index + 1): snapshot = Snapshot.by_id(index) if self.cache.get(snapshot.key) is None: logging.info("Cached snapshot (%d of %d)" % (snapshot.id, max_index)) self.cache.set(snapshot.key, snapshot.to_dict()) logging.info("History load complete.") except KeyboardInterrupt: logging.info("History load stopped by user.")
def _load(self): ''' Moves snapshots from db into the cache ''' logging.info("Loading game history from database ...") if Snapshot.by_id(1) is None: self.__now__() # Take starting snapshot self.epoch = Snapshot.by_id(1).created try: max_index = len(self) start_index = 1 if len(self) <= 10 else max_index - 9 for index in range(start_index, max_index + 1): snapshot = Snapshot.by_id(index) if self.cache.get(snapshot.key) is None: logging.info("Cached snapshot (%d of %d)" % ( snapshot.id, max_index )) self.cache.set(snapshot.key, snapshot.to_dict()) logging.info("History load complete.") except KeyboardInterrupt: logging.info("History load stopped by user.")
def _load(self): """ Moves snapshots from db into the cache """ logging.info("Loading game history from database ...") snaps = Snapshot.all() if len(snaps) > 0: snap = snaps[0] else: snap = self.__now__() # Take starting snapshot self.epoch = snap.created try: max_index = len(self) start_index = snap.id if len(self) <= (snap.id + 9) else max_index - 9 for index in range(start_index, max_index + 1): snapshot = Snapshot.by_id(index) if snapshot and self.cache.get(snapshot.key) is None: logging.info( "Cached snapshot (%d of %d)" % (snapshot.id, max_index) ) self.cache.set(snapshot.key, snapshot.to_dict()) logging.info("History load complete.") except TypeError: logging.error("Error Loading Cache (try to restart memcached)") except KeyboardInterrupt: logging.info("History load stopped by user.")
def __now__(self): ''' Returns snapshot object it as a dict ''' snapshot = Snapshot() bot_manager = BotManager.instance() #self.dbsession = DBSession() for team in Team.all(): snapshot_team = SnapshotTeam(team_id=team.id, money=team.money, bots=bot_manager.count_by_team(team)) snapshot_team.game_levels = team.game_levels snapshot_team.flags = team.flags self.dbsession.add(snapshot_team) self.dbsession.flush() snapshot.teams.append(snapshot_team) self.dbsession.add(snapshot) self.dbsession.commit() return snapshot
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 __contains__(self, index): return True if Snapshot.by_id(index) is not None else False